r48015 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r48014‎ | r48015 | r48016 >
Date:06:56, 4 March 2009
Author:philip
Status:ok (Comments)
Tags:
Comment:
New function to convert content text to specified language (only applies on wiki with
LanguageConverter class)
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)
  • /trunk/phase3/languages/classes/LanguageZh.php (modified) (history)

Diff [purge]

Index: trunk/phase3/languages/LanguageConverter.php
@@ -77,7 +77,7 @@
7878 // these flags above are reserved for program
7979 'A'=>'A', // add rule for convert code (all text convert)
8080 'T'=>'T', // title convert
81 - 'R'=>'R', // raw content
 81+ 'R'=>'R', // row content
8282 'D'=>'D', // convert description (subclass implement)
8383 '-'=>'-', // remove convert (not implement)
8484 'H'=>'H', // add rule for convert code (but no display in placed code )
@@ -91,6 +91,7 @@
9292 $this->mManualAddTables[$v] = array();
9393 $this->mManualRemoveTables[$v] = array();
9494 $this->mNamespaceTables[$v] = array();
 95+ $this->mFlags[$v] = $v;
9596 }
9697 }
9798
@@ -873,7 +874,7 @@
874875 /**
875876 * parser for rules of language conversion , parse rules in -{ }- tag
876877 * @ingroup Language
877 - * @author fdcn <fdcn64@gmail.com>
 878+ * @author fdcn <fdcn64@gmail.com>, PhiLiP <philip.npc@gmail.com>
878879 */
879880 class ConverterRule {
880881 var $mText; // original text in -{text}-
@@ -935,6 +936,7 @@
936937 $flags = array();
937938 $markup = $this->mConverter->mMarkup;
938939 $validFlags = $this->mConverter->mFlags;
 940+ $variants = $this->mConverter->mVariants;
939941
940942 $tt = explode($markup['flagsep'], $text, 2);
941943 if(count($tt) == 2) {
@@ -966,14 +968,21 @@
967969 if(in_array('D',$flags)) $temp[] = 'D';
968970 $flags = $temp;
969971 } else {
970 - if ( in_array('A',$flags)) {
 972+ if ( in_array('A',$flags) ) {
971973 $flags[]='+';
972974 $flags[]='S';
973975 }
974976 if ( in_array('D',$flags) )
975977 $flags=array_diff($flags,array('S'));
 978+ $flags_temp = array();
 979+ foreach ($variants as $variant) {
 980+ if ( in_array($variant, $flags) )
 981+ $flags_temp[] = $variant;
 982+ }
 983+ if ( count($flags_temp) == 0 )
 984+ $flags = $flags_temp;
976985 }
977 - if ( count($flags)==0 )
 986+ if ( count($flags) == 0 )
978987 $flags = array('S');
979988 $this->mRules=$rules;
980989 $this->mFlags=$flags;
@@ -992,23 +1001,24 @@
9931002
9941003 $choice = explode($markup['varsep'], $rules);
9951004 foreach($choice as $c) {
996 - $v = explode($markup['codesep'], $c, 2);
997 - if(count($v) != 2)
 1005+ $v = explode($markup['codesep'], $c, 2);
 1006+ if( count($v) != 2 )
9981007 continue;// syntax error, skip
999 - $to=trim($v[1]);
1000 - $v=trim($v[0]);
1001 - $u = explode($markup['unidsep'], $v);
1002 - if(count($u) == 1) {
 1008+ $to = trim($v[1]);
 1009+ $v = trim($v[0]);
 1010+ $u = explode($markup['unidsep'], $v);
 1011+ if( count($u) == 1 ) {
10031012 $bidtable[$v] = $to;
10041013 } else if(count($u) == 2){
1005 - $from=trim($u[0]);$v=trim($u[1]);
1006 - if( array_key_exists($v,$unidtable) && !is_array($unidtable[$v]) )
1007 - $unidtable[$v]=array($from=>$to);
 1014+ $from = trim($u[0]);
 1015+ $v = trim($u[1]);
 1016+ if( array_key_exists( $v, $unidtable ) && !is_array( $unidtable[$v] ) )
 1017+ $unidtable[$v] = array( $from=>$to );
10081018 else
1009 - $unidtable[$v][$from]=$to;
 1019+ $unidtable[$v][$from] = $to;
10101020 }
10111021 // syntax error, pass
1012 - if (!array_key_exists($v,$this->mConverter->mVariantNames)){
 1022+ if ( !array_key_exists( $v, $this->mConverter->mVariantNames ) ){
10131023 $bidtable = array();
10141024 $unidtable = array();
10151025 break;
@@ -1124,12 +1134,30 @@
11251135 * @public
11261136 */
11271137 function parse($variant){
1128 - if(!$variant) $variant = $this->mConverter->getPreferredVariant();
 1138+ if(!$variant)
 1139+ $variant = $this->mConverter->getPreferredVariant();
11291140
 1141+ $variants = $this->mConverter->mVariants;
11301142 $this->parseFlags();
11311143 $flags = $this->mFlags;
11321144
1133 - if( !in_array('R',$flags) || !in_array('N',$flags) ){
 1145+ //convert to specified variant
 1146+ if( count( array_diff( $flags, $variants ) ) == 0 and count( $flags ) != 0 ) {
 1147+ if ( in_array( $variant, $flags ) )
 1148+ $this->mRules = $this->mConverter->autoConvert( $this->mRules, $variant );
 1149+ else {
 1150+ $variantFallbacks = $this->mConverter->getVariantFallbacks($variant);
 1151+ foreach ( $variantFallbacks as $variantFallback ) {
 1152+ if ( in_array( $variantFallback, $flags ) ) {
 1153+ $this->mRules = $this->mConverter->autoConvert( $this->mRules, $variantFallback );
 1154+ break;
 1155+ }
 1156+ }
 1157+ }
 1158+ $this->mFlags = $flags = array('R');
 1159+ }
 1160+
 1161+ if( !in_array( 'R', $flags ) || !in_array( 'N', $flags ) ) {
11341162 //FIXME: may cause trouble here...
11351163 //strip &nbsp; since it interferes with the parsing, plus,
11361164 //all spaces should be stripped in this tag anyway.
@@ -1141,7 +1169,7 @@
11421170 }
11431171 $rules = $this->mRules;
11441172
1145 - if(count($this->mBidtable)==0 && count($this->mUnidtable)==0){
 1173+ if( count( $this->mBidtable ) == 0 && count( $this->mUnidtable ) == 0 ){
11461174 if(in_array('+',$flags) || in_array('-',$flags))
11471175 // fill all variants if text in -{A/H/-|text} without rules
11481176 foreach($this->mConverter->mVariants as $v)
Index: trunk/phase3/languages/classes/LanguageZh.php
@@ -53,7 +53,7 @@
5454 'Special' => '特殊',
5555 'Talk' => '讨论',
5656 'User' => '用户',
57 - 'User Talk' => '用户讨论',
 57+ 'User talk' => '用户讨论',
5858 $nsproject
5959 => isset($projecttable[$nsproject]) ?
6060 $projecttable[$nsproject] : $nsproject,
Index: trunk/phase3/RELEASE-NOTES
@@ -125,6 +125,8 @@
126126 with class 'mw-specialpage-summary'
127127 * $wgSummarySpamRegex added to handle edit summary spam. This is used *instead*
128128 of $wgSpamRegex for edit summary checks. Text checks still use $wgSpamRegex.
 129+* New function to convert content text to specified language (only applies on wiki with
 130+ LanguageConverter class)
129131
130132 === Bug fixes in 1.15 ===
131133 * (bug 16968) Special:Upload no longer throws useless warnings.

Follow-up revisions

RevisionCommit summaryAuthorDate
r48836Follow up on r48015, fix a bug which caused 'A' and 'D' won't work.philip17:30, 25 March 2009

Comments

#Comment by PhiLiP (talk | contribs)   07:00, 4 March 2009

And fixed a typo error in LanguageZh.php.

#Comment by Aaron Schulz (talk | contribs)   14:41, 4 March 2009

This needs more code comments.

#Comment by PhiLiP (talk | contribs)   02:24, 5 March 2009

More code comments added on r48040.

#Comment by PhiLiP (talk | contribs)   17:31, 25 March 2009

Something goes wrong, '-{A|' and '-{D|' can't work now. Fixed on 48836.

Status & tagging log