r70210 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r70209‎ | r70210 | r70211 >
Date:22:03, 30 July 2010
Author:jeroendedauw
Status:deferred
Tags:
Comment:
Follow up to r70206
Modified paths:
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php
@@ -1,4 +1,10 @@
22 <?php
 3+
 4+/**
 5+ * Class for localization updates.
 6+ *
 7+ * TODO: refactor code to remove duplication
 8+ */
39 class LocalisationUpdate {
410
511 private static $newHashes = null;
@@ -212,10 +218,45 @@
213219 // Return the cleaned up file.
214220 return $contents;
215221 }
 222+
 223+ /**
 224+ * Removes all unneeded content from a file and returns it.
 225+ *
 226+ * FIXME: duplicated cleanupFile code
 227+ *
 228+ * @param $contents String
 229+ *
 230+ * @return string
 231+ */
 232+ public static function cleanupExtensionFile( $contents ) {
 233+ // We don't want PHP tags.
 234+ $contents = preg_replace( "/<\?php/", "", $contents );
 235+ $contents = preg_replace( "/\?" . ">/", "", $contents );
 236+
 237+ $results = array();
 238+
 239+ // And we only want message arrays.
 240+ preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
 241+
 242+ // But we want them all in one string.
 243+ if( !empty( $results[0] ) && is_array( $results[0] ) ) {
 244+ $contents = implode( "\n\n", $results[0] );
 245+ } else {
 246+ $contents = '';
 247+ }
216248
 249+ // And we hate the windows vs linux linebreaks.
 250+ $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
 251+
 252+ return $contents;
 253+ }
 254+
217255 /**
 256+ * Returns the contents of a file or false on failiure.
218257 *
219258 * @param $basefile String
 259+ *
 260+ * @return string or false
220261 */
221262 public static function getFileContents( $basefile ) {
222263 global $wgLocalisationUpdateRetryAttempts;
@@ -249,24 +290,39 @@
250291 return $basefilecontents;
251292 }
252293
253 - public static function compareFiles( $basefile, $comparefile, $verbose, $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
 294+ /**
 295+ * Returns an array containing the differences between the files.
 296+ *
 297+ * @param $basefile String
 298+ * @param $comparefile String
 299+ * @param $verbose Boolean
 300+ * @param $forbiddenKeys Array
 301+ * @param $alwaysGetResult Boolean
 302+ * @param $saveResults Boolean
 303+ *
 304+ * @return array
 305+ */
 306+ public static function compareFiles( $basefile, $comparefile, $verbose, array $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
254307 $compare_messages = array();
255308 $base_messages = array();
256309
257 - // Get the languagecode
 310+ // Get the languagecode.
258311 $langcode = Language::getCodeFromFileName( $basefile, 'Messages' );
259312
260313 $basefilecontents = self::getFileContents( $basefile );
261 - if ( $basefilecontents === false || $basefilecontents === "" ) return array(); // Failed
 314+
 315+ if ( $basefilecontents === false || $basefilecontents === '' ) {
 316+ return array(); // Failed
 317+ }
262318
263 - // Only get the part we need
 319+ // Only get the part we need.
264320 $basefilecontents = self::cleanupFile( $basefilecontents );
265321
266 - // Change the variable name
 322+ // Change the variable name.
267323 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
268 -
269324 $basehash = md5( $basefilecontents );
270 - // Check if the file has changed since our last update
 325+
 326+ // Check if the file has changed since our last update.
271327 if ( !$alwaysGetResult ) {
272328 if ( !self::checkHash( $basefile, $basehash ) ) {
273329 self::myLog( "Skipping {$langcode} since the remote file hasn't changed since our last update" );
@@ -274,52 +330,57 @@
275331 }
276332 }
277333
278 - // Get the array with messages
 334+ // Get the array with messages.
279335 $base_messages = self::parsePHP( $basefilecontents, 'base_messages' );
280336
281337 $comparefilecontents = self::getFileContents( $comparefile );
282 - if ( $comparefilecontents === false || $comparefilecontents === "" ) return array(); // Failed
 338+
 339+ if ( $comparefilecontents === false || $comparefilecontents === '' ) {
 340+ return array(); // Failed
 341+ }
283342
284 - // only get the stuff we need
 343+ // Only get the stuff we need.
285344 $comparefilecontents = self::cleanupFile( $comparefilecontents );
286345
287 - // rename the array
 346+ // Rename the array.
288347 $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
289 -
290348 $comparehash = md5( $comparefilecontents );
291 - // If this is the remote file check if the file has changed since our last update
 349+
 350+ // If this is the remote file check if the file has changed since our last update.
292351 if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
293352 if ( !self::checkHash( $comparefile, $comparehash ) ) {
294353 self::myLog( "Skipping {$langcode} since the remote file has not changed since our last update" );
295354 return array();
296355 }
297356 }
298 - // Get the array
 357+
 358+ // Get the array.
299359 $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
300360
301 - // if the localfile and the remote file are the same, skip them!
 361+ // If the localfile and the remote file are the same, skip them!
302362 if ( $basehash == $comparehash && !$alwaysGetResult ) {
303363 self::myLog( "Skipping {$langcode} since the remote file is the same as the local file" );
304364 return array();
305365 }
306366
307 - // Add the messages we got with our previous update(s) to the local array (as we already got these as well)
308 - $compare_messages = array_merge( $compare_messages,
309 - self::readFile( $langcode ) );
 367+ // Add the messages we got with our previous update(s) to the local array (as we already got these as well).
 368+ $compare_messages = array_merge(
 369+ $compare_messages,
 370+ self::readFile( $langcode )
 371+ );
310372
311 - // Compare the remote and local message arrays
 373+ // Compare the remote and local message arrays.
312374 $changedStrings = array_diff_assoc( $base_messages, $compare_messages );
313375
314 - // If we want to save the differences
315 - if ( $saveResults && !empty($changedStrings) && is_array($changedStrings)) {
 376+ // If we want to save the differences.
 377+ if ( $saveResults && !empty( $changedStrings ) && is_array( $changedStrings ) ) {
316378 self::myLog( "--Checking languagecode {$langcode}--" );
317 - // The save them
 379+ // Save the differences.
318380 $updates = self::saveChanges( $changedStrings, $forbiddenKeys, $compare_messages, $base_messages, $langcode, $verbose );
319381 self::myLog( "{$updates} messages updated for {$langcode}." );
320382 } elseif ( $saveResults ) {
321383 self::myLog( "--{$langcode} hasn't changed--" );
322384 }
323 -
324385
325386 self::saveHash( $basefile, $basehash );
326387
@@ -329,10 +390,13 @@
330391 }
331392
332393 /**
333 - * Checks whether a messages file has a certain hash
 394+ * Checks whether a messages file has a certain hash.
 395+ *
334396 * TODO: Swap return values, this is insane
 397+ *
335398 * @param $file string Filename
336399 * @param $hash string Hash
 400+ *
337401 * @return bool True if $file does NOT have hash $hash, false if it does
338402 */
339403 public static function checkHash( $file, $hash ) {
@@ -341,8 +405,10 @@
342406 }
343407
344408 public static function saveHash( $file, $hash ) {
345 - if ( is_null( self::$newHashes ) )
 409+ if ( is_null( self::$newHashes ) ) {
346410 self::$newHashes = self::readFile( 'hashes' );
 411+ }
 412+
347413 self::$newHashes[$file] = $hash;
348414 }
349415
@@ -350,68 +416,78 @@
351417 self::writeFile( 'hashes', self::$newHashes );
352418 }
353419
354 - public static function saveChanges( $changedStrings, $forbiddenKeys, $compare_messages, $base_messages, $langcode, $verbose ) {
355 - // Count the updates
 420+ /**
 421+ *
 422+ *
 423+ * @param $changedStrings Array
 424+ * @param $forbiddenKeys Array
 425+ * @param $compare_messages Array
 426+ * @param $base_messages Array
 427+ * @param $langcode String
 428+ * @param $verbose Boolean
 429+ *
 430+ * @return Integer: the amount of updated messages
 431+ */
 432+ public static function saveChanges( $changedStrings, array $forbiddenKeys, array $compare_messages, array $base_messages, $langcode, $verbose ) {
 433+ // Count the updates.
356434 $updates = 0;
357 - if(!is_array($changedStrings)) {
358 - self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file ".(__FILE__)." at line ".(__LINE__));
 435+
 436+ if( !is_array( $changedStrings ) ) {
 437+ self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file " . (__FILE__) . ' at line ' .( __LINE__ ) );
359438 return 0;
360439 }
361440
362441 $new_messages = self::readFile( $langcode );
 442+
363443 foreach ( $changedStrings as $key => $value ) {
364 - // If this message wasn't changed in English
 444+ // If this message wasn't changed in English.
365445 if ( !isset( $forbiddenKeys[$key] ) ) {
366446 $new_messages[$key] = $base_messages[$key];
367447
368 - // Output extra logmessages when needed
 448+ // Output extra logmessages when needed.
369449 if ( $verbose ) {
370450 $oldmsg = isset( $compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set';
371451 self::myLog( "Updated message {$key} from $oldmsg to '{$base_messages[$key]}'" );
372452 }
373453
374 - // Update the counter
 454+ // Update the counter.
375455 $updates++;
376456 }
377457 }
378458 self::writeFile( $langcode, $new_messages );
 459+
379460 return $updates;
380461 }
381462
382 - public static function cleanupExtensionFile( $contents ) {
383 - // We don't want PHP tags
384 - $contents = preg_replace( "/<\?php/", "", $contents );
385 - $contents = preg_replace( "/\?" . ">/", "", $contents );
386 - $results = array();
387 - // And we only want message arrays
388 - preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
389 - // But we want them all in one string
390 - if(!empty($results[0]) && is_array($results[0])) {
391 - $contents = implode( "\n\n", $results[0] );
392 - } else {
393 - $contents = "";
394 - }
395 -
396 - // And we hate the windows vs linux linebreaks
397 - $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
398 - return $contents;
399 - }
400 -
 463+ /**
 464+ *
 465+ * @param $extension String
 466+ * @param $basefile String
 467+ * @param $comparefile String
 468+ * @param $verbose Boolean
 469+ * @param $alwaysGetResult Boolean
 470+ * @param $saveResults Boolean
 471+ *
 472+ * @return Integer: the amount of updated messages
 473+ */
401474 public static function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) {
402475 // FIXME: Factor out duplicated code?
403476 $compare_messages = array();
404477 $base_messages = array();
405478
406479 $basefilecontents = self::getFileContents( $basefile );
407 - if ( $basefilecontents === false || $basefilecontents === "" ) return 0; // Failed
 480+
 481+ if ( $basefilecontents === false || $basefilecontents === '' ) {
 482+ return 0; // Failed
 483+ }
408484
409 - // Cleanup the file where needed
 485+ // Cleanup the file where needed.
410486 $basefilecontents = self::cleanupExtensionFile( $basefilecontents );
411487
412 - // Rename the arrays
 488+ // Rename the arrays.
413489 $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
414 -
415490 $basehash = md5( $basefilecontents );
 491+
416492 // If this is the remote file
417493 if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
418494 // Check if the hash has changed
@@ -425,14 +501,18 @@
426502 $base_messages = self::parsePHP( $basefilecontents, 'base_messages' );
427503
428504 $comparefilecontents = self::getFileContents( $comparefile );
429 - if ( $comparefilecontents === false || $comparefilecontents === "" ) return 0; // Failed
 505+
 506+ if ( $comparefilecontents === false || $comparefilecontents === '' ) {
 507+ return 0; // Failed
 508+ }
430509
431 - // Only get what we need
 510+ // Only get what we need.
432511 $comparefilecontents = self::cleanupExtensionFile( $comparefilecontents );
433512
434 - // Rename the array
 513+ // Rename the array.
435514 $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
436515 $comparehash = md5( $comparefilecontents );
 516+
437517 if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
438518 // Check if the remote file has changed
439519 if ( !self::checkHash( $comparefile, $comparehash ) ) {
@@ -440,16 +520,17 @@
441521 return 0;
442522 }
443523 }
444 - // Get the real array
 524+
 525+ // Get the real array.
445526 $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' );
446527
447 - // If both files are the same, they can be skipped
 528+ // If both files are the same, they can be skipped.
448529 if ( $basehash == $comparehash && !$alwaysGetResult ) {
449530 self::myLog( "Skipping {$extension} since the remote file is the same as the local file" );
450531 return 0;
451532 }
452533
453 - // Update counter
 534+ // Update counter.
454535 $updates = 0;
455536
456537 if ( !is_array( $base_messages ) ) {
@@ -468,28 +549,29 @@
469550 $compare_messages['en'] = array();
470551 }
471552
472 - // Find the changed english strings
 553+ // Find the changed english strings.
473554 $forbiddenKeys = array_diff_assoc( $base_messages['en'], $compare_messages['en'] );
474555
475 - // Do an update for each language
 556+ // Do an update for each language.
476557 foreach ( $base_messages as $language => $messages ) {
477 - if ( $language == "en" ) { // Skip english
 558+ if ( $language == 'en' ) { // Skip english.
478559 continue;
479560 }
480561
481 - // Add the already known messages to the array so we will only find new changes
 562+ // Add the already known messages to the array so we will only find new changes.
482563 $compare_messages[$language] = array_merge(
483564 $compare_messages[$language],
484 - self::readFile( $language ) );
 565+ self::readFile( $language )
 566+ );
485567
486568 if ( empty( $compare_messages[$language] ) || !is_array( $compare_messages[$language] ) ) {
487569 $compare_messages[$language] = array();
488570 }
489571
490 - // Get the array of changed strings
 572+ // Get the array of changed strings.
491573 $changedStrings = array_diff_assoc( $messages, $compare_messages[$language] );
492574
493 - // If we want to save the changes
 575+ // If we want to save the changes.
494576 if ( $saveResults === true && !empty( $changedStrings ) && is_array( $changedStrings ) ) {
495577 self::myLog( "--Checking languagecode {$language}--" );
496578 // The save them
@@ -500,7 +582,7 @@
501583 }
502584 }
503585
504 - // And log some stuff
 586+ // And log some stuff.
505587 self::myLog( "Updated " . $updates . " messages for the '{$extension}' extension" );
506588
507589 self::saveHash( $basefile, $basehash );
@@ -510,6 +592,11 @@
511593 return $updates;
512594 }
513595
 596+ /**
 597+ * Logs a message.
 598+ *
 599+ * @param $log String
 600+ */
514601 public static function myLog( $log ) {
515602 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
516603 wfDebug( $log . "\n" );

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r70206Documentation and style improvementsjeroendedauw20:14, 30 July 2010

Status & tagging log