Revision: 50818 Author: siebrand Date: 2009-05-20 09:30:12 +0000 (Wed, 20 May 2009)
Log Message: ----------- Tweaks for LocalisationUpdate Modified Paths: -------------- trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php trunk/extensions/LocalisationUpdate/LocalisationUpdate.php trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php trunk/extensions/LocalisationUpdate/README_FIRST.txt Modified: trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php =================================================================== --- trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php 2009-05-20 09:13:01 UTC (rev 50817) +++ trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php 2009-05-20 09:30:12 UTC (rev 50818) @@ -1,9 +1,18 @@ <?php +/** + * Internationalisation file for LocalisationUpdate extension. + * + * @file + * @ingroup Extensions + */ $messages = array(); +/** English + * @author Tom Maaswinkel + */ $messages['en'] = array( - 'localisationupdate-desc' => 'Extension to keep the localized messages as up to date as possible', + 'localisationupdate-desc' => 'Keeps the localised messages as up to date as possible', $messages['yue'] = array( 'localisationupdate-desc' => '一個擴展將本地化嘅信息保持最新', Modified: trunk/extensions/LocalisationUpdate/LocalisationUpdate.php =================================================================== --- trunk/extensions/LocalisationUpdate/LocalisationUpdate.php 2009-05-20 09:13:01 UTC (rev 50817) +++ trunk/extensions/LocalisationUpdate/LocalisationUpdate.php 2009-05-20 09:30:12 UTC (rev 50818) @@ -5,209 +5,209 @@ Mediawiki core */ -//Info about me! +// Info about me! $wgExtensionCredits['other'][] = array( 'path' => __FILE__, 'name' => 'LocalisationUpdate', 'author' => 'Tom Maaswinkel', 'version' => '0.1', - 'description' => 'Extension to keep the localized messages as up to date as possible', + 'description' => 'Keeps the localised messages as up to date as possible', 'descriptionmsg' => 'localisationupdate-desc', ); $wgExtensionMessagesFiles['LocalisationUpdate'] = dirname( __FILE__ ) . '/LocalisationUpdate.i18n.php'; -//Use the right hook +// Use the right hook $wgHooks['MessageNotInMwNs'][] = "FindUpdatedMessage"; -//DB Search funtion -function FindUpdatedMessage(&$message,$lckey,$langcode,$isFullKey) { - //Define a cache +// DB Search funtion +function FindUpdatedMessage( &$message, $lckey, $langcode, $isFullKey ) { + // Define a cache static $cache = array(); $db = wfGetDB ( DB_SLAVE ); // If the key also contains the language code remove the language code from the key - if($isFullKey) { - $lckey = preg_replace("/\/".$langcode."/","",$lckey); + if ( $isFullKey ) { + $lckey = preg_replace( "/\/" . $langcode . "/", "", $lckey ); } - //If message is in the cache, don't get an update! - if(array_key_exists($lckey."/".$langcode,$cache)) { - $message = $cache[$lckey."/".$langcode]; + // If message is in the cache, don't get an update! + if ( array_key_exists( $lckey . "/" . $langcode, $cache ) ) { + $message = $cache[$lckey . "/" . $langcode]; return true; } - //Get the message from the database - $query = "select value from localisation where identifier = '".$db->strencode($lckey)."' and language = '".$db->strencode($langcode)."'"; - $result = $db->query($query); // Check if the database has any updated message - if($db->numRows($result) == 0) { // If no results found, exit here + // Get the message from the database + $query = "SELECT value FROM localisation WHERE identifier = '" . $db->strencode( $lckey ) . "' AND language = '" . $db->strencode( $langcode ) . "'"; + $result = $db->query( $query ); // Check if the database has any updated message + if ( $db->numRows( $result ) == 0 ) { // If no results found, exit here return true; } - $row = $db->fetchObject($result); // Get the result + $row = $db->fetchObject( $result ); // Get the result $message = $row->value; // And change the message variable - $cache[$lckey."/".$langcode] = $message; //Update the cache + $cache[$lckey . "/" . $langcode] = $message; // Update the cache return true; } -//Called from the cronjob to fetch new messages from SVN -function updateMessages($verbose = false) { - //Need this later +// Called from the cronjob to fetch new messages from SVN +function updateMessages( $verbose = false ) { + // Need this later global $wgExtensionMessagesFiles; - //Prevent the script from timing out - set_time_limit(0); - ini_set("max_execution_time",0); + // Prevent the script from timing out + set_time_limit( 0 ); + ini_set( "max_execution_time", 0 ); - //Update all MW core messages - $result = updateMediawikiMessages($verbose); + // Update all MW core messages + $result = updateMediawikiMessages( $verbose ); - //Update all Extension messages - foreach($wgExtensionMessagesFiles as $extension => $locFile) { - $result += updateExtensionMessages($locFile,$extension,$verbose); + // Update all Extension messages + foreach ( $wgExtensionMessagesFiles as $extension => $locFile ) { + $result += updateExtensionMessages( $locFile, $extension, $verbose ); } - //And output the result! - myLog("Updated {$result} messages in total"); - myLog("Done"); + // And output the result! + myLog( "Updated {$result} messages in total" ); + myLog( "Done" ); return true; } -//Update Extension Messages -function updateExtensionMessages($file,$extension,$verbose) { +// Update Extension Messages +function updateExtensionMessages( $file, $extension, $verbose ) { global $IP; - //Define the wikimedia SVN path + // Define the wikimedia SVN path $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/'; - //Find the right SVN folder - $svnFolder = SpecialVersion::getSvnRevision(dirname($file),false,false,true); + // Find the right SVN folder + $svnFolder = SpecialVersion::getSvnRevision( dirname( $file ), false, false, true ); - //Create a full path - if(!empty($IP)) { - $localfile = $IP."/".$file; + // Create a full path + if ( !empty( $IP ) ) { + $localfile = $IP . "/" . $file; } - //Get the full SVN directory path - $svndir = "http://".$wmSvnPath.$svnFolder; + // Get the full SVN directory path + $svndir = "http://" . $wmSvnPath . $svnFolder; - //Compare the 2 files - $result = compareExtensionFiles($extension,$svndir."/".basename($file),$file,$verbose,false,true); + // Compare the 2 files + $result = compareExtensionFiles( $extension, $svndir . "/" . basename( $file ), $file, $verbose, false, true ); return $result; } -//Update the Mediawiki Core Messages -function updateMediawikiMessages($verbose) { +// Update the Mediawiki Core Messages +function updateMediawikiMessages( $verbose ) { global $IP; - //Define the wikimedia SVN path + // Define the wikimedia SVN path $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/'; - //Create an array which will later contain all the files that we want to try to update + // Create an array which will later contain all the files that we want to try to update $files = array(); - //The directory which contains the files + // The directory which contains the files $dirname = "languages/messages"; - //Get the full path to the directory - if(!empty($IP)) { - $dirname = $IP."/".$dirname; + // Get the full path to the directory + if ( !empty( $IP ) ) { + $dirname = $IP . "/" . $dirname; } - //Open the directory + // Open the directory $dir = opendir( $dirname ); - while( false !== ( $file = readdir( $dir ) ) ) { + while ( false !== ( $file = readdir( $dir ) ) ) { $m = array(); - //And save all the filenames of files containing messages - if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) { - if($m[1] != 'En') { //Except for the english one + // And save all the filenames of files containing messages + if ( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) { + if ( $m[1] != 'En' ) { // Except for the english one $files[] = $file; } } } closedir( $dir ); - //Get the SVN folder used for the checkout - $svnFolder = SpecialVersion::getSvnRevision($dirname,false,false,true); + // Get the SVN folder used for the checkout + $svnFolder = SpecialVersion::getSvnRevision( $dirname, false, false, true ); - //Do not update if not from SVN - if(empty($svnFolder)) { - myLog("Can't update localisation as the files are not retrieved from SVN"); + // Do not update if not from SVN + if ( empty( $svnFolder ) ) { + myLog( 'Cannot update localisation as the files are not retrieved from SVN' ); return 0; } - //Get the full SVN Path - $svndir = "http://".$wmSvnPath.$svnFolder; + // Get the full SVN Path + $svndir = "http://" . $wmSvnPath . $svnFolder; - //Find the changed English strings (as these messages won't be updated in ANY language) - $changedEnglishStrings = compareFiles($dirname."/MessagesEn.php",$svndir."/MessagesEn.php",$verbose); + // Find the changed English strings (as these messages won't be updated in ANY language) + $changedEnglishStrings = compareFiles( $dirname . "/MessagesEn.php", $svndir . "/MessagesEn.php", $verbose ); - //Count the changes + // Count the changes $changedCount = 0; - //For each language - foreach($files as $file) { - $svnfile = $svndir."/".$file; - $localfile = $dirname."/".$file; + // For each language + foreach ( $files as $file ) { + $svnfile = $svndir . "/" . $file; + $localfile = $dirname . "/" . $file; - //Compare the files - $result = compareFiles($svnfile,$localfile,$verbose,$changedEnglishStrings,false,true); + // Compare the files + $result = compareFiles( $svnfile, $localfile, $verbose, $changedEnglishStrings, false, true ); - //And update the change counter - $changedCount += count($result); + // And update the change counter + $changedCount += count( $result ); } - //Log some nice info - myLog("{$changedCount} Mediawiki messages are updated"); + // Log some nice info + myLog( "{$changedCount} Mediawiki messages are updated" ); return $changedCount; } -//Remove all unneeded content -function cleanupFile($contents) { - //We don't need any PHP tags - $contents = preg_replace("/<\\?php/","",$contents); - $contents = preg_replace("/\?>/","",$contents); +// Remove all unneeded content +function cleanupFile( $contents ) { + // We don't need any PHP tags + $contents = preg_replace( "/<\\?php/", "", $contents ); + $contents = preg_replace( "/\?>/", "", $contents ); $results = array(); - //And we only want the messages array - preg_match("/\\\$messages(.*\s)*?\);/",$contents,$results); + // And we only want the messages array + preg_match( "/\\\$messages(.*\s)*?\);/", $contents, $results ); - //If there is any! - if(!empty($results[0])) { + // If there is any! + if ( !empty( $results[0] ) ) { $contents = $results[0]; } else { $contents = ""; } - //Windows vs Unix always stinks when comparing files - $contents = preg_replace("/\\\r/","",$contents); + // Windows vs Unix always stinks when comparing files + $contents = preg_replace( "/\\\r/", "", $contents ); - //return the cleaned up file + // return the cleaned up file return $contents; } -function compareFiles($basefile,$comparefile,$verbose,$forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false) { - //We need to write to the DB later +function compareFiles( $basefile, $comparefile, $verbose, $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) { + // We need to write to the DB later $db = wfGetDB ( DB_MASTER ); $compare_messages = array(); $base_messages = array(); - //Get the languagecode + // Get the languagecode $m = array(); preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $basefile, $m ); - $langcode = strtolower($m[1]); + $langcode = strtolower( $m[1] ); - //use cURL to get the SVN contents - if(preg_match("/^http/",$basefile)) { - $basefilecontents = Http::get($basefile); - if(empty($basefilecontents)) { - myLog("Can't get the contents of ".$basefile." (curl)"); + // use cURL to get the SVN contents + if ( preg_match( "/^http/", $basefile ) ) { + $basefilecontents = Http::get( $basefile ); + if ( empty( $basefilecontents ) ) { + myLog( "Cannot get the contents of " . $basefile . " (curl)" ); return array(); } - } else {//otherwise try file_get_contents - if(!$basefilecontents = file_get_contents($basefile)) { - myLog("Can't get the contents of ".$basefile); + } else {// otherwise try file_get_contents + if ( !$basefilecontents = file_get_contents( $basefile ) ) { + myLog( "Cannot get the contents of " . $basefile ); return array(); } } @@ -215,158 +215,151 @@ $basehash = ""; $comparehash = ""; - //Only get the part we need - $basefilecontents = cleanupFile($basefilecontents); + // Only get the part we need + $basefilecontents = cleanupFile( $basefilecontents ); - //Change the variable name - $basefilecontents = preg_replace("/\\\$messages/","\$base_messages",$basefilecontents); + // Change the variable name + $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents ); - $basehash = md5($basefilecontents); - //If this is the remote file check if the file has changed since our last update - if(preg_match("/^http/",$basefile) && !$alwaysGetResult) { - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($basefile)."' and hash = '{$basehash}'"); - if($db->numRows($result) == 0) { //If it did, save the new file hash - $db->query("delete from localisation_file_hash where file = '".$db->strencode($basefile)."'"); - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($basefile)."','{$basehash}')"); + $basehash = md5( $basefilecontents ); + // If this is the remote file check if the file has changed since our last update + if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) { + $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "' AND hash = '{$basehash}'" ); + if ( $db->numRows( $result ) == 0 ) { // If it did, save the new file hash + $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "'" ); + $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $basefile ) . "','{$basehash}')" ); } else { - myLog("Skipping {$langcode} since the remote file hasn't changed since our last update"); - myLog(""); + myLog( "Skipping {$langcode} since the remote file hasn't changed since our last update" ); return array(); } } - //Get the array with messages - eval($basefilecontents); + // Get the array with messages + eval( $basefilecontents ); - //use cURL to get the contents - if(preg_match("/^http/",$comparefile)) { - $comparefilecontents = Http::get($comparefile); - if(empty($comparefilecontents)) { - myLog("Can't get the contents of ".$comparefile." (curl)"); + // use cURL to get the contents + if ( preg_match( "/^http/", $comparefile ) ) { + $comparefilecontents = Http::get( $comparefile ); + if ( empty( $comparefilecontents ) ) { + myLog( "Cannot get the contents of " . $comparefile . " (curl)" ); return array(); } - } else { //otherwise use file_get_contents - if(!$comparefilecontents = file_get_contents($comparefile)) { - myLog("Can't get the contents of ".$comparefile); + } else { // otherwise use file_get_contents + if ( !$comparefilecontents = file_get_contents( $comparefile ) ) { + myLog( "Cannot get the contents of " . $comparefile ); return array(); } } - //only get the stuff we need - $comparefilecontents = cleanupFile($comparefilecontents); + // only get the stuff we need + $comparefilecontents = cleanupFile( $comparefilecontents ); - //rename the array - $comparefilecontents = preg_replace("/\\\$messages/","\$compare_messages",$comparefilecontents); + // 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(preg_match("/^http/",$comparefile) && !$alwaysGetResult) { - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($comparefile)."' and hash = '{$comparehash}'"); - if($db->numRows($result) == 0) {//If it did, save the new file hash - $db->query("delete from localisation_file_hash where file = '".$db->strencode($comparefile)."'"); - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($comparefile)."','{$comparehash}')"); + $comparehash = md5( $comparefilecontents ); + // If this is the remote file check if the file has changed since our last update + if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) { + $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "' and hash = '{$comparehash}'" ); + if ( $db->numRows( $result ) == 0 ) {// If it did, save the new file hash + $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "'" ); + $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $comparefile ) . "','{$comparehash}')" ); } else { - myLog("Skipping {$langcode} since the remote file hasn't changed since our last update"); - myLog(""); + myLog( "Skipping {$langcode} since the remote file has not changed since our last update" ); return array(); } } - //Get the array - eval($comparefilecontents); + // Get the array + eval( $comparefilecontents ); - - //if the localfile and the remote file are the same, skip them! - if($basehash == $comparehash && !$alwaysGetResult) { - myLog("Skipping {$langcode} since the remote file is the same as the local file"); - myLog(""); + // if the localfile and the remote file are the same, skip them! + if ( $basehash == $comparehash && !$alwaysGetResult ) { + 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) - $result = $db->query("select identifier,value from localisation where language = '".$db->strencode($langcode)."'"); - while($resObj = $db->fetchObject($result)) { + // Add the messages we got with our previous update(s) to the local array (as we already got these as well) + $result = $db->query( "SELECT identifier,value FROM localisation WHERE language = '" . $db->strencode( $langcode ) . "'" ); + while ( $resObj = $db->fetchObject( $result ) ) { $compare_messages[$resObj->identifier] = $resObj->value; } - //Compare the remote and local message arrays - $changedStrings = array_diff_assoc($base_messages,$compare_messages); + // Compare the remote and local message arrays + $changedStrings = array_diff_assoc( $base_messages, $compare_messages ); - //If we want to save the differences - if($saveResults) { - myLog("--Checking languagecode {$langcode}--"); - //The save them - $updates = saveChanges($changedStrings,$forbiddenKeys,$base_messages,$langcode); - myLog("{$updates} messages updated for {$langcode}."); - - myLog(""); + // If we want to save the differences + if ( $saveResults ) { + myLog( "--Checking languagecode {$langcode}--" ); + // The save them + $updates = saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode ); + myLog( "{$updates} messages updated for {$langcode}." ); } return $changedStrings; } - -function saveChanges($changedStrings,$forbiddenKeys,$base_messages,$langcode) { +function saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode ) { global $verbose; - //Gonna write to the DB again + // Gonna write to the DB again $db = wfGetDB ( DB_MASTER ); - //Count the updates + // Count the updates $updates = 0; - foreach($changedStrings as $key => $value) { - //If this message wasn't changed in english - if(!array_key_exists($key , $forbiddenKeys )){ - //See if we can update the database - $db->query("update localisation set value = '".$db->strencode($base_messages[$key])."' where language = '".$db->strencode($langcode)."' and identifier like '".$db->strencode($key)."'"); - if($db->affectedRows() == 0) { //Otherwise do a new insert - $db->query("insert into localisation (value, language, identifier) values ('".$db->strencode($base_messages[$key])."', '".$db->strencode($langcode)."','".$db->strencode($key)."')"); - if($db->affectedRows() == 0) { - throw new MWException( "An error has occured while inserting a new message to the database to the database!" ); + foreach ( $changedStrings as $key => $value ) { + // If this message wasn't changed in english + if ( !array_key_exists( $key , $forbiddenKeys ) ) { + // See if we can update the database + $db->query( "UPDATE localisation SET value = '" . $db->strencode( $base_messages[$key] ) . "' WHERE language = '" . $db->strencode( $langcode ) . "' and identifier like '" . $db->strencode( $key ) . "'" ); + if ( $db->affectedRows() == 0 ) { // Otherwise do a new insert + $db->query( "INSERT INTO localisation (value, language, identifier) VALUES ('" . $db->strencode( $base_messages[$key] ) . "', '" . $db->strencode( $langcode ) . "','" . $db->strencode( $key ) . "')" ); + if ( $db->affectedRows() == 0 ) { + throw new MWException( "An error has occured while inserting a new message into the database!" ); } } - //Output extra logmessages when needed - if($verbose) { - myLog("Updated message {$key} from {$compare_messages[$key]} to {$base_messages[$key]}"); + // Output extra logmessages when needed + if ( $verbose ) { + myLog( "Updated message {$key} from {$compare_messages[$key]} to {$base_messages[$key]}" ); } - //Update the counter + // Update the counter $updates++; } } return $updates; } -function cleanupExtensionFile($contents) { - //We don't want PHP tags - $contents = preg_replace("/<\?php/","",$contents); - $contents = preg_replace("/\?>/","",$contents); +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 - $contents = implode("\n\n",$results[0]); + // And we only want message arrays + preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results ); + // But we want them all in one string + $contents = implode( "\n\n", $results[0] ); - //And we hate the windows vs linux linebreaks - $contents = preg_replace("/\\\r/","",$contents); + // And we hate the windows vs linux linebreaks + $contents = preg_replace( "/\\\r/", "", $contents ); return $contents; } -function compareExtensionFiles($extension,$basefile,$comparefile,$verbose, $alwaysGetResult = true, $saveResults = false) { - //Let's mess with the database again +function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) { + // Let's mess with the database again $db = wfGetDB ( DB_MASTER ); $compare_messages = array(); $base_messages = array(); - if(preg_match("/^http/",$basefile)) { - $basefilecontents = Http::get($basefile); - if(empty($basefilecontents)) { - myLog("Can't get the contents of ".$basefile." (curl)"); + if ( preg_match( "/^http/", $basefile ) ) { + $basefilecontents = Http::get( $basefile ); + if ( empty( $basefilecontents ) ) { + myLog( "Cannot get the contents of " . $basefile . " (curl)" ); return 0; } - } else { //or otherwise file _get_contents - if(!$basefilecontents = file_get_contents($basefile)) { - myLog("Can't get the contents of ".$basefile); + } else { // or otherwise file _get_contents + if ( !$basefilecontents = file_get_contents( $basefile ) ) { + myLog( "Cannot get the contents of " . $basefile ); return 0; } } @@ -374,116 +367,112 @@ $basehash = ""; $comparehash = ""; - //Cleanup the file where needed - $basefilecontents = cleanupExtensionFile($basefilecontents); + // Cleanup the file where needed + $basefilecontents = cleanupExtensionFile( $basefilecontents ); - //Rename the arrays - $basefilecontents = preg_replace("/\\\$messages/","\$base_messages",$basefilecontents); + // 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 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($basefile)."' and hash = '{$basehash}'"); - if($db->numRows($result) == 0) { - $db->query("delete from localisation_file_hash where file = '".$db->strencode($basefile)."'"); - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($basefile)."','{$basehash}')"); + $basehash = md5( $basefilecontents ); + // If this is the remote file + if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) { + // Check if the hash has changed + $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "' AND hash = '{$basehash}'" ); + if ( $db->numRows( $result ) == 0 ) { + $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "'" ); + $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $basefile ) . "','{$basehash}')" ); } else { - myLog("Skipping {$extension} since the remote file hasn't changed since our last update"); - myLog(""); + myLog( "Skipping {$extension} since the remote file has not changed since our last update" ); return 0; } } - //And get the real contents - eval($basefilecontents); + // And get the real contents + eval( $basefilecontents ); - //Use cURL when available - if(preg_match("/^http/",$comparefile)) { - $comparefilecontents = Http::get($comparefile); - if(empty($comparefilecontents)) { - myLog("Can't get the contents of ".$comparefile." (curl)"); + // Use cURL when available + if ( preg_match( "/^http/", $comparefile ) ) { + $comparefilecontents = Http::get( $comparefile ); + if ( empty( $comparefilecontents ) ) { + myLog( "Cannot get the contents of " . $comparefile . " (curl)" ); return 0; } - } else { //Otherwise use file_get_contents - if(!$comparefilecontents = file_get_contents($comparefile)) { - myLog("Can't get the contents of ".$comparefile); + } else { // Otherwise use file_get_contents + if ( !$comparefilecontents = file_get_contents( $comparefile ) ) { + myLog( "Cannot get the contents of " . $comparefile ); return 0; } } - //Only get what we need - $comparefilecontents = cleanupExtensionFile($comparefilecontents); + // Only get what we need + $comparefilecontents = cleanupExtensionFile( $comparefilecontents ); - //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 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($comparefile)."' and hash = '{$comparehash}'"); - if($db->numRows($result) == 0) {//If so, save the new hash - $db->query("delete from localisation_file_hash where file = '".$db->strencode($comparefile)."'"); - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($comparefile)."','{$comparehash}')"); + // 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 + $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "' AND hash = '{$comparehash}'" ); + if ( $db->numRows( $result ) == 0 ) {// If so, save the new hash + $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "'" ); + $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $comparefile ) . "','{$comparehash}')" ); } else { - myLog("Skipping {$extension} since the remote file hasn't changed since our last update"); - myLog(""); + myLog( "Skipping {$extension} since the remote file has not changed since our last update" ); return 0; } } - //Get the real array - eval($comparefilecontents); + // Get the real array + eval( $comparefilecontents ); - //If both files are the same, they can be skipped - if($basehash == $comparehash && !$alwaysGetResult) { - myLog("Skipping {$extension} since the remote file is the same as the local file"); - myLog(""); + // If both files are the same, they can be skipped + if ( $basehash == $comparehash && !$alwaysGetResult ) { + myLog( "Skipping {$extension} since the remote file is the same as the local file" ); return 0; } - //Update counter + // Update counter $updates = 0; - if(empty($base_messages['en'])) { + if ( empty( $base_messages['en'] ) ) { $base_messages['en'] = array(); } - if(empty($compare_messages['en'])) { + if ( empty( $compare_messages['en'] ) ) { $compare_messages['en'] = array(); } - //Find the changed english strings - $forbiddenKeys = array_diff_assoc($base_messages['en'],$compare_messages['en']); + // Find the changed english strings + $forbiddenKeys = array_diff_assoc( $base_messages['en'], $compare_messages['en'] ); - //Do an update for each language - foreach($base_messages as $language => $messages) { - if($language == "en") { //Skip english + // Do an update for each language + foreach ( $base_messages as $language => $messages ) { + if ( $language == "en" ) { // Skip english continue; } - //Add the already known messages to the array so we will only find new changes - $result = $db->query("select identifier,value from localisation where language = '".$db->strencode($language)."'"); - while($resObj = $db->fetchObject($result)) { + // Add the already known messages to the array so we will only find new changes + $result = $db->query( "SELECT identifier,value FROM localisation WHERE language = '" . $db->strencode( $language ) . "'" ); + while ( $resObj = $db->fetchObject( $result ) ) { $compare_messages[$language][$resObj->identifier] = $resObj->value; } - if(empty($compare_messages[$language])) { + if ( empty( $compare_messages[$language] ) ) { $compare_messages[$language] = array(); } - //Get the array of changed strings - $changedStrings = array_diff_assoc($messages,$compare_messages[$language]); + // Get the array of changed strings + $changedStrings = array_diff_assoc( $messages, $compare_messages[$language] ); - //If we want to save the changes - if($saveResults) { - myLog("--Checking languagecode {$language} for extension {$extension}--"); - //Do really save the changes - $updates += saveChanges($changedStrings,$forbiddenKeys,$messages,$language); + // If we want to save the changes + if ( $saveResults ) { + myLog( "--Checking languagecode {$language} for extension {$extension}--" ); + // Do really save the changes + $updates += saveChanges( $changedStrings, $forbiddenKeys, $messages, $language ); } } - //And log some stuff - myLog("Updated ".$updates." messages for the '{$extension}' extension"); - myLog(""); + // And log some stuff + myLog( "Updated " . $updates . " messages for the '{$extension}' extension" ); return $updates; } Modified: trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php =================================================================== --- trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php 2009-05-20 09:13:01 UTC (rev 50817) +++ trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php 2009-05-20 09:30:12 UTC (rev 50818) @@ -1,50 +1,50 @@ <?php -function myLog($log){ +function myLog( $log ) { if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) { - print($log."<br />"); + print( $log . "<br />" ); } else { - print($log."\n"); + print( $log . "\n" ); } } $verbose = false; -if(($argc > 0 && $argv[1] == "verbose") OR (isset($_GET['verbose']))) +if ( ( $argc > 0 && $argv[1] == "verbose" ) OR ( isset( $_GET['verbose'] ) ) ) $verbose = true; - + $mtime = microtime(); -$mtime = explode(" ",$mtime); +$mtime = explode( " ", $mtime ); $mtime = $mtime[1] + $mtime[0]; -$starttime = $mtime; +$starttime = $mtime; -define("MEDIAWIKI",true); +define( "MEDIAWIKI", true ); -$IP = strval( getenv('MW_INSTALL_PATH') ) !== '' - ? getenv('MW_INSTALL_PATH') - : realpath( dirname( __FILE__ )."/../../" ); +$IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== '' + ? getenv( 'MW_INSTALL_PATH' ) + : realpath( dirname( __FILE__ ) . "/../../" ); -if ( file_exists( $IP."/StartProfiler.php" ) ) { - require_once( $IP."/StartProfiler.php" ); +if ( file_exists( $IP . "/StartProfiler.php" ) ) { + require_once( $IP . "/StartProfiler.php" ); } else { - require_once( $IP."/includes/ProfilerStub.php" ); + require_once( $IP . "/includes/ProfilerStub.php" ); } -require_once( $IP."/includes/AutoLoader.php" ); -require_once( $IP."/includes/Defines.php" ); -require_once( $IP."/LocalSettings.php" ); +require_once( $IP . "/includes/AutoLoader.php" ); +require_once( $IP . "/includes/Defines.php" ); +require_once( $IP . "/LocalSettings.php" ); -require_once( $IP."/includes/Setup.php" ); -require_once( $IP."/install-utils.inc" ); +require_once( $IP . "/includes/Setup.php" ); +require_once( $IP . "/install-utils.inc" ); -if(is_callable("updateMessages")) - updateMessages($verbose); +if ( is_callable( "updateMessages" ) ) + updateMessages( $verbose ); else - myLog("Error: LocalisationUpdate extension is not (correctly) included in LocalSettings.php"); + myLog( "Error: LocalisationUpdate extension is not (correctly) included in LocalSettings.php" ); $mtime = microtime(); -$mtime = explode(" ",$mtime); +$mtime = explode( " ", $mtime ); $mtime = $mtime[1] + $mtime[0]; $endtime = $mtime; -$totaltime = ($endtime - $starttime); -myLog("All done in ".$totaltime." seconds"); \ No newline at end of file +$totaltime = ( $endtime - $starttime ); +myLog( "All done in " . $totaltime . " seconds" ); Modified: trunk/extensions/LocalisationUpdate/README_FIRST.txt =================================================================== --- trunk/extensions/LocalisationUpdate/README_FIRST.txt 2009-05-20 09:13:01 UTC (rev 50817) +++ trunk/extensions/LocalisationUpdate/README_FIRST.txt 2009-05-20 09:30:12 UTC (rev 50818) @@ -1,5 +1,8 @@ -To install this extension first add the required new tables to your database by using install.sql. +To install this extension first add the required new tables to your database by +using install.sql. Include LocalisationUpdate/LocalisationUpdate.php in your LocalSettings.php -Whenever you want to run an update, run LocalisationUpdate/LocalisationUpdate.update.php in your browser, on the commandline, trough a cronjob or whatever method you wish \ No newline at end of file +Whenever you want to run an update, run +LocalisationUpdate/LocalisationUpdate.update.php in your browser, on the +commandline, trough a cronjob or whatever method you wish _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs