32 private $httpRequestFactory;
41 $this->httpRequestFactory = $httpRequestFactory;
67 public function normalizePageName(
string $pageName, $apiUrl, $followRedirect = self::FOLLOW_REDIRECT ) {
68 if ( $followRedirect === self::FOLLOW_REDIRECT ) {
70 } elseif ( $followRedirect === self::NOFOLLOW_REDIRECT ) {
73 throw new InvalidArgumentException(
'$followRedirect is not properly set: ' . $followRedirect );
81 $pageName = Validator::cleanUp( $pageName );
87 'redirects' => $redirects,
88 'converttitles' =>
true,
90 'titles' => $pageName,
102 $ret = $this->httpRequestFactory->get(
$url, [], __METHOD__ );
104 if ( $ret ===
null ) {
105 wfDebugLog(
"MediaWikiSite",
"call to external site failed: $url" );
109 $data = FormatJson::decode( $ret,
true );
111 if ( !is_array( $data ) ) {
112 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned bad json: " . $ret );
116 $page = static::extractPageRecord( $data, $pageName );
118 if ( isset( $page[
'missing'] ) ) {
119 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for a missing page title! "
124 if ( isset( $page[
'invalid'] ) ) {
125 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for an invalid page title! "
130 if ( !isset( $page[
'title'] ) ) {
131 wfDebugLog(
"MediaWikiSite",
"call to <$url> did not return a page title! " . $ret );
135 return $page[
'title'];
146 private static function extractPageRecord( $externalData, $pageTitle ) {
150 if ( isset( $externalData[
'query'][
'pages'] ) ) {
151 $pages = array_values( $externalData[
'query'][
'pages'] );
152 if ( count( $pages ) === 1 ) {
159 if ( !is_array( $externalData ) || !isset( $externalData[
'query'] ) ) {
164 'normalized' =>
'from',
165 'converted' =>
'from',
166 'redirects' =>
'from',
169 foreach ( $structs as $listId => $fieldId ) {
171 if ( !isset( $externalData[
'query'][$listId] ) ) {
175 $collectedHits = array_filter(
176 array_values( $externalData[
'query'][$listId] ),
177 static function ( $a ) use ( $fieldId, $pageTitle ) {
178 return $a[$fieldId] === $pageTitle;
183 if ( $fieldId ===
'from' && is_array( $collectedHits ) ) {
184 switch ( count( $collectedHits ) ) {
188 $pageTitle = $collectedHits[0][
'to'];
193 } elseif ( $fieldId ===
'title' && is_array( $collectedHits ) ) {
196 switch ( count( $collectedHits ) ) {
200 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.