From 1162995b211461d4f021af9fdb4f19a7dc69aa7c Mon Sep 17 00:00:00 2001 From: Bryan Tong Minh Date: Sat, 10 Jul 2010 11:53:22 +0000 Subject: [PATCH] (bug 24296) Added converttitles parameter to convert titles to their canonical language variant. --- RELEASE-NOTES | 3 ++- includes/api/ApiPageSet.php | 35 +++++++++++++++++++++++++++++++++-- includes/api/ApiQuery.php | 21 +++++++++++++++++++-- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 951f7187ea..2068d62787 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -272,7 +272,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 24185) Titles in the Media and Special namespace are now supported for title normalization in action=query. Special pages have their name resolved to the local alias. - +* (bug 24296) Added converttitles parameter to convert titles to their + canonical language variant. === Languages updated in 1.17 === diff --git a/includes/api/ApiPageSet.php b/includes/api/ApiPageSet.php index 96be5ef02e..ada85432d4 100644 --- a/includes/api/ApiPageSet.php +++ b/includes/api/ApiPageSet.php @@ -48,6 +48,7 @@ class ApiPageSet extends ApiQueryBase { private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles; private $mNormalizedTitles, $mInterwikiTitles; private $mResolveRedirects, $mPendingRedirectIDs; + private $mConvertTitles, $mConvertedTitles; private $mGoodRevIDs, $mMissingRevIDs; private $mFakePageId; @@ -58,7 +59,7 @@ class ApiPageSet extends ApiQueryBase { * @param $query ApiQuery * @param $resolveRedirects bool Whether redirects should be resolved */ - public function __construct( $query, $resolveRedirects = false ) { + public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) { parent::__construct( $query, 'query' ); $this->mAllPages = array(); @@ -79,6 +80,9 @@ class ApiPageSet extends ApiQueryBase { if ( $resolveRedirects ) { $this->mPendingRedirectIDs = array(); } + + $this->mConvertTitles = $convertTitles; + $this->mConvertedTitles = array(); $this->mFakePageId = - 1; } @@ -221,6 +225,15 @@ class ApiPageSet extends ApiQueryBase { public function getNormalizedTitles() { return $this->mNormalizedTitles; } + + /** + * Get a list of title conversions - maps a title to its converted + * version. + * @return array raw_prefixed_title (string) => prefixed_title (string) + */ + public function getConvertedTitles() { + return $this->mConvertedTitles; + } /** * Get a list of interwiki titles - maps a title to its interwiki @@ -653,16 +666,32 @@ class ApiPageSet extends ApiQueryBase { $this->mFakePageId--; continue; // There's nothing else we can do } + $unconvertedTitle = $titleObj->getPrefixedText(); + $titleWasConverted = false; $iw = $titleObj->getInterwiki(); if ( strval( $iw ) !== '' ) { // This title is an interwiki link. $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw; } else { + // Variants checking + global $wgContLang; + if ( $this->mConvertTitles && + count( $wgContLang->getVariants() ) > 1 && + $titleObj->getArticleID() == 0 ) { + // Language::findVariantLink will modify titleObj into + // the canonical variant if possible + $wgContLang->findVariantLink( $title, $titleObj ); + $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText(); + } + + if ( $titleObj->getNamespace() < 0 ) { + // Handle Special and Media pages $titleObj = $titleObj->fixSpecialName(); $this->mSpecialTitles[$this->mFakePageId] = $titleObj; $this->mFakePageId--; } else { + // Regular page $linkBatch->addObj( $titleObj ); } } @@ -672,7 +701,9 @@ class ApiPageSet extends ApiQueryBase { // titles with the originally requested when e.g. the // namespace is localized or the capitalization is // different - if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) { + if ( $titleWasConverted ) { + $this->mConvertedTitles[$title] = $titleObj->getPrefixedText(); + } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) { $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); } } diff --git a/includes/api/ApiQuery.php b/includes/api/ApiQuery.php index 03d0b44c84..c32b6f70f6 100644 --- a/includes/api/ApiQuery.php +++ b/includes/api/ApiQuery.php @@ -221,9 +221,10 @@ class ApiQuery extends ApiBase { public function execute() { $this->params = $this->extractRequestParams(); $this->redirects = $this->params['redirects']; + $this->convertTitles = $this->params['converttitles']; // Create PageSet - $this->mPageSet = new ApiPageSet( $this, $this->redirects ); + $this->mPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles ); // Instantiate requested modules $modules = array(); @@ -307,6 +308,20 @@ class ApiQuery extends ApiBase { $result->setIndexedTagName( $normValues, 'n' ); $result->addValue( 'query', 'normalized', $normValues ); } + + // Title conversions + $convValues = array(); + foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) { + $convValues[] = array( + 'from' => $rawTitleStr, + 'to' => $titleStr + ); + } + + if ( count( $convValues ) ) { + $result->setIndexedTagName( $convValues, 'c' ); + $result->addValue( 'query', 'converted', $convValues ); + } // Interwiki titles $intrwValues = array(); @@ -456,7 +471,7 @@ class ApiQuery extends ApiBase { } // Generator results - $resultPageSet = new ApiPageSet( $this, $this->redirects ); + $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles ); // Create and execute the generator $generator = new $className ( $this, $generatorName ); @@ -502,6 +517,7 @@ class ApiQuery extends ApiBase { ApiBase::PARAM_TYPE => $this->mAllowedGenerators ), 'redirects' => false, + 'converttitles' => false, 'indexpageids' => false, 'export' => false, 'exportnowrap' => false, @@ -586,6 +602,7 @@ class ApiQuery extends ApiBase { 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items', 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ), 'redirects' => 'Automatically resolve redirects', + 'converttitles' => 'Automatically convert titles to their canonical form', 'indexpageids' => 'Include an additional pageids section listing all returned page IDs', 'export' => 'Export the current revisions of all given or generated pages', 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export', -- 2.20.1