+ /**
+ * Modify $this->internals and $colours according to language variant linking rules
+ */
+ protected function doVariants( &$colours ) {
+ global $wgContLang;
+ $linkBatch = new LinkBatch();
+ $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
+ $output = $this->parent->getOutput();
+ $linkCache = LinkCache::singleton();
+ $threshold = $this->parent->getOptions()->getStubThreshold();
+ $titlesToBeConverted = '';
+ $titlesAttrs = array();
+
+ // Concatenate titles to a single string, thus we only need auto convert the
+ // single string to all variants. This would improve parser's performance
+ // significantly.
+ foreach ( $this->internals as $ns => $entries ) {
+ foreach ( $entries as $index => $entry ) {
+ $pdbk = $entry['pdbk'];
+ // we only deal with new links (in its first query)
+ if ( !isset( $colours[$pdbk] ) ) {
+ $title = $entry['title'];
+ $titleText = $title->getText();
+ $titlesAttrs[] = array(
+ 'ns' => $ns,
+ 'key' => "$ns:$index",
+ 'titleText' => $titleText,
+ );
+ // separate titles with \0 because it would never appears
+ // in a valid title
+ $titlesToBeConverted .= $titleText . "\0";
+ }
+ }
+ }
+
+ // Now do the conversion and explode string to text of titles
+ $titlesAllVariants = $wgContLang->autoConvertToAllVariants( $titlesToBeConverted );
+ $allVariantsName = array_keys( $titlesAllVariants );
+ foreach ( $titlesAllVariants as &$titlesVariant ) {
+ $titlesVariant = explode( "\0", $titlesVariant );
+ }
+ $l = count( $titlesAttrs );
+ // Then add variants of links to link batch
+ for ( $i = 0; $i < $l; $i ++ ) {
+ foreach ( $allVariantsName as $variantName ) {
+ $textVariant = $titlesAllVariants[$variantName][$i];
+ if ( $textVariant != $titlesAttrs[$i]['titleText'] ) {
+ $variantTitle = Title::makeTitle( $titlesAttrs[$i]['ns'], $textVariant );
+ if( is_null( $variantTitle ) ) {
+ continue;
+ }
+ $linkBatch->addObj( $variantTitle );
+ $variantMap[$variantTitle->getPrefixedDBkey()][] = $titlesAttrs[$i]['key'];
+ }
+ }
+ }
+
+ // process categories, check if a category exists in some variant
+ $categoryMap = array(); // maps $category_variant => $category (dbkeys)
+ $varCategories = array(); // category replacements oldDBkey => newDBkey
+ foreach( $output->getCategoryLinks() as $category ){
+ $variants = $wgContLang->autoConvertToAllVariants( $category );
+ foreach($variants as $variant){
+ if($variant != $category){
+ $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
+ if(is_null($variantTitle)) continue;
+ $linkBatch->addObj( $variantTitle );
+ $categoryMap[$variant] = $category;
+ }
+ }
+ }
+
+
+ if(!$linkBatch->isEmpty()){
+ // construct query
+ $dbr = wfGetDB( DB_SLAVE );
+ $varRes = $dbr->select( 'page',
+ array( 'page_id', 'page_namespace', 'page_title', 'page_is_redirect', 'page_len' ),
+ $linkBatch->constructSet( 'page', $dbr ),
+ __METHOD__
+ );
+
+ $linkcolour_ids = array();
+
+ // for each found variants, figure out link holders and replace
+ foreach ( $varRes as $s ) {
+
+ $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
+ $varPdbk = $variantTitle->getPrefixedDBkey();
+ $vardbk = $variantTitle->getDBkey();
+
+ $holderKeys = array();
+ if( isset( $variantMap[$varPdbk] ) ) {
+ $holderKeys = $variantMap[$varPdbk];
+ $linkCache->addGoodLinkObj( $s->page_id, $variantTitle, $s->page_len, $s->page_is_redirect );
+ $output->addLink( $variantTitle, $s->page_id );
+ }
+
+ // loop over link holders
+ foreach( $holderKeys as $key ) {
+ list( $ns, $index ) = explode( ':', $key, 2 );
+ $entry =& $this->internals[$ns][$index];
+ $pdbk = $entry['pdbk'];
+
+ if(!isset($colours[$pdbk])){
+ // found link in some of the variants, replace the link holder data
+ $entry['title'] = $variantTitle;
+ $entry['pdbk'] = $varPdbk;
+
+ // set pdbk and colour
+ # @todo FIXME: Convoluted data flow
+ # The redirect status and length is passed to getLinkColour via the LinkCache
+ # Use formal parameters instead
+ $colours[$varPdbk] = Linker::getLinkColour( $variantTitle, $threshold );
+ $linkcolour_ids[$s->page_id] = $pdbk;
+ }
+ }
+
+ // check if the object is a variant of a category
+ if(isset($categoryMap[$vardbk])){
+ $oldkey = $categoryMap[$vardbk];
+ if($oldkey != $vardbk)
+ $varCategories[$oldkey]=$vardbk;
+ }
+ }
+ wfRunHooks( 'GetLinkColours', array( $linkcolour_ids, &$colours ) );
+
+ // rebuild the categories in original order (if there are replacements)
+ if(count($varCategories)>0){
+ $newCats = array();
+ $originalCats = $output->getCategories();
+ foreach($originalCats as $cat => $sortkey){
+ // make the replacement
+ if( array_key_exists($cat,$varCategories) )
+ $newCats[$varCategories[$cat]] = $sortkey;
+ else $newCats[$cat] = $sortkey;
+ }
+ $output->setCategoryLinks($newCats);
+ }
+ }
+ }
+