http://www.mediawiki.org/wiki/Special:Code/MediaWiki/69146
Revision: 69146 Author: philip Date: 2010-07-07 14:46:27 +0000 (Wed, 07 Jul 2010) Log Message: ----------- Continue to clean up the LanguageConverter.php. Modified Paths: -------------- trunk/phase3/languages/LanguageConverter.php Modified: trunk/phase3/languages/LanguageConverter.php =================================================================== --- trunk/phase3/languages/LanguageConverter.php 2010-07-07 14:46:11 UTC (rev 69145) +++ trunk/phase3/languages/LanguageConverter.php 2010-07-07 14:46:27 UTC (rev 69146) @@ -69,7 +69,7 @@ 'D' => 'D', // convert description (subclass implement) '-' => '-', // remove convert (not implement) 'H' => 'H', // add rule for convert code - // (but no display in placed code ) + // (but no display in placed code ) 'N' => 'N' // current variant name ); $this->mFlags = array_merge( $defaultflags, $flags ); @@ -102,7 +102,7 @@ * * @param $variant String: the language code of the variant * @return String: The code of the fallback language or the - * main code if there is no fallback + * main code if there is no fallback */ public function getVariantFallbacks( $variant ) { if ( isset( $this->mVariantFallbacks[$variant] ) ) { @@ -446,20 +446,22 @@ } /** - * Prepare manual conversion table. - * @private + * Apply manual conversion rules. + * + * @param $convRule Object: Object of ConverterRule */ - function applyManualConv( $convRule ) { + protected function applyManualConv( $convRule ) { // Use syntax -{T|zh-cn:TitleCN; zh-tw:TitleTw}- to custom // title conversion. - // Bug 24072: mConvRuleTitle won't work if the title conversion - // rule was followed by other manual conversion rule(s). + // Bug 24072: $mConvRuleTitle was overwritten by other manual + // rule(s) not for title, this breaks the title conversion. $newConvRuleTitle = $convRule->getTitle(); if( $newConvRuleTitle ) { + // So I add an empty check for getTitle() $this->mConvRuleTitle = $newConvRuleTitle; } - // apply manual conversion table to global table + // merge/remove manual conversion rules to/from global table $convTable = $convRule->getConvTable(); $action = $convRule->getRulesAction(); foreach ( $convTable as $variant => $pair ) { @@ -483,31 +485,12 @@ } /** - * Convert text to different variants of a language. The automatic - * conversion is done in autoConvert(). Here we parse the text - * marked with -{}-, which specifies special conversions of the - * text that can not be accomplished in autoConvert(). + * Auto convert a Title object to a readable string in the + * preferred variant. * - * Syntax of the markup: - * -{code1:text1;code2:text2;...}- or - * -{flags|code1:text1;code2:text2;...}- or - * -{text}- in which case no conversion should take place for text - * - * @param $text String: text to be converted - * @return String: converted text + *...@param $title Object: a object of Title + *...@return String: converted title text */ - public function convert( $text ) { - global $wgDisableLangConversion; - if ( $wgDisableLangConversion ) return $text; - - $variant = $this->getPreferredVariant(); - - return $this->recursiveConvertTopLevel( $text, $variant ); - } - - /** - * Convert a Title object to a readable string in the preferred variant - */ public function convertTitle( $title ) { $variant = $this->getPreferredVariant(); $index = $title->getNamespace(); @@ -527,10 +510,51 @@ $text .= ':'; } $text .= $title->getText(); - $text = $this->autoConvert( $text, $variant ); + $text = $this->translate( $text, $variant ); return $text; } + /** + * Convert text to different variants of a language. The automatic + * conversion is done in autoConvert(). Here we parse the text + * marked with -{}-, which specifies special conversions of the + * text that can not be accomplished in autoConvert(). + * + * Syntax of the markup: + * -{code1:text1;code2:text2;...}- or + * -{flags|code1:text1;code2:text2;...}- or + * -{text}- in which case no conversion should take place for text + * + * @param $text String: text to be converted + * @return String: converted text + */ + public function convert( $text ) { + $variant = $this->getPreferredVariant(); + return $this->convertTo( $text, $variant ); + } + + /** + * Same as convert() except a extra parameter to custom variant. + * + * @param $text String: text to be converted + * @param $variant String: the target variant code + * @return String: converted text + */ + public function convertTo( $text, $variant ) { + global $wgDisableLangConversion; + if ( $wgDisableLangConversion ) return $text; + return $this->recursiveConvertTopLevel( $text, $variant ); + } + + /** + * Recursively convert text on the outside. Allow to use nested + * markups to custom rules. + * + * @param $text String: text to be converted + * @param $variant String: the target variant code + * @param $depth Integer: depth of recursion + * @return String: converted text + */ protected function recursiveConvertTopLevel( $text, $variant, $depth = 0 ) { $startPos = 0; $out = ''; @@ -560,6 +584,14 @@ return $out; } + /** + * Recursively convert text on the inside. + * + * @param $text String: text to be converted + * @param $variant String: the target variant code + * @param $depth Integer: depth of recursion + * @return String: converted text + */ protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) { // Quick sanity check (no function calls) if ( $text[$startPos] !== '-' || $text[$startPos + 1] !== '{' ) { @@ -636,7 +668,7 @@ * @param $link String: the name of the link * @param $nt Mixed: the title object of the link * @param $ignoreOtherCond Boolean: to disable other conditions when - * we need to transclude a template or update a category's link + * we need to transclude a template or update a category's link * @return Null, the input parameters may be modified upon return */ public function findVariantLink( &$link, &$nt, $ignoreOtherCond = false ) { @@ -700,7 +732,7 @@ } } - /** + /** * Returns language specific hash options. */ public function getExtraHashOptions() { @@ -757,13 +789,13 @@ wfProfileOut( __METHOD__ ); } - /** + /** * Hook for post processig after conversion tables are loaded. * */ function postLoadTables() { } - /** + /** * Reload the conversion tables. * * @private @@ -960,7 +992,7 @@ // text should be splited by ";" only if a valid variant // name exist after the markup, for example: // -{zh-hans:<span style="font-size:120%;">xxx</span>;zh-hant:\ - // <span style="font-size:120%;">yyy</span>;}- + // <span style="font-size:120%;">yyy</span>;}- // we should split it as: // array( // [0] => 'zh-hans:<span style="font-size:120%;">xxx</span>' @@ -1121,7 +1153,7 @@ $bidtable[$v] = $to; } elseif ( count( $u ) == 2 ) { $from = trim( $u[0] ); - $v = trim( $u[1] ); + $v = trim( $u[1] ); if ( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) && $to _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs