r69237 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69236‎ | r69237 | r69238 >
Date:11:53, 10 July 2010
Author:btongminh
Status:ok
Tags:
Comment:
(bug 24296) Added converttitles parameter to convert titles to their canonical language variant.
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/api/ApiPageSet.php (modified) (history)
  • /trunk/phase3/includes/api/ApiQuery.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/api/ApiQuery.php
@@ -221,9 +221,10 @@
222222 public function execute() {
223223 $this->params = $this->extractRequestParams();
224224 $this->redirects = $this->params['redirects'];
 225+ $this->convertTitles = $this->params['converttitles'];
225226
226227 // Create PageSet
227 - $this->mPageSet = new ApiPageSet( $this, $this->redirects );
 228+ $this->mPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
228229
229230 // Instantiate requested modules
230231 $modules = array();
@@ -307,7 +308,21 @@
308309 $result->setIndexedTagName( $normValues, 'n' );
309310 $result->addValue( 'query', 'normalized', $normValues );
310311 }
 312+
 313+ // Title conversions
 314+ $convValues = array();
 315+ foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) {
 316+ $convValues[] = array(
 317+ 'from' => $rawTitleStr,
 318+ 'to' => $titleStr
 319+ );
 320+ }
311321
 322+ if ( count( $convValues ) ) {
 323+ $result->setIndexedTagName( $convValues, 'c' );
 324+ $result->addValue( 'query', 'converted', $convValues );
 325+ }
 326+
312327 // Interwiki titles
313328 $intrwValues = array();
314329 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
@@ -456,7 +471,7 @@
457472 }
458473
459474 // Generator results
460 - $resultPageSet = new ApiPageSet( $this, $this->redirects );
 475+ $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
461476
462477 // Create and execute the generator
463478 $generator = new $className ( $this, $generatorName );
@@ -502,6 +517,7 @@
503518 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
504519 ),
505520 'redirects' => false,
 521+ 'converttitles' => false,
506522 'indexpageids' => false,
507523 'export' => false,
508524 'exportnowrap' => false,
@@ -586,6 +602,7 @@
587603 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
588604 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
589605 'redirects' => 'Automatically resolve redirects',
 606+ 'converttitles' => 'Automatically convert titles to their canonical form',
590607 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
591608 'export' => 'Export the current revisions of all given or generated pages',
592609 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
Index: trunk/phase3/includes/api/ApiPageSet.php
@@ -48,6 +48,7 @@
4949 private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles;
5050 private $mNormalizedTitles, $mInterwikiTitles;
5151 private $mResolveRedirects, $mPendingRedirectIDs;
 52+ private $mConvertTitles, $mConvertedTitles;
5253 private $mGoodRevIDs, $mMissingRevIDs;
5354 private $mFakePageId;
5455
@@ -58,7 +59,7 @@
5960 * @param $query ApiQuery
6061 * @param $resolveRedirects bool Whether redirects should be resolved
6162 */
62 - public function __construct( $query, $resolveRedirects = false ) {
 63+ public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) {
6364 parent::__construct( $query, 'query' );
6465
6566 $this->mAllPages = array();
@@ -79,6 +80,9 @@
8081 if ( $resolveRedirects ) {
8182 $this->mPendingRedirectIDs = array();
8283 }
 84+
 85+ $this->mConvertTitles = $convertTitles;
 86+ $this->mConvertedTitles = array();
8387
8488 $this->mFakePageId = - 1;
8589 }
@@ -221,6 +225,15 @@
222226 public function getNormalizedTitles() {
223227 return $this->mNormalizedTitles;
224228 }
 229+
 230+ /**
 231+ * Get a list of title conversions - maps a title to its converted
 232+ * version.
 233+ * @return array raw_prefixed_title (string) => prefixed_title (string)
 234+ */
 235+ public function getConvertedTitles() {
 236+ return $this->mConvertedTitles;
 237+ }
225238
226239 /**
227240 * Get a list of interwiki titles - maps a title to its interwiki
@@ -653,16 +666,32 @@
654667 $this->mFakePageId--;
655668 continue; // There's nothing else we can do
656669 }
 670+ $unconvertedTitle = $titleObj->getPrefixedText();
 671+ $titleWasConverted = false;
657672 $iw = $titleObj->getInterwiki();
658673 if ( strval( $iw ) !== '' ) {
659674 // This title is an interwiki link.
660675 $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
661676 } else {
 677+ // Variants checking
 678+ global $wgContLang;
 679+ if ( $this->mConvertTitles &&
 680+ count( $wgContLang->getVariants() ) > 1 &&
 681+ $titleObj->getArticleID() == 0 ) {
 682+ // Language::findVariantLink will modify titleObj into
 683+ // the canonical variant if possible
 684+ $wgContLang->findVariantLink( $title, $titleObj );
 685+ $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
 686+ }
 687+
 688+
662689 if ( $titleObj->getNamespace() < 0 ) {
 690+ // Handle Special and Media pages
663691 $titleObj = $titleObj->fixSpecialName();
664692 $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
665693 $this->mFakePageId--;
666694 } else {
 695+ // Regular page
667696 $linkBatch->addObj( $titleObj );
668697 }
669698 }
@@ -672,7 +701,9 @@
673702 // titles with the originally requested when e.g. the
674703 // namespace is localized or the capitalization is
675704 // different
676 - if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
 705+ if ( $titleWasConverted ) {
 706+ $this->mConvertedTitles[$title] = $titleObj->getPrefixedText();
 707+ } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
677708 $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
678709 }
679710 }
Index: trunk/phase3/RELEASE-NOTES
@@ -272,7 +272,8 @@
273273 * (bug 24185) Titles in the Media and Special namespace are now supported for
274274 title normalization in action=query. Special pages have their name resolved
275275 to the local alias.
276 -
 276+* (bug 24296) Added converttitles parameter to convert titles to their
 277+ canonical language variant.
277278
278279 === Languages updated in 1.17 ===
279280

Follow-up revisions

RevisionCommit summaryAuthorDate
r69587Followup to r69237: clarify documentation to avoid confusion with title norma...catrope09:24, 20 July 2010
r69671UsabilityInitiative: Partial fix for bug 24052: make existence check succeed ...catrope10:50, 21 July 2010

Status & tagging log