Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / site / MediaWikiSite.php
blob03597f4ac1d10374f20b66b65e1b7a03543da84c
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Site;
23 use MediaWiki\Title\Title;
24 use RuntimeException;
26 /**
27 * Class representing a MediaWiki site.
29 * @since 1.21
30 * @ingroup Site
31 * @author John Erling Blad < jeblad@gmail.com >
32 * @author Daniel Kinzler
33 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
35 class MediaWikiSite extends Site {
36 /** The script path of a site, e.g. `/w/$1` related to $wgScriptPath */
37 public const PATH_FILE = 'file_path';
38 /** The article path of a site, e.g. `/wiki/$1` like $wgArticlePath */
39 public const PATH_PAGE = 'page_path';
41 /**
42 * @since 1.21
43 * @param string $type
45 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
46 parent::__construct( $type );
49 /**
50 * Get the database form of the given title.
52 * @since 1.21
53 * @param string $title The target page's title, in normalized form.
54 * @return string
56 public function toDBKey( $title ) {
57 return str_replace( ' ', '_', $title );
60 /**
61 * Get the normalized form of the given page title.
63 * This uses to normalization rules of the given site. If $followRedirect is set to true
64 * and the given title is a redirect, the redirect will be resolved and
65 * the redirect target is returned.
66 * Only titles of existing pages will be returned.
68 * @note This actually makes an API request to the remote site, so beware
69 * that this function is slow and depends on an external service.
71 * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
72 * skipped, and the title is normalized using the local normalization
73 * rules as implemented by the Title class.
75 * @see Site::normalizePageName
76 * @since 1.21
77 * @since 1.37 Added $followRedirect
78 * @param string $pageName
79 * @param int $followRedirect either MediaWikiPageNameNormalizer::FOLLOW_REDIRECT or
80 * MediaWikiPageNameNormalizer::NOFOLLOW_REDIRECT
81 * @return string|false The normalized form of the title,
82 * or false to indicate an invalid title, a missing page,
83 * or some other kind of error.
85 public function normalizePageName( $pageName, $followRedirect = MediaWikiPageNameNormalizer::FOLLOW_REDIRECT ) {
86 if ( defined( 'MW_PHPUNIT_TEST' ) || defined( 'MW_DEV_ENV' ) ) {
87 // If the code is under test, don't call out to other sites, just
88 // normalize locally.
89 // Note: this may cause results to be inconsistent with the actual
90 // normalization used by the respective remote site!
92 $t = Title::newFromText( $pageName );
93 return $t->getPrefixedText();
94 } else {
95 static $mediaWikiPageNameNormalizer = null;
96 $mediaWikiPageNameNormalizer ??= new MediaWikiPageNameNormalizer();
98 return $mediaWikiPageNameNormalizer->normalizePageName(
99 $pageName,
100 $this->getFileUrl( 'api.php' ),
101 $followRedirect
107 * Get the constant for getting or setting the script path.
109 * This configures how Site::setLinkPath() and Site::getLinkPath()
110 * will work internally in terms of Site::setPath() and Site::getPath().
112 * @see Site::getLinkPathType
113 * @since 1.21
114 * @return string
116 public function getLinkPathType() {
117 return self::PATH_PAGE;
121 * Get the article path, as relative path only (without server).
123 * @since 1.21
124 * @return string
126 public function getRelativePagePath() {
127 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
131 * Get the script script, as relative path only (without server).
133 * @since 1.21
134 * @return string
136 public function getRelativeFilePath() {
137 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
141 * Set the article path.
143 * @since 1.21
144 * @param string $path
146 public function setPagePath( $path ) {
147 $this->setPath( self::PATH_PAGE, $path );
151 * Set the script path.
153 * @since 1.21
154 * @param string $path
156 public function setFilePath( $path ) {
157 $this->setPath( self::PATH_FILE, $path );
161 * Get the full URL for the given page on the site.
163 * This implementation returns a URL constructed using the path returned by getLinkPath().
164 * In addition to the default behavior implemented by Site::getPageUrl(), this
165 * method converts the $pageName to DBKey-format by replacing spaces with underscores
166 * before using it in the URL.
168 * @see Site::getPageUrl
169 * @since 1.21
170 * @param string|false $pageName Page name or false (default: false)
171 * @return string|null
173 public function getPageUrl( $pageName = false ) {
174 $url = $this->getLinkPath();
176 if ( $url === null ) {
177 return null;
180 if ( $pageName !== false ) {
181 $pageName = $this->toDBKey( trim( $pageName ) );
182 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
185 return $url;
189 * Get the full URL to an entry point under a wiki's script path.
191 * This is the equivalent of wfScript() for other sites.
193 * The path should go at the `$1` marker. If the $path
194 * argument is provided, the marker will be replaced by its value.
196 * @since 1.21
197 * @param string|false $path Not passing a string for this is deprecated since 1.40.
198 * @return string
200 public function getFileUrl( $path = false ) {
201 $filePath = $this->getPath( self::PATH_FILE );
202 if ( $filePath === null ) {
203 throw new RuntimeException( "getFileUrl called for {$this->getGlobalId()} while PATH_FILE is unset" );
206 if ( $path !== false ) {
207 $filePath = str_replace( '$1', $path, $filePath );
208 } else {
209 wfDeprecatedMsg( __METHOD__ . ': omitting $path is deprecated', '1.40' );
212 return $filePath;
216 /** @deprecated class alias since 1.42 */
217 class_alias( MediaWikiSite::class, 'MediaWikiSite' );