wfMsgForContentNoTrans() was removed
[mediawiki.git] / includes / site / MediaWikiSite.php
blob9fec1f4fe1263d3a334b4724e4ec1d0ef95652b7
1 <?php
2 /**
3 * Class representing a MediaWiki site.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @file
21 * @ingroup Site
22 * @license GNU GPL v2+
23 * @author John Erling Blad < jeblad@gmail.com >
24 * @author Daniel Kinzler
25 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
28 /**
29 * Class representing a MediaWiki site.
31 * @since 1.21
33 * @ingroup Site
35 class MediaWikiSite extends Site {
36 const PATH_FILE = 'file_path';
37 const PATH_PAGE = 'page_path';
39 /**
40 * @since 1.21
41 * @deprecated since 1.21 Just use the constructor or the factory Site::newForType
43 * @param int $globalId
45 * @return MediaWikiSite
47 public static function newFromGlobalId( $globalId ) {
48 $site = new static();
49 $site->setGlobalId( $globalId );
50 return $site;
53 /**
54 * Constructor.
56 * @since 1.21
58 * @param string $type
60 public function __construct( $type = self::TYPE_MEDIAWIKI ) {
61 parent::__construct( $type );
64 /**
65 * Returns the database form of the given title.
67 * @since 1.21
69 * @param string $title The target page's title, in normalized form.
71 * @return string
73 public function toDBKey( $title ) {
74 return str_replace( ' ', '_', $title );
77 /**
78 * Returns the normalized form of the given page title, using the
79 * normalization rules of the given site. If the given title is a redirect,
80 * the redirect weill be resolved and the redirect target is returned.
82 * @note This actually makes an API request to the remote site, so beware
83 * that this function is slow and depends on an external service.
85 * @note If MW_PHPUNIT_TEST is defined, the call to the external site is
86 * skipped, and the title is normalized using the local normalization
87 * rules as implemented by the Title class.
89 * @see Site::normalizePageName
91 * @since 1.21
93 * @param string $pageName
95 * @return string
96 * @throws MWException
98 public function normalizePageName( $pageName ) {
100 // Check if we have strings as arguments.
101 if ( !is_string( $pageName ) ) {
102 throw new MWException( '$pageName must be a string' );
105 // Go on call the external site
106 if ( defined( 'MW_PHPUNIT_TEST' ) ) {
107 // If the code is under test, don't call out to other sites, just
108 // normalize locally.
109 // Note: this may cause results to be inconsistent with the actual
110 // normalization used by the respective remote site!
112 $t = Title::newFromText( $pageName );
113 return $t->getPrefixedText();
114 } else {
116 // Make sure the string is normalized into NFC (due to T42017)
117 // but do nothing to the whitespaces, that should work appropriately.
118 // @see https://phabricator.wikimedia.org/T42017
119 $pageName = UtfNormal\Validator::cleanUp( $pageName );
121 // Build the args for the specific call
122 $args = array(
123 'action' => 'query',
124 'prop' => 'info',
125 'redirects' => true,
126 'converttitles' => true,
127 'format' => 'json',
128 'titles' => $pageName,
129 // @todo options for maxlag and maxage
130 // Note that maxlag will lead to a long delay before a reply is made,
131 // but that maxage can avoid the extreme delay. On the other hand
132 // maxage could be nice to use anyhow as it stops unnecessary requests.
133 // Also consider smaxage if maxage is used.
136 $url = wfAppendQuery( $this->getFileUrl( 'api.php' ), $args );
138 // Go on call the external site
139 // @todo we need a good way to specify a timeout here.
140 $ret = Http::get( $url, array(), __METHOD__ );
143 if ( $ret === false ) {
144 wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
145 return false;
148 $data = FormatJson::decode( $ret, true );
150 if ( !is_array( $data ) ) {
151 wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
152 return false;
155 $page = static::extractPageRecord( $data, $pageName );
157 if ( isset( $page['missing'] ) ) {
158 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! "
159 . $ret );
160 return false;
163 if ( isset( $page['invalid'] ) ) {
164 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! "
165 . $ret );
166 return false;
169 if ( !isset( $page['title'] ) ) {
170 wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
171 return false;
174 return $page['title'];
178 * Get normalization record for a given page title from an API response.
180 * @since 1.21
182 * @param array $externalData A reply from the API on a external server.
183 * @param string $pageTitle Identifies the page at the external site, needing normalization.
185 * @return array|bool A 'page' structure representing the page identified by $pageTitle.
187 private static function extractPageRecord( $externalData, $pageTitle ) {
188 // If there is a special case with only one returned page
189 // we can cheat, and only return
190 // the single page in the "pages" substructure.
191 if ( isset( $externalData['query']['pages'] ) ) {
192 $pages = array_values( $externalData['query']['pages'] );
193 if ( count( $pages ) === 1 ) {
194 return $pages[0];
197 // This is only used during internal testing, as it is assumed
198 // a more optimal (and lossfree) storage.
199 // Make initial checks and return if prerequisites are not meet.
200 if ( !is_array( $externalData ) || !isset( $externalData['query'] ) ) {
201 return false;
203 // Loop over the tree different named structures, that otherwise are similar
204 $structs = array(
205 'normalized' => 'from',
206 'converted' => 'from',
207 'redirects' => 'from',
208 'pages' => 'title'
210 foreach ( $structs as $listId => $fieldId ) {
211 // Check if the substructure exist at all.
212 if ( !isset( $externalData['query'][$listId] ) ) {
213 continue;
215 // Filter the substructure down to what we actually are using.
216 $collectedHits = array_filter(
217 array_values( $externalData['query'][$listId] ),
218 function ( $a ) use ( $fieldId, $pageTitle ) {
219 return $a[$fieldId] === $pageTitle;
222 // If still looping over normalization, conversion or redirects,
223 // then we need to keep the new page title for later rounds.
224 if ( $fieldId === 'from' && is_array( $collectedHits ) ) {
225 switch ( count( $collectedHits ) ) {
226 case 0:
227 break;
228 case 1:
229 $pageTitle = $collectedHits[0]['to'];
230 break;
231 default:
232 return false;
234 } elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
235 // If on the pages structure we should prepare for returning.
236 switch ( count( $collectedHits ) ) {
237 case 0:
238 return false;
239 case 1:
240 return array_shift( $collectedHits );
241 default:
242 return false;
246 // should never be here
247 return false;
251 * @see Site::getLinkPathType
252 * Returns Site::PATH_PAGE
254 * @since 1.21
256 * @return string
258 public function getLinkPathType() {
259 return self::PATH_PAGE;
263 * Returns the relative page path.
265 * @since 1.21
267 * @return string
269 public function getRelativePagePath() {
270 return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
274 * Returns the relative file path.
276 * @since 1.21
278 * @return string
280 public function getRelativeFilePath() {
281 return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
285 * Sets the relative page path.
287 * @since 1.21
289 * @param string $path
291 public function setPagePath( $path ) {
292 $this->setPath( self::PATH_PAGE, $path );
296 * Sets the relative file path.
298 * @since 1.21
300 * @param string $path
302 public function setFilePath( $path ) {
303 $this->setPath( self::PATH_FILE, $path );
307 * @see Site::getPageUrl
309 * This implementation returns a URL constructed using the path returned by getLinkPath().
310 * In addition to the default behavior implemented by Site::getPageUrl(), this
311 * method converts the $pageName to DBKey-format by replacing spaces with underscores
312 * before using it in the URL.
314 * @since 1.21
316 * @param string|bool $pageName Page name or false (default: false)
318 * @return string
320 public function getPageUrl( $pageName = false ) {
321 $url = $this->getLinkPath();
323 if ( $url === false ) {
324 return false;
327 if ( $pageName !== false ) {
328 $pageName = $this->toDBKey( trim( $pageName ) );
329 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
332 return $url;
336 * Returns the full file path (ie site url + relative file path).
337 * The path should go at the $1 marker. If the $path
338 * argument is provided, the marker will be replaced by it's value.
340 * @since 1.21
342 * @param string|bool $path
344 * @return string
346 public function getFileUrl( $path = false ) {
347 $filePath = $this->getPath( self::PATH_FILE );
349 if ( $filePath !== false ) {
350 $filePath = str_replace( '$1', $path, $filePath );
353 return $filePath;