* Enable category names to be written in variants (use single linkbatch for both...
authorRobert Stojnić <rainman@users.mediawiki.org>
Mon, 11 Dec 2006 23:33:27 +0000 (23:33 +0000)
committerRobert Stojnić <rainman@users.mediawiki.org>
Mon, 11 Dec 2006 23:33:27 +0000 (23:33 +0000)
* introduce armourMath(), use to put -{ }- around math if needed (not needed for zh, but needed for sr, and probably kk)

includes/Parser.php
includes/StubObject.php
languages/Language.php
languages/LanguageConverter.php
languages/classes/LanguageSr.php

index b1f5fbb..ad39cbd 100644 (file)
@@ -529,6 +529,7 @@ class Parser
         * @private
         */
        function strip( $text, $state, $stripcomments = false , $dontstrip = array () ) {
+               global $wgContLang;
                wfProfileIn( __METHOD__ );
                $render = ($this->mOutputType == OT_HTML);
 
@@ -581,7 +582,7 @@ class Parser
                                        $output = Xml::escapeTagsOnly( $content );
                                        break;
                                case 'math':
-                                       $output = MathRenderer::renderMath( $content );
+                                       $output = $wgContLang->armourMath( MathRenderer::renderMath( $content ) );
                                        break;
                                case 'gallery':
                                        $output = $this->renderImageGallery( $content, $params );
@@ -3013,9 +3014,8 @@ class Parser
 
                        if ( !is_null( $title ) ) {
                                $titleText = $title->getPrefixedText();
-                               $checkVariantLink = sizeof($wgContLang->getVariants())>1;
                                # Check for language variants if the template is not found
-                               if($checkVariantLink && $title->getArticleID() == 0){
+                               if($wgContLang->hasVariants() && $title->getArticleID() == 0){
                                        $wgContLang->findVariantLink($part1, $title);
                                }
 
@@ -4038,10 +4038,14 @@ class Parser
                        }
                        wfProfileOut( $fname.'-check' );
 
-                       # Do a second query for different language variants of links (if needed)
+                       # Do a second query for different language variants of links and categories
                        if($wgContLang->hasVariants()){
                                $linkBatch = new LinkBatch();
-                               $variantMap = array(); // maps $pdbkey_Variant => $pdbkey_original
+                               $variantMap = array(); // maps $pdbkey_Variant => $keys (of link holders)
+                               $categoryMap = array(); // maps $category_variant => $category (dbkeys)
+                               $varCategories = array(); // category replacements oldDBkey => newDBkey
+
+                               $categories = $this->mOutput->getCategoryLinks();
 
                                // Add variants of links to link batch
                                foreach ( $this->mLinkHolders['namespaces'] as $key => $ns ) {
@@ -4050,21 +4054,35 @@ class Parser
                                                continue;
 
                                        $pdbk = $title->getPrefixedDBkey();
+                                       $titleText = $title->getText();
 
                                        // generate all variants of the link title text
-                                       $allTextVariants = $wgContLang->convertLinkToAllVariants($title->getText());
+                                       $allTextVariants = $wgContLang->convertLinkToAllVariants($titleText);
 
                                        // if link was not found (in first query), add all variants to query
                                        if ( !isset($colours[$pdbk]) ){
                                                foreach($allTextVariants as $textVariant){
-                                                       $variantTitle = Title::makeTitle( $ns, $textVariant );
-                                                       if(is_null($variantTitle)) continue;
-                                                       $linkBatch->addObj( $variantTitle );
-                                                       $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
+                                                       if($textVariant != $titleText){
+                                                               $variantTitle = Title::makeTitle( $ns, $textVariant );
+                                                               if(is_null($variantTitle)) continue;
+                                                               $linkBatch->addObj( $variantTitle );
+                                                               $variantMap[$variantTitle->getPrefixedDBkey()][] = $key;
+                                                       }
                                                }
                                        }
                                }
 
+                               // process categories, check if a category exists in some variant
+                               foreach( $categories as $category){
+                                       $variants = $wgContLang->convertLinkToAllVariants($category);
+                                       foreach($variants as $variant){
+                                               $variantTitle = Title::newFromDBkey( Title::makeName(NS_CATEGORY,$variant) );
+                                               if(is_null($variantTitle)) continue;
+                                               $linkBatch->addObj( $variantTitle );
+                                               $categoryMap[$variant] = $category;
+                                       }
+                               }
+
 
                                if(!$linkBatch->isEmpty()){
                                        // construct query
@@ -4087,10 +4105,13 @@ class Parser
 
                                                $variantTitle = Title::makeTitle( $s->page_namespace, $s->page_title );
                                                $varPdbk = $variantTitle->getPrefixedDBkey();
+                                               $vardbk = $variantTitle->getDBkey();
                                                $linkCache->addGoodLinkObj( $s->page_id, $variantTitle );
                                                $this->mOutput->addLink( $variantTitle, $s->page_id );
 
-                                               $holderKeys = $variantMap[$varPdbk];
+                                               $holderKeys = array();
+                                               if(isset($variantMap[$varPdbk]))
+                                                       $holderKeys = $variantMap[$varPdbk];
 
                                                // loop over link holders
                                                foreach($holderKeys as $key){
@@ -4119,6 +4140,26 @@ class Parser
                                                                }
                                                        }
                                                }
+
+                                               // check if the object is a variant of a category
+                                               if(isset($categoryMap[$vardbk])){
+                                                       $oldkey = $categoryMap[$vardbk];
+                                                       if($oldkey != $vardbk)
+                                                               $varCategories[$oldkey]=$vardbk;                                                        
+                                               }                                               
+                                       }
+
+                                       // rebuild the categories in original order (if there are replacements)
+                                       if(count($varCategories)>0){
+                                               $newCats = array();
+                                               $originalCats = $this->mOutput->getCategories();
+                                               foreach($originalCats as $cat => $sortkey){
+                                                       // make the replacement
+                                                       if( array_key_exists($cat,$varCategories) )
+                                                               $newCats[$varCategories[$cat]] = $sortkey;
+                                                       else $newCats[$cat] = $sortkey;
+                                               }
+                                               $this->mOutput->setCategoryLinks($newCats);
                                        }
                                }
                        }
index 4068c31..0ab44c9 100644 (file)
@@ -96,7 +96,7 @@ class StubUserLang extends StubObject {
                // if variant is explicitely selected, use it instead the one from wgUser
                // see bug #7605
                if($wgContLang->hasVariants()){
-                       $variant = $wgContLang->getPreferredVariant(false);
+                       $variant = $wgContLang->getPreferredVariant();
                        if($variant != $wgContLanguageCode)
                                $code = $variant;
                }        
index dacb2aa..493b6a0 100644 (file)
@@ -51,7 +51,7 @@ class FakeConverter {
        function markNoConversion($text, $noParse=false) {return $text;}
        function convertCategoryKey( $key ) {return $key; }
        function convertLinkToAllVariants($text){ return array( $this->mLang->getCode() => $text); }
-       function setNoTitleConvert(){}
+       function armourMath($text){ return $text; }
 }
 
 #--------------------------------------------------------------------------
@@ -1340,16 +1340,16 @@ class Language {
                return $this->mConverter->parserConvert( $text, $parser );
        }
 
-       # Tell the converter that it shouldn't convert titles
-       function setNoTitleConvert(){
-               $this->mConverter->setNotitleConvert();
-       }
-
        # Check if this is a language with variants
        function hasVariants(){
                return sizeof($this->getVariants())>1;
        }
 
+       # Put custom tags (e.g. -{ }-) around math to prevent conversion
+       function armourMath($text){ 
+               return $this->mConverter->armourMath($text);
+       }
+
 
        /**
         * Perform output conversion on a string, and encode for safe HTML output.
index 86e025c..09225e2 100644 (file)
@@ -20,7 +20,6 @@ class LanguageConverter {
        var $mMarkup;
        var $mFlags;
        var $mUcfirst = false;
-       var $mNoTitleConvert = false;
        /**
      * Constructor
         *
@@ -303,11 +302,6 @@ class LanguageConverter {
                        return $text;
 
                if( $isTitle ) {
-                       if($this->mNoTitleConvert){
-                               $this->mTitleDisplay = $text;                   
-                               return $text;
-                       }
-
                        if( !$this->mDoTitleConvert ) {
                                $this->mTitleDisplay = $text;
                                return $text;
@@ -449,17 +443,34 @@ class LanguageConverter {
         */
        function findVariantLink( &$link, &$nt ) {
                global $wgDisableLangConversion;
-               $pref = $this->getPreferredVariant();
-               $ns=0;
+               $linkBatch = new LinkBatch();
+
+               $ns=NS_MAIN;
+
                if(is_object($nt))
                        $ns = $nt->getNamespace();
 
                $variants = $this->autoConvertToAllVariants($link);
                if($variants == false) //give up
                        return;
+
+               $titles = array();
+
                foreach( $variants as $v ) {
-                       $varnt = Title::newFromText( $v, $ns );
-                       if( $varnt && $varnt->getArticleID() > 0 ) {
+                       if($v != $link){
+                               $varnt = Title::newFromText( $v, $ns );
+                               if(!is_null($varnt)){
+                                       $linkBatch->addObj($varnt);
+                                       $titles[]=$varnt;
+                               }
+                       }
+               }
+
+               // fetch all variants in single query
+               $linkBatch->execute();
+
+               foreach( $titles as $varnt ) {
+                       if( $varnt->getArticleID() > 0 ) {
                                $nt = $varnt;
                                if( !$wgDisableLangConversion )
                                        $link = $v;
@@ -724,10 +735,16 @@ class LanguageConverter {
                return true;
        }
 
-       function setNoTitleConvert(){
-               $this->mNoTitleConvert = true;
+       /** 
+        * Armour rendered math against conversion
+        * Default is do nothing, since the process can interfere with 
+        * parseManualRule() if format -{ alter1 ; alter2 }- is enabled
+        */
+       function armourMath($text){ 
+               return $text;
        }
 
+
 }
 
 ?>
index fadfb35..7f3e63c 100644 (file)
@@ -52,6 +52,8 @@ class SrConverter extends LanguageConverter {
                'Nj' => 'Њ', 'n!j' => 'нј', 'N!j'=> 'Нј', 'N!J'=> 'НЈ'
        );
 
+       var $mParsingContent=false;
+
        function loadDefaultTables() {
                $this->mTables = array(
                        'sr-ec' => new ReplacementArray( $this->mToCyrillics ),
@@ -76,17 +78,27 @@ class SrConverter extends LanguageConverter {
                return $carray;
        }
 
+       // Set a flag when parsing content, this is used to prevent 
+       // conversion of content within talk pages
+       function parserConvert( $text, &$parser ){
+               $this->mParsingContent = true;
+               $output = parent::parserConvert($text, $parser );
+               $this->mParsingContent = false;
+               return $output;
+               
+       }
+
        /*
         * Override function from LanguageConvertor
         * Additional checks: 
         *  - There should be no conversion for Talk pages
         */
-       function getPreferredVariant(){
+       function getPreferredVariant( $fromUser=true ){
                global $wgTitle;
-               if($wgTitle!=NULL && $wgTitle->isTalkPage()){
+               if(is_object($wgTitle) && $wgTitle->isTalkPage() && $this->mParsingContent){
                        return $this->mMainLanguageCode;
                }
-               return parent::getPreferredVariant();
+               return parent::getPreferredVariant($fromUser);
        }
 
 
@@ -101,7 +113,6 @@ class SrConverter extends LanguageConverter {
                        $link=$oldlink;
        }
 
-
        /*
         * We want our external link captions to be converted in variants,
         * so we return the original text instead -{$text}-, except for URLs
@@ -118,7 +129,7 @@ class SrConverter extends LanguageConverter {
         */
        function autoConvert($text, $toVariant=false) {
                global $wgTitle;
-               if($wgTitle->getNameSpace()==NS_IMAGE){ 
+               if(is_object($wgTitle) && $wgTitle->getNameSpace()==NS_IMAGE){ 
                        $imagename = $wgTitle->getNsText();
                        if(preg_match("/^$imagename:/",$text)) return $text;
                }
@@ -150,6 +161,11 @@ class SrConverter extends LanguageConverter {
 
                return $ret;
        }
+
+       function armourMath($text){ 
+               return parent::markNoConversion($text);
+       }
+
 }
 
 class LanguageSr extends LanguageSr_ec {