46 private $httpRequestFactory;
55 $this->httpRequestFactory = $httpRequestFactory;
81 public function normalizePageName(
string $pageName, $apiUrl, $followRedirect = self::FOLLOW_REDIRECT ) {
82 if ( $followRedirect === self::FOLLOW_REDIRECT ) {
84 } elseif ( $followRedirect === self::NOFOLLOW_REDIRECT ) {
87 throw new InvalidArgumentException(
'$followRedirect is not properly set: ' . $followRedirect );
95 $pageName = Validator::cleanUp( $pageName );
101 'redirects' => $redirects,
102 'converttitles' =>
true,
104 'titles' => $pageName,
116 $ret = $this->httpRequestFactory->get( $url, [], __METHOD__ );
118 if ( $ret ===
null ) {
119 wfDebugLog(
"MediaWikiSite",
"call to external site failed: $url" );
123 $data = FormatJson::decode( $ret,
true );
125 if ( !is_array( $data ) ) {
126 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned bad json: " . $ret );
130 $page = static::extractPageRecord( $data, $pageName );
132 if ( isset( $page[
'missing'] ) ) {
133 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for a missing page title! "
138 if ( isset( $page[
'invalid'] ) ) {
139 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for an invalid page title! "
144 if ( !isset( $page[
'title'] ) ) {
145 wfDebugLog(
"MediaWikiSite",
"call to <$url> did not return a page title! " . $ret );
149 return $page[
'title'];
160 private static function extractPageRecord( $externalData, $pageTitle ) {
164 if ( isset( $externalData[
'query'][
'pages'] ) ) {
165 $pages = array_values( $externalData[
'query'][
'pages'] );
166 if ( count( $pages ) === 1 ) {
173 if ( !is_array( $externalData ) || !isset( $externalData[
'query'] ) ) {
178 'normalized' =>
'from',
179 'converted' =>
'from',
180 'redirects' =>
'from',
183 foreach ( $structs as $listId => $fieldId ) {
185 if ( !isset( $externalData[
'query'][$listId] ) ) {
189 $collectedHits = array_filter(
190 array_values( $externalData[
'query'][$listId] ),
191 static function ( $a ) use ( $fieldId, $pageTitle ) {
192 return $a[$fieldId] === $pageTitle;
197 if ( $fieldId ===
'from' && is_array( $collectedHits ) ) {
198 switch ( count( $collectedHits ) ) {
202 $pageTitle = $collectedHits[0][
'to'];
207 } elseif ( $fieldId ===
'title' && is_array( $collectedHits ) ) {
210 switch ( count( $collectedHits ) ) {
214 return array_shift( $collectedHits );
wfDebugLog( $logGroup, $text, $dest='all', array $context=[])
Send a line to a supplementary debug log file, if configured, or main debug log if not.