82 public function normalizePageName( $pageName, $apiUrl, $followRedirect = self::FOLLOW_REDIRECT ) {
84 if ( !is_string( $pageName ) ) {
85 throw new \MWException(
'$pageName must be a string' );
88 if ( $followRedirect === self::FOLLOW_REDIRECT ) {
90 } elseif ( $followRedirect === self::NOFOLLOW_REDIRECT ) {
93 throw new InvalidArgumentException(
'$followRedirect is not properly set: ' . $followRedirect );
101 $pageName = Validator::cleanUp( $pageName );
107 'redirects' => $redirects,
108 'converttitles' =>
true,
110 'titles' => $pageName,
122 $ret = $this->http->get( $url, [], __METHOD__ );
124 if ( $ret ===
false ) {
125 wfDebugLog(
"MediaWikiSite",
"call to external site failed: $url" );
129 $data = FormatJson::decode( $ret,
true );
131 if ( !is_array( $data ) ) {
132 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned bad json: " . $ret );
136 $page = static::extractPageRecord( $data, $pageName );
138 if ( isset( $page[
'missing'] ) ) {
139 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for a missing page title! "
144 if ( isset( $page[
'invalid'] ) ) {
145 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for an invalid page title! "
150 if ( !isset( $page[
'title'] ) ) {
151 wfDebugLog(
"MediaWikiSite",
"call to <$url> did not return a page title! " . $ret );
155 return $page[
'title'];
170 if ( isset( $externalData[
'query'][
'pages'] ) ) {
171 $pages = array_values( $externalData[
'query'][
'pages'] );
172 if ( count( $pages ) === 1 ) {
179 if ( !is_array( $externalData ) || !isset( $externalData[
'query'] ) ) {
184 'normalized' =>
'from',
185 'converted' =>
'from',
186 'redirects' =>
'from',
189 foreach ( $structs as $listId => $fieldId ) {
191 if ( !isset( $externalData[
'query'][$listId] ) ) {
195 $collectedHits = array_filter(
196 array_values( $externalData[
'query'][$listId] ),
197 static function ( $a ) use ( $fieldId, $pageTitle ) {
198 return $a[$fieldId] === $pageTitle;
203 if ( $fieldId ===
'from' && is_array( $collectedHits ) ) {
204 switch ( count( $collectedHits ) ) {
208 $pageTitle = $collectedHits[0][
'to'];
213 } elseif ( $fieldId ===
'title' && is_array( $collectedHits ) ) {
216 switch ( count( $collectedHits ) ) {
220 return array_shift( $collectedHits );