MediaWiki  1.27.3
MediaWikiPageNameNormalizer.php
Go to the documentation of this file.
1 <?php
2 
3 namespace MediaWiki\Site;
4 
6 use Http;
8 
36 
55  public function normalizePageName( $pageName, $apiUrl ) {
56 
57  // Check if we have strings as arguments.
58  if ( !is_string( $pageName ) ) {
59  throw new \MWException( '$pageName must be a string' );
60  }
61 
62  // Go on call the external site
63 
64  // Make sure the string is normalized into NFC (due to T42017)
65  // but do nothing to the whitespaces, that should work appropriately.
66  // @see https://phabricator.wikimedia.org/T42017
67  $pageName = Validator::cleanUp( $pageName );
68 
69  // Build the args for the specific call
70  $args = [
71  'action' => 'query',
72  'prop' => 'info',
73  'redirects' => true,
74  'converttitles' => true,
75  'format' => 'json',
76  'titles' => $pageName,
77  // @todo options for maxlag and maxage
78  // Note that maxlag will lead to a long delay before a reply is made,
79  // but that maxage can avoid the extreme delay. On the other hand
80  // maxage could be nice to use anyhow as it stops unnecessary requests.
81  // Also consider smaxage if maxage is used.
82  ];
83 
84  $url = wfAppendQuery( $apiUrl, $args );
85 
86  // Go on call the external site
87  // @todo we need a good way to specify a timeout here.
88  $ret = Http::get( $url, [], __METHOD__ );
89 
90  if ( $ret === false ) {
91  wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
92  return false;
93  }
94 
95  $data = FormatJson::decode( $ret, true );
96 
97  if ( !is_array( $data ) ) {
98  wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
99  return false;
100  }
101 
102  $page = static::extractPageRecord( $data, $pageName );
103 
104  if ( isset( $page['missing'] ) ) {
105  wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! "
106  . $ret );
107  return false;
108  }
109 
110  if ( isset( $page['invalid'] ) ) {
111  wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! "
112  . $ret );
113  return false;
114  }
115 
116  if ( !isset( $page['title'] ) ) {
117  wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
118  return false;
119  }
120 
121  return $page['title'];
122  }
123 
132  private static function extractPageRecord( $externalData, $pageTitle ) {
133  // If there is a special case with only one returned page
134  // we can cheat, and only return
135  // the single page in the "pages" substructure.
136  if ( isset( $externalData['query']['pages'] ) ) {
137  $pages = array_values( $externalData['query']['pages'] );
138  if ( count( $pages ) === 1 ) {
139  return $pages[0];
140  }
141  }
142  // This is only used during internal testing, as it is assumed
143  // a more optimal (and lossfree) storage.
144  // Make initial checks and return if prerequisites are not meet.
145  if ( !is_array( $externalData ) || !isset( $externalData['query'] ) ) {
146  return false;
147  }
148  // Loop over the tree different named structures, that otherwise are similar
149  $structs = [
150  'normalized' => 'from',
151  'converted' => 'from',
152  'redirects' => 'from',
153  'pages' => 'title'
154  ];
155  foreach ( $structs as $listId => $fieldId ) {
156  // Check if the substructure exist at all.
157  if ( !isset( $externalData['query'][$listId] ) ) {
158  continue;
159  }
160  // Filter the substructure down to what we actually are using.
161  $collectedHits = array_filter(
162  array_values( $externalData['query'][$listId] ),
163  function ( $a ) use ( $fieldId, $pageTitle ) {
164  return $a[$fieldId] === $pageTitle;
165  }
166  );
167  // If still looping over normalization, conversion or redirects,
168  // then we need to keep the new page title for later rounds.
169  if ( $fieldId === 'from' && is_array( $collectedHits ) ) {
170  switch ( count( $collectedHits ) ) {
171  case 0:
172  break;
173  case 1:
174  $pageTitle = $collectedHits[0]['to'];
175  break;
176  default:
177  return false;
178  }
179  } elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
180  // If on the pages structure we should prepare for returning.
181 
182  switch ( count( $collectedHits ) ) {
183  case 0:
184  return false;
185  case 1:
186  return array_shift( $collectedHits );
187  default:
188  return false;
189  }
190  }
191  }
192  // should never be here
193  return false;
194  }
195 
196 }
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
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:1802
if($line===false) $args
Definition: cdb.php:64
wfDebugLog($logGroup, $text, $dest= 'all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not...
wfAppendQuery($url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Service for normalizing a page name using a MediaWiki api.
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
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
static extractPageRecord($externalData, $pageTitle)
Get normalization record for a given page title from an API response.
static get($url, $options=[], $caller=__METHOD__)
Simple wrapper for Http::request( 'GET' )
static decode($value, $assoc=false)
Decodes a JSON string.
Definition: FormatJson.php:187
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2342
normalizePageName($pageName, $apiUrl)
Returns the normalized form of the given page title, using the normalization rules of the given site...