r51005 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r51004‎ | r51005 | r51006 >
Date:10:25, 26 May 2009
Author:thedevilonline
Status:deferred
Tags:
Comment:
Cleaned up some code and added some error prevention
Modified paths:
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php (modified) (history)

Diff [purge]

Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php
@@ -123,6 +123,10 @@
124124 $changedCount = 0;
125125
126126 // For each language
 127+ if(!is_array($files)) {
 128+ self::myLog("CRITICAL ERROR: \$files is not an array in file ".(__FILE__)." at line ".(__LINE__));
 129+ return 0;
 130+ }
127131 sort($files);
128132 foreach ( $files as $file ) {
129133 $svnfile = $svndir . "/" . $file;
@@ -164,6 +168,7 @@
165169 }
166170
167171 public static function getFileContents( $basefile ) {
 172+ $basefilecontents = "";
168173 // use cURL to get the SVN contents
169174 if ( preg_match( "/^http/", $basefile ) ) {
170175 $basefilecontents = Http::get( $basefile );
@@ -193,11 +198,8 @@
194199 $langcode = strtolower( $m[1] );
195200
196201 $basefilecontents = self::getFileContents( $basefile );
197 - if ( $basefilecontents === false ) return array(); // Failed
 202+ if ( $basefilecontents === false || $basefilecontents === "" ) return array(); // Failed
198203
199 - $basehash = "";
200 - $comparehash = "";
201 -
202204 // Only get the part we need
203205 $basefilecontents = self::cleanupFile( $basefilecontents );
204206
@@ -217,7 +219,7 @@
218220 eval( $basefilecontents );
219221
220222 $comparefilecontents = self::getFileContents( $comparefile );
221 - if ( $comparefilecontents === false ) return array(); // Failed
 223+ if ( $comparefilecontents === false || $comparefilecontents === "" ) return array(); // Failed
222224
223225 // only get the stuff we need
224226 $comparefilecontents = self::cleanupFile( $comparefilecontents );
@@ -254,11 +256,13 @@
255257 $changedStrings = array_diff_assoc( $base_messages, $compare_messages );
256258
257259 // If we want to save the differences
258 - if ( $saveResults ) {
 260+ if ( $saveResults === true && !empty($changedStrings)) {
259261 self::myLog( "--Checking languagecode {$langcode}--" );
260262 // The save them
261263 $updates = self::saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode );
262264 self::myLog( "{$updates} messages updated for {$langcode}." );
 265+ } elseif($saveResult === true) {
 266+ self::myLog( "--{$langcode} hasn't changed--" );
263267 }
264268
265269 return $changedStrings;
@@ -287,6 +291,11 @@
288292
289293 // Count the updates
290294 $updates = 0;
 295+ if(!is_array($changedStrings)) {
 296+ self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file ".(__FILE__)." at line ".(__LINE__));
 297+ return 0;
 298+ }
 299+
291300 foreach ( $changedStrings as $key => $value ) {
292301 // If this message wasn't changed in english
293302 if ( !array_key_exists( $key , $forbiddenKeys ) ) {
@@ -326,7 +335,11 @@
327336 // And we only want message arrays
328337 preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
329338 // But we want them all in one string
330 - $contents = implode( "\n\n", $results[0] );
 339+ if(!empty($results[0]) && is_array($results[0])) {
 340+ $contents = implode( "\n\n", $results[0] );
 341+ } else {
 342+ $contents = "";
 343+ }
331344
332345 // And we hate the windows vs linux linebreaks
333346 $contents = preg_replace( "/\\\r/", "", $contents );
@@ -340,11 +353,8 @@
341354 $base_messages = array();
342355
343356 $basefilecontents = self::getFileContents( $basefile );
344 - if ( $basefilecontents === false ) return array(); // Failed
 357+ if ( $basefilecontents === false || $basefilecontents === "" ) return array(); // Failed
345358
346 - $basehash = "";
347 - $comparehash = "";
348 -
349359 // Cleanup the file where needed
350360 $basefilecontents = self::cleanupExtensionFile( $basefilecontents );
351361
@@ -365,7 +375,7 @@
366376 eval( $basefilecontents );
367377
368378 $comparefilecontents = self::getFileContents( $comparefile );
369 - if ( $comparefilecontents === false ) return array(); // Failed
 379+ if ( $comparefilecontents === false || $comparefilecontents === "" ) return array(); // Failed
370380
371381 // Only get what we need
372382 $comparefilecontents = self::cleanupExtensionFile( $comparefilecontents );
@@ -392,10 +402,16 @@
393403 // Update counter
394404 $updates = 0;
395405
 406+ if(!is_array($base_messages))
 407+ $base_messages = array();
 408+
396409 if ( empty( $base_messages['en'] ) ) {
397410 $base_messages['en'] = array();
398411 }
399412
 413+ if(!is_array($compare_messages))
 414+ $compare_messages = array();
 415+
400416 if ( empty( $compare_messages['en'] ) ) {
401417 $compare_messages['en'] = array();
402418 }

Status & tagging log