r31325 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r31324‎ | r31325 | r31326 >
Date:21:09, 26 February 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Support branches for core messages
Modified paths:
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/utils (added) (history)
  • /trunk/extensions/Translate/utils/ResourceLoader.php (added) (history)
  • /trunk/extensions/Translate/utils/StringMatcher.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -11,7 +11,7 @@
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1313 */
1414
15 -define( 'TRANSLATE_VERSION', '8.15' );
 15+define( 'TRANSLATE_VERSION', '8.16' );
1616
1717 $wgExtensionCredits['specialpage'][] = array(
1818 'name' => 'Translate',
@@ -28,6 +28,8 @@
2929
3030 $wgAutoloadClasses['TranslateUtils'] = $dir . 'TranslateUtils.php';
3131 $wgAutoloadClasses['HTMLSelector'] = $dir . 'TranslateUtils.php';
 32+$wgAutoloadClasses['ResourceLoader'] = $dir . 'utils/ResourceLoader.php';
 33+$wgAutoloadClasses['StringMatcher'] = $dir . 'utils/StringMatcher.php';
3234
3335 $wgAutoloadClasses['MessageChecks'] = $dir . 'MessageChecks.php';
3436 $wgAutoloadClasses['MessageGroups'] = $dir . 'MessageGroups.php';
Index: trunk/extensions/Translate/README
@@ -35,6 +35,7 @@
3636 == Chages in version 9 ==
3737 * Not released
3838 * (bug 12955) Numbers should be localized in Translate extension
 39+* Support branches for core messages
3940
4041 == Changes in version 8 ==
4142 * Released 2008-02-06
Index: trunk/extensions/Translate/utils/StringMatcher.php
@@ -0,0 +1,110 @@
 2+<?php
 3+
 4+class StringMatcher {
 5+ protected $sPrefix = '';
 6+ protected $aExact = array();
 7+ protected $aPrefix = array();
 8+ protected $aRegex = array();
 9+
 10+ public static function EmptyMatcher() {
 11+ return new StringMatcher( '', array() );
 12+ }
 13+
 14+ public function __construct( $prefix, Array $strings ) {
 15+ $this->sPrefix = $prefix;
 16+ $this->init( $strings );
 17+ }
 18+
 19+ protected function init( Array $strings ) {
 20+ foreach ( $strings as $string ) {
 21+ $pos = strpos( $string, '*' );
 22+ if ( $pos === false ) {
 23+ $this->aExact[] = $string;
 24+ } elseif ( $pos +1 === strlen($string) ) {
 25+ $prefix = substr( $string, 0, -1 );
 26+ $this->aPrefix[$prefix] = strlen($prefix);
 27+ } else {
 28+ $string = str_replace( '\\*', '.+', preg_quote( $string ) );
 29+ $this->aRegex[] = "/^$string$/";
 30+ }
 31+ }
 32+ }
 33+
 34+ public function match( $string ) {
 35+ if ( in_array($string, $this->aExact) ) return true;
 36+
 37+ foreach ( $this->aPrefix as $prefix => $len ) {
 38+ if ( strncmp( $string, $prefix, $len ) === 0 ) return true;
 39+ }
 40+
 41+ foreach ( $this->aRegex as $regex ) {
 42+ if ( preg_match( $regex, $string ) ) return true;
 43+ }
 44+
 45+ return false;
 46+ }
 47+
 48+ public function mangle( $data ) {
 49+ if ( !$this->sPrefix ) { return $data; }
 50+ if ( is_array( $data ) ) {
 51+ return $this->mangleArray( $data );
 52+ } elseif ( is_string( $data ) ) {
 53+ return $this->mangleString( $data );
 54+ } elseif ( $data === null ) {
 55+ return $data;
 56+ } else {
 57+ throw new MWException( __METHOD__ . ": Unsupported datatype" );
 58+ }
 59+ }
 60+
 61+ public function unMangle( $data ) {
 62+ if ( !$this->sPrefix ) { return $data; }
 63+ if ( is_array( $data ) ) {
 64+ return $this->mangleArray( $data, true);
 65+ } elseif ( is_string( $data ) ) {
 66+ return $this->mangleString( $data, true );
 67+ } elseif ( $data === null ) {
 68+ return $data;
 69+ } else {
 70+ throw new MWException( __METHOD__ . ": Unsupported datatype" );
 71+ }
 72+ }
 73+
 74+
 75+ protected function mangleString( $string, $reverse = false ) {
 76+ if ( $reverse ) {
 77+ return $this->unMangleString( $string );
 78+ } elseif ( $this->match( $string ) ) {
 79+ return $this->sPrefix . $string;
 80+ } else {
 81+ return $string;
 82+ }
 83+ }
 84+
 85+ protected function unMangleString( $string ) {
 86+ if ( strncmp( $string, $this->sPrefix, strlen($this->sPrefix) ) === 0 ) {
 87+ return substr( $string, strlen( $this->sPrefix ) );
 88+ } else {
 89+ return $string;
 90+ }
 91+ }
 92+
 93+ protected function mangleArray( Array $array, $reverse = false ) {
 94+ $temp = array();
 95+
 96+ if ( isset($array[0]) ) {
 97+ foreach ( $array as $key => &$value ) {
 98+ $value = $this->mangleString( $value, $reverse );
 99+ $temp[$key] = $value; // Assign a reference
 100+ }
 101+ } else {
 102+ foreach ( $array as $key => &$value ) {
 103+ $key = $this->mangleString( $key, $reverse );
 104+ $temp[$key] = $value; // Assign a reference
 105+ }
 106+ }
 107+
 108+ return $temp;
 109+ }
 110+
 111+}
\ No newline at end of file
Property changes on: trunk/extensions/Translate/utils/StringMatcher.php
___________________________________________________________________
Added: svn:eol-style
1112 + native
Index: trunk/extensions/Translate/utils/ResourceLoader.php
@@ -0,0 +1,14 @@
 2+<?php
 3+
 4+class ResourceLoader {
 5+
 6+ public static function loadVariableFromPHPFile( $_filename, $_variable ) {
 7+ if ( !file_exists( $_filename ) ) {
 8+ return null;
 9+ } else {
 10+ require( $_filename );
 11+ return isset( $$_variable ) ? $$_variable : null;
 12+ }
 13+ }
 14+
 15+}
\ No newline at end of file
Property changes on: trunk/extensions/Translate/utils/ResourceLoader.php
___________________________________________________________________
Added: svn:eol-style
116 + native
Index: trunk/extensions/Translate/MessageGroups.php
@@ -32,6 +32,9 @@
3333 */
3434 protected $fileExporter = null;
3535
 36+ /*******/
 37+ protected $mangler = null;
 38+
3639 /**
3740 * Returns a human readable name of this group.
3841 */
@@ -149,6 +152,8 @@
150153 // We copy only relevant translations to this new array
151154 $new = array();
152155 foreach( $messages as $key => $m ) {
 156+ $key = $this->mangler->unMangle( $key );
 157+
153158 # CASE1: ignored
154159 if ( $m->ignored ) continue;
155160
@@ -185,6 +190,11 @@
186191 return implode( "\n", $s );
187192 }
188193
 194+
 195+ public function __construct() {
 196+ $this->mangler = StringMatcher::emptyMatcher();
 197+ }
 198+
189199 }
190200
191201 class CoreMessageGroup extends MessageGroup {
@@ -197,6 +207,10 @@
198208 return "Messages$code.php";
199209 }
200210
 211+ protected function getFileLocation( $code ) {
 212+ return Language::getMessagesFileName( $code );
 213+ }
 214+
201215 public function getMessage( $key, $code ) {
202216 $messages = $this->loadMessages( $code );
203217 return isset( $messages[$key] ) ? $messages[$key] : null;
@@ -208,7 +222,7 @@
209223 }
210224
211225 public function exportToFile( MessageCollection $messages, $authors ) {
212 - $filename = Language::getMessagesFileName( $messages->code );
 226+ $filename = $this->getFileLocation( $messages->code );
213227
214228 $messagesAsString = $this->export( $messages );
215229 $name = TranslateUtils::getLanguageName( $messages->code );
@@ -232,28 +246,24 @@
233247 }
234248
235249 function getDefinitions() {
236 - return Language::getMessagesFor( 'en' );
 250+ return $this->loadMessages( 'en' );
237251 }
238252
239253 public function getBools() {
240254 $l = new languages();
241255 return array(
242 - 'optional' => $l->getOptionalMessages(),
243 - 'ignored' => $l->getIgnoredMessages(),
 256+ 'optional' => $this->mangler->mangle( $l->getOptionalMessages() ),
 257+ 'ignored' => $this->mangler->mangle( $l->getIgnoredMessages() ),
244258 );
245259 }
246260
247 - private function loadMessages( $code ) {
 261+ protected function loadMessages( $code ) {
248262 if ( !isset($this->mcache[$code]) ) {
249 - $file = Language::getMessagesFileName( $code );
250 - if ( !file_exists( $file ) ) {
251 - $this->mcache[$code] = null;
252 - } else {
253 - require( $file );
254 - return $this->mcache[$code] = isset( $messages ) ? $messages : null;
255 - }
 263+ $file = $this->getFileLocation( $code );
 264+ $this->mcache[$code] = $this->mangler->mangle(
 265+ ResourceLoader::loadVariableFromPHPFile( $file, 'messages' )
 266+ );
256267 }
257 -
258268 return $this->mcache[$code];
259269 }
260270
@@ -268,9 +278,6 @@
269279 if ( isset($infile[$key]) ) {
270280 $messages[$key]->infile = $infile[$key];
271281 }
272 - if ( $infbfile && isset($infbfile[$key]) ) {
273 - $messages[$key]->fallback = $infbfile[$key];
274 - }
275282 }
276283 }
277284
@@ -305,6 +312,25 @@
306313 }
307314 }
308315
 316+class BranchedCoreMessageGroup extends CoreMessageGroup {
 317+ protected $label = 'MediaWiki messages ($1)';
 318+ protected $id = 'core-$1';
 319+ protected $fileExporter = 'CoreExporter';
 320+ protected $path = '__BUG__';
 321+ protected $meta = true;
 322+
 323+ public function __construct( $path, $branch, StringMatcher $mangler ) {
 324+ $this->path = $path;
 325+ $this->label = str_replace( '$1', $branch, $this->label );
 326+ $this->id = Sanitizer::escapeId( str_replace( '$1', $branch, $this->id ) );
 327+ $this->mangler = $mangler;
 328+ }
 329+
 330+ protected function getFileLocation( $code ) {
 331+ return $this->path . '/' . $this->getMessageFile( $code );
 332+ }
 333+}
 334+
309335 abstract class ExtensionMessageGroup extends MessageGroup {
310336 protected $fileExporter = 'StandardExtensionExporter';
311337 /**
@@ -2236,6 +2262,7 @@
22372263 private $fileDir = 'freecol/';
22382264
22392265 public function __construct() {
 2266+ parent::__construct();
22402267 global $wgTranslateExtensionDirectory;
22412268 $this->fileDir = $wgTranslateExtensionDirectory . 'freecol/';
22422269 }
@@ -2323,6 +2350,7 @@
23242351 * @param $source Mediawiki message that contains list of message keys.
23252352 */
23262353 public function __construct( $id, $source ) {
 2354+ parent::__construct();
23272355 $this->id = $id;
23282356 $this->source = $source;
23292357 }

Status & tagging log