Add varianttitles parameter to API query prop=info
authorMichael Holloway <mholloway@wikimedia.org>
Wed, 18 Oct 2017 19:42:42 +0000 (15:42 -0400)
committerGergő Tisza <gtisza@wikimedia.org>
Mon, 9 Apr 2018 22:03:19 +0000 (22:03 +0000)
Returns the displaytitle (if present) or title->getPrefixedText for a
page, converted for each language variant configured on the wiki.

Bug: T178446
Change-Id: I35100af3824ca65c4fe5c106d4a6fbe4e5f75046

includes/api/ApiQueryInfo.php
includes/api/i18n/en.json
includes/api/i18n/qqq.json
languages/Language.php

index 5294b1d..1d3c110 100644 (file)
@@ -34,7 +34,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $fld_readable = false, $fld_watched = false,
                $fld_watchers = false, $fld_visitingwatchers = false,
                $fld_notificationtimestamp = false,
-               $fld_preload = false, $fld_displaytitle = false;
+               $fld_preload = false, $fld_displaytitle = false, $fld_varianttitles = false;
 
        private $params;
 
@@ -49,7 +49,7 @@ class ApiQueryInfo extends ApiQueryBase {
                $pageLatest, $pageLength;
 
        private $protections, $restrictionTypes, $watched, $watchers, $visitingwatchers,
-               $notificationtimestamps, $talkids, $subjectids, $displaytitles;
+               $notificationtimestamps, $talkids, $subjectids, $displaytitles, $variantTitles;
        private $showZeroWatchers = false;
 
        private $tokenFunctions;
@@ -306,6 +306,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->fld_readable = isset( $prop['readable'] );
                        $this->fld_preload = isset( $prop['preload'] );
                        $this->fld_displaytitle = isset( $prop['displaytitle'] );
+                       $this->fld_varianttitles = isset( $prop['varianttitles'] );
                }
 
                $pageSet = $this->getPageSet();
@@ -368,6 +369,10 @@ class ApiQueryInfo extends ApiQueryBase {
                        $this->getDisplayTitle();
                }
 
+               if ( $this->fld_varianttitles ) {
+                       $this->getVariantTitles();
+               }
+
                /** @var Title $title */
                foreach ( $this->everything as $pageid => $title ) {
                        $pageInfo = $this->extractPageInfo( $pageid, $title );
@@ -510,6 +515,12 @@ class ApiQueryInfo extends ApiQueryBase {
                        }
                }
 
+               if ( $this->fld_varianttitles ) {
+                       if ( isset( $this->variantTitles[$pageid] ) ) {
+                               $pageInfo['varianttitles'] = $this->variantTitles[$pageid];
+                       }
+               }
+
                if ( $this->params['testactions'] ) {
                        $limit = $this->getMain()->canApiHighLimits() ? self::LIMIT_SML1 : self::LIMIT_SML2;
                        if ( $this->countTestedActions >= $limit ) {
@@ -740,6 +751,32 @@ class ApiQueryInfo extends ApiQueryBase {
                }
        }
 
+       private function getVariantTitles() {
+               if ( !count( $this->titles ) ) {
+                       return;
+               }
+               $this->variantTitles = [];
+               foreach ( $this->titles as $pageId => $t ) {
+                       $this->variantTitles[$pageId] = isset( $this->displaytitles[$pageId] )
+                               ? $this->getAllVariants( $this->displaytitles[$pageId] )
+                               : $this->getAllVariants( $t->getText(), $t->getNamespace() );
+               }
+       }
+
+       private function getAllVariants( $text, $ns = NS_MAIN ) {
+               global $wgContLang;
+               $result = [];
+               foreach ( $wgContLang->getVariants() as $variant ) {
+                       $convertTitle = $wgContLang->autoConvert( $text, $variant );
+                       if ( $ns !== NS_MAIN ) {
+                               $convertNs = $wgContLang->convertNamespace( $ns, $variant );
+                               $convertTitle = $convertNs . ':' . $convertTitle;
+                       }
+                       $result[$variant] = $convertTitle;
+               }
+               return $result;
+       }
+
        /**
         * Get information about watched status and put it in $this->watched
         * and $this->notificationtimestamps
@@ -879,6 +916,7 @@ class ApiQueryInfo extends ApiQueryBase {
                        'url',
                        'preload',
                        'displaytitle',
+                       'varianttitles',
                ];
                if ( array_diff( (array)$params['prop'], $publicProps ) ) {
                        return 'private';
@@ -912,6 +950,7 @@ class ApiQueryInfo extends ApiQueryBase {
                                        'readable', # private
                                        'preload',
                                        'displaytitle',
+                                       'varianttitles',
                                        // If you add more properties here, please consider whether they
                                        // need to be added to getCacheMode()
                                ],
index 35e164f..f05ec09 100644 (file)
        "apihelp-query+info-paramvalue-prop-readable": "Whether the user can read this page.",
        "apihelp-query+info-paramvalue-prop-preload": "Gives the text returned by EditFormPreloadText.",
        "apihelp-query+info-paramvalue-prop-displaytitle": "Gives the manner in which the page title is actually displayed.",
+       "apihelp-query+info-paramvalue-prop-varianttitles": "Gives the display title in all variants of the site content language.",
        "apihelp-query+info-param-testactions": "Test whether the current user can perform certain actions on the page.",
        "apihelp-query+info-param-token": "Use [[Special:ApiHelp/query+tokens|action=query&meta=tokens]] instead.",
        "apihelp-query+info-example-simple": "Get information about the page <kbd>Main Page</kbd>.",
index 96ff10f..787066a 100644 (file)
        "apihelp-query+info-paramvalue-prop-readable": "{{doc-apihelp-paramvalue|query+info|prop|readable}}",
        "apihelp-query+info-paramvalue-prop-preload": "{{doc-apihelp-paramvalue|query+info|prop|preload}}",
        "apihelp-query+info-paramvalue-prop-displaytitle": "{{doc-apihelp-paramvalue|query+info|prop|displaytitle}}",
+       "apihelp-query+info-paramvalue-prop-varianttitles": "{{doc-apihelp-paramvalue|query+info|prop|varianttitles}}",
        "apihelp-query+info-param-testactions": "{{doc-apihelp-param|query+info|testactions}}",
        "apihelp-query+info-param-token": "{{doc-apihelp-param|query+info|token}}",
        "apihelp-query+info-example-simple": "{{doc-apihelp-example|query+info}}",
index b71defa..d750f7d 100644 (file)
@@ -4148,6 +4148,18 @@ class Language {
                return $this->mConverter;
        }
 
+       /**
+        * convert text to a variant
+        *
+        * @param string $text text to convert
+        * @param string|bool $variant variant to convert to, or false to use the user's preferred
+        *      variant (if logged in), or the project default variant
+        * @return string the converted string
+        */
+       public function autoConvert( $text, $variant = false ) {
+               return $this->mConverter->autoConvert( $text, $variant );
+       }
+
        /**
         * convert text to all supported variants
         *
@@ -4181,11 +4193,13 @@ class Language {
        /**
         * Convert a namespace index to a string in the preferred variant
         *
-        * @param int $ns
-        * @return string
+        * @param int $ns namespace index (https://www.mediawiki.org/wiki/Manual:Namespace)
+        * @param string|null $variant variant to convert to, or null to use the user's preferred
+        *      variant (if logged in), or the project default variant
+        * @return string a string representation of the namespace
         */
-       public function convertNamespace( $ns ) {
-               return $this->mConverter->convertNamespace( $ns );
+       public function convertNamespace( $ns, $variant = null ) {
+               return $this->mConverter->convertNamespace( $ns, $variant );
        }
 
        /**