MediaWiki REL1_30
MediaWikiPageNameNormalizer.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Site;
4
5use FormatJson;
6use Http;
7use UtfNormal\Validator;
8
36
40 private $http;
41
45 public function __construct( Http $http = null ) {
46 if ( !$http ) {
47 $http = new Http();
48 }
49
50 $this->http = $http;
51 }
52
71 public function normalizePageName( $pageName, $apiUrl ) {
72 // Check if we have strings as arguments.
73 if ( !is_string( $pageName ) ) {
74 throw new \MWException( '$pageName must be a string' );
75 }
76
77 // Go on call the external site
78
79 // Make sure the string is normalized into NFC (due to T42017)
80 // but do nothing to the whitespaces, that should work appropriately.
81 // @see https://phabricator.wikimedia.org/T42017
82 $pageName = Validator::cleanUp( $pageName );
83
84 // Build the args for the specific call
85 $args = [
86 'action' => 'query',
87 'prop' => 'info',
88 'redirects' => true,
89 'converttitles' => true,
90 'format' => 'json',
91 'titles' => $pageName,
92 // @todo options for maxlag and maxage
93 // Note that maxlag will lead to a long delay before a reply is made,
94 // but that maxage can avoid the extreme delay. On the other hand
95 // maxage could be nice to use anyhow as it stops unnecessary requests.
96 // Also consider smaxage if maxage is used.
97 ];
98
99 $url = wfAppendQuery( $apiUrl, $args );
100
101 // Go on call the external site
102 // @todo we need a good way to specify a timeout here.
103 $ret = $this->http->get( $url, [], __METHOD__ );
104
105 if ( $ret === false ) {
106 wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
107 return false;
108 }
109
110 $data = FormatJson::decode( $ret, true );
111
112 if ( !is_array( $data ) ) {
113 wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
114 return false;
115 }
116
117 $page = static::extractPageRecord( $data, $pageName );
118
119 if ( isset( $page['missing'] ) ) {
120 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! "
121 . $ret );
122 return false;
123 }
124
125 if ( isset( $page['invalid'] ) ) {
126 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! "
127 . $ret );
128 return false;
129 }
130
131 if ( !isset( $page['title'] ) ) {
132 wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
133 return false;
134 }
135
136 return $page['title'];
137 }
138
147 private static function extractPageRecord( $externalData, $pageTitle ) {
148 // If there is a special case with only one returned page
149 // we can cheat, and only return
150 // the single page in the "pages" substructure.
151 if ( isset( $externalData['query']['pages'] ) ) {
152 $pages = array_values( $externalData['query']['pages'] );
153 if ( count( $pages ) === 1 ) {
154 return $pages[0];
155 }
156 }
157 // This is only used during internal testing, as it is assumed
158 // a more optimal (and lossfree) storage.
159 // Make initial checks and return if prerequisites are not meet.
160 if ( !is_array( $externalData ) || !isset( $externalData['query'] ) ) {
161 return false;
162 }
163 // Loop over the tree different named structures, that otherwise are similar
164 $structs = [
165 'normalized' => 'from',
166 'converted' => 'from',
167 'redirects' => 'from',
168 'pages' => 'title'
169 ];
170 foreach ( $structs as $listId => $fieldId ) {
171 // Check if the substructure exist at all.
172 if ( !isset( $externalData['query'][$listId] ) ) {
173 continue;
174 }
175 // Filter the substructure down to what we actually are using.
176 $collectedHits = array_filter(
177 array_values( $externalData['query'][$listId] ),
178 function ( $a ) use ( $fieldId, $pageTitle ) {
179 return $a[$fieldId] === $pageTitle;
180 }
181 );
182 // If still looping over normalization, conversion or redirects,
183 // then we need to keep the new page title for later rounds.
184 if ( $fieldId === 'from' && is_array( $collectedHits ) ) {
185 switch ( count( $collectedHits ) ) {
186 case 0:
187 break;
188 case 1:
189 $pageTitle = $collectedHits[0]['to'];
190 break;
191 default:
192 return false;
193 }
194 } elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
195 // If on the pages structure we should prepare for returning.
196
197 switch ( count( $collectedHits ) ) {
198 case 0:
199 return false;
200 case 1:
201 return array_shift( $collectedHits );
202 default:
203 return false;
204 }
205 }
206 }
207 // should never be here
208 return false;
209 }
210
211}
Apache License January http
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...
if( $line===false) $args
Definition cdb.php:63
JSON formatter wrapper class.
static decode( $value, $assoc=false)
Decodes a JSON string.
Various HTTP related functions.
Definition Http.php:27
Service for normalizing a page name using a MediaWiki api.
static extractPageRecord( $externalData, $pageTitle)
Get normalization record for a given page title from an API response.
normalizePageName( $pageName, $apiUrl)
Returns the normalized form of the given page title, using the normalization rules of the given site.
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:1975