Index: trunk/phase3/includes/api/ApiQuery.php |
— | — | @@ -221,9 +221,10 @@ |
222 | 222 | public function execute() { |
223 | 223 | $this->params = $this->extractRequestParams(); |
224 | 224 | $this->redirects = $this->params['redirects']; |
| 225 | + $this->convertTitles = $this->params['converttitles']; |
225 | 226 | |
226 | 227 | // Create PageSet |
227 | | - $this->mPageSet = new ApiPageSet( $this, $this->redirects ); |
| 228 | + $this->mPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles ); |
228 | 229 | |
229 | 230 | // Instantiate requested modules |
230 | 231 | $modules = array(); |
— | — | @@ -307,7 +308,21 @@ |
308 | 309 | $result->setIndexedTagName( $normValues, 'n' ); |
309 | 310 | $result->addValue( 'query', 'normalized', $normValues ); |
310 | 311 | } |
| 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 | + } |
311 | 321 | |
| 322 | + if ( count( $convValues ) ) { |
| 323 | + $result->setIndexedTagName( $convValues, 'c' ); |
| 324 | + $result->addValue( 'query', 'converted', $convValues ); |
| 325 | + } |
| 326 | + |
312 | 327 | // Interwiki titles |
313 | 328 | $intrwValues = array(); |
314 | 329 | foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) { |
— | — | @@ -456,7 +471,7 @@ |
457 | 472 | } |
458 | 473 | |
459 | 474 | // Generator results |
460 | | - $resultPageSet = new ApiPageSet( $this, $this->redirects ); |
| 475 | + $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles ); |
461 | 476 | |
462 | 477 | // Create and execute the generator |
463 | 478 | $generator = new $className ( $this, $generatorName ); |
— | — | @@ -502,6 +517,7 @@ |
503 | 518 | ApiBase::PARAM_TYPE => $this->mAllowedGenerators |
504 | 519 | ), |
505 | 520 | 'redirects' => false, |
| 521 | + 'converttitles' => false, |
506 | 522 | 'indexpageids' => false, |
507 | 523 | 'export' => false, |
508 | 524 | 'exportnowrap' => false, |
— | — | @@ -586,6 +602,7 @@ |
587 | 603 | 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items', |
588 | 604 | 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ), |
589 | 605 | 'redirects' => 'Automatically resolve redirects', |
| 606 | + 'converttitles' => 'Automatically convert titles to their canonical form', |
590 | 607 | 'indexpageids' => 'Include an additional pageids section listing all returned page IDs', |
591 | 608 | 'export' => 'Export the current revisions of all given or generated pages', |
592 | 609 | '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 @@ |
49 | 49 | private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles; |
50 | 50 | private $mNormalizedTitles, $mInterwikiTitles; |
51 | 51 | private $mResolveRedirects, $mPendingRedirectIDs; |
| 52 | + private $mConvertTitles, $mConvertedTitles; |
52 | 53 | private $mGoodRevIDs, $mMissingRevIDs; |
53 | 54 | private $mFakePageId; |
54 | 55 | |
— | — | @@ -58,7 +59,7 @@ |
59 | 60 | * @param $query ApiQuery |
60 | 61 | * @param $resolveRedirects bool Whether redirects should be resolved |
61 | 62 | */ |
62 | | - public function __construct( $query, $resolveRedirects = false ) { |
| 63 | + public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) { |
63 | 64 | parent::__construct( $query, 'query' ); |
64 | 65 | |
65 | 66 | $this->mAllPages = array(); |
— | — | @@ -79,6 +80,9 @@ |
80 | 81 | if ( $resolveRedirects ) { |
81 | 82 | $this->mPendingRedirectIDs = array(); |
82 | 83 | } |
| 84 | + |
| 85 | + $this->mConvertTitles = $convertTitles; |
| 86 | + $this->mConvertedTitles = array(); |
83 | 87 | |
84 | 88 | $this->mFakePageId = - 1; |
85 | 89 | } |
— | — | @@ -221,6 +225,15 @@ |
222 | 226 | public function getNormalizedTitles() { |
223 | 227 | return $this->mNormalizedTitles; |
224 | 228 | } |
| 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 | + } |
225 | 238 | |
226 | 239 | /** |
227 | 240 | * Get a list of interwiki titles - maps a title to its interwiki |
— | — | @@ -653,16 +666,32 @@ |
654 | 667 | $this->mFakePageId--; |
655 | 668 | continue; // There's nothing else we can do |
656 | 669 | } |
| 670 | + $unconvertedTitle = $titleObj->getPrefixedText(); |
| 671 | + $titleWasConverted = false; |
657 | 672 | $iw = $titleObj->getInterwiki(); |
658 | 673 | if ( strval( $iw ) !== '' ) { |
659 | 674 | // This title is an interwiki link. |
660 | 675 | $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw; |
661 | 676 | } 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 | + |
662 | 689 | if ( $titleObj->getNamespace() < 0 ) { |
| 690 | + // Handle Special and Media pages |
663 | 691 | $titleObj = $titleObj->fixSpecialName(); |
664 | 692 | $this->mSpecialTitles[$this->mFakePageId] = $titleObj; |
665 | 693 | $this->mFakePageId--; |
666 | 694 | } else { |
| 695 | + // Regular page |
667 | 696 | $linkBatch->addObj( $titleObj ); |
668 | 697 | } |
669 | 698 | } |
— | — | @@ -672,7 +701,9 @@ |
673 | 702 | // titles with the originally requested when e.g. the |
674 | 703 | // namespace is localized or the capitalization is |
675 | 704 | // 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() ) { |
677 | 708 | $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText(); |
678 | 709 | } |
679 | 710 | } |
Index: trunk/phase3/RELEASE-NOTES |
— | — | @@ -272,7 +272,8 @@ |
273 | 273 | * (bug 24185) Titles in the Media and Special namespace are now supported for |
274 | 274 | title normalization in action=query. Special pages have their name resolved |
275 | 275 | to the local alias. |
276 | | - |
| 276 | +* (bug 24296) Added converttitles parameter to convert titles to their |
| 277 | + canonical language variant. |
277 | 278 | |
278 | 279 | === Languages updated in 1.17 === |
279 | 280 | |