r94220 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94219‎ | r94220 | r94221 >
Date:01:50, 11 August 2011
Author:reedy
Status:resolved (Comments)
Tags:
Comment:
Trim whitespace
Modified paths:
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php (modified) (history)
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.php (modified) (history)

Diff [purge]

Index: trunk/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,7 +33,6 @@
3434
3535 $wgLocalisationUpdateRetryAttempts = 5;
3636
37 -
3837 // Info about me!
3938 $wgExtensionCredits['other'][] = array(
4039 'path' => __FILE__,
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php
@@ -2,21 +2,21 @@
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,50 +24,50 @@
2525 $cache['messages'],
2626 self::readFile( $langcode )
2727 );
28 -
 28+
2929 $cache['deps'][] = new FileDependency(
3030 self::filename( $langcode )
3131 );
32 -
 32+
3333 return true;
3434 }
3535
3636 /**
3737 * Called from the cronjob to fetch new messages from SVN.
38 - *
 38+ *
3939 * @param $options Array
40 - *
 40+ *
4141 * @return true
4242 */
4343 public static function updateMessages( array $options ) {
4444 global $wgLocalisationUpdateDirectory;
45 -
 45+
4646 $verbose = !isset( $options['quiet'] );
4747 $all = isset( $options['all'] );
4848 $skipCore = isset( $options['skip-core'] );
4949 $skipExtensions = isset( $options['skip-extensions'] );
50 -
 50+
5151 if( isset( $options['outdir'] ) ) {
5252 $wgLocalisationUpdateDirectory = $options['outdir'];
5353 }
54 -
 54+
5555 $result = 0;
56 -
 56+
5757 // Update all MW core messages.
5858 if( !$skipCore ) {
5959 $result = self::updateMediawikiMessages( $verbose );
6060 }
61 -
 61+
6262 // Update all Extension messages.
6363 if( !$skipExtensions ) {
6464 if( $all ) {
6565 global $IP;
6666 $extFiles = array();
67 -
 67+
6868 // Look in extensions/ for all available items...
6969 // TODO: add support for $wgExtensionAssetsPath
7070 $dirs = new RecursiveDirectoryIterator( "$IP/extensions/" );
71 -
 71+
7272 // I ain't kidding... RecursiveIteratorIterator.
7373 foreach( new RecursiveIteratorIterator( $dirs ) as $pathname => $item ) {
7474 $filename = basename( $pathname );
@@ -85,30 +85,30 @@
8686 $result += self::updateExtensionMessages( $locFile, $extension, $verbose );
8787 }
8888 }
89 -
 89+
9090 self::writeHashes();
9191
9292 // And output the result!
9393 self::myLog( "Updated {$result} messages in total" );
9494 self::myLog( "Done" );
95 -
 95+
9696 return true;
9797 }
9898
9999 /**
100100 * Update Extension Messages.
101 - *
 101+ *
102102 * @param $file String
103103 * @param $extension String
104104 * @param $verbose Boolean
105 - *
 105+ *
106106 * @return Integer: the amount of updated messages
107107 */
108108 public static function updateExtensionMessages( $file, $extension, $verbose ) {
109109 global $IP, $wgLocalisationUpdateSVNURL;
110 -
 110+
111111 $relfile = wfRelativePath( $file, "$IP/extensions" );
112 -
 112+
113113 // Create a full path.
114114 // TODO: add support for $wgExtensionAssetsPath
115115 $localfile = "$IP/extensions/$relfile";
@@ -119,15 +119,15 @@
120120
121121 // Compare the 2 files.
122122 $result = self::compareExtensionFiles( $extension, $svnfile, $file, $verbose, false, true );
123 -
 123+
124124 return $result;
125125 }
126126
127127 /**
128128 * Update the Mediawiki Core Messages.
129 - *
 129+ *
130130 * @param $verbose Boolean
131 - *
 131+ *
132132 * @return Integer: the amount of updated messages
133133 */
134134 public static function updateMediawikiMessages( $verbose ) {
@@ -173,22 +173,22 @@
174174
175175 // Compare the files.
176176 $result = self::compareFiles( $svnfile, $localfile, $verbose, $changedEnglishStrings, false, true );
177 -
 177+
178178 // And update the change counter.
179179 $changedCount += count( $result );
180180 }
181181
182182 // Log some nice info.
183183 self::myLog( "{$changedCount} Mediawiki messages are updated" );
184 -
 184+
185185 return $changedCount;
186186 }
187187
188188 /**
189189 * Removes all unneeded content from a file and returns it.
190 - *
 190+ *
191191 * @param $contents String
192 - *
 192+ *
193193 * @return String
194194 */
195195 public static function cleanupFile( $contents ) {
@@ -199,9 +199,9 @@
200200 '?' . '>' => ''
201201 )
202202 );
203 -
 203+
204204 $results = array();
205 -
 205+
206206 // And we only want the messages array.
207207 preg_match( "/\\\$messages(.*\s)*?\);/", $contents, $results );
208208
@@ -218,26 +218,26 @@
219219 // Return the cleaned up file.
220220 return $contents;
221221 }
222 -
 222+
223223 /**
224224 * Removes all unneeded content from a file and returns it.
225 - *
226 - * FIXME: duplicated cleanupFile code
227 - *
 225+ *
 226+ * FIXME: duplicated cleanupFile code
 227+ *
228228 * @param $contents String
229 - *
 229+ *
230230 * @return string
231231 */
232232 public static function cleanupExtensionFile( $contents ) {
233233 // We don't want PHP tags.
234234 $contents = preg_replace( "/<\?php/", "", $contents );
235235 $contents = preg_replace( "/\?" . ">/", "", $contents );
236 -
 236+
237237 $results = array();
238 -
 238+
239239 // And we only want message arrays.
240240 preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
241 -
 241+
242242 // But we want them all in one string.
243243 if( !empty( $results[0] ) && is_array( $results[0] ) ) {
244244 $contents = implode( "\n\n", $results[0] );
@@ -247,23 +247,23 @@
248248
249249 // And we hate the windows vs linux linebreaks.
250250 $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
251 -
 251+
252252 return $contents;
253 - }
 253+ }
254254
255255 /**
256256 * Returns the contents of a file or false on failiure.
257 - *
 257+ *
258258 * @param $basefile String
259 - *
 259+ *
260260 * @return string or false
261261 */
262262 public static function getFileContents( $basefile ) {
263263 global $wgLocalisationUpdateRetryAttempts;
264 -
 264+
265265 $attempts = 0;
266266 $basefilecontents = '';
267 -
 267+
268268 // Use cURL to get the SVN contents.
269269 if ( preg_match( "/^http/", $basefile ) ) {
270270 while( !$basefilecontents && $attempts <= $wgLocalisationUpdateRetryAttempts ) {
@@ -272,7 +272,7 @@
273273 self::myLog( 'Failed to download ' . $basefile . "; retrying in ${delay}s..." );
274274 sleep( $delay );
275275 }
276 -
 276+
277277 $basefilecontents = Http::get( $basefile );
278278 $attempts++;
279279 }
@@ -286,20 +286,20 @@
287287 return false;
288288 }
289289 }
290 -
 290+
291291 return $basefilecontents;
292292 }
293293
294294 /**
295295 * Returns an array containing the differences between the files.
296 - *
 296+ *
297297 * @param $basefile String
298298 * @param $comparefile String
299299 * @param $verbose Boolean
300300 * @param $forbiddenKeys Array
301301 * @param $alwaysGetResult Boolean
302302 * @param $saveResults Boolean
303 - *
 303+ *
304304 * @return array
305305 */
306306 public static function compareFiles( $basefile, $comparefile, $verbose, array $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
@@ -307,7 +307,7 @@
308308 $langcode = Language::getCodeFromFileName( $basefile, 'Messages' );
309309
310310 $basefilecontents = self::getFileContents( $basefile );
311 -
 311+
312312 if ( $basefilecontents === false || $basefilecontents === '' ) {
313313 return array(); // Failed
314314 }
@@ -318,7 +318,7 @@
319319 // Change the variable name.
320320 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
321321 $basehash = md5( $basefilecontents );
322 -
 322+
323323 // Check if the file has changed since our last update.
324324 if ( !$alwaysGetResult ) {
325325 if ( !self::checkHash( $basefile, $basehash ) ) {
@@ -331,7 +331,7 @@
332332 $base_messages = self::parsePHP( $basefilecontents, 'base_messages' );
333333
334334 $comparefilecontents = self::getFileContents( $comparefile );
335 -
 335+
336336 if ( $comparefilecontents === false || $comparefilecontents === '' ) {
337337 return array(); // Failed
338338 }
@@ -342,7 +342,7 @@
343343 // Rename the array.
344344 $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
345345 $comparehash = md5( $comparefilecontents );
346 -
 346+
347347 // If this is the remote file check if the file has changed since our last update.
348348 if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
349349 if ( !self::checkHash( $comparefile, $comparehash ) ) {
@@ -350,7 +350,7 @@
351351 return array();
352352 }
353353 }
354 -
 354+
355355 // Get the array.
356356 $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
357357
@@ -378,69 +378,69 @@
379379 } elseif ( $saveResults ) {
380380 self::myLog( "--{$langcode} hasn't changed--", $verbose );
381381 }
382 -
 382+
383383 self::saveHash( $basefile, $basehash );
384 -
 384+
385385 self::saveHash( $comparefile, $comparehash );
386 -
 386+
387387 return $changedStrings;
388388 }
389389
390390 /**
391391 * Checks whether a messages file has a certain hash.
392 - *
 392+ *
393393 * TODO: Swap return values, this is insane
394 - *
 394+ *
395395 * @param $file string Filename
396396 * @param $hash string Hash
397 - *
 397+ *
398398 * @return bool True if $file does NOT have hash $hash, false if it does
399399 */
400400 public static function checkHash( $file, $hash ) {
401401 $hashes = self::readFile( 'hashes' );
402402 return @$hashes[$file] !== $hash;
403403 }
404 -
 404+
405405 public static function saveHash( $file, $hash ) {
406406 if ( is_null( self::$newHashes ) ) {
407407 self::$newHashes = self::readFile( 'hashes' );
408408 }
409 -
 409+
410410 self::$newHashes[$file] = $hash;
411411 }
412 -
 412+
413413 public static function writeHashes() {
414414 self::writeFile( 'hashes', self::$newHashes );
415415 }
416416
417417 /**
418 - *
419 - *
 418+ *
 419+ *
420420 * @param $changedStrings Array
421421 * @param $forbiddenKeys Array
422422 * @param $compare_messages Array
423423 * @param $base_messages Array
424424 * @param $langcode String
425425 * @param $verbose Boolean
426 - *
 426+ *
427427 * @return Integer: the amount of updated messages
428428 */
429429 public static function saveChanges( $changedStrings, array $forbiddenKeys, array $compare_messages, array $base_messages, $langcode, $verbose ) {
430430 // Count the updates.
431431 $updates = 0;
432 -
 432+
433433 if( !is_array( $changedStrings ) ) {
434434 self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file " . (__FILE__) . ' at line ' .( __LINE__ ) );
435435 return 0;
436436 }
437437
438438 $new_messages = self::readFile( $langcode );
439 -
 439+
440440 foreach ( $changedStrings as $key => $value ) {
441441 // If this message wasn't changed in English.
442442 if ( !isset( $forbiddenKeys[$key] ) ) {
443443 $new_messages[$key] = $base_messages[$key];
444 -
 444+
445445 // Output extra logmessages when needed.
446446 if ( $verbose ) {
447447 $oldmsg = isset( $compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set';
@@ -452,26 +452,26 @@
453453 }
454454 }
455455 self::writeFile( $langcode, $new_messages );
456 -
 456+
457457 return $updates;
458458 }
459459
460460 /**
461 - *
 461+ *
462462 * @param $extension String
463463 * @param $basefile String
464464 * @param $comparefile String
465465 * @param $verbose Boolean
466466 * @param $alwaysGetResult Boolean
467467 * @param $saveResults Boolean
468 - *
 468+ *
469469 * @return Integer: the amount of updated messages
470470 */
471471 public static function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) {
472472 // FIXME: Factor out duplicated code?
473473
474474 $basefilecontents = self::getFileContents( $basefile );
475 -
 475+
476476 if ( $basefilecontents === false || $basefilecontents === '' ) {
477477 return 0; // Failed
478478 }
@@ -482,7 +482,7 @@
483483 // Rename the arrays.
484484 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
485485 $basehash = md5( $basefilecontents );
486 -
 486+
487487 // If this is the remote file
488488 if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
489489 // Check if the hash has changed
@@ -516,8 +516,13 @@
517517 }
518518 }
519519
520 - // Get the real array.
521 - $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
 520+ try {
 521+ // Get the real array.
 522+ $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
 523+ } catch (Exception $ex) {
 524+ var_dump( $basefile, $comparefile );
 525+ die();
 526+ }
522527
523528 // If both files are the same, they can be skipped.
524529 if ( $basehash == $comparehash && !$alwaysGetResult ) {
@@ -578,21 +583,21 @@
579584 } elseif($saveResults === true) {
580585 self::myLog( "--{$language} hasn't changed--", $verbose );
581586 }
582 - }
 587+ }
583588
584589 // And log some stuff.
585590 self::myLog( "Updated " . $updates . " messages for the '{$extension}' extension", $verbose );
586591
587592 self::saveHash( $basefile, $basehash );
588 -
 593+
589594 self::saveHash( $comparefile, $comparehash );
590 -
 595+
591596 return $updates;
592597 }
593 -
 598+
594599 /**
595600 * Logs a message.
596 - *
 601+ *
597602 * @param $log String
598603 */
599604 public static function myLog( $log, $verbose = true ) {
@@ -605,7 +610,7 @@
606611 print( $log . "\n" );
607612 }
608613 }
609 -
 614+
610615 public static function parsePHP( $php, $varname ) {
611616 try {
612617 $reader = new QuickArrayReader("<?php $php");
@@ -615,32 +620,32 @@
616621 return false;
617622 }
618623 }
619 -
 624+
620625 public static function filename( $lang ) {
621626 global $wgLocalisationUpdateDirectory, $wgCacheDirectory;
622 -
 627+
623628 $dir = $wgLocalisationUpdateDirectory ?
624629 $wgLocalisationUpdateDirectory :
625630 $wgCacheDirectory;
626 -
 631+
627632 if ( !$dir ) {
628633 throw new MWException( 'No cache directory configured' );
629634 }
630 -
 635+
631636 return "$dir/l10nupdate-$lang.cache";
632637 }
633 -
 638+
634639 public static function readFile( $lang ) {
635640 if ( !isset( self::$filecache[$lang] ) ) {
636641 $file = self::filename( $lang );
637642 $contents = @file_get_contents( $file );
638 -
 643+
639644 if ( $contents === false ) {
640645 wfDebug( "Failed to read file '$file'\n" );
641646 $retval = array();
642647 } else {
643648 $retval = unserialize( $contents );
644 -
 649+
645650 if ( $retval === false ) {
646651 wfDebug( "Corrupted data in file '$file'\n" );
647652 $retval = array();
@@ -648,18 +653,18 @@
649654 }
650655 self::$filecache[$lang] = $retval;
651656 }
652 -
 657+
653658 return self::$filecache[$lang];
654659 }
655 -
 660+
656661 public static function writeFile( $lang, $var ) {
657662 $file = self::filename( $lang );
658 -
 663+
659664 if ( !@file_put_contents( $file, serialize( $var ) ) ) {
660665 throw new MWException( "Failed to write to file '$file'" );
661666 }
662 -
 667+
663668 self::$filecache[$lang] = $var;
664669 }
665 -
666 -}
\ No newline at end of file
 670+
 671+}

Follow-up revisions

RevisionCommit summaryAuthorDate
r94222Fix r94220, kill debugging codereedy02:16, 11 August 2011

Comments

#Comment by Aaron Schulz (talk | contribs)   01:55, 11 August 2011

Not just whitespace changes.

#Comment by Krinkle (talk | contribs)   11:33, 11 August 2011
+		var_dump( $basefile, $comparefile );

Reverted in r94222.

Status & tagging log