3 namespace MediaWiki\Site
;
7 use UtfNormal\Validator
;
10 * Service for normalizing a page name using a MediaWiki api.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 * http://www.gnu.org/copyleft/gpl.html
29 * @license GNU GPL v2+
30 * @author John Erling Blad < jeblad@gmail.com >
31 * @author Daniel Kinzler
32 * @author Jeroen De Dauw < jeroendedauw@gmail.com >
35 class MediaWikiPageNameNormalizer
{
43 * @param Http|null $http
45 public function __construct( Http
$http = null ) {
54 * Returns the normalized form of the given page title, using the
55 * normalization rules of the given site. If the given title is a redirect,
56 * the redirect weill be resolved and the redirect target is returned.
58 * @note This actually makes an API request to the remote site, so beware
59 * that this function is slow and depends on an external service.
61 * @see Site::normalizePageName
65 * @param string $pageName
66 * @param string $apiUrl
69 * @throws \MWException
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' );
77 // Go on call the external site
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 );
84 // Build the args for the specific call
89 'converttitles' => true,
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.
99 $url = wfAppendQuery( $apiUrl, $args );
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__
);
105 if ( $ret === false ) {
106 wfDebugLog( "MediaWikiSite", "call to external site failed: $url" );
110 $data = FormatJson
::decode( $ret, true );
112 if ( !is_array( $data ) ) {
113 wfDebugLog( "MediaWikiSite", "call to <$url> returned bad json: " . $ret );
117 $page = static::extractPageRecord( $data, $pageName );
119 if ( isset( $page['missing'] ) ) {
120 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for a missing page title! "
125 if ( isset( $page['invalid'] ) ) {
126 wfDebugLog( "MediaWikiSite", "call to <$url> returned a marker for an invalid page title! "
131 if ( !isset( $page['title'] ) ) {
132 wfDebugLog( "MediaWikiSite", "call to <$url> did not return a page title! " . $ret );
136 return $page['title'];
140 * Get normalization record for a given page title from an API response.
142 * @param array $externalData A reply from the API on a external server.
143 * @param string $pageTitle Identifies the page at the external site, needing normalization.
145 * @return array|bool A 'page' structure representing the page identified by $pageTitle.
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 ) {
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'] ) ) {
163 // Loop over the tree different named structures, that otherwise are similar
165 'normalized' => 'from',
166 'converted' => 'from',
167 'redirects' => 'from',
170 foreach ( $structs as $listId => $fieldId ) {
171 // Check if the substructure exist at all.
172 if ( !isset( $externalData['query'][$listId] ) ) {
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;
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 ) ) {
189 $pageTitle = $collectedHits[0]['to'];
194 } elseif ( $fieldId === 'title' && is_array( $collectedHits ) ) {
195 // If on the pages structure we should prepare for returning.
197 switch ( count( $collectedHits ) ) {
201 return array_shift( $collectedHits );
207 // should never be here