r114048 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r114047‎ | r114048 | r114049 >
Date:05:54, 17 March 2012
Author:nikola
Status:deferred
Tags:
Comment:
Sync with trunk
Modified paths:
  • /branches/nikola/phase3/languages/LanguageConverter.php (modified) (history)
  • /branches/nikola/phase3/tests/phpunit/languages/LanguageSrTest.php (modified) (history)

Diff [purge]

Index: branches/nikola/phase3/languages/LanguageConverter.php
@@ -67,12 +67,12 @@
6868 public function __construct( $langobj, $maincode, $variants = array(),
6969 $variantfallbacks = array(), $flags = array(),
7070 $manualLevel = array() ) {
71 - global $wgDisabledVariants, $wgLanguageNames;
 71+ global $wgDisabledVariants;
7272 $this->mLangObj = $langobj;
7373 $this->mMainLanguageCode = $maincode;
7474 $this->mVariants = array_diff( $variants, $wgDisabledVariants );
7575 $this->mVariantFallbacks = $variantfallbacks;
76 - $this->mVariantNames = $wgLanguageNames;
 76+ $this->mVariantNames = Language::fetchLanguageNames();
7777 $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
7878 $defaultflags = array(
7979 // 'S' show converted text
@@ -117,7 +117,7 @@
118118 * in this case. Right now this is only used by zh.
119119 *
120120 * @param $variant String: the language code of the variant
121 - * @return String: The code of the fallback language or the
 121+ * @return String|array: The code of the fallback language or the
122122 * main code if there is no fallback
123123 */
124124 public function getVariantFallbacks( $variant ) {
@@ -158,7 +158,7 @@
159159 // not memoized (i.e. there return value is not cached) since
160160 // new information might appear during processing after this
161161 // is first called.
162 - if ( $req ) {
 162+ if ( $this->validateVariant( $req ) ) {
163163 return $req;
164164 }
165165 return $this->mMainLanguageCode;
@@ -189,7 +189,7 @@
190190 * @param $variant String: the variant to validate
191191 * @return Mixed: returns the variant if it is valid, null otherwise
192192 */
193 - protected function validateVariant( $variant = null ) {
 193+ public function validateVariant( $variant = null ) {
194194 if ( $variant !== null && in_array( $variant, $this->mVariants ) ) {
195195 return $variant;
196196 }
@@ -305,9 +305,9 @@
306306 * If you want to parse rules, try to use convert() or
307307 * convertTo().
308308 *
309 - * @param $text String: the text to be converted
310 - * @param $toVariant String: the target language code
311 - * @return String: the converted text
 309+ * @param $text String the text to be converted
 310+ * @param $toVariant bool|string the target language code
 311+ * @return String the converted text
312312 */
313313 public function autoConvert( $text, $toVariant = false ) {
314314 wfProfileIn( __METHOD__ );
@@ -323,6 +323,7 @@
324324 }
325325
326326 if( $this->guessVariant( $text, $toVariant ) ) {
 327+ wfProfileOut( __METHOD__ );
327328 return $text;
328329 }
329330
@@ -372,11 +373,11 @@
373374 $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000";
374375
375376 // Advance to the next position
376 - $startPos = $elementPos + strlen( $element );
 377+ $startPos = $elementPos + strlen( $element );
377378
378379 // Translate any alt or title attributes inside the matched element
379 - if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element,
380 - $elementMatches ) )
 380+ if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element,
 381+ $elementMatches ) )
381382 {
382383 $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
383384 $changed = false;
@@ -389,7 +390,7 @@
390391 if ( !strpos( $attr, '://' ) ) {
391392 $attr = $this->translate( $attr, $toVariant );
392393 }
393 -
 394+
394395 // Remove HTML tags to avoid disrupting the layout
395396 $attr = preg_replace( '/<[^>]+>/', '', $attr );
396397 if ( $attr !== $attrs[$attrName] ) {
@@ -398,7 +399,7 @@
399400 }
400401 }
401402 if ( $changed ) {
402 - $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
 403+ $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
403404 $elementMatches[3];
404405 }
405406 }
@@ -472,7 +473,7 @@
473474 *
474475 * @param $text String: the text to be converted
475476 * @return Array: variant => converted text
476 - * @deprecated Use autoConvertToAllVariants() instead
 477+ * @deprecated since 1.17 Use autoConvertToAllVariants() instead
477478 */
478479 public function convertLinkToAllVariants( $text ) {
479480 return $this->autoConvertToAllVariants( $text );
@@ -481,7 +482,7 @@
482483 /**
483484 * Apply manual conversion rules.
484485 *
485 - * @param $convRule Object: Object of ConverterRule
 486+ * @param $convRule ConverterRule Object of ConverterRule
486487 */
487488 protected function applyManualConv( $convRule ) {
488489 // Use syntax -{T|zh-cn:TitleCN; zh-tw:TitleTw}- to custom
@@ -521,7 +522,7 @@
522523 * Auto convert a Title object to a readable string in the
523524 * preferred variant.
524525 *
525 - * @param $title Object: a object of Title
 526+ * @param $title Title a object of Title
526527 * @return String: converted title text
527528 */
528529 public function convertTitle( $title ) {
@@ -531,9 +532,9 @@
532533 $text = '';
533534 } else {
534535 // first let's check if a message has given us a converted name
535 - $nsConvKey = 'conversion-ns' . $index;
536 - if ( !wfEmptyMsg( $nsConvKey ) ) {
537 - $text = wfMsgForContentNoTrans( $nsConvKey );
 536+ $nsConvMsg = wfMessage( 'conversion-ns' . $index )->inContentLanguage();
 537+ if ( $nsConvMsg->exists() ) {
 538+ $text = $nsConvMsg->plain();
538539 } else {
539540 // the message does not exist, try retrieve it from the current
540541 // variant's namespace names.
@@ -622,7 +623,10 @@
623624 *
624625 * @param $text String: text to be converted
625626 * @param $variant String: the target variant code
 627+ * @param $startPos int
626628 * @param $depth Integer: depth of recursion
 629+ *
 630+ * @throws MWException
627631 * @return String: converted text
628632 */
629633 protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
@@ -766,6 +770,8 @@
767771
768772 /**
769773 * Returns language specific hash options.
 774+ *
 775+ * @return string
770776 */
771777 public function getExtraHashOptions() {
772778 $variant = $this->getPreferredVariant();
@@ -780,7 +786,7 @@
781787 * @return bool true if $text appears to be written in $variant, false if not
782788 *
783789 * @author Nikola Smolenski <smolensk@eunet.rs>
784 - * @since 1.18
 790+ * @since 1.19
785791 */
786792 public function guessVariant($text, $variant) {
787793 return false;
@@ -791,10 +797,11 @@
792798 * This method must be implemented in derived class.
793799 *
794800 * @private
 801+ * @throws MWException
795802 */
796803 function loadDefaultTables() {
797804 $name = get_class( $this );
798 - wfDie( "Must implement loadDefaultTables() method in class $name" );
 805+ throw new MWException( "Must implement loadDefaultTables() method in class $name" );
799806 }
800807
801808 /**
@@ -870,6 +877,8 @@
871878 * @param $code String: language code
872879 * @param $subpage String: subpage name
873880 * @param $recursive Boolean: parse subpages recursively? Defaults to true.
 881+ *
 882+ * @return array
874883 */
875884 function parseCachedTable( $code, $subpage = '', $recursive = true ) {
876885 static $parsed = array();
@@ -882,26 +891,26 @@
883892 return array();
884893 }
885894
886 - if ( strpos( $code, '/' ) === false ) {
887 - $txt = MessageCache::singleton()->get( 'Conversiontable', true, $code );
888 - if ( $txt === false ) {
889 - # FIXME: this method doesn't seem to be expecting
890 - # this possible outcome...
891 - $txt = '&lt;Conversiontable&gt;';
892 - }
 895+ $parsed[$key] = true;
 896+
 897+ if ( $subpage === '' ) {
 898+ $txt = MessageCache::singleton()->get( 'conversiontable', true, $code );
893899 } else {
894 - $title = Title::makeTitleSafe(
895 - NS_MEDIAWIKI,
896 - "Conversiontable/$code"
897 - );
 900+ $txt = false;
 901+ $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
898902 if ( $title && $title->exists() ) {
899 - $article = new Article( $title );
900 - $txt = $article->getContents();
901 - } else {
902 - $txt = '';
 903+ $revision = Revision::newFromTitle( $title );
 904+ if ( $revision ) {
 905+ $txt = $revision->getRawText();
 906+ }
903907 }
904908 }
905909
 910+ # Nothing to parse if there's no text
 911+ if ( $txt === false || $txt === null || $txt === '' ) {
 912+ return array();
 913+ }
 914+
906915 // get all subpage links of the form
907916 // [[MediaWiki:Conversiontable/zh-xx/...|...]]
908917 $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
@@ -950,7 +959,6 @@
951960 $ret[trim( $m[0] )] = trim( $tt[0] );
952961 }
953962 }
954 - $parsed[$key] = true;
955963
956964 // recursively parse the subpages
957965 if ( $recursive ) {
@@ -989,6 +997,10 @@
990998 /**
991999 * Convert the sorting key for category links. This should make different
9921000 * keys that are variants of each other map to the same key.
 1001+ *
 1002+ * @param $key string
 1003+ *
 1004+ * @return string
9931005 */
9941006 function convertCategoryKey( $key ) {
9951007 return $key;
@@ -999,14 +1011,14 @@
10001012 * MediaWiki:Conversiontable* is updated.
10011013 * @private
10021014 *
1003 - * @param $article Object: Article object
 1015+ * @param $article Article object
10041016 * @param $user Object: User object for the current user
10051017 * @param $text String: article text (?)
10061018 * @param $summary String: edit summary of the edit
10071019 * @param $isMinor Boolean: was the edit marked as minor?
10081020 * @param $isWatch Boolean: did the user watch this page or not?
1009 - * @param $section Unused
1010 - * @param $flags Bitfield
 1021+ * @param $section
 1022+ * @param $flags int Bitfield
10111023 * @param $revision Object: new Revision object or null
10121024 * @return Boolean: true
10131025 */
@@ -1233,6 +1245,8 @@
12341246
12351247 /**
12361248 * @private
 1249+ *
 1250+ * @return string
12371251 */
12381252 function getRulesDesc() {
12391253 $codesep = $this->mConverter->mDescCodeSep;
@@ -1253,6 +1267,10 @@
12541268 /**
12551269 * Parse rules conversion.
12561270 * @private
 1271+ *
 1272+ * @param $variant
 1273+ *
 1274+ * @return string
12571275 */
12581276 function getRuleConvertedStr( $variant ) {
12591277 $bidtable = $this->mBidtable;
@@ -1366,19 +1384,21 @@
13671385 if ( isset( $this->mVariantFlags[$variant] ) ) {
13681386 // then convert <text to convert> to current language
13691387 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
1370 - $variant );
 1388+ $variant );
13711389 } else { // if current variant no in flags,
13721390 // then we check its fallback variants.
13731391 $variantFallbacks =
13741392 $this->mConverter->getVariantFallbacks( $variant );
1375 - foreach ( $variantFallbacks as $variantFallback ) {
1376 - // if current variant's fallback exist in flags
1377 - if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
1378 - // then convert <text to convert> to fallback language
1379 - $this->mRules =
1380 - $this->mConverter->autoConvert( $this->mRules,
1381 - $variantFallback );
1382 - break;
 1393+ if( is_array( $variantFallbacks ) ) {
 1394+ foreach ( $variantFallbacks as $variantFallback ) {
 1395+ // if current variant's fallback exist in flags
 1396+ if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
 1397+ // then convert <text to convert> to fallback language
 1398+ $this->mRules =
 1399+ $this->mConverter->autoConvert( $this->mRules,
 1400+ $variantFallback );
 1401+ break;
 1402+ }
13831403 }
13841404 }
13851405 }
@@ -1462,6 +1482,7 @@
14631483
14641484 /**
14651485 * Get display text on markup -{...}-
 1486+ * @return string
14661487 */
14671488 public function getDisplay() {
14681489 return $this->mRuleDisplay;
@@ -1469,6 +1490,7 @@
14701491
14711492 /**
14721493 * Get converted title.
 1494+ * @return string
14731495 */
14741496 public function getTitle() {
14751497 return $this->mRuleTitle;
@@ -1476,6 +1498,7 @@
14771499
14781500 /**
14791501 * Return how deal with conversion rules.
 1502+ * @return string
14801503 */
14811504 public function getRulesAction() {
14821505 return $this->mRulesAction;
@@ -1484,6 +1507,7 @@
14851508 /**
14861509 * Get conversion table. (bidirectional and unidirectional
14871510 * conversion table)
 1511+ * @return array
14881512 */
14891513 public function getConvTable() {
14901514 return $this->mConvTable;
@@ -1491,6 +1515,7 @@
14921516
14931517 /**
14941518 * Get conversion rules string.
 1519+ * @return string
14951520 */
14961521 public function getRules() {
14971522 return $this->mRules;
@@ -1498,6 +1523,7 @@
14991524
15001525 /**
15011526 * Get conversion flags.
 1527+ * @return array
15021528 */
15031529 public function getFlags() {
15041530 return $this->mFlags;
Index: branches/nikola/phase3/tests/phpunit/languages/LanguageSrTest.php
@@ -12,7 +12,7 @@
1313 * @file
1414 */
1515
16 -require_once dirname(dirname(__FILE__)). '/bootstrap.php';
 16+require_once dirname( dirname( __FILE__ ) ) . '/bootstrap.php';
1717
1818 /** Tests for MediaWiki languages/LanguageTr.php */
1919 class LanguageSrTest extends MediaWikiTestCase {
@@ -20,7 +20,7 @@
2121 private $lang;
2222
2323 function setUp() {
24 - $this->lang = Language::factory( 'Sr' );
 24+ $this->lang = Language::factory( 'sr' );
2525 }
2626 function tearDown() {
2727 unset( $this->lang );
@@ -62,7 +62,7 @@
6363 }
6464
6565 /**
66 - * @author Nikola Smolenski
 66+ * @author Nikola Smolenski
6767 */
6868 function testConversionToCyrillic() {
6969 $this->assertEquals( 'абвг',
@@ -77,7 +77,7 @@
7878 $this->assertEquals( 'абвгшђжчћ',
7979 $this->convertToCyrillic( 'абвгšđžčć' )
8080 );
81 - //Roman numerals are not converted
 81+ // Roman numerals are not converted
8282 $this->assertEquals( 'а I б II в III г IV шђжчћ',
8383 $this->convertToCyrillic( 'a I b II v III g IV šđžčć' )
8484 );
@@ -96,9 +96,43 @@
9797 $this->assertEquals( 'абцдšđžčć',
9898 $this->convertToLatin( 'абцдšđžčć' )
9999 );
 100+ }
100101
 102+ /** @dataProvider providePluralFourForms */
 103+ function testPluralFourForms( $result, $value ) {
 104+ $forms = array( 'one', 'few', 'many', 'other' );
 105+ $this->assertEquals( $result, $this->lang->convertPlural( $value, $forms ) );
101106 }
102107
 108+ function providePluralFourForms() {
 109+ return array (
 110+ array( 'one', 1 ),
 111+ array( 'many', 11 ),
 112+ array( 'one', 91 ),
 113+ array( 'one', 121 ),
 114+ array( 'few', 2 ),
 115+ array( 'few', 3 ),
 116+ array( 'few', 4 ),
 117+ array( 'few', 334 ),
 118+ array( 'many', 5 ),
 119+ array( 'many', 15 ),
 120+ array( 'many', 120 ),
 121+ );
 122+ }
 123+ /** @dataProvider providePluralTwoForms */
 124+ function testPluralTwoForms( $result, $value ) {
 125+ $forms = array( 'one', 'several' );
 126+ $this->assertEquals( $result, $this->lang->convertPlural( $value, $forms ) );
 127+ }
 128+ function providePluralTwoForms() {
 129+ return array (
 130+ array( 'one', 1 ),
 131+ array( 'several', 11 ),
 132+ array( 'several', 91 ),
 133+ array( 'several', 121 ),
 134+ );
 135+ }
 136+
103137 ##### HELPERS #####################################################
104138 /**
105139 *Wrapper to verify text stay the same after applying conversion

Status & tagging log