Aude has uploaded a new change for review. https://gerrit.wikimedia.org/r/246110
Change subject: Introduce displaytext ApiQuery prop [WIP] ...................................................................... Introduce displaytext ApiQuery prop [WIP] Having a (possibly) alternative display text is needed in various places in mobile (nearby, search, watchlist) and elsewhere. "displaytext" uses displaytitle, if set, and otherwise uses Title::getPrefixedText, and has a hook point for extensions such as Wikibase to customize this. For user page titles, displaytitle might sometimes have strange coloring / formatting, but nothing too extreme and usages could normalize for that. Bug: T115014 Bug: T115013 Change-Id: Ie58e042ca560d1da235aa0010896eb6854bb7ce5 --- M autoload.php M includes/api/ApiQuery.php A includes/api/ApiQueryDisplayText.php M includes/api/i18n/en.json M includes/api/i18n/qqq.json 5 files changed, 146 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core refs/changes/10/246110/1 diff --git a/autoload.php b/autoload.php index 673072b..02d662f 100644 --- a/autoload.php +++ b/autoload.php @@ -88,6 +88,7 @@ 'ApiQueryDeletedRevisions' => __DIR__ . '/includes/api/ApiQueryDeletedRevisions.php', 'ApiQueryDeletedrevs' => __DIR__ . '/includes/api/ApiQueryDeletedrevs.php', 'ApiQueryDisabled' => __DIR__ . '/includes/api/ApiQueryDisabled.php', + 'ApiQueryDisplayText' => __DIR__ . '/includes/api/ApiQueryDisplayText.php', 'ApiQueryDuplicateFiles' => __DIR__ . '/includes/api/ApiQueryDuplicateFiles.php', 'ApiQueryExtLinksUsage' => __DIR__ . '/includes/api/ApiQueryExtLinksUsage.php', 'ApiQueryExternalLinks' => __DIR__ . '/includes/api/ApiQueryExternalLinks.php', diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 902bca7..8e2c243 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -46,6 +46,7 @@ 'categoryinfo' => 'ApiQueryCategoryInfo', 'contributors' => 'ApiQueryContributors', 'deletedrevisions' => 'ApiQueryDeletedRevisions', + 'displaytext' => 'ApiQueryDisplayText', 'duplicatefiles' => 'ApiQueryDuplicateFiles', 'extlinks' => 'ApiQueryExternalLinks', 'fileusage' => 'ApiQueryBacklinksprop', diff --git a/includes/api/ApiQueryDisplayText.php b/includes/api/ApiQueryDisplayText.php new file mode 100644 index 0000000..116365c --- /dev/null +++ b/includes/api/ApiQueryDisplayText.php @@ -0,0 +1,140 @@ +<?php + +/** + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * http://www.gnu.org/copyleft/gpl.html + * + * @file + */ + +/** + * Query module to get display text for a page. Default display text + * is Title::getPrefixedText, unless there is a displaytitle or + * other display text set via a hook. + * + * @ingroup API + */ +class ApiQueryDisplayText extends ApiQueryBase { + + /** + * @var array + */ + private $params; + + /** + * @param ApiQuery $query + * @param string $moduleName + */ + public function __construct( ApiQuery $query, $moduleName ) { + parent::__construct( $query, $moduleName, 'dt' ); + } + + public function execute() { + $this->params = $this->extractRequestParams(); + + $titles = $this->getPageSet()->getGoodTitles(); + + if ( !count( $titles ) ) { + return; + } + + $result = $this->getResult(); + $displayTexts = $this->getDisplayTexts( $titles, $this->params['lang'] ); + + foreach ( $titles as $pageId => $title ) { + if ( !isset( $displayTexts[$pageId] ) ) { + throw new RuntimeException( '$displayTexts not set for $pageId ' . $pageId ); + } + + $result->addValue( + array( 'query', 'pages', $pageId ), + 'displaytext', + $displayTexts[$pageId] + ); + } + } + + /** + * @param Title[] $titles + * @param string $langCode + * + * @return array page_id -> display text + */ + private function getDisplayTexts( array $titles, $langCode ) { + $displayTexts = $this->getDisplayTitlePageProps( $titles ); + + Hooks::run( 'ApiQueryDisplayText', array( &$displayTexts, $titles, $langCode ) ); + + foreach ( $titles as $pageId => $title ) { + if ( !isset( $displayTexts[$pageId] ) ) { + $displayTexts[$pageId] = $title->getPrefixedText(); + } + } + + return $displayTexts; + } + + /** + * @param Title[] $titles + * + * @return array page_id -> display title value + */ + private function getDisplayTitlePageProps( array $titles ) { + $this->addTables( 'page_props' ); + $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) ); + $this->addWhereFld( 'pp_page', array_keys( $titles ) ); + + if ( $this->params['continue'] ) { + $this->addWhere( 'pp_page >=' . intval( $this->params['continue'] ) ); + } + + $this->addWhereFld( 'pp_propname', 'displaytitle' ); + + $res = $this->select( __METHOD__ ); + + $displayTitles = array(); + + foreach ( $res as $row ) { + $displayTitles[$row->pp_page] = $row->pp_value; + } + + return $displayTitles; + } + + public function getAllowedParams() { + global $wgContLang; + + return array( + 'lang' => array( + ApiBase::PARAM_DFLT => $wgContLang->getCode() + ), + 'continue' => array( + ApiBase::PARAM_HELP_MSG => 'api-help-param-continue', + ), + ); + } + + protected function getExamplesMessages() { + return array( + 'action=query&prop=displaytext&titles=Main%20Page' + => 'apihelp-query+displaytext-example-simple', + ); + } + + public function getHelpUrls() { + return 'https://www.mediawiki.org/wiki/API:Displaytext'; + } + +} diff --git a/includes/api/i18n/en.json b/includes/api/i18n/en.json index 90d7fa7..bc53053 100644 --- a/includes/api/i18n/en.json +++ b/includes/api/i18n/en.json @@ -658,6 +658,9 @@ "apihelp-query+disabled-description": "This query module has been disabled.", + "apihelp-query+displaytext-param-lang": "Return display text for given language code.", + "apihelp-query+displaytext-example-simple": "Get the display text for the <kbd>Main Page</kbd>.", + "apihelp-query+duplicatefiles-description": "List all files that are duplicates of the given files based on hash values.", "apihelp-query+duplicatefiles-param-limit": "How many duplicate files to return.", "apihelp-query+duplicatefiles-param-dir": "The direction in which to list.", diff --git a/includes/api/i18n/qqq.json b/includes/api/i18n/qqq.json index 9710e16..729602e 100644 --- a/includes/api/i18n/qqq.json +++ b/includes/api/i18n/qqq.json @@ -616,6 +616,7 @@ "apihelp-query+deletedrevs-example-mode3-main": "{{doc-apihelp-example|query+deletedrevs}}", "apihelp-query+deletedrevs-example-mode3-talk": "{{doc-apihelp-example|query+deletedrevs}}", "apihelp-query+disabled-description": "{{doc-apihelp-description|query+disabled}}", + "apihelp-query+displaytext-lang": "{{doc-apihelp-lang|query+displaytext}}", "apihelp-query+duplicatefiles-description": "{{doc-apihelp-description|query+duplicatefiles}}", "apihelp-query+duplicatefiles-param-limit": "{{doc-apihelp-param|query+duplicatefiles|limit}}", "apihelp-query+duplicatefiles-param-dir": "{{doc-apihelp-param|query+duplicatefiles|dir}}", -- To view, visit https://gerrit.wikimedia.org/r/246110 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie58e042ca560d1da235aa0010896eb6854bb7ce5 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/core Gerrit-Branch: master Gerrit-Owner: Aude <aude.w...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits