http://www.mediawiki.org/wiki/Special:Code/MediaWiki/70210

Revision: 70210
Author:   jeroendedauw
Date:     2010-07-30 22:03:12 +0000 (Fri, 30 Jul 2010)

Log Message:
-----------
Follow up to r70206

Modified Paths:
--------------
    trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php

Modified: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php
===================================================================
--- trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php    
2010-07-30 20:46:59 UTC (rev 70209)
+++ trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php    
2010-07-30 22:03:12 UTC (rev 70210)
@@ -1,4 +1,10 @@
 <?php
+
+/**
+ * Class for localization updates.
+ * 
+ * TODO: refactor code to remove duplication
+ */
 class LocalisationUpdate {
        
        private static $newHashes = null;
@@ -212,10 +218,45 @@
                // Return the cleaned up file.
                return $contents;
        }
+       
+       /**
+        * Removes all unneeded content from a file and returns it.
+        * 
+        * FIXME: duplicated cleanupFile code 
+        * 
+        * @param $contents String
+        * 
+        * @return string
+        */
+       public static function cleanupExtensionFile( $contents ) {
+               // We don't want PHP tags.
+               $contents = preg_replace( "/<\?php/", "", $contents );
+               $contents = preg_replace( "/\?" . ">/", "", $contents );
+               
+               $results = array();
+               
+               // And we only want message arrays.
+               preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, 
$results );
+               
+               // But we want them all in one string.
+               if( !empty( $results[0] ) && is_array( $results[0] ) ) {
+                       $contents = implode( "\n\n", $results[0] );
+               } else {
+                       $contents = '';
+               }
 
+               // And we hate the windows vs linux linebreaks.
+               $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
+               
+               return $contents;
+       }       
+
        /**
+        * Returns the contents of a file or false on failiure.
         * 
         * @param $basefile String
+        * 
+        * @return string or false
         */
        public static function getFileContents( $basefile ) {
                global $wgLocalisationUpdateRetryAttempts;
@@ -249,24 +290,39 @@
                return $basefilecontents;
        }
 
-       public static function compareFiles( $basefile, $comparefile, $verbose, 
$forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
+       /**
+        * Returns an array containing the differences between the files.
+        * 
+        * @param $basefile String
+        * @param $comparefile String
+        * @param $verbose Boolean
+        * @param $forbiddenKeys Array
+        * @param $alwaysGetResult Boolean
+        * @param $saveResults Boolean
+        * 
+        * @return array
+        */
+       public static function compareFiles( $basefile, $comparefile, $verbose, 
array $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) 
{
                $compare_messages = array();
                $base_messages = array();
 
-               // Get the languagecode
+               // Get the languagecode.
                $langcode = Language::getCodeFromFileName( $basefile, 
'Messages' );
 
                $basefilecontents = self::getFileContents( $basefile );
-               if ( $basefilecontents === false || $basefilecontents === "" ) 
return array(); // Failed
+               
+               if ( $basefilecontents === false || $basefilecontents === '' ) {
+                       return array(); // Failed
+               }
 
-               // Only get the part we need
+               // Only get the part we need.
                $basefilecontents = self::cleanupFile( $basefilecontents );
 
-               // Change the variable name
+               // Change the variable name.
                $basefilecontents = preg_replace( "/\\\$messages/", 
"\$base_messages", $basefilecontents );
-
                $basehash = md5( $basefilecontents );
-               // Check if the file has changed since our last update
+               
+               // Check if the file has changed since our last update.
                if ( !$alwaysGetResult ) {
                        if ( !self::checkHash( $basefile, $basehash ) ) {
                                self::myLog( "Skipping {$langcode} since the 
remote file hasn't changed since our last update" );
@@ -274,52 +330,57 @@
                        }
                }
 
-               // Get the array with messages
+               // Get the array with messages.
                $base_messages = self::parsePHP( $basefilecontents, 
'base_messages' );
 
                $comparefilecontents = self::getFileContents( $comparefile );
-               if ( $comparefilecontents === false || $comparefilecontents === 
"" ) return array(); // Failed
+               
+               if ( $comparefilecontents === false || $comparefilecontents === 
'' ) {
+                       return array(); // Failed
+               }
 
-               // only get the stuff we need
+               // Only get the stuff we need.
                $comparefilecontents = self::cleanupFile( $comparefilecontents 
);
 
-               // rename the array
+               // Rename the array.
                $comparefilecontents = preg_replace( "/\\\$messages/", 
"\$compare_messages", $comparefilecontents );
-
                $comparehash = md5( $comparefilecontents );
-               // If this is the remote file check if the file has changed 
since our last update
+               
+               // If this is the remote file check if the file has changed 
since our last update.
                if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult 
) {
                        if ( !self::checkHash( $comparefile, $comparehash ) ) {
                                self::myLog( "Skipping {$langcode} since the 
remote file has not changed since our last update" );
                                return array();
                        }
                }
-               // Get the array
+               
+               // Get the array.
                $compare_messages = self::parsePHP( $comparefilecontents, 
'compare_messages' );
 
-               // if the localfile and the remote file are the same, skip them!
+               // If the localfile and the remote file are the same, skip them!
                if ( $basehash == $comparehash && !$alwaysGetResult ) {
                        self::myLog( "Skipping {$langcode} since the remote 
file is the same as the local file" );
                        return array();
                }
 
-               // Add the messages we got with our previous update(s) to the 
local array (as we already got these as well)
-               $compare_messages = array_merge( $compare_messages,
-                       self::readFile( $langcode ) );
+               // Add the messages we got with our previous update(s) to the 
local array (as we already got these as well).
+               $compare_messages = array_merge(
+                       $compare_messages,
+                       self::readFile( $langcode )
+               );
 
-               // Compare the remote and local message arrays
+               // Compare the remote and local message arrays.
                $changedStrings = array_diff_assoc( $base_messages, 
$compare_messages );
 
-               // If we want to save the differences
-               if ( $saveResults && !empty($changedStrings) && 
is_array($changedStrings)) {
+               // If we want to save the differences.
+               if ( $saveResults && !empty( $changedStrings ) && is_array( 
$changedStrings ) ) {
                        self::myLog( "--Checking languagecode {$langcode}--" );
-                       // The save them
+                       // Save the differences.
                        $updates = self::saveChanges( $changedStrings, 
$forbiddenKeys, $compare_messages, $base_messages, $langcode, $verbose );
                        self::myLog( "{$updates} messages updated for 
{$langcode}." );
                } elseif ( $saveResults ) {
                        self::myLog( "--{$langcode} hasn't changed--" );
                }
-
                
                self::saveHash( $basefile, $basehash );
                
@@ -329,10 +390,13 @@
        }
 
        /**
-        * Checks whether a messages file has a certain hash
+        * Checks whether a messages file has a certain hash.
+        * 
         * TODO: Swap return values, this is insane
+        * 
         * @param $file string Filename
         * @param $hash string Hash
+        * 
         * @return bool True if $file does NOT have hash $hash, false if it does
         */
        public static function checkHash( $file, $hash ) {
@@ -341,8 +405,10 @@
        }
        
        public static function saveHash( $file, $hash ) {
-               if ( is_null( self::$newHashes ) )
+               if ( is_null( self::$newHashes ) ) {
                        self::$newHashes = self::readFile( 'hashes' );
+               }
+                       
                self::$newHashes[$file] = $hash;
        }
        
@@ -350,68 +416,78 @@
                self::writeFile( 'hashes', self::$newHashes );
        }
 
-       public static function saveChanges( $changedStrings, $forbiddenKeys, 
$compare_messages, $base_messages, $langcode, $verbose ) {
-               // Count the updates
+       /**
+        * 
+        * 
+        * @param $changedStrings Array
+        * @param $forbiddenKeys Array
+        * @param $compare_messages Array
+        * @param $base_messages Array
+        * @param $langcode String
+        * @param $verbose Boolean
+        * 
+        * @return Integer: the amount of updated messages
+        */
+       public static function saveChanges( $changedStrings, array 
$forbiddenKeys, array $compare_messages, array $base_messages, $langcode, 
$verbose ) {
+               // Count the updates.
                $updates = 0;
-               if(!is_array($changedStrings)) {
-                       self::myLog("CRITICAL ERROR: \$changedStrings is not an 
array in file ".(__FILE__)." at line ".(__LINE__));
+               
+               if( !is_array( $changedStrings ) ) {
+                       self::myLog("CRITICAL ERROR: \$changedStrings is not an 
array in file " . (__FILE__) . ' at line ' .( __LINE__ ) );
                        return 0;
                }
 
                $new_messages = self::readFile( $langcode );
+               
                foreach ( $changedStrings as $key => $value ) {
-                       // If this message wasn't changed in English
+                       // If this message wasn't changed in English.
                        if ( !isset( $forbiddenKeys[$key] ) ) {
                                $new_messages[$key] = $base_messages[$key];
                                
-                               // Output extra logmessages when needed
+                               // Output extra logmessages when needed.
                                if ( $verbose ) {
                                        $oldmsg = isset( 
$compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set';
                                        self::myLog( "Updated message {$key} 
from $oldmsg to '{$base_messages[$key]}'" );
                                }
 
-                               // Update the counter
+                               // Update the counter.
                                $updates++;
                        }
                }
                self::writeFile( $langcode, $new_messages );
+               
                return $updates;
        }
 
-       public static function cleanupExtensionFile( $contents ) {
-               // We don't want PHP tags
-               $contents = preg_replace( "/<\?php/", "", $contents );
-               $contents = preg_replace( "/\?" . ">/", "", $contents );
-               $results = array();
-               // And we only want message arrays
-               preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, 
$results );
-               // But we want them all in one string
-               if(!empty($results[0]) && is_array($results[0])) {
-                       $contents = implode( "\n\n", $results[0] );
-               } else {
-                       $contents = "";
-               }
-
-               // And we hate the windows vs linux linebreaks
-               $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents );
-               return $contents;
-       }
-
+       /**
+        * 
+        * @param $extension String
+        * @param $basefile String
+        * @param $comparefile String
+        * @param $verbose Boolean
+        * @param $alwaysGetResult Boolean
+        * @param $saveResults Boolean
+        * 
+        * @return Integer: the amount of updated messages
+        */
        public static function compareExtensionFiles( $extension, $basefile, 
$comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) {
                // FIXME: Factor out duplicated code?
                $compare_messages = array();
                $base_messages = array();
 
                $basefilecontents = self::getFileContents( $basefile );
-               if ( $basefilecontents === false || $basefilecontents === "" ) 
return 0; // Failed
+               
+               if ( $basefilecontents === false || $basefilecontents === '' ) {
+                       return 0; // Failed
+               }
 
-               // Cleanup the file where needed
+               // Cleanup the file where needed.
                $basefilecontents = self::cleanupExtensionFile( 
$basefilecontents );
 
-               // Rename the arrays
+               // Rename the arrays.
                $basefilecontents = preg_replace( "/\\\$messages/", 
"\$base_messages", $basefilecontents );
-
                $basehash = md5( $basefilecontents );
+               
                // If this is the remote file
                if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
                        // Check if the hash has changed
@@ -425,14 +501,18 @@
                $base_messages = self::parsePHP( $basefilecontents, 
'base_messages' );
                
                $comparefilecontents = self::getFileContents( $comparefile );
-               if ( $comparefilecontents === false || $comparefilecontents === 
"" ) return 0; // Failed
+               
+               if ( $comparefilecontents === false || $comparefilecontents === 
'' ) {
+                       return 0; // Failed     
+               }
 
-               // Only get what we need
+               // Only get what we need.
                $comparefilecontents = self::cleanupExtensionFile( 
$comparefilecontents );
 
-               // Rename the array
+               // Rename the array.
                $comparefilecontents = preg_replace( "/\\\$messages/", 
"\$compare_messages", $comparefilecontents );
                $comparehash = md5( $comparefilecontents );
+               
                if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult 
) {
                        // Check if the remote file has changed
                        if ( !self::checkHash( $comparefile, $comparehash ) ) {
@@ -440,16 +520,17 @@
                                return 0;
                        }
                }
-               // Get the real array
+               
+               // Get the real array.
                $compare_messages = self::parsePHP( $comparefilecontents, 
'compare_messages' );
 
-               // If both files are the same, they can be skipped
+               // If both files are the same, they can be skipped.
                if ( $basehash == $comparehash && !$alwaysGetResult ) {
                        self::myLog( "Skipping {$extension} since the remote 
file is the same as the local file" );
                        return 0;
                }
 
-               // Update counter
+               // Update counter.
                $updates = 0;
 
                if ( !is_array( $base_messages ) ) {
@@ -468,28 +549,29 @@
                        $compare_messages['en'] = array();
                }
 
-               // Find the changed english strings
+               // Find the changed english strings.
                $forbiddenKeys = array_diff_assoc( $base_messages['en'], 
$compare_messages['en'] );
 
-               // Do an update for each language
+               // Do an update for each language.
                foreach ( $base_messages as $language => $messages ) {
-                       if ( $language == "en" ) { // Skip english
+                       if ( $language == 'en' ) { // Skip english.
                                continue;
                        }
 
-                       // Add the already known messages to the array so we 
will only find new changes
+                       // Add the already known messages to the array so we 
will only find new changes.
                        $compare_messages[$language] = array_merge(
                                $compare_messages[$language],
-                               self::readFile( $language ) );
+                               self::readFile( $language )
+                       );
 
                        if ( empty( $compare_messages[$language] ) || 
!is_array( $compare_messages[$language] ) ) {
                                $compare_messages[$language] = array();
                        }
 
-                       // Get the array of changed strings
+                       // Get the array of changed strings.
                        $changedStrings = array_diff_assoc( $messages, 
$compare_messages[$language] );
 
-                       // If we want to save the changes
+                       // If we want to save the changes.
                        if ( $saveResults === true && !empty( $changedStrings ) 
&& is_array( $changedStrings ) ) {
                                self::myLog( "--Checking languagecode 
{$language}--" );
                                // The save them
@@ -500,7 +582,7 @@
                        }
                } 
 
-               // And log some stuff
+               // And log some stuff.
                self::myLog( "Updated " . $updates . " messages for the 
'{$extension}' extension" );
 
                self::saveHash( $basefile, $basehash );
@@ -510,6 +592,11 @@
                return $updates;
        }
        
+       /**
+        * Logs a message.
+        * 
+        * @param $log String
+        */
        public static function myLog( $log ) {
                if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', 
$_SERVER ) ) {
                        wfDebug( $log . "\n" );



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to