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
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 >
29 * Class representing a MediaWiki site.
35 class MediaWikiSite
extends Site
{
36 const PATH_FILE
= 'file_path';
37 const PATH_PAGE
= 'page_path';
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 ) {
49 $site->setGlobalId( $globalId );
60 public function __construct( $type = self
::TYPE_MEDIAWIKI
) {
61 parent
::__construct( $type );
65 * Returns the database form of the given title.
69 * @param string $title The target page's title, in normalized form.
73 public function toDBKey( $title ) {
74 return str_replace( ' ', '_', $title );
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
93 * @param string $pageName
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();
116 // Make sure the string is normalized into NFC (due to the bug 40017)
117 // but do nothing to the whitespaces, that should work appropriately.
118 // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=40017
119 $pageName = UtfNormal
::cleanUp( $pageName );
121 // Build the args for the specific call
126 'converttitles' => true,
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 );
143 if ( $ret === false ) {
144 wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
148 $data = FormatJson
::decode( $ret, true );
150 if ( !is_array( $data ) ) {
151 wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
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! "
163 if ( isset( $page['invalid'] ) ) {
164 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! "
169 if ( !isset( $page['title'] ) ) {
170 wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
174 return $page['title'];
178 * Get normalization record for a given page title from an API response.
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 ) {
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'] ) ) {
203 // Loop over the tree different named structures, that otherwise are similar
205 'normalized' => 'from',
206 'converted' => 'from',
207 'redirects' => 'from',
210 foreach ( $structs as $listId => $fieldId ) {
211 // Check if the substructure exist at all.
212 if ( !isset( $externalData['query'][$listId] ) ) {
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 ) ) {
229 $pageTitle = $collectedHits[0]['to'];
235 // If on the pages structure we should prepare for returning.
236 elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
237 switch ( count( $collectedHits ) ) {
241 return array_shift( $collectedHits );
247 // should never be here
252 * @see Site::getLinkPathType
253 * Returns Site::PATH_PAGE
259 public function getLinkPathType() {
260 return self
::PATH_PAGE
;
264 * Returns the relative page path.
270 public function getRelativePagePath() {
271 return parse_url( $this->getPath( self
::PATH_PAGE
), PHP_URL_PATH
);
275 * Returns the relative file path.
281 public function getRelativeFilePath() {
282 return parse_url( $this->getPath( self
::PATH_FILE
), PHP_URL_PATH
);
286 * Sets the relative page path.
290 * @param string $path
292 public function setPagePath( $path ) {
293 $this->setPath( self
::PATH_PAGE
, $path );
297 * Sets the relative file path.
301 * @param string $path
303 public function setFilePath( $path ) {
304 $this->setPath( self
::PATH_FILE
, $path );
308 * @see Site::getPageUrl
310 * This implementation returns a URL constructed using the path returned by getLinkPath().
311 * In addition to the default behavior implemented by Site::getPageUrl(), this
312 * method converts the $pageName to DBKey-format by replacing spaces with underscores
313 * before using it in the URL.
317 * @param string|bool $pageName Page name or false (default: false)
321 public function getPageUrl( $pageName = false ) {
322 $url = $this->getLinkPath();
324 if ( $url === false ) {
328 if ( $pageName !== false ) {
329 $pageName = $this->toDBKey( trim( $pageName ) );
330 $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
337 * Returns the full file path (ie site url + relative file path).
338 * The path should go at the $1 marker. If the $path
339 * argument is provided, the marker will be replaced by it's value.
343 * @param string|bool $path
347 public function getFileUrl( $path = false ) {
348 $filePath = $this->getPath( self
::PATH_FILE
);
350 if ( $filePath !== false ) {
351 $filePath = str_replace( '$1', $path, $filePath );