r45828 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45827‎ | r45828 | r45829 >
Date:05:01, 17 January 2009
Author:shinjiman
Status:ok (Comments)
Tags:
Comment:
* Fixing the caching issue by using -{T|xxx}- syntax (only applies on wiki with LanguageConverter class)
* Improving the efficiency by using -{A|xxx}- syntax (only applies on wiki with LanguageConverter class)
patches by PhiLiP
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/parser/Parser.php (modified) (history)
  • /trunk/phase3/languages/LanguageConverter.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/parser/Parser.php
@@ -3304,6 +3304,7 @@
33053305 throw new MWException( '<html> extension tag encountered unexpectedly' );
33063306 }
33073307 case 'nowiki':
 3308+ $content = strtr($content, array('-{' => '-&#123;', '}-' => '&#125;-'));
33083309 $output = Xml::escapeTagsOnly( $content );
33093310 break;
33103311 case 'math':
Index: trunk/phase3/languages/LanguageConverter.php
@@ -13,7 +13,7 @@
1414 * @ingroup Language
1515 *
1616 * @author Zhengzhu Feng <zhengzhu@gmail.com>
17 - * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>
 17+ * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
1818 */
1919 class LanguageConverter {
2020 var $mPreferredVariant='';
@@ -21,6 +21,8 @@
2222 var $mVariants, $mVariantFallbacks, $mVariantNames;
2323 var $mTablesLoaded = false;
2424 var $mTables;
 25+ var $mManualAddTables;
 26+ var $mManualRemoveTables;
2527 var $mTitleDisplay='';
2628 var $mDoTitleConvert=true, $mDoContentConvert=true;
2729 var $mManualLevel; // 'bidirectional' 'unidirectional' 'disable' for each variants
@@ -81,10 +83,13 @@
8284 'N'=>'N' // current variant name
8385 );
8486 $this->mFlags = array_merge($f, $flags);
85 - foreach( $this->mVariants as $v)
 87+ foreach( $this->mVariants as $v) {
8688 $this->mManualLevel[$v]=array_key_exists($v,$manualLevel)
8789 ?$manualLevel[$v]
8890 :'bidirectional';
 91+ $this->mManualAddTables[$v] = array();
 92+ $this->mManualRemoveTables[$v] = array();
 93+ }
8994 }
9095
9196 /**
@@ -323,13 +328,12 @@
324329
325330 return $ret;
326331 }
327 -
328 -
 332+
329333 /**
330 - * apply manual conversion
 334+ * prepare manual conversion table
331335 * @private
332336 */
333 - function applyManualConv($convRule){
 337+ function prepareManualConv($convRule){
334338 // use syntax -{T|zh:TitleZh;zh-tw:TitleTw}- for custom conversion in title
335339 $title = $convRule->getTitle();
336340 if($title){
@@ -342,14 +346,33 @@
343347 $action = $convRule->getRulesAction();
344348 foreach($convTable as $v=>$t) {
345349 if( !in_array($v,$this->mVariants) )continue;
346 - if( $action=="add" )
347 - $this->mTables[$v]->mergeArray($t);
 350+ if( $action=="add" ) {
 351+ foreach($t as $from=>$to) {
 352+ // more efficient than array_merge(), about 2.5 times.
 353+ $this->mManualAddTables[$v][$from] = $to;
 354+ }
 355+ }
348356 elseif ( $action=="remove" )
349 - $this->mTables[$v]->removeArray($t);
 357+ $this->mManualRemoveTables[$v] = array_merge($this->mManualRemoveTables[$v], $t);
350358 }
351359 }
352360
353361 /**
 362+ * apply manual conversion from $this->mManualAddTables and $this->mManualRemoveTables
 363+ * @private
 364+ */
 365+ function applyManualConv(){
 366+ //apply manual conversion table to global table
 367+ foreach($this->mVariants as $v) {
 368+ if (count($this->mManualAddTables[$v]) > 0) {
 369+ $this->mTables[$v]->mergeArray($this->mManualAddTables[$v]);
 370+ }
 371+ if (count($this->mManualRemoveTables[$v]) > 0)
 372+ $this->mTables[$v]->removeArray($this->mManualRemoveTables[$v]);
 373+ }
 374+ }
 375+
 376+ /**
354377 * Convert text using a parser object for context
355378 * @public
356379 */
@@ -438,33 +461,31 @@
439462 $plang = $this->getPreferredVariant();
440463 $tarray = StringUtils::explode($this->mMarkup['end'], $text);
441464 $text = '';
442 - $lastDelim = false;
 465+
 466+ $marks = array();
443467 foreach($tarray as $txt) {
444468 $marked = explode($this->mMarkup['begin'], $txt, 2);
445 -
 469+ if (array_key_exists(1, $marked)) {
 470+ $crule = new ConverterRule($marked[1], $this);
 471+ $crule->parse($plang);
 472+ $marked[1] = $crule->getDisplay();
 473+ $this->prepareManualConv($crule);
 474+ }
 475+ else
 476+ $marked[0] .= $this->mMarkup['end'];
 477+ array_push($marks, $marked);
 478+ }
 479+ $this->applyManualConv();
 480+ foreach ($marks as $marked) {
446481 if( $this->mDoContentConvert )
447482 $text .= $this->autoConvert($marked[0],$plang);
448483 else
449484 $text .= $marked[0];
450 -
451 - if(array_key_exists(1, $marked)){
452 - // strip the flags from syntax like -{T| ... }-
453 - $crule = new ConverterRule($marked[1], $this);
454 - $crule->parse($plang);
455 -
456 - $text .= $crule->getDisplay();
457 - $this->applyManualConv($crule);
458 - $lastDelim = false;
459 - } else {
460 - // Reinsert the }- which wasn't part of anything
461 - $text .= $this->mMarkup['end'];
462 - $lastDelim = true;
463 - }
 485+ if( array_key_exists(1, $marked) )
 486+ $text .= $marked[1];
464487 }
465 - if ( $lastDelim ) {
466 - // Remove the last delimiter (wasn't real)
467 - $text = substr( $text, 0, -strlen( $this->mMarkup['end'] ) );
468 - }
 488+ // Remove the last delimiter (wasn't real)
 489+ $text = substr( $text, 0, -strlen( $this->mMarkup['end'] ) );
469490
470491 return $text;
471492 }
Index: trunk/phase3/RELEASE-NOTES
@@ -50,6 +50,8 @@
5151 * Page moves should not be minor edits
5252 * (bug 17035) Special:Upload now fails gracefully if PHP's file_uploads has been
5353 disabled
 54+* Fixing the caching issue by using -{T|xxx}- syntax (only applies on wiki with LanguageConverter class)
 55+* Improving the efficiency by using -{A|xxx}- syntax (only applies on wiki with LanguageConverter class)
5456
5557 == API changes in 1.15 ==
5658 * (bug 16858) Revamped list=deletedrevs to make listing deleted contributions

Comments

#Comment by Shinjiman (talk | contribs)   05:06, 17 January 2009

This one is the follow up of rev:45642 (reverted by brion on rev:45742), Patch by PhiLiP.

#Comment by Brion VIBBER (talk | contribs)   23:40, 20 January 2009

Doesn't seem to be breaking test cases this time. Yay!

Status & tagging log