24 use InvalidArgumentException;
27 use UtfNormal\Validator;
46 private $httpRequestFactory;
55 $this->httpRequestFactory = $httpRequestFactory;
83 public function normalizePageName( $pageName, $apiUrl, $followRedirect = self::FOLLOW_REDIRECT ) {
85 if ( !is_string( $pageName ) ) {
86 throw new \MWException(
'$pageName must be a string' );
89 if ( $followRedirect === self::FOLLOW_REDIRECT ) {
91 } elseif ( $followRedirect === self::NOFOLLOW_REDIRECT ) {
94 throw new InvalidArgumentException(
'$followRedirect is not properly set: ' . $followRedirect );
102 $pageName = Validator::cleanUp( $pageName );
108 'redirects' => $redirects,
109 'converttitles' =>
true,
111 'titles' => $pageName,
123 $ret = $this->httpRequestFactory->get( $url, [], __METHOD__ );
125 if ( $ret ===
null ) {
126 wfDebugLog(
"MediaWikiSite",
"call to external site failed: $url" );
132 if ( !is_array( $data ) ) {
133 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned bad json: " . $ret );
137 $page = static::extractPageRecord( $data, $pageName );
139 if ( isset( $page[
'missing'] ) ) {
140 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for a missing page title! "
145 if ( isset( $page[
'invalid'] ) ) {
146 wfDebugLog(
"MediaWikiSite",
"call to <$url> returned a marker for an invalid page title! "
151 if ( !isset( $page[
'title'] ) ) {
152 wfDebugLog(
"MediaWikiSite",
"call to <$url> did not return a page title! " . $ret );
156 return $page[
'title'];
167 private static function extractPageRecord( $externalData, $pageTitle ) {
171 if ( isset( $externalData[
'query'][
'pages'] ) ) {
172 $pages = array_values( $externalData[
'query'][
'pages'] );
173 if ( count( $pages ) === 1 ) {
180 if ( !is_array( $externalData ) || !isset( $externalData[
'query'] ) ) {
185 'normalized' =>
'from',
186 'converted' =>
'from',
187 'redirects' =>
'from',
190 foreach ( $structs as $listId => $fieldId ) {
192 if ( !isset( $externalData[
'query'][$listId] ) ) {
196 $collectedHits = array_filter(
197 array_values( $externalData[
'query'][$listId] ),
198 static function ( $a ) use ( $fieldId, $pageTitle ) {
199 return $a[$fieldId] === $pageTitle;
204 if ( $fieldId ===
'from' && is_array( $collectedHits ) ) {
205 switch ( count( $collectedHits ) ) {
209 $pageTitle = $collectedHits[0][
'to'];
214 } elseif ( $fieldId ===
'title' && is_array( $collectedHits ) ) {
217 switch ( count( $collectedHits ) ) {
221 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.
wfAppendQuery( $url, $query)
Append a query string to an existing URL, which may or may not already have query string parameters a...
Service for normalizing a page name via a MediaWiki action API.
normalizePageName( $pageName, $apiUrl, $followRedirect=self::FOLLOW_REDIRECT)
Returns the normalized form of the given page title, using the normalization rules of the given site.
__construct( $httpRequestFactory=null)