r57350 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r57349‎ | r57350 | r57351 >
Date:23:06, 3 October 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
Fix multiple bugs in plural handling:
* variable name collisions
* broken regexp
* import with wrong syntax
Modified paths:
  • /trunk/extensions/Translate/FFS.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/FFS.php
@@ -609,7 +609,7 @@
610610
611611 // Put the "other" alternative last, without other= prefix
612612 $other = isset($messages['other']) ? '|' . $messages['other'] : '';
613 - $pls .= "$other|}}";
 613+ $pls .= "$other}}";
614614 return $pls;
615615 }
616616
@@ -622,13 +622,13 @@
623623
624624 // Replace all variables with placeholders. Possible source of bugs
625625 // if other characters that given below are used.
626 - $regex = '~\{\{a-zA-Z_-}}~';
 626+ $regex = '~\{\{[a-zA-Z_-]+}}~';
627627 $placeholders = array();
628628 $match = null;
629 - while ( preg_match( $other, $message, $match ) ) {
630 - $key = self::placeholder();
631 - $placeholders[$key] = $match[0];
632 - $message = preg_replace( $other, $key, $message );
 629+ while ( preg_match( $regex, $message, $match ) ) {
 630+ $uniqkey = self::placeholder();
 631+ $placeholders[$uniqkey] = $match[0];
 632+ $message = preg_replace( $regex, $uniqkey, $message );
633633 }
634634
635635 // Then replace (possible multiple) plural instances into placeholders
@@ -636,9 +636,9 @@
637637 $matches = array();
638638 $match = null;
639639 while ( preg_match( $regex, $message, $match ) ) {
640 - $key = self::placeholder();
641 - $matches[$key] = $match;
642 - $message = preg_replace( $regex, $key, $message );
 640+ $uniqkey = self::placeholder();
 641+ $matches[$uniqkey] = $match;
 642+ $message = preg_replace( $regex, $uniqkey, $message );
643643 }
644644
645645 // No plurals, should not happen
@@ -655,6 +655,7 @@
656656 foreach ( $matches as $ph => $plu ) {
657657 $forms = explode( '|', $plu[1] );
658658 foreach ( $forms as $form ) {
 659+ if ( $form === '' ) continue;
659660 $match = array();
660661 if ( preg_match( $regex, $form, $match ) ) {
661662 $formWord = "$key.{$match[1]}";
@@ -680,6 +681,6 @@
681682
682683 protected function placeholder() {
683684 static $i = 0;
684 - return "\x7fUNIQ" . dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff)) . '|' . $i++;
 685+ return "\x7fUNIQ" . dechex(mt_rand(0, 0x7fffffff)) . dechex(mt_rand(0, 0x7fffffff)) . $i++;
685686 }
686687 }
\ No newline at end of file

Status & tagging log