From e6e9a8279bc1aa553fb6efeef2e097930127eb51 Mon Sep 17 00:00:00 2001 From: Michael Holloway Date: Wed, 18 Oct 2017 15:42:42 -0400 Subject: [PATCH] Add varianttitles parameter to API query prop=info 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 | 43 +++++++++++++++++++++++++++++++++-- includes/api/i18n/en.json | 1 + includes/api/i18n/qqq.json | 1 + languages/Language.php | 22 ++++++++++++++---- 4 files changed, 61 insertions(+), 6 deletions(-) diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index 5294b1d8e7..1d3c110c3a 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -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() ], diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 35e164fd6f..f05ec09b0b 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -898,6 +898,7 @@ "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 Main Page.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 96ff10fda2..787066acd6 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -840,6 +840,7 @@ "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}}", diff --git a/languages/Language.php b/languages/Language.php index b71defa46f..d750f7d519 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -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 ); } /** -- 2.20.1