r50818 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r50817‎ | r50818 | r50819 >
Date:09:30, 20 May 2009
Author:siebrand
Status:deferred
Tags:
Comment:
Tweaks for LocalisationUpdate
Modified paths:
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php (modified) (history)
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.php (modified) (history)
  • /trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php (modified) (history)
  • /trunk/extensions/LocalisationUpdate/README_FIRST.txt (modified) (history)

Diff [purge]

Index: trunk/extensions/LocalisationUpdate/README_FIRST.txt
@@ -1,5 +1,8 @@
2 -To install this extension first add the required new tables to your database by using install.sql.
 2+To install this extension first add the required new tables to your database by
 3+using install.sql.
34
45 Include LocalisationUpdate/LocalisationUpdate.php in your LocalSettings.php
56
6 -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
 7+Whenever you want to run an update, run
 8+LocalisationUpdate/LocalisationUpdate.update.php in your browser, on the
 9+commandline, trough a cronjob or whatever method you wish
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php
@@ -1,9 +1,18 @@
22 <?php
 3+/**
 4+ * Internationalisation file for LocalisationUpdate extension.
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
39
410 $messages = array();
511
 12+/** English
 13+ * @author Tom Maaswinkel
 14+ */
615 $messages['en'] = array(
7 - 'localisationupdate-desc' => 'Extension to keep the localized messages as up to date as possible',
 16+ 'localisationupdate-desc' => 'Keeps the localised messages as up to date as possible',
817
918 $messages['yue'] = array(
1019 'localisationupdate-desc' => '一個擴展將本地化嘅信息保持最新',
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.php
@@ -5,209 +5,209 @@
66 Mediawiki core
77 */
88
9 -//Info about me!
 9+// Info about me!
1010 $wgExtensionCredits['other'][] = array(
1111 'path' => __FILE__,
1212 'name' => 'LocalisationUpdate',
1313 'author' => 'Tom Maaswinkel',
1414 'version' => '0.1',
15 - 'description' => 'Extension to keep the localized messages as up to date as possible',
 15+ 'description' => 'Keeps the localised messages as up to date as possible',
1616 'descriptionmsg' => 'localisationupdate-desc',
1717 );
1818
1919 $wgExtensionMessagesFiles['LocalisationUpdate'] = dirname( __FILE__ ) . '/LocalisationUpdate.i18n.php';
2020
21 -//Use the right hook
 21+// Use the right hook
2222 $wgHooks['MessageNotInMwNs'][] = "FindUpdatedMessage";
2323
24 -//DB Search funtion
25 -function FindUpdatedMessage(&$message,$lckey,$langcode,$isFullKey) {
26 - //Define a cache
 24+// DB Search funtion
 25+function FindUpdatedMessage( &$message, $lckey, $langcode, $isFullKey ) {
 26+ // Define a cache
2727 static $cache = array();
2828 $db = wfGetDB ( DB_SLAVE );
2929
3030 // If the key also contains the language code remove the language code from the key
31 - if($isFullKey) {
32 - $lckey = preg_replace("/\/".$langcode."/","",$lckey);
 31+ if ( $isFullKey ) {
 32+ $lckey = preg_replace( "/\/" . $langcode . "/", "", $lckey );
3333 }
3434
35 - //If message is in the cache, don't get an update!
36 - if(array_key_exists($lckey."/".$langcode,$cache)) {
37 - $message = $cache[$lckey."/".$langcode];
 35+ // If message is in the cache, don't get an update!
 36+ if ( array_key_exists( $lckey . "/" . $langcode, $cache ) ) {
 37+ $message = $cache[$lckey . "/" . $langcode];
3838 return true;
3939 }
4040
41 - //Get the message from the database
42 - $query = "select value from localisation where identifier = '".$db->strencode($lckey)."' and language = '".$db->strencode($langcode)."'";
43 - $result = $db->query($query); // Check if the database has any updated message
44 - if($db->numRows($result) == 0) { // If no results found, exit here
 41+ // Get the message from the database
 42+ $query = "SELECT value FROM localisation WHERE identifier = '" . $db->strencode( $lckey ) . "' AND language = '" . $db->strencode( $langcode ) . "'";
 43+ $result = $db->query( $query ); // Check if the database has any updated message
 44+ if ( $db->numRows( $result ) == 0 ) { // If no results found, exit here
4545 return true;
4646 }
4747
48 - $row = $db->fetchObject($result); // Get the result
 48+ $row = $db->fetchObject( $result ); // Get the result
4949 $message = $row->value; // And change the message variable
50 - $cache[$lckey."/".$langcode] = $message; //Update the cache
 50+ $cache[$lckey . "/" . $langcode] = $message; // Update the cache
5151 return true;
5252 }
5353
54 -//Called from the cronjob to fetch new messages from SVN
55 -function updateMessages($verbose = false) {
56 - //Need this later
 54+// Called from the cronjob to fetch new messages from SVN
 55+function updateMessages( $verbose = false ) {
 56+ // Need this later
5757 global $wgExtensionMessagesFiles;
5858
59 - //Prevent the script from timing out
60 - set_time_limit(0);
61 - ini_set("max_execution_time",0);
 59+ // Prevent the script from timing out
 60+ set_time_limit( 0 );
 61+ ini_set( "max_execution_time", 0 );
6262
63 - //Update all MW core messages
64 - $result = updateMediawikiMessages($verbose);
 63+ // Update all MW core messages
 64+ $result = updateMediawikiMessages( $verbose );
6565
66 - //Update all Extension messages
67 - foreach($wgExtensionMessagesFiles as $extension => $locFile) {
68 - $result += updateExtensionMessages($locFile,$extension,$verbose);
 66+ // Update all Extension messages
 67+ foreach ( $wgExtensionMessagesFiles as $extension => $locFile ) {
 68+ $result += updateExtensionMessages( $locFile, $extension, $verbose );
6969 }
7070
71 - //And output the result!
72 - myLog("Updated {$result} messages in total");
73 - myLog("Done");
 71+ // And output the result!
 72+ myLog( "Updated {$result} messages in total" );
 73+ myLog( "Done" );
7474 return true;
7575 }
7676
77 -//Update Extension Messages
78 -function updateExtensionMessages($file,$extension,$verbose) {
 77+// Update Extension Messages
 78+function updateExtensionMessages( $file, $extension, $verbose ) {
7979 global $IP;
8080
81 - //Define the wikimedia SVN path
 81+ // Define the wikimedia SVN path
8282 $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/';
8383
84 - //Find the right SVN folder
85 - $svnFolder = SpecialVersion::getSvnRevision(dirname($file),false,false,true);
 84+ // Find the right SVN folder
 85+ $svnFolder = SpecialVersion::getSvnRevision( dirname( $file ), false, false, true );
8686
87 - //Create a full path
88 - if(!empty($IP)) {
89 - $localfile = $IP."/".$file;
 87+ // Create a full path
 88+ if ( !empty( $IP ) ) {
 89+ $localfile = $IP . "/" . $file;
9090 }
9191
92 - //Get the full SVN directory path
93 - $svndir = "http://".$wmSvnPath.$svnFolder;
 92+ // Get the full SVN directory path
 93+ $svndir = "http://" . $wmSvnPath . $svnFolder;
9494
95 - //Compare the 2 files
96 - $result = compareExtensionFiles($extension,$svndir."/".basename($file),$file,$verbose,false,true);
 95+ // Compare the 2 files
 96+ $result = compareExtensionFiles( $extension, $svndir . "/" . basename( $file ), $file, $verbose, false, true );
9797 return $result;
9898 }
9999
100 -//Update the Mediawiki Core Messages
101 -function updateMediawikiMessages($verbose) {
 100+// Update the Mediawiki Core Messages
 101+function updateMediawikiMessages( $verbose ) {
102102 global $IP;
103103
104 - //Define the wikimedia SVN path
 104+ // Define the wikimedia SVN path
105105 $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/';
106106
107 - //Create an array which will later contain all the files that we want to try to update
 107+ // Create an array which will later contain all the files that we want to try to update
108108 $files = array();
109109
110 - //The directory which contains the files
 110+ // The directory which contains the files
111111 $dirname = "languages/messages";
112112
113 - //Get the full path to the directory
114 - if(!empty($IP)) {
115 - $dirname = $IP."/".$dirname;
 113+ // Get the full path to the directory
 114+ if ( !empty( $IP ) ) {
 115+ $dirname = $IP . "/" . $dirname;
116116 }
117117
118 - //Open the directory
 118+ // Open the directory
119119 $dir = opendir( $dirname );
120 - while( false !== ( $file = readdir( $dir ) ) ) {
 120+ while ( false !== ( $file = readdir( $dir ) ) ) {
121121 $m = array();
122122
123 - //And save all the filenames of files containing messages
124 - if( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
125 - if($m[1] != 'En') { //Except for the english one
 123+ // And save all the filenames of files containing messages
 124+ if ( preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $file, $m ) ) {
 125+ if ( $m[1] != 'En' ) { // Except for the english one
126126 $files[] = $file;
127127 }
128128 }
129129 }
130130 closedir( $dir );
131131
132 - //Get the SVN folder used for the checkout
133 - $svnFolder = SpecialVersion::getSvnRevision($dirname,false,false,true);
 132+ // Get the SVN folder used for the checkout
 133+ $svnFolder = SpecialVersion::getSvnRevision( $dirname, false, false, true );
134134
135 - //Do not update if not from SVN
136 - if(empty($svnFolder)) {
137 - myLog("Can't update localisation as the files are not retrieved from SVN");
 135+ // Do not update if not from SVN
 136+ if ( empty( $svnFolder ) ) {
 137+ myLog( 'Cannot update localisation as the files are not retrieved from SVN' );
138138 return 0;
139139 }
140140
141 - //Get the full SVN Path
142 - $svndir = "http://".$wmSvnPath.$svnFolder;
 141+ // Get the full SVN Path
 142+ $svndir = "http://" . $wmSvnPath . $svnFolder;
143143
144 - //Find the changed English strings (as these messages won't be updated in ANY language)
145 - $changedEnglishStrings = compareFiles($dirname."/MessagesEn.php",$svndir."/MessagesEn.php",$verbose);
 144+ // Find the changed English strings (as these messages won't be updated in ANY language)
 145+ $changedEnglishStrings = compareFiles( $dirname . "/MessagesEn.php", $svndir . "/MessagesEn.php", $verbose );
146146
147 - //Count the changes
 147+ // Count the changes
148148 $changedCount = 0;
149149
150 - //For each language
151 - foreach($files as $file) {
152 - $svnfile = $svndir."/".$file;
153 - $localfile = $dirname."/".$file;
 150+ // For each language
 151+ foreach ( $files as $file ) {
 152+ $svnfile = $svndir . "/" . $file;
 153+ $localfile = $dirname . "/" . $file;
154154
155 - //Compare the files
156 - $result = compareFiles($svnfile,$localfile,$verbose,$changedEnglishStrings,false,true);
 155+ // Compare the files
 156+ $result = compareFiles( $svnfile, $localfile, $verbose, $changedEnglishStrings, false, true );
157157
158 - //And update the change counter
159 - $changedCount += count($result);
 158+ // And update the change counter
 159+ $changedCount += count( $result );
160160 }
161161
162 - //Log some nice info
163 - myLog("{$changedCount} Mediawiki messages are updated");
 162+ // Log some nice info
 163+ myLog( "{$changedCount} Mediawiki messages are updated" );
164164 return $changedCount;
165165 }
166166
167 -//Remove all unneeded content
168 -function cleanupFile($contents) {
169 - //We don't need any PHP tags
170 - $contents = preg_replace("/<\\?php/","",$contents);
171 - $contents = preg_replace("/\?>/","",$contents);
 167+// Remove all unneeded content
 168+function cleanupFile( $contents ) {
 169+ // We don't need any PHP tags
 170+ $contents = preg_replace( "/<\\?php/", "", $contents );
 171+ $contents = preg_replace( "/\?>/", "", $contents );
172172 $results = array();
173 - //And we only want the messages array
174 - preg_match("/\\\$messages(.*\s)*?\);/",$contents,$results);
 173+ // And we only want the messages array
 174+ preg_match( "/\\\$messages(.*\s)*?\);/", $contents, $results );
175175
176 - //If there is any!
177 - if(!empty($results[0])) {
 176+ // If there is any!
 177+ if ( !empty( $results[0] ) ) {
178178 $contents = $results[0];
179179 } else {
180180 $contents = "";
181181 }
182182
183 - //Windows vs Unix always stinks when comparing files
184 - $contents = preg_replace("/\\\r/","",$contents);
 183+ // Windows vs Unix always stinks when comparing files
 184+ $contents = preg_replace( "/\\\r/", "", $contents );
185185
186 - //return the cleaned up file
 186+ // return the cleaned up file
187187 return $contents;
188188 }
189189
190 -function compareFiles($basefile,$comparefile,$verbose,$forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false) {
191 - //We need to write to the DB later
 190+function compareFiles( $basefile, $comparefile, $verbose, $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) {
 191+ // We need to write to the DB later
192192 $db = wfGetDB ( DB_MASTER );
193193
194194 $compare_messages = array();
195195 $base_messages = array();
196196
197 - //Get the languagecode
 197+ // Get the languagecode
198198 $m = array();
199199 preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $basefile, $m );
200 - $langcode = strtolower($m[1]);
 200+ $langcode = strtolower( $m[1] );
201201
202 - //use cURL to get the SVN contents
203 - if(preg_match("/^http/",$basefile)) {
204 - $basefilecontents = Http::get($basefile);
205 - if(empty($basefilecontents)) {
206 - myLog("Can't get the contents of ".$basefile." (curl)");
 202+ // use cURL to get the SVN contents
 203+ if ( preg_match( "/^http/", $basefile ) ) {
 204+ $basefilecontents = Http::get( $basefile );
 205+ if ( empty( $basefilecontents ) ) {
 206+ myLog( "Cannot get the contents of " . $basefile . " (curl)" );
207207 return array();
208208 }
209 - } else {//otherwise try file_get_contents
210 - if(!$basefilecontents = file_get_contents($basefile)) {
211 - myLog("Can't get the contents of ".$basefile);
 209+ } else {// otherwise try file_get_contents
 210+ if ( !$basefilecontents = file_get_contents( $basefile ) ) {
 211+ myLog( "Cannot get the contents of " . $basefile );
212212 return array();
213213 }
214214 }
@@ -215,158 +215,151 @@
216216 $basehash = "";
217217 $comparehash = "";
218218
219 - //Only get the part we need
220 - $basefilecontents = cleanupFile($basefilecontents);
 219+ // Only get the part we need
 220+ $basefilecontents = cleanupFile( $basefilecontents );
221221
222 - //Change the variable name
223 - $basefilecontents = preg_replace("/\\\$messages/","\$base_messages",$basefilecontents);
 222+ // Change the variable name
 223+ $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
224224
225 - $basehash = md5($basefilecontents);
226 - //If this is the remote file check if the file has changed since our last update
227 - if(preg_match("/^http/",$basefile) && !$alwaysGetResult) {
228 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($basefile)."' and hash = '{$basehash}'");
229 - if($db->numRows($result) == 0) { //If it did, save the new file hash
230 - $db->query("delete from localisation_file_hash where file = '".$db->strencode($basefile)."'");
231 - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($basefile)."','{$basehash}')");
 225+ $basehash = md5( $basefilecontents );
 226+ // If this is the remote file check if the file has changed since our last update
 227+ if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
 228+ $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "' AND hash = '{$basehash}'" );
 229+ if ( $db->numRows( $result ) == 0 ) { // If it did, save the new file hash
 230+ $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "'" );
 231+ $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $basefile ) . "','{$basehash}')" );
232232 } else {
233 - myLog("Skipping {$langcode} since the remote file hasn't changed since our last update");
234 - myLog("");
 233+ myLog( "Skipping {$langcode} since the remote file hasn't changed since our last update" );
235234 return array();
236235 }
237236 }
238237
239 - //Get the array with messages
240 - eval($basefilecontents);
 238+ // Get the array with messages
 239+ eval( $basefilecontents );
241240
242 - //use cURL to get the contents
243 - if(preg_match("/^http/",$comparefile)) {
244 - $comparefilecontents = Http::get($comparefile);
245 - if(empty($comparefilecontents)) {
246 - myLog("Can't get the contents of ".$comparefile." (curl)");
 241+ // use cURL to get the contents
 242+ if ( preg_match( "/^http/", $comparefile ) ) {
 243+ $comparefilecontents = Http::get( $comparefile );
 244+ if ( empty( $comparefilecontents ) ) {
 245+ myLog( "Cannot get the contents of " . $comparefile . " (curl)" );
247246 return array();
248247 }
249 - } else { //otherwise use file_get_contents
250 - if(!$comparefilecontents = file_get_contents($comparefile)) {
251 - myLog("Can't get the contents of ".$comparefile);
 248+ } else { // otherwise use file_get_contents
 249+ if ( !$comparefilecontents = file_get_contents( $comparefile ) ) {
 250+ myLog( "Cannot get the contents of " . $comparefile );
252251 return array();
253252 }
254253 }
255254
256 - //only get the stuff we need
257 - $comparefilecontents = cleanupFile($comparefilecontents);
 255+ // only get the stuff we need
 256+ $comparefilecontents = cleanupFile( $comparefilecontents );
258257
259 - //rename the array
260 - $comparefilecontents = preg_replace("/\\\$messages/","\$compare_messages",$comparefilecontents);
 258+ // rename the array
 259+ $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
261260
262 - $comparehash = md5($comparefilecontents);
263 - //If this is the remote file check if the file has changed since our last update
264 - if(preg_match("/^http/",$comparefile) && !$alwaysGetResult) {
265 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($comparefile)."' and hash = '{$comparehash}'");
266 - if($db->numRows($result) == 0) {//If it did, save the new file hash
267 - $db->query("delete from localisation_file_hash where file = '".$db->strencode($comparefile)."'");
268 - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($comparefile)."','{$comparehash}')");
 261+ $comparehash = md5( $comparefilecontents );
 262+ // If this is the remote file check if the file has changed since our last update
 263+ if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
 264+ $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "' and hash = '{$comparehash}'" );
 265+ if ( $db->numRows( $result ) == 0 ) {// If it did, save the new file hash
 266+ $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "'" );
 267+ $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $comparefile ) . "','{$comparehash}')" );
269268 } else {
270 - myLog("Skipping {$langcode} since the remote file hasn't changed since our last update");
271 - myLog("");
 269+ myLog( "Skipping {$langcode} since the remote file has not changed since our last update" );
272270 return array();
273271 }
274272 }
275 - //Get the array
276 - eval($comparefilecontents);
 273+ // Get the array
 274+ eval( $comparefilecontents );
277275
278 -
279 - //if the localfile and the remote file are the same, skip them!
280 - if($basehash == $comparehash && !$alwaysGetResult) {
281 - myLog("Skipping {$langcode} since the remote file is the same as the local file");
282 - myLog("");
 276+ // if the localfile and the remote file are the same, skip them!
 277+ if ( $basehash == $comparehash && !$alwaysGetResult ) {
 278+ myLog( "Skipping {$langcode} since the remote file is the same as the local file" );
283279 return array();
284280 }
285281
286 - //Add the messages we got with our previous update(s) to the local array (as we already got these as well)
287 - $result = $db->query("select identifier,value from localisation where language = '".$db->strencode($langcode)."'");
288 - while($resObj = $db->fetchObject($result)) {
 282+ // Add the messages we got with our previous update(s) to the local array (as we already got these as well)
 283+ $result = $db->query( "SELECT identifier,value FROM localisation WHERE language = '" . $db->strencode( $langcode ) . "'" );
 284+ while ( $resObj = $db->fetchObject( $result ) ) {
289285 $compare_messages[$resObj->identifier] = $resObj->value;
290286 }
291287
292 - //Compare the remote and local message arrays
293 - $changedStrings = array_diff_assoc($base_messages,$compare_messages);
 288+ // Compare the remote and local message arrays
 289+ $changedStrings = array_diff_assoc( $base_messages, $compare_messages );
294290
295 - //If we want to save the differences
296 - if($saveResults) {
297 - myLog("--Checking languagecode {$langcode}--");
298 - //The save them
299 - $updates = saveChanges($changedStrings,$forbiddenKeys,$base_messages,$langcode);
300 - myLog("{$updates} messages updated for {$langcode}.");
301 -
302 - myLog("");
 291+ // If we want to save the differences
 292+ if ( $saveResults ) {
 293+ myLog( "--Checking languagecode {$langcode}--" );
 294+ // The save them
 295+ $updates = saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode );
 296+ myLog( "{$updates} messages updated for {$langcode}." );
303297 }
304298
305299 return $changedStrings;
306300 }
307301
308 -
309 -function saveChanges($changedStrings,$forbiddenKeys,$base_messages,$langcode) {
 302+function saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode ) {
310303 global $verbose;
311304
312 - //Gonna write to the DB again
 305+ // Gonna write to the DB again
313306 $db = wfGetDB ( DB_MASTER );
314307
315 - //Count the updates
 308+ // Count the updates
316309 $updates = 0;
317 - foreach($changedStrings as $key => $value) {
318 - //If this message wasn't changed in english
319 - if(!array_key_exists($key , $forbiddenKeys )){
320 - //See if we can update the database
321 - $db->query("update localisation set value = '".$db->strencode($base_messages[$key])."' where language = '".$db->strencode($langcode)."' and identifier like '".$db->strencode($key)."'");
322 - if($db->affectedRows() == 0) { //Otherwise do a new insert
323 - $db->query("insert into localisation (value, language, identifier) values ('".$db->strencode($base_messages[$key])."', '".$db->strencode($langcode)."','".$db->strencode($key)."')");
324 - if($db->affectedRows() == 0) {
325 - throw new MWException( "An error has occured while inserting a new message to the database to the database!" );
 310+ foreach ( $changedStrings as $key => $value ) {
 311+ // If this message wasn't changed in english
 312+ if ( !array_key_exists( $key , $forbiddenKeys ) ) {
 313+ // See if we can update the database
 314+ $db->query( "UPDATE localisation SET value = '" . $db->strencode( $base_messages[$key] ) . "' WHERE language = '" . $db->strencode( $langcode ) . "' and identifier like '" . $db->strencode( $key ) . "'" );
 315+ if ( $db->affectedRows() == 0 ) { // Otherwise do a new insert
 316+ $db->query( "INSERT INTO localisation (value, language, identifier) VALUES ('" . $db->strencode( $base_messages[$key] ) . "', '" . $db->strencode( $langcode ) . "','" . $db->strencode( $key ) . "')" );
 317+ if ( $db->affectedRows() == 0 ) {
 318+ throw new MWException( "An error has occured while inserting a new message into the database!" );
326319 }
327320 }
328321
329 - //Output extra logmessages when needed
330 - if($verbose) {
331 - myLog("Updated message {$key} from {$compare_messages[$key]} to {$base_messages[$key]}");
 322+ // Output extra logmessages when needed
 323+ if ( $verbose ) {
 324+ myLog( "Updated message {$key} from {$compare_messages[$key]} to {$base_messages[$key]}" );
332325 }
333326
334 - //Update the counter
 327+ // Update the counter
335328 $updates++;
336329 }
337330 }
338331 return $updates;
339332 }
340333
341 -function cleanupExtensionFile($contents) {
342 - //We don't want PHP tags
343 - $contents = preg_replace("/<\?php/","",$contents);
344 - $contents = preg_replace("/\?>/","",$contents);
 334+function cleanupExtensionFile( $contents ) {
 335+ // We don't want PHP tags
 336+ $contents = preg_replace( "/<\?php/", "", $contents );
 337+ $contents = preg_replace( "/\?>/", "", $contents );
345338 $results = array();
346 - //And we only want message arrays
347 - preg_match_all("/\\\$messages(.*\s)*?\);/",$contents,$results);
348 - //But we want them all in one string
349 - $contents = implode("\n\n",$results[0]);
 339+ // And we only want message arrays
 340+ preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results );
 341+ // But we want them all in one string
 342+ $contents = implode( "\n\n", $results[0] );
350343
351 - //And we hate the windows vs linux linebreaks
352 - $contents = preg_replace("/\\\r/","",$contents);
 344+ // And we hate the windows vs linux linebreaks
 345+ $contents = preg_replace( "/\\\r/", "", $contents );
353346 return $contents;
354347 }
355348
356 -function compareExtensionFiles($extension,$basefile,$comparefile,$verbose, $alwaysGetResult = true, $saveResults = false) {
357 - //Let's mess with the database again
 349+function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) {
 350+ // Let's mess with the database again
358351 $db = wfGetDB ( DB_MASTER );
359352 $compare_messages = array();
360353 $base_messages = array();
361354
362 - if(preg_match("/^http/",$basefile)) {
363 - $basefilecontents = Http::get($basefile);
364 - if(empty($basefilecontents)) {
365 - myLog("Can't get the contents of ".$basefile." (curl)");
 355+ if ( preg_match( "/^http/", $basefile ) ) {
 356+ $basefilecontents = Http::get( $basefile );
 357+ if ( empty( $basefilecontents ) ) {
 358+ myLog( "Cannot get the contents of " . $basefile . " (curl)" );
366359 return 0;
367360 }
368 - } else { //or otherwise file _get_contents
369 - if(!$basefilecontents = file_get_contents($basefile)) {
370 - myLog("Can't get the contents of ".$basefile);
 361+ } else { // or otherwise file _get_contents
 362+ if ( !$basefilecontents = file_get_contents( $basefile ) ) {
 363+ myLog( "Cannot get the contents of " . $basefile );
371364 return 0;
372365 }
373366 }
@@ -374,116 +367,112 @@
375368 $basehash = "";
376369 $comparehash = "";
377370
378 - //Cleanup the file where needed
379 - $basefilecontents = cleanupExtensionFile($basefilecontents);
 371+ // Cleanup the file where needed
 372+ $basefilecontents = cleanupExtensionFile( $basefilecontents );
380373
381 - //Rename the arrays
382 - $basefilecontents = preg_replace("/\\\$messages/","\$base_messages",$basefilecontents);
 374+ // Rename the arrays
 375+ $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents );
383376
384 - $basehash = md5($basefilecontents);
385 - //If this is the remote file
386 - if(preg_match("/^http/",$basefile) && !$alwaysGetResult) {
387 - //Check if the hash has changed
388 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($basefile)."' and hash = '{$basehash}'");
389 - if($db->numRows($result) == 0) {
390 - $db->query("delete from localisation_file_hash where file = '".$db->strencode($basefile)."'");
391 - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($basefile)."','{$basehash}')");
 377+ $basehash = md5( $basefilecontents );
 378+ // If this is the remote file
 379+ if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) {
 380+ // Check if the hash has changed
 381+ $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "' AND hash = '{$basehash}'" );
 382+ if ( $db->numRows( $result ) == 0 ) {
 383+ $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $basefile ) . "'" );
 384+ $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $basefile ) . "','{$basehash}')" );
392385 } else {
393 - myLog("Skipping {$extension} since the remote file hasn't changed since our last update");
394 - myLog("");
 386+ myLog( "Skipping {$extension} since the remote file has not changed since our last update" );
395387 return 0;
396388 }
397389 }
398390
399 - //And get the real contents
400 - eval($basefilecontents);
 391+ // And get the real contents
 392+ eval( $basefilecontents );
401393
402 - //Use cURL when available
403 - if(preg_match("/^http/",$comparefile)) {
404 - $comparefilecontents = Http::get($comparefile);
405 - if(empty($comparefilecontents)) {
406 - myLog("Can't get the contents of ".$comparefile." (curl)");
 394+ // Use cURL when available
 395+ if ( preg_match( "/^http/", $comparefile ) ) {
 396+ $comparefilecontents = Http::get( $comparefile );
 397+ if ( empty( $comparefilecontents ) ) {
 398+ myLog( "Cannot get the contents of " . $comparefile . " (curl)" );
407399 return 0;
408400 }
409 - } else { //Otherwise use file_get_contents
410 - if(!$comparefilecontents = file_get_contents($comparefile)) {
411 - myLog("Can't get the contents of ".$comparefile);
 401+ } else { // Otherwise use file_get_contents
 402+ if ( !$comparefilecontents = file_get_contents( $comparefile ) ) {
 403+ myLog( "Cannot get the contents of " . $comparefile );
412404 return 0;
413405 }
414406 }
415407
416 - //Only get what we need
417 - $comparefilecontents = cleanupExtensionFile($comparefilecontents);
 408+ // Only get what we need
 409+ $comparefilecontents = cleanupExtensionFile( $comparefilecontents );
418410
419 - //Rename the array
420 - $comparefilecontents = preg_replace("/\\\$messages/","\$compare_messages",$comparefilecontents);
421 - $comparehash = md5($comparefilecontents);
422 - if(preg_match("/^http/",$comparefile) && !$alwaysGetResult) {
423 - //Check if the remote file has changed
424 - $result = $db->query("select * from localisation_file_hash where file = '".$db->strencode($comparefile)."' and hash = '{$comparehash}'");
425 - if($db->numRows($result) == 0) {//If so, save the new hash
426 - $db->query("delete from localisation_file_hash where file = '".$db->strencode($comparefile)."'");
427 - $db->query("insert into localisation_file_hash (file,hash) values ('".$db->strencode($comparefile)."','{$comparehash}')");
 411+ // Rename the array
 412+ $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents );
 413+ $comparehash = md5( $comparefilecontents );
 414+ if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) {
 415+ // Check if the remote file has changed
 416+ $result = $db->query( "SELECT * FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "' AND hash = '{$comparehash}'" );
 417+ if ( $db->numRows( $result ) == 0 ) {// If so, save the new hash
 418+ $db->query( "DELETE FROM localisation_file_hash WHERE file = '" . $db->strencode( $comparefile ) . "'" );
 419+ $db->query( "INSERT INTO localisation_file_hash (file,hash) VALUES ('" . $db->strencode( $comparefile ) . "','{$comparehash}')" );
428420 } else {
429 - myLog("Skipping {$extension} since the remote file hasn't changed since our last update");
430 - myLog("");
 421+ myLog( "Skipping {$extension} since the remote file has not changed since our last update" );
431422 return 0;
432423 }
433424 }
434 - //Get the real array
435 - eval($comparefilecontents);
 425+ // Get the real array
 426+ eval( $comparefilecontents );
436427
437 - //If both files are the same, they can be skipped
438 - if($basehash == $comparehash && !$alwaysGetResult) {
439 - myLog("Skipping {$extension} since the remote file is the same as the local file");
440 - myLog("");
 428+ // If both files are the same, they can be skipped
 429+ if ( $basehash == $comparehash && !$alwaysGetResult ) {
 430+ myLog( "Skipping {$extension} since the remote file is the same as the local file" );
441431 return 0;
442432 }
443433
444 - //Update counter
 434+ // Update counter
445435 $updates = 0;
446436
447 - if(empty($base_messages['en'])) {
 437+ if ( empty( $base_messages['en'] ) ) {
448438 $base_messages['en'] = array();
449439 }
450440
451 - if(empty($compare_messages['en'])) {
 441+ if ( empty( $compare_messages['en'] ) ) {
452442 $compare_messages['en'] = array();
453443 }
454444
455 - //Find the changed english strings
456 - $forbiddenKeys = array_diff_assoc($base_messages['en'],$compare_messages['en']);
 445+ // Find the changed english strings
 446+ $forbiddenKeys = array_diff_assoc( $base_messages['en'], $compare_messages['en'] );
457447
458 - //Do an update for each language
459 - foreach($base_messages as $language => $messages) {
460 - if($language == "en") { //Skip english
 448+ // Do an update for each language
 449+ foreach ( $base_messages as $language => $messages ) {
 450+ if ( $language == "en" ) { // Skip english
461451 continue;
462452 }
463453
464 - //Add the already known messages to the array so we will only find new changes
465 - $result = $db->query("select identifier,value from localisation where language = '".$db->strencode($language)."'");
466 - while($resObj = $db->fetchObject($result)) {
 454+ // Add the already known messages to the array so we will only find new changes
 455+ $result = $db->query( "SELECT identifier,value FROM localisation WHERE language = '" . $db->strencode( $language ) . "'" );
 456+ while ( $resObj = $db->fetchObject( $result ) ) {
467457 $compare_messages[$language][$resObj->identifier] = $resObj->value;
468458 }
469459
470 - if(empty($compare_messages[$language])) {
 460+ if ( empty( $compare_messages[$language] ) ) {
471461 $compare_messages[$language] = array();
472462 }
473463
474 - //Get the array of changed strings
475 - $changedStrings = array_diff_assoc($messages,$compare_messages[$language]);
 464+ // Get the array of changed strings
 465+ $changedStrings = array_diff_assoc( $messages, $compare_messages[$language] );
476466
477 - //If we want to save the changes
478 - if($saveResults) {
479 - myLog("--Checking languagecode {$language} for extension {$extension}--");
480 - //Do really save the changes
481 - $updates += saveChanges($changedStrings,$forbiddenKeys,$messages,$language);
 467+ // If we want to save the changes
 468+ if ( $saveResults ) {
 469+ myLog( "--Checking languagecode {$language} for extension {$extension}--" );
 470+ // Do really save the changes
 471+ $updates += saveChanges( $changedStrings, $forbiddenKeys, $messages, $language );
482472 }
483473 }
484474
485 - //And log some stuff
486 - myLog("Updated ".$updates." messages for the '{$extension}' extension");
487 - myLog("");
 475+ // And log some stuff
 476+ myLog( "Updated " . $updates . " messages for the '{$extension}' extension" );
488477
489478 return $updates;
490479 }
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php
@@ -1,50 +1,50 @@
22 <?php
3 -function myLog($log){
 3+function myLog( $log ) {
44 if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) {
5 - print($log."<br />");
 5+ print( $log . "<br />" );
66 } else {
7 - print($log."\n");
 7+ print( $log . "\n" );
88 }
99 }
1010
1111 $verbose = false;
1212
13 -if(($argc > 0 && $argv[1] == "verbose") OR (isset($_GET['verbose'])))
 13+if ( ( $argc > 0 && $argv[1] == "verbose" ) OR ( isset( $_GET['verbose'] ) ) )
1414 $verbose = true;
15 -
 15+
1616 $mtime = microtime();
17 -$mtime = explode(" ",$mtime);
 17+$mtime = explode( " ", $mtime );
1818 $mtime = $mtime[1] + $mtime[0];
19 -$starttime = $mtime;
 19+$starttime = $mtime;
2020
21 -define("MEDIAWIKI",true);
 21+define( "MEDIAWIKI", true );
2222
23 -$IP = strval( getenv('MW_INSTALL_PATH') ) !== ''
24 - ? getenv('MW_INSTALL_PATH')
25 - : realpath( dirname( __FILE__ )."/../../" );
 23+$IP = strval( getenv( 'MW_INSTALL_PATH' ) ) !== ''
 24+ ? getenv( 'MW_INSTALL_PATH' )
 25+ : realpath( dirname( __FILE__ ) . "/../../" );
2626
27 -if ( file_exists( $IP."/StartProfiler.php" ) ) {
28 - require_once( $IP."/StartProfiler.php" );
 27+if ( file_exists( $IP . "/StartProfiler.php" ) ) {
 28+ require_once( $IP . "/StartProfiler.php" );
2929 } else {
30 - require_once( $IP."/includes/ProfilerStub.php" );
 30+ require_once( $IP . "/includes/ProfilerStub.php" );
3131 }
3232
33 -require_once( $IP."/includes/AutoLoader.php" );
34 -require_once( $IP."/includes/Defines.php" );
35 -require_once( $IP."/LocalSettings.php" );
 33+require_once( $IP . "/includes/AutoLoader.php" );
 34+require_once( $IP . "/includes/Defines.php" );
 35+require_once( $IP . "/LocalSettings.php" );
3636
37 -require_once( $IP."/includes/Setup.php" );
38 -require_once( $IP."/install-utils.inc" );
 37+require_once( $IP . "/includes/Setup.php" );
 38+require_once( $IP . "/install-utils.inc" );
3939
4040
41 -if(is_callable("updateMessages"))
42 - updateMessages($verbose);
 41+if ( is_callable( "updateMessages" ) )
 42+ updateMessages( $verbose );
4343 else
44 - myLog("Error: LocalisationUpdate extension is not (correctly) included in LocalSettings.php");
 44+ myLog( "Error: LocalisationUpdate extension is not (correctly) included in LocalSettings.php" );
4545
4646 $mtime = microtime();
47 -$mtime = explode(" ",$mtime);
 47+$mtime = explode( " ", $mtime );
4848 $mtime = $mtime[1] + $mtime[0];
4949 $endtime = $mtime;
50 -$totaltime = ($endtime - $starttime);
51 -myLog("All done in ".$totaltime." seconds");
\ No newline at end of file
 50+$totaltime = ( $endtime - $starttime );
 51+myLog( "All done in " . $totaltime . " seconds" );

Status & tagging log