r106190 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106189‎ | r106190 | r106191 >
Date:13:30, 14 December 2011
Author:catrope
Status:ok
Tags:
Comment:
1.18wmf1: Update LocalisationUpdate to trunk state
Modified paths:
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.class.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/QuickArrayReader.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/tests/tokenTest.php (modified) (history)
  • /branches/wmf/1.18wmf1/extensions/LocalisationUpdate/update.php (modified) (history)

Diff [purge]

Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/tests/tokenTest.php
@@ -32,7 +32,7 @@
3333 if( count( $args ) ) {
3434 $sources = $args;
3535 } else {
36 - $sources =
 36+ $sources =
3737 array_merge(
3838 glob("$IP/extensions/*/*.i18n.php"),
3939 glob("$IP/languages/messages/Messages*.php") );
@@ -41,9 +41,9 @@
4242 foreach( $sources as $sourceFile ) {
4343 $rel = basename( $sourceFile );
4444 $out = str_replace( '/', '-', $rel );
45 -
 45+
4646 $sourceData = file_get_contents( $sourceFile );
47 -
 47+
4848 if( preg_match( '!extensions/!', $sourceFile ) ) {
4949 $sourceData = LocalisationUpdate::cleanupExtensionFile( $sourceData );
5050 $items = 'langs';
@@ -51,32 +51,32 @@
5252 $sourceData = LocalisationUpdate::cleanupFile( $sourceData );
5353 $items = 'messages';
5454 }
55 -
 55+
5656 file_put_contents( "$out.txt", $sourceData );
5757
5858 $start = microtime(true);
5959 $eval = evalExtractArray( $sourceData, 'messages' );
6060 $deltaEval = microtime(true) - $start;
61 -
 61+
6262 $start = microtime(true);
6363 $quick = quickTokenExtractArray( $sourceData, 'messages' );
6464 $deltaQuick = microtime(true) - $start;
65 -
 65+
6666 $start = microtime(true);
6767 $token = confExtractArray( $sourceData, 'messages' );
6868 $deltaToken = microtime(true) - $start;
69 -
 69+
7070 $hashEval = md5(serialize($eval));
7171 $hashToken = md5(serialize($token));
7272 $hashQuick = md5(serialize($quick));
7373 $countEval = count( (array)$eval);
7474 $countToken = count( (array)$token );
7575 $countQuick = count( (array)$quick );
76 -
 76+
7777 printf( "%s %s %d $items - %0.1fms - eval\n", $rel, $hashEval, $countEval, $deltaEval * 1000 );
7878 printf( "%s %s %d $items - %0.1fms - QuickArrayReader\n", $rel, $hashQuick, $countQuick, $deltaQuick * 1000 );
7979 printf( "%s %s %d $items - %0.1fms - ConfEditor\n", $rel, $hashToken, $countToken, $deltaToken * 1000 );
80 -
 80+
8181 if( $hashEval !== $hashToken || $hashEval !== $hashQuick ) {
8282 echo "FAILED on $rel\n";
8383 file_put_contents( "$out-eval.txt", var_export( $eval, true ) );
Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/QuickArrayReader.php
@@ -9,7 +9,10 @@
1010 */
1111 class QuickArrayReader {
1212 var $vars = array();
13 -
 13+
 14+ /**
 15+ * @param $string string
 16+ */
1417 function __construct( $string ) {
1518 $scalarTypes = array(
1619 T_LNUMBER => true,
@@ -36,30 +39,30 @@
3740 // '$messages' -> 'messages'
3841 $varname = trim( substr( $tokens[$i][1], 1 ) );
3942 $varindex = null;
40 -
 43+
4144 while( isset($skipTypes[$tokens[++$i][0]] ) );
42 -
 45+
4346 if( $tokens[$i] === '[' ) {
4447 while( isset($skipTypes[$tokens[++$i][0]] ) );
45 -
 48+
4649 if( isset($scalarTypes[$tokens[$i][0]] ) ) {
4750 $varindex = $this->parseScalar( $tokens[$i] );
4851 } else {
4952 throw $this->except( $tokens[$i], 'scalar index' );
5053 }
5154 while( isset($skipTypes[$tokens[++$i][0]] ) );
52 -
 55+
5356 if( $tokens[$i] !== ']' ) {
5457 throw $this->except( $tokens[$i], ']' );
5558 }
5659 while( isset($skipTypes[$tokens[++$i][0]] ) );
5760 }
58 -
 61+
5962 if( $tokens[$i] !== '=' ) {
6063 throw $this->except( $tokens[$i], '=' );
6164 }
6265 while( isset($skipTypes[$tokens[++$i][0]] ) );
63 -
 66+
6467 if( isset($scalarTypes[$tokens[$i][0]] ) ) {
6568 $buildval = $this->parseScalar( $tokens[$i] );
6669 } elseif( $tokens[$i][0] === T_ARRAY ) {
@@ -70,7 +73,7 @@
7174 $buildval = array();
7275 do {
7376 while( isset($skipTypes[$tokens[++$i][0]] ) );
74 -
 77+
7578 if( $tokens[$i] === ')' ) {
7679 break;
7780 }
@@ -78,18 +81,18 @@
7982 $key = $this->parseScalar( $tokens[$i] );
8083 }
8184 while( isset($skipTypes[$tokens[++$i][0]] ) );
82 -
 85+
8386 if( $tokens[$i][0] !== T_DOUBLE_ARROW ) {
8487 throw $this->except( $tokens[$i], '=>' );
8588 }
8689 while( isset($skipTypes[$tokens[++$i][0]] ) );
87 -
 90+
8891 if( isset($scalarTypes[$tokens[$i][0]] ) ) {
8992 $val = $this->parseScalar( $tokens[$i] );
9093 }
9194 @$buildval[$key] = $val;
9295 while( isset($skipTypes[$tokens[++$i][0]] ) );
93 -
 96+
9497 if( $tokens[$i] === ',' ) {
9598 continue;
9699 } elseif( $tokens[$i] === ')' ) {
@@ -117,7 +120,12 @@
118121 }
119122 }
120123 }
121 -
 124+
 125+ /**
 126+ * @param $got string
 127+ * @param $expected string
 128+ * @return Exception
 129+ */
122130 private function except( $got, $expected ) {
123131 if( is_array( $got ) ) {
124132 $got = token_name( $got[0] ) . " ('" . $got[1] . "')";
@@ -129,6 +137,9 @@
130138
131139 /**
132140 * Parse a scalar value in PHP
 141+ *
 142+ * @param $token string
 143+ *
133144 * @return mixed Parsed value
134145 */
135146 function parseScalar( $token ) {
@@ -160,7 +171,11 @@
161172 // be useful for a change
162173 return $str;
163174 }
164 -
 175+
 176+ /**
 177+ * @param $varname string
 178+ * @return null|string
 179+ */
165180 function getVar( $varname ) {
166181 if( isset( $this->vars[$varname] ) ) {
167182 return $this->vars[$varname];
Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php
@@ -37,6 +37,13 @@
3838 'localisationupdate-desc' => 'يبقي الرسائل المترجمة محدثة كأفضل ما يكون',
3939 );
4040
 41+/** Asturian (Asturianu)
 42+ * @author Xuacu
 43+ */
 44+$messages['ast'] = array(
 45+ 'localisationupdate-desc' => 'Caltién los mensaxes llocalizaos tan anovaos como se pueda',
 46+);
 47+
4148 /** Bashkir (Башҡортса)
4249 * @author Assele
4350 */
@@ -107,11 +114,19 @@
108115 'localisationupdate-desc' => "Yn diweddaru'r cyfieithiadau o negeseuon mor aml â phosib",
109116 );
110117
 118+/** Danish (Dansk)
 119+ * @author Peter Alberti
 120+ */
 121+$messages['da'] = array(
 122+ 'localisationupdate-desc' => 'Holder de lokaliserede meddelelser så opdaterede som muligt',
 123+);
 124+
111125 /** German (Deutsch)
 126+ * @author Kghbln
112127 * @author Purodha
113128 */
114129 $messages['de'] = array(
115 - 'localisationupdate-desc' => 'Lokalisierte Texte und Nachrichten möglichst aktuell halten',
 130+ 'localisationupdate-desc' => 'Ermöglicht es lokalisierte Texte und Nachrichten so aktuell wie möglich zu halten',
116131 );
117132
118133 /** Lower Sorbian (Dolnoserbski)
@@ -290,6 +305,13 @@
291306 'localisationupdate-desc' => 'പ്രാദേശികഭാഷയിലാക്കിയ സന്ദേശങ്ങൾ കഴിയുന്നത്ര വേഗം ചേർക്കാൻ ഉപയോഗിക്കുന്നു',
292307 );
293308
 309+/** Malay (Bahasa Melayu)
 310+ * @author Anakmalaysia
 311+ */
 312+$messages['ms'] = array(
 313+ 'localisationupdate-desc' => 'Memastikan kekemaskinian mesej-mesej yang disetempatkan',
 314+);
 315+
294316 /** Dutch (Nederlands)
295317 * @author Siebrand
296318 */
@@ -375,14 +397,14 @@
376398 'localisationupdate-desc' => 'Udržiava lokalizované správy čo najaktuálnejšie',
377399 );
378400
379 -/** Serbian Cyrillic ekavian (‪Српски (ћирилица)‬)
 401+/** Serbian (Cyrillic script) (‪Српски (ћирилица)‬)
380402 * @author Михајло Анђелковић
381403 */
382404 $messages['sr-ec'] = array(
383405 'localisationupdate-desc' => 'Ажурира локализоване поруке колико је то могуће',
384406 );
385407
386 -/** Serbian Latin ekavian (‪Srpski (latinica)‬)
 408+/** Serbian (Latin script) (‪Srpski (latinica)‬)
387409 * @author Liangent
388410 */
389411 $messages['sr-el'] = array(
Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/update.php
@@ -4,6 +4,7 @@
55 ? getenv( 'MW_INSTALL_PATH' )
66 : realpath( dirname( __FILE__ ) . "/../../" );
77
 8+// TODO: migrate to maintenance class
89 require_once( "$IP/maintenance/commandLine.inc" );
910
1011 if( isset( $options['help'] ) ) {
@@ -17,6 +18,7 @@
1819 print " --skip-extensions Don't fetch any extension files\n";
1920 print " --all Fetch all present extensions, not just those enabled\n";
2021 print " --outdir=<dir> Override output directory for serialized update files\n";
 22+ print " --svnurl=<url> URL to SVN repository, or path to local SVN checkout. Default: $wgLocalisationUpdateSVNURL\n";
2123 print "\n";
2224 exit( 0 );
2325 }
Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.php
@@ -11,7 +11,7 @@
1212 /**
1313 * Directory to store serialized cache files in. Defaults to $wgCacheDirectory.
1414 * It's OK to share this directory among wikis as long as the wiki you run
15 - * update.php on has all extensions the other wikis using the same directory
 15+ * update.php on has all extensions the other wikis using the same directory
1616 * have.
1717 * NOTE: If this variable and $wgCacheDirectory are both false, this extension
1818 * WILL NOT WORK.
@@ -33,14 +33,13 @@
3434
3535 $wgLocalisationUpdateRetryAttempts = 5;
3636
37 -
3837 // Info about me!
3938 $wgExtensionCredits['other'][] = array(
4039 'path' => __FILE__,
4140 'name' => 'LocalisationUpdate',
4241 'author' => array( 'Tom Maaswinkel', 'Niklas Laxström', 'Roan Kattouw' ),
4342 'version' => '0.3',
44 - 'url' => 'http://www.mediawiki.org/wiki/Extension:LocalisationUpdate',
 43+ 'url' => 'https://www.mediawiki.org/wiki/Extension:LocalisationUpdate',
4544 'descriptionmsg' => 'localisationupdate-desc',
4645 );
4746
Index: branches/wmf/1.18wmf1/extensions/LocalisationUpdate/LocalisationUpdate.class.php
@@ -2,72 +2,89 @@
33
44 /**
55 * Class for localization updates.
6 - *
 6+ *
77 * TODO: refactor code to remove duplication
88 */
99 class LocalisationUpdate {
10 -
 10+
1111 private static $newHashes = null;
1212 private static $filecache = array();
13 -
 13+
1414 /**
1515 * LocalisationCacheRecache hook handler.
16 - *
 16+ *
1717 * @param $lc LocalisationCache
1818 * @param $langcode String
1919 * @param $cache Array
20 - *
 20+ *
2121 * @return true
2222 */
2323 public static function onRecache( LocalisationCache $lc, $langcode, array &$cache ) {
24 - $cache['messages'] = array_merge(
25 - $cache['messages'],
26 - self::readFile( $langcode )
27 - );
28 -
29 - $cache['deps'][] = new FileDependency(
30 - self::filename( $langcode )
31 - );
32 -
 24+ // Handle fallback sequence and load all fallback messages from the cache
 25+ $codeSequence = array_merge( array( $langcode ), $cache['fallbackSequence'] );
 26+ // Iterate over the fallback sequence in reverse, otherwise the fallback
 27+ // language will override the requested language
 28+ foreach ( array_reverse( $codeSequence ) as $code ) {
 29+ if ( $code == 'en' ) {
 30+ // Skip English, otherwise we end up trying to read
 31+ // the nonexistent cache file for en a couple hundred times
 32+ continue;
 33+ }
 34+
 35+ $cache['messages'] = array_merge(
 36+ $cache['messages'],
 37+ self::readFile( $code )
 38+ );
 39+
 40+ $cache['deps'][] = new FileDependency(
 41+ self::filename( $code )
 42+ );
 43+ }
 44+
3345 return true;
3446 }
3547
3648 /**
3749 * Called from the cronjob to fetch new messages from SVN.
38 - *
 50+ *
3951 * @param $options Array
40 - *
 52+ *
4153 * @return true
4254 */
4355 public static function updateMessages( array $options ) {
44 - global $wgLocalisationUpdateDirectory;
45 -
 56+ global $wgLocalisationUpdateDirectory, $wgLocalisationUpdateSVNURL;
 57+
4658 $verbose = !isset( $options['quiet'] );
4759 $all = isset( $options['all'] );
4860 $skipCore = isset( $options['skip-core'] );
4961 $skipExtensions = isset( $options['skip-extensions'] );
50 -
 62+
5163 if( isset( $options['outdir'] ) ) {
5264 $wgLocalisationUpdateDirectory = $options['outdir'];
5365 }
5466
 67+ if ( isset( $options['svnurl['] ) ) {
 68+ // FIXME: Ewwwww. Refactor so this can be done properly
 69+ $wgLocalisationUpdateSVNURL = $options['svnurl'];
 70+ }
 71+
5572 $result = 0;
56 -
 73+
5774 // Update all MW core messages.
5875 if( !$skipCore ) {
5976 $result = self::updateMediawikiMessages( $verbose );
6077 }
61 -
 78+
6279 // Update all Extension messages.
6380 if( !$skipExtensions ) {
6481 if( $all ) {
6582 global $IP;
6683 $extFiles = array();
67 -
 84+
6885 // Look in extensions/ for all available items...
6986 // TODO: add support for $wgExtensionAssetsPath
7087 $dirs = new RecursiveDirectoryIterator( "$IP/extensions/" );
71 -
 88+
7289 // I ain't kidding... RecursiveIteratorIterator.
7390 foreach( new RecursiveIteratorIterator( $dirs ) as $pathname => $item ) {
7491 $filename = basename( $pathname );
@@ -85,33 +102,33 @@
86103 $result += self::updateExtensionMessages( $locFile, $extension, $verbose );
87104 }
88105 }
89 -
 106+
90107 self::writeHashes();
91108
92109 // And output the result!
93110 self::myLog( "Updated {$result} messages in total" );
94111 self::myLog( "Done" );
95 -
 112+
96113 return true;
97114 }
98115
99116 /**
100117 * Update Extension Messages.
101 - *
 118+ *
102119 * @param $file String
103120 * @param $extension String
104121 * @param $verbose Boolean
105 - *
 122+ *
106123 * @return Integer: the amount of updated messages
107124 */
108125 public static function updateExtensionMessages( $file, $extension, $verbose ) {
109126 global $IP, $wgLocalisationUpdateSVNURL;
110 -
 127+
111128 $relfile = wfRelativePath( $file, "$IP/extensions" );
112 -
 129+
113130 // Create a full path.
114131 // TODO: add support for $wgExtensionAssetsPath
115 - $localfile = "$IP/extensions/$relfile";
 132+ // $localfile = "$IP/extensions/$relfile";
116133
117134 // Get the full SVN directory path.
118135 // TODO: add support for $wgExtensionAssetsPath
@@ -119,15 +136,15 @@
120137
121138 // Compare the 2 files.
122139 $result = self::compareExtensionFiles( $extension, $svnfile, $file, $verbose, false, true );
123 -
 140+
124141 return $result;
125142 }
126143
127144 /**
128 - * Update the Mediawiki Core Messages.
129 - *
 145+ * Update the MediaWiki Core Messages.
 146+ *
130147 * @param $verbose Boolean
131 - *
 148+ *
132149 * @return Integer: the amount of updated messages
133150 */
134151 public static function updateMediawikiMessages( $verbose ) {
@@ -173,22 +190,22 @@
174191
175192 // Compare the files.
176193 $result = self::compareFiles( $svnfile, $localfile, $verbose, $changedEnglishStrings, false, true );
177 -
 194+
178195 // And update the change counter.
179196 $changedCount += count( $result );
180197 }
181198
182199 // Log some nice info.
183 - self::myLog( "{$changedCount} Mediawiki messages are updated" );
184 -
 200+ self::myLog( "{$changedCount} MediaWiki messages are updated" );
 201+
185202 return $changedCount;
186203 }
187204
188205 /**
189206 * Removes all unneeded content from a file and returns it.
190 - *
 207+ *
191208 * @param $contents String
192 - *
 209+ *
193210 * @return String
194211 */
195212 public static function cleanupFile( $contents ) {
@@ -199,9 +216,9 @@
200217 '?' . '>' => ''
201218 )
202219 );
203 -
 220+
204221 $results = array();
205 -
 222+
206223 // And we only want the messages array.
207224 preg_match( "/\\\$messages(.*\s)*?\);/", $contents, $results );
208225
@@ -218,26 +235,26 @@
219236 // Return the cleaned up file.
220237 return $contents;
221238 }
222 -
 239+
223240 /**
224241 * Removes all unneeded content from a file and returns it.
225 - *
226 - * FIXME: duplicated cleanupFile code
227 - *
 242+ *
 243+ * FIXME: duplicated cleanupFile code
 244+ *
228245 * @param $contents String
229 - *
 246+ *
230247 * @return string
231248 */
232249 public static function cleanupExtensionFile( $contents ) {
233250 // We don't want PHP tags.
234251 $contents = preg_replace( "/<\?php/", "", $contents );
235252 $contents = preg_replace( "/\?" . ">/", "", $contents );
236 -
 253+
237254 $results = array();
238 -
 255+
239256 // And we only want message arrays.
240257 preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
241 -
 258+
242259 // But we want them all in one string.
243260 if( !empty( $results[0] ) && is_array( $results[0] ) ) {
244261 $contents = implode( "\n\n", $results[0] );
@@ -247,23 +264,23 @@
248265
249266 // And we hate the windows vs linux linebreaks.
250267 $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
251 -
 268+
252269 return $contents;
253 - }
 270+ }
254271
255272 /**
256273 * Returns the contents of a file or false on failiure.
257 - *
 274+ *
258275 * @param $basefile String
259 - *
 276+ *
260277 * @return string or false
261278 */
262279 public static function getFileContents( $basefile ) {
263280 global $wgLocalisationUpdateRetryAttempts;
264 -
 281+
265282 $attempts = 0;
266283 $basefilecontents = '';
267 -
 284+
268285 // Use cURL to get the SVN contents.
269286 if ( preg_match( "/^http/", $basefile ) ) {
270287 while( !$basefilecontents && $attempts <= $wgLocalisationUpdateRetryAttempts ) {
@@ -272,7 +289,7 @@
273290 self::myLog( 'Failed to download ' . $basefile . "; retrying in ${delay}s..." );
274291 sleep( $delay );
275292 }
276 -
 293+
277294 $basefilecontents = Http::get( $basefile );
278295 $attempts++;
279296 }
@@ -286,20 +303,20 @@
287304 return false;
288305 }
289306 }
290 -
 307+
291308 return $basefilecontents;
292309 }
293310
294311 /**
295312 * Returns an array containing the differences between the files.
296 - *
 313+ *
297314 * @param $basefile String
298315 * @param $comparefile String
299316 * @param $verbose Boolean
300317 * @param $forbiddenKeys Array
301318 * @param $alwaysGetResult Boolean
302319 * @param $saveResults Boolean
303 - *
 320+ *
304321 * @return array
305322 */
306323 public static function compareFiles( $basefile, $comparefile, $verbose, array $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
@@ -307,7 +324,7 @@
308325 $langcode = Language::getCodeFromFileName( $basefile, 'Messages' );
309326
310327 $basefilecontents = self::getFileContents( $basefile );
311 -
 328+
312329 if ( $basefilecontents === false || $basefilecontents === '' ) {
313330 return array(); // Failed
314331 }
@@ -318,7 +335,7 @@
319336 // Change the variable name.
320337 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
321338 $basehash = md5( $basefilecontents );
322 -
 339+
323340 // Check if the file has changed since our last update.
324341 if ( !$alwaysGetResult ) {
325342 if ( !self::checkHash( $basefile, $basehash ) ) {
@@ -331,7 +348,7 @@
332349 $base_messages = self::parsePHP( $basefilecontents, 'base_messages' );
333350
334351 $comparefilecontents = self::getFileContents( $comparefile );
335 -
 352+
336353 if ( $comparefilecontents === false || $comparefilecontents === '' ) {
337354 return array(); // Failed
338355 }
@@ -342,7 +359,7 @@
343360 // Rename the array.
344361 $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
345362 $comparehash = md5( $comparefilecontents );
346 -
 363+
347364 // If this is the remote file check if the file has changed since our last update.
348365 if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
349366 if ( !self::checkHash( $comparefile, $comparehash ) ) {
@@ -350,7 +367,7 @@
351368 return array();
352369 }
353370 }
354 -
 371+
355372 // Get the array.
356373 $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
357374
@@ -378,100 +395,113 @@
379396 } elseif ( $saveResults ) {
380397 self::myLog( "--{$langcode} hasn't changed--", $verbose );
381398 }
382 -
 399+
383400 self::saveHash( $basefile, $basehash );
384 -
 401+
385402 self::saveHash( $comparefile, $comparehash );
386 -
 403+
387404 return $changedStrings;
388405 }
389406
390407 /**
391408 * Checks whether a messages file has a certain hash.
392 - *
 409+ *
393410 * TODO: Swap return values, this is insane
394 - *
 411+ *
395412 * @param $file string Filename
396413 * @param $hash string Hash
397 - *
 414+ *
398415 * @return bool True if $file does NOT have hash $hash, false if it does
399416 */
400417 public static function checkHash( $file, $hash ) {
401418 $hashes = self::readFile( 'hashes' );
402419 return @$hashes[$file] !== $hash;
403420 }
404 -
 421+
 422+ /**
 423+ * @param $file
 424+ * @param $hash
 425+ */
405426 public static function saveHash( $file, $hash ) {
406427 if ( is_null( self::$newHashes ) ) {
407428 self::$newHashes = self::readFile( 'hashes' );
408429 }
409 -
 430+
410431 self::$newHashes[$file] = $hash;
411432 }
412 -
 433+
413434 public static function writeHashes() {
414435 self::writeFile( 'hashes', self::$newHashes );
415436 }
416437
417438 /**
418 - *
419 - *
 439+ *
 440+ *
420441 * @param $changedStrings Array
421442 * @param $forbiddenKeys Array
422443 * @param $compare_messages Array
423444 * @param $base_messages Array
424445 * @param $langcode String
425446 * @param $verbose Boolean
426 - *
 447+ *
427448 * @return Integer: the amount of updated messages
428449 */
429450 public static function saveChanges( $changedStrings, array $forbiddenKeys, array $compare_messages, array $base_messages, $langcode, $verbose ) {
430451 // Count the updates.
431452 $updates = 0;
432 -
 453+
433454 if( !is_array( $changedStrings ) ) {
434455 self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file " . (__FILE__) . ' at line ' .( __LINE__ ) );
435456 return 0;
436457 }
437458
438 - $new_messages = self::readFile( $langcode );
439 -
440 - foreach ( $changedStrings as $key => $value ) {
441 - // If this message wasn't changed in English.
442 - if ( !isset( $forbiddenKeys[$key] ) ) {
 459+ $new_messages = array();
 460+
 461+ //foreach ( $changedStrings as $key => $value ) {
 462+ // HACK for r103763 CR: store all messages, even unchanged ones
 463+ // TODO this file is a mess and needs to be rewritten
 464+ foreach ( array_merge( array_keys( $base_messages ), array_keys( $compare_messages ) ) as $key ) {
 465+ // Only update the translation if this message wasn't changed in English
 466+ if ( !isset( $forbiddenKeys[$key] ) && isset( $base_messages[$key] ) ) {
443467 $new_messages[$key] = $base_messages[$key];
444 -
445 - // Output extra logmessages when needed.
446 - if ( $verbose ) {
447 - $oldmsg = isset( $compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set';
448 - self::myLog( "Updated message {$key} from $oldmsg to '{$base_messages[$key]}'", $verbose );
 468+
 469+ if ( !isset( $compare_messages[$key] ) || $compare_messages[$key] !== $base_messages[$key] ) {
 470+ // Output extra logmessages when needed.
 471+ if ( $verbose ) {
 472+ $oldmsg = isset( $compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set';
 473+ self::myLog( "Updated message {$key} from $oldmsg to '{$base_messages[$key]}'", $verbose );
 474+ }
 475+
 476+ // Update the counter.
 477+ $updates++;
449478 }
450 -
451 - // Update the counter.
452 - $updates++;
 479+ } elseif ( isset( $forbiddenKeys[$key] ) && isset( $compare_messages[$key] ) ) {
 480+ // The message was changed in English, but a previous translation still exists in the cache.
 481+ // Use that previous translation rather than falling back to the .i18n.php file
 482+ $new_messages[$key] = $compare_messages[$key];
453483 }
454484 }
455485 self::writeFile( $langcode, $new_messages );
456 -
 486+
457487 return $updates;
458488 }
459489
460490 /**
461 - *
 491+ *
462492 * @param $extension String
463493 * @param $basefile String
464494 * @param $comparefile String
465495 * @param $verbose Boolean
466496 * @param $alwaysGetResult Boolean
467497 * @param $saveResults Boolean
468 - *
 498+ *
469499 * @return Integer: the amount of updated messages
470500 */
471501 public static function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) {
472502 // FIXME: Factor out duplicated code?
473503
474504 $basefilecontents = self::getFileContents( $basefile );
475 -
 505+
476506 if ( $basefilecontents === false || $basefilecontents === '' ) {
477507 return 0; // Failed
478508 }
@@ -482,7 +512,7 @@
483513 // Rename the arrays.
484514 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
485515 $basehash = md5( $basefilecontents );
486 -
 516+
487517 // If this is the remote file
488518 if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
489519 // Check if the hash has changed
@@ -572,22 +602,23 @@
573603 } elseif($saveResults === true) {
574604 self::myLog( "--{$language} hasn't changed--", $verbose );
575605 }
576 - }
 606+ }
577607
578608 // And log some stuff.
579609 self::myLog( "Updated " . $updates . " messages for the '{$extension}' extension", $verbose );
580610
581611 self::saveHash( $basefile, $basehash );
582 -
 612+
583613 self::saveHash( $comparefile, $comparehash );
584 -
 614+
585615 return $updates;
586616 }
587 -
 617+
588618 /**
589619 * Logs a message.
590 - *
 620+ *
591621 * @param $log String
 622+ * @param bool $verbose
592623 */
593624 public static function myLog( $log, $verbose = true ) {
594625 if ( !$verbose ) {
@@ -599,7 +630,12 @@
600631 print( $log . "\n" );
601632 }
602633 }
603 -
 634+
 635+ /**
 636+ * @param $php
 637+ * @param $varname
 638+ * @return bool|array
 639+ */
604640 public static function parsePHP( $php, $varname ) {
605641 try {
606642 $reader = new QuickArrayReader("<?php $php");
@@ -609,32 +645,41 @@
610646 return false;
611647 }
612648 }
613 -
 649+
 650+ /**
 651+ * @param $lang
 652+ * @return string
 653+ * @throws MWException
 654+ */
614655 public static function filename( $lang ) {
615656 global $wgLocalisationUpdateDirectory, $wgCacheDirectory;
616 -
 657+
617658 $dir = $wgLocalisationUpdateDirectory ?
618659 $wgLocalisationUpdateDirectory :
619660 $wgCacheDirectory;
620 -
 661+
621662 if ( !$dir ) {
622663 throw new MWException( 'No cache directory configured' );
623664 }
624 -
 665+
625666 return "$dir/l10nupdate-$lang.cache";
626667 }
627 -
 668+
 669+ /**
 670+ * @param $lang
 671+ * @return mixed
 672+ */
628673 public static function readFile( $lang ) {
629674 if ( !isset( self::$filecache[$lang] ) ) {
630675 $file = self::filename( $lang );
631676 $contents = @file_get_contents( $file );
632 -
 677+
633678 if ( $contents === false ) {
634679 wfDebug( "Failed to read file '$file'\n" );
635680 $retval = array();
636681 } else {
637682 $retval = unserialize( $contents );
638 -
 683+
639684 if ( $retval === false ) {
640685 wfDebug( "Corrupted data in file '$file'\n" );
641686 $retval = array();
@@ -642,18 +687,23 @@
643688 }
644689 self::$filecache[$lang] = $retval;
645690 }
646 -
 691+
647692 return self::$filecache[$lang];
648693 }
649 -
 694+
 695+ /**
 696+ * @param $lang
 697+ * @param $var
 698+ * @throws MWException
 699+ */
650700 public static function writeFile( $lang, $var ) {
651701 $file = self::filename( $lang );
652 -
 702+
653703 if ( !@file_put_contents( $file, serialize( $var ) ) ) {
654704 throw new MWException( "Failed to write to file '$file'" );
655705 }
656 -
 706+
657707 self::$filecache[$lang] = $var;
658708 }
659 -
660 -}
\ No newline at end of file
 709+
 710+}
Property changes on: branches/wmf/1.18wmf1/extensions/LocalisationUpdate
___________________________________________________________________
Modified: svn:mergeinfo
661711 Merged /trunk/extensions/LocalisationUpdate:r92477-106189

Status & tagging log