MediaWiki  1.23.2
MediaWikiSite.php
Go to the documentation of this file.
1 <?php
35 class MediaWikiSite extends Site {
36 
37  const PATH_FILE = 'file_path';
38  const PATH_PAGE = 'page_path';
39 
48  public static function newFromGlobalId( $globalId ) {
49  $site = new static();
50  $site->setGlobalId( $globalId );
51  return $site;
52  }
53 
61  public function __construct( $type = self::TYPE_MEDIAWIKI ) {
62  parent::__construct( $type );
63  }
64 
74  public function toDBKey( $title ) {
75  return str_replace( ' ', '_', $title );
76  }
77 
97  public function normalizePageName( $pageName ) {
98 
99  // Check if we have strings as arguments.
100  if ( !is_string( $pageName ) ) {
101  throw new MWException( '$pageName must be a string' );
102  }
103 
104  // Go on call the external site
105  if ( defined( 'MW_PHPUNIT_TEST' ) ) {
106  // If the code is under test, don't call out to other sites, just normalize locally.
107  // Note: this may cause results to be inconsistent with the actual normalization used by the respective remote site!
108 
109  $t = Title::newFromText( $pageName );
110  return $t->getPrefixedText();
111  } else {
112 
113  // Make sure the string is normalized into NFC (due to the bug 40017)
114  // but do nothing to the whitespaces, that should work appropriately.
115  // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=40017
116  $pageName = UtfNormal::cleanUp( $pageName );
117 
118  // Build the args for the specific call
119  $args = array(
120  'action' => 'query',
121  'prop' => 'info',
122  'redirects' => true,
123  'converttitles' => true,
124  'format' => 'json',
125  'titles' => $pageName,
126  // @todo options for maxlag and maxage
127  // Note that maxlag will lead to a long delay before a reply is made,
128  // but that maxage can avoid the extreme delay. On the other hand
129  // maxage could be nice to use anyhow as it stops unnecessary requests.
130  // Also consider smaxage if maxage is used.
131  );
132 
133  $url = wfAppendQuery( $this->getFileUrl( 'api.php' ), $args );
134 
135  // Go on call the external site
136  // @todo we need a good way to specify a timeout here.
137  $ret = Http::get( $url );
138  }
139 
140  if ( $ret === false ) {
141  wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
142  return false;
143  }
144 
145  $data = FormatJson::decode( $ret, true );
146 
147  if ( !is_array( $data ) ) {
148  wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
149  return false;
150  }
151 
152  $page = static::extractPageRecord( $data, $pageName );
153 
154  if ( isset( $page['missing'] ) ) {
155  wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! " . $ret );
156  return false;
157  }
158 
159  if ( isset( $page['invalid'] ) ) {
160  wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! " . $ret );
161  return false;
162  }
163 
164  if ( !isset( $page['title'] ) ) {
165  wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
166  return false;
167  }
168 
169  return $page['title'];
170  }
171 
182  private static function extractPageRecord( $externalData, $pageTitle ) {
183  // If there is a special case with only one returned page
184  // we can cheat, and only return
185  // the single page in the "pages" substructure.
186  if ( isset( $externalData['query']['pages'] ) ) {
187  $pages = array_values( $externalData['query']['pages'] );
188  if ( count( $pages ) === 1 ) {
189  return $pages[0];
190  }
191  }
192  // This is only used during internal testing, as it is assumed
193  // a more optimal (and lossfree) storage.
194  // Make initial checks and return if prerequisites are not meet.
195  if ( !is_array( $externalData ) || !isset( $externalData['query'] ) ) {
196  return false;
197  }
198  // Loop over the tree different named structures, that otherwise are similar
199  $structs = array(
200  'normalized' => 'from',
201  'converted' => 'from',
202  'redirects' => 'from',
203  'pages' => 'title'
204  );
205  foreach ( $structs as $listId => $fieldId ) {
206  // Check if the substructure exist at all.
207  if ( !isset( $externalData['query'][$listId] ) ) {
208  continue;
209  }
210  // Filter the substructure down to what we actually are using.
211  $collectedHits = array_filter(
212  array_values( $externalData['query'][$listId] ),
213  function( $a ) use ( $fieldId, $pageTitle ) {
214  return $a[$fieldId] === $pageTitle;
215  }
216  );
217  // If still looping over normalization, conversion or redirects,
218  // then we need to keep the new page title for later rounds.
219  if ( $fieldId === 'from' && is_array( $collectedHits ) ) {
220  switch ( count( $collectedHits ) ) {
221  case 0:
222  break;
223  case 1:
224  $pageTitle = $collectedHits[0]['to'];
225  break;
226  default:
227  return false;
228  }
229  }
230  // If on the pages structure we should prepare for returning.
231  elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
232  switch ( count( $collectedHits ) ) {
233  case 0:
234  return false;
235  case 1:
236  return array_shift( $collectedHits );
237  default:
238  return false;
239  }
240  }
241  }
242  // should never be here
243  return false;
244  }
245 
254  public function getLinkPathType() {
255  return self::PATH_PAGE;
256  }
257 
265  public function getRelativePagePath() {
266  return parse_url( $this->getPath( self::PATH_PAGE ), PHP_URL_PATH );
267  }
268 
276  public function getRelativeFilePath() {
277  return parse_url( $this->getPath( self::PATH_FILE ), PHP_URL_PATH );
278  }
279 
287  public function setPagePath( $path ) {
288  $this->setPath( self::PATH_PAGE, $path );
289  }
290 
298  public function setFilePath( $path ) {
299  $this->setPath( self::PATH_FILE, $path );
300  }
301 
316  public function getPageUrl( $pageName = false ) {
317  $url = $this->getLinkPath();
318 
319  if ( $url === false ) {
320  return false;
321  }
322 
323  if ( $pageName !== false ) {
324  $pageName = $this->toDBKey( trim( $pageName ) );
325  $url = str_replace( '$1', wfUrlencode( $pageName ), $url );
326  }
327 
328  return $url;
329  }
330 
342  public function getFileUrl( $path = false ) {
343  $filePath = $this->getPath( self::PATH_FILE );
344 
345  if ( $filePath !== false ) {
346  $filePath = str_replace( '$1', $path, $filePath );
347  }
348 
349  return $filePath;
350  }
351 
352 }
MediaWikiSite\toDBKey
toDBKey( $title)
Returns the database form of the given title.
Definition: MediaWikiSite.php:74
MediaWikiSite\getFileUrl
getFileUrl( $path=false)
Returns the full file path (ie site url + relative file path).
Definition: MediaWikiSite.php:342
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:189
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
MediaWikiSite\getRelativeFilePath
getRelativeFilePath()
Returns the relative file path.
Definition: MediaWikiSite.php:276
MediaWikiSite\getRelativePagePath
getRelativePagePath()
Returns the relative page path.
Definition: MediaWikiSite.php:265
UtfNormal\cleanUp
static cleanUp( $string)
The ultimate convenience function! Clean up invalid UTF-8 sequences, and convert to normal form C,...
Definition: UtfNormal.php:79
wfDebugLog
wfDebugLog( $logGroup, $text, $dest='all')
Send a line to a supplementary debug log file, if configured, or main debug log if not.
Definition: GlobalFunctions.php:1040
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
wfUrlencode
wfUrlencode( $s)
We want some things to be included as literal characters in our title URLs for prettiness,...
Definition: GlobalFunctions.php:330
Site\getPath
getPath( $pathType)
Returns the path of the provided type or false if there is no such path.
Definition: Site.php:592
Site\$type
string $type
Definition: Site.php:62
wfAppendQuery
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Definition: GlobalFunctions.php:459
FormatJson\decode
static decode( $value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:126
MWException
MediaWiki exception.
Definition: MWException.php:26
Site\$globalId
string null $globalId
Definition: Site.php:56
MediaWikiSite\setPagePath
setPagePath( $path)
Sets the relative page path.
Definition: MediaWikiSite.php:287
MediaWikiSite\getLinkPathType
getLinkPathType()
Definition: MediaWikiSite.php:254
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
MediaWikiSite\setFilePath
setFilePath( $path)
Sets the relative file path.
Definition: MediaWikiSite.php:298
MediaWikiSite\PATH_FILE
const PATH_FILE
Definition: MediaWikiSite.php:37
Site
Definition: Site.php:29
Http\get
static get( $url, $timeout='default', $options=array())
Simple wrapper for Http::request( 'GET' )
Definition: HttpFunctions.php:93
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
Site\setPath
setPath( $pathType, $fullUrl)
Sets the path used to construct links with.
Definition: Site.php:571
MediaWikiSite\__construct
__construct( $type=self::TYPE_MEDIAWIKI)
Constructor.
Definition: MediaWikiSite.php:61
MediaWikiSite\getPageUrl
getPageUrl( $pageName=false)
Definition: MediaWikiSite.php:316
$args
if( $line===false) $args
Definition: cdb.php:62
MediaWikiSite\PATH_PAGE
const PATH_PAGE
Definition: MediaWikiSite.php:38
MediaWikiSite\normalizePageName
normalizePageName( $pageName)
Returns the normalized form of the given page title, using the normalization rules of the given site.
Definition: MediaWikiSite.php:97
$path
$path
Definition: NoLocalSettings.php:35
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
MediaWikiSite
Class representing a MediaWiki site.
Definition: MediaWikiSite.php:35
MediaWikiSite\newFromGlobalId
static newFromGlobalId( $globalId)
Definition: MediaWikiSite.php:48
$t
$t
Definition: testCompression.php:65
Site\getLinkPath
getLinkPath()
Returns the path used to construct links with or false if there is no such path.
Definition: Site.php:327
MediaWikiSite\extractPageRecord
static extractPageRecord( $externalData, $pageTitle)
Get normalization record for a given page title from an API response.
Definition: MediaWikiSite.php:182