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