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. |
3 | 4 | |
4 | 5 | Include LocalisationUpdate/LocalisationUpdate.php in your LocalSettings.php |
5 | 6 | |
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 @@ |
2 | 2 | <?php |
| 3 | +/** |
| 4 | + * Internationalisation file for LocalisationUpdate extension. |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + */ |
3 | 9 | |
4 | 10 | $messages = array(); |
5 | 11 | |
| 12 | +/** English |
| 13 | + * @author Tom Maaswinkel |
| 14 | + */ |
6 | 15 | $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', |
8 | 17 | |
9 | 18 | $messages['yue'] = array( |
10 | 19 | 'localisationupdate-desc' => '一個擴展將本地化嘅信息保持最新', |
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.php |
— | — | @@ -5,209 +5,209 @@ |
6 | 6 | Mediawiki core |
7 | 7 | */ |
8 | 8 | |
9 | | -//Info about me! |
| 9 | +// Info about me! |
10 | 10 | $wgExtensionCredits['other'][] = array( |
11 | 11 | 'path' => __FILE__, |
12 | 12 | 'name' => 'LocalisationUpdate', |
13 | 13 | 'author' => 'Tom Maaswinkel', |
14 | 14 | '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', |
16 | 16 | 'descriptionmsg' => 'localisationupdate-desc', |
17 | 17 | ); |
18 | 18 | |
19 | 19 | $wgExtensionMessagesFiles['LocalisationUpdate'] = dirname( __FILE__ ) . '/LocalisationUpdate.i18n.php'; |
20 | 20 | |
21 | | -//Use the right hook |
| 21 | +// Use the right hook |
22 | 22 | $wgHooks['MessageNotInMwNs'][] = "FindUpdatedMessage"; |
23 | 23 | |
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 |
27 | 27 | static $cache = array(); |
28 | 28 | $db = wfGetDB ( DB_SLAVE ); |
29 | 29 | |
30 | 30 | // 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 ); |
33 | 33 | } |
34 | 34 | |
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]; |
38 | 38 | return true; |
39 | 39 | } |
40 | 40 | |
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 |
45 | 45 | return true; |
46 | 46 | } |
47 | 47 | |
48 | | - $row = $db->fetchObject($result); // Get the result |
| 48 | + $row = $db->fetchObject( $result ); // Get the result |
49 | 49 | $message = $row->value; // And change the message variable |
50 | | - $cache[$lckey."/".$langcode] = $message; //Update the cache |
| 50 | + $cache[$lckey . "/" . $langcode] = $message; // Update the cache |
51 | 51 | return true; |
52 | 52 | } |
53 | 53 | |
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 |
57 | 57 | global $wgExtensionMessagesFiles; |
58 | 58 | |
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 ); |
62 | 62 | |
63 | | - //Update all MW core messages |
64 | | - $result = updateMediawikiMessages($verbose); |
| 63 | + // Update all MW core messages |
| 64 | + $result = updateMediawikiMessages( $verbose ); |
65 | 65 | |
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 ); |
69 | 69 | } |
70 | 70 | |
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" ); |
74 | 74 | return true; |
75 | 75 | } |
76 | 76 | |
77 | | -//Update Extension Messages |
78 | | -function updateExtensionMessages($file,$extension,$verbose) { |
| 77 | +// Update Extension Messages |
| 78 | +function updateExtensionMessages( $file, $extension, $verbose ) { |
79 | 79 | global $IP; |
80 | 80 | |
81 | | - //Define the wikimedia SVN path |
| 81 | + // Define the wikimedia SVN path |
82 | 82 | $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/'; |
83 | 83 | |
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 ); |
86 | 86 | |
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; |
90 | 90 | } |
91 | 91 | |
92 | | - //Get the full SVN directory path |
93 | | - $svndir = "http://".$wmSvnPath.$svnFolder; |
| 92 | + // Get the full SVN directory path |
| 93 | + $svndir = "http://" . $wmSvnPath . $svnFolder; |
94 | 94 | |
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 ); |
97 | 97 | return $result; |
98 | 98 | } |
99 | 99 | |
100 | | -//Update the Mediawiki Core Messages |
101 | | -function updateMediawikiMessages($verbose) { |
| 100 | +// Update the Mediawiki Core Messages |
| 101 | +function updateMediawikiMessages( $verbose ) { |
102 | 102 | global $IP; |
103 | 103 | |
104 | | - //Define the wikimedia SVN path |
| 104 | + // Define the wikimedia SVN path |
105 | 105 | $wmSvnPath = 'svn.wikimedia.org/svnroot/mediawiki/'; |
106 | 106 | |
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 |
108 | 108 | $files = array(); |
109 | 109 | |
110 | | - //The directory which contains the files |
| 110 | + // The directory which contains the files |
111 | 111 | $dirname = "languages/messages"; |
112 | 112 | |
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; |
116 | 116 | } |
117 | 117 | |
118 | | - //Open the directory |
| 118 | + // Open the directory |
119 | 119 | $dir = opendir( $dirname ); |
120 | | - while( false !== ( $file = readdir( $dir ) ) ) { |
| 120 | + while ( false !== ( $file = readdir( $dir ) ) ) { |
121 | 121 | $m = array(); |
122 | 122 | |
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 |
126 | 126 | $files[] = $file; |
127 | 127 | } |
128 | 128 | } |
129 | 129 | } |
130 | 130 | closedir( $dir ); |
131 | 131 | |
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 ); |
134 | 134 | |
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' ); |
138 | 138 | return 0; |
139 | 139 | } |
140 | 140 | |
141 | | - //Get the full SVN Path |
142 | | - $svndir = "http://".$wmSvnPath.$svnFolder; |
| 141 | + // Get the full SVN Path |
| 142 | + $svndir = "http://" . $wmSvnPath . $svnFolder; |
143 | 143 | |
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 ); |
146 | 146 | |
147 | | - //Count the changes |
| 147 | + // Count the changes |
148 | 148 | $changedCount = 0; |
149 | 149 | |
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; |
154 | 154 | |
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 ); |
157 | 157 | |
158 | | - //And update the change counter |
159 | | - $changedCount += count($result); |
| 158 | + // And update the change counter |
| 159 | + $changedCount += count( $result ); |
160 | 160 | } |
161 | 161 | |
162 | | - //Log some nice info |
163 | | - myLog("{$changedCount} Mediawiki messages are updated"); |
| 162 | + // Log some nice info |
| 163 | + myLog( "{$changedCount} Mediawiki messages are updated" ); |
164 | 164 | return $changedCount; |
165 | 165 | } |
166 | 166 | |
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 ); |
172 | 172 | $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 ); |
175 | 175 | |
176 | | - //If there is any! |
177 | | - if(!empty($results[0])) { |
| 176 | + // If there is any! |
| 177 | + if ( !empty( $results[0] ) ) { |
178 | 178 | $contents = $results[0]; |
179 | 179 | } else { |
180 | 180 | $contents = ""; |
181 | 181 | } |
182 | 182 | |
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 ); |
185 | 185 | |
186 | | - //return the cleaned up file |
| 186 | + // return the cleaned up file |
187 | 187 | return $contents; |
188 | 188 | } |
189 | 189 | |
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 |
192 | 192 | $db = wfGetDB ( DB_MASTER ); |
193 | 193 | |
194 | 194 | $compare_messages = array(); |
195 | 195 | $base_messages = array(); |
196 | 196 | |
197 | | - //Get the languagecode |
| 197 | + // Get the languagecode |
198 | 198 | $m = array(); |
199 | 199 | preg_match( '/Messages([A-Z][a-z_]+)\.php$/', $basefile, $m ); |
200 | | - $langcode = strtolower($m[1]); |
| 200 | + $langcode = strtolower( $m[1] ); |
201 | 201 | |
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)" ); |
207 | 207 | return array(); |
208 | 208 | } |
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 ); |
212 | 212 | return array(); |
213 | 213 | } |
214 | 214 | } |
— | — | @@ -215,158 +215,151 @@ |
216 | 216 | $basehash = ""; |
217 | 217 | $comparehash = ""; |
218 | 218 | |
219 | | - //Only get the part we need |
220 | | - $basefilecontents = cleanupFile($basefilecontents); |
| 219 | + // Only get the part we need |
| 220 | + $basefilecontents = cleanupFile( $basefilecontents ); |
221 | 221 | |
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 ); |
224 | 224 | |
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}')" ); |
232 | 232 | } 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" ); |
235 | 234 | return array(); |
236 | 235 | } |
237 | 236 | } |
238 | 237 | |
239 | | - //Get the array with messages |
240 | | - eval($basefilecontents); |
| 238 | + // Get the array with messages |
| 239 | + eval( $basefilecontents ); |
241 | 240 | |
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)" ); |
247 | 246 | return array(); |
248 | 247 | } |
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 ); |
252 | 251 | return array(); |
253 | 252 | } |
254 | 253 | } |
255 | 254 | |
256 | | - //only get the stuff we need |
257 | | - $comparefilecontents = cleanupFile($comparefilecontents); |
| 255 | + // only get the stuff we need |
| 256 | + $comparefilecontents = cleanupFile( $comparefilecontents ); |
258 | 257 | |
259 | | - //rename the array |
260 | | - $comparefilecontents = preg_replace("/\\\$messages/","\$compare_messages",$comparefilecontents); |
| 258 | + // rename the array |
| 259 | + $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents ); |
261 | 260 | |
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}')" ); |
269 | 268 | } 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" ); |
272 | 270 | return array(); |
273 | 271 | } |
274 | 272 | } |
275 | | - //Get the array |
276 | | - eval($comparefilecontents); |
| 273 | + // Get the array |
| 274 | + eval( $comparefilecontents ); |
277 | 275 | |
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" ); |
283 | 279 | return array(); |
284 | 280 | } |
285 | 281 | |
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 ) ) { |
289 | 285 | $compare_messages[$resObj->identifier] = $resObj->value; |
290 | 286 | } |
291 | 287 | |
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 ); |
294 | 290 | |
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}." ); |
303 | 297 | } |
304 | 298 | |
305 | 299 | return $changedStrings; |
306 | 300 | } |
307 | 301 | |
308 | | - |
309 | | -function saveChanges($changedStrings,$forbiddenKeys,$base_messages,$langcode) { |
| 302 | +function saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode ) { |
310 | 303 | global $verbose; |
311 | 304 | |
312 | | - //Gonna write to the DB again |
| 305 | + // Gonna write to the DB again |
313 | 306 | $db = wfGetDB ( DB_MASTER ); |
314 | 307 | |
315 | | - //Count the updates |
| 308 | + // Count the updates |
316 | 309 | $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!" ); |
326 | 319 | } |
327 | 320 | } |
328 | 321 | |
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]}" ); |
332 | 325 | } |
333 | 326 | |
334 | | - //Update the counter |
| 327 | + // Update the counter |
335 | 328 | $updates++; |
336 | 329 | } |
337 | 330 | } |
338 | 331 | return $updates; |
339 | 332 | } |
340 | 333 | |
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 ); |
345 | 338 | $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] ); |
350 | 343 | |
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 ); |
353 | 346 | return $contents; |
354 | 347 | } |
355 | 348 | |
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 |
358 | 351 | $db = wfGetDB ( DB_MASTER ); |
359 | 352 | $compare_messages = array(); |
360 | 353 | $base_messages = array(); |
361 | 354 | |
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)" ); |
366 | 359 | return 0; |
367 | 360 | } |
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 ); |
371 | 364 | return 0; |
372 | 365 | } |
373 | 366 | } |
— | — | @@ -374,116 +367,112 @@ |
375 | 368 | $basehash = ""; |
376 | 369 | $comparehash = ""; |
377 | 370 | |
378 | | - //Cleanup the file where needed |
379 | | - $basefilecontents = cleanupExtensionFile($basefilecontents); |
| 371 | + // Cleanup the file where needed |
| 372 | + $basefilecontents = cleanupExtensionFile( $basefilecontents ); |
380 | 373 | |
381 | | - //Rename the arrays |
382 | | - $basefilecontents = preg_replace("/\\\$messages/","\$base_messages",$basefilecontents); |
| 374 | + // Rename the arrays |
| 375 | + $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents ); |
383 | 376 | |
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}')" ); |
392 | 385 | } 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" ); |
395 | 387 | return 0; |
396 | 388 | } |
397 | 389 | } |
398 | 390 | |
399 | | - //And get the real contents |
400 | | - eval($basefilecontents); |
| 391 | + // And get the real contents |
| 392 | + eval( $basefilecontents ); |
401 | 393 | |
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)" ); |
407 | 399 | return 0; |
408 | 400 | } |
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 ); |
412 | 404 | return 0; |
413 | 405 | } |
414 | 406 | } |
415 | 407 | |
416 | | - //Only get what we need |
417 | | - $comparefilecontents = cleanupExtensionFile($comparefilecontents); |
| 408 | + // Only get what we need |
| 409 | + $comparefilecontents = cleanupExtensionFile( $comparefilecontents ); |
418 | 410 | |
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}')" ); |
428 | 420 | } 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" ); |
431 | 422 | return 0; |
432 | 423 | } |
433 | 424 | } |
434 | | - //Get the real array |
435 | | - eval($comparefilecontents); |
| 425 | + // Get the real array |
| 426 | + eval( $comparefilecontents ); |
436 | 427 | |
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" ); |
441 | 431 | return 0; |
442 | 432 | } |
443 | 433 | |
444 | | - //Update counter |
| 434 | + // Update counter |
445 | 435 | $updates = 0; |
446 | 436 | |
447 | | - if(empty($base_messages['en'])) { |
| 437 | + if ( empty( $base_messages['en'] ) ) { |
448 | 438 | $base_messages['en'] = array(); |
449 | 439 | } |
450 | 440 | |
451 | | - if(empty($compare_messages['en'])) { |
| 441 | + if ( empty( $compare_messages['en'] ) ) { |
452 | 442 | $compare_messages['en'] = array(); |
453 | 443 | } |
454 | 444 | |
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'] ); |
457 | 447 | |
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 |
461 | 451 | continue; |
462 | 452 | } |
463 | 453 | |
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 ) ) { |
467 | 457 | $compare_messages[$language][$resObj->identifier] = $resObj->value; |
468 | 458 | } |
469 | 459 | |
470 | | - if(empty($compare_messages[$language])) { |
| 460 | + if ( empty( $compare_messages[$language] ) ) { |
471 | 461 | $compare_messages[$language] = array(); |
472 | 462 | } |
473 | 463 | |
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] ); |
476 | 466 | |
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 ); |
482 | 472 | } |
483 | 473 | } |
484 | 474 | |
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" ); |
488 | 477 | |
489 | 478 | return $updates; |
490 | 479 | } |
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.update.php |
— | — | @@ -1,50 +1,50 @@ |
2 | 2 | <?php |
3 | | -function myLog($log){ |
| 3 | +function myLog( $log ) { |
4 | 4 | if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) { |
5 | | - print($log."<br />"); |
| 5 | + print( $log . "<br />" ); |
6 | 6 | } else { |
7 | | - print($log."\n"); |
| 7 | + print( $log . "\n" ); |
8 | 8 | } |
9 | 9 | } |
10 | 10 | |
11 | 11 | $verbose = false; |
12 | 12 | |
13 | | -if(($argc > 0 && $argv[1] == "verbose") OR (isset($_GET['verbose']))) |
| 13 | +if ( ( $argc > 0 && $argv[1] == "verbose" ) OR ( isset( $_GET['verbose'] ) ) ) |
14 | 14 | $verbose = true; |
15 | | - |
| 15 | + |
16 | 16 | $mtime = microtime(); |
17 | | -$mtime = explode(" ",$mtime); |
| 17 | +$mtime = explode( " ", $mtime ); |
18 | 18 | $mtime = $mtime[1] + $mtime[0]; |
19 | | -$starttime = $mtime; |
| 19 | +$starttime = $mtime; |
20 | 20 | |
21 | | -define("MEDIAWIKI",true); |
| 21 | +define( "MEDIAWIKI", true ); |
22 | 22 | |
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__ ) . "/../../" ); |
26 | 26 | |
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" ); |
29 | 29 | } else { |
30 | | - require_once( $IP."/includes/ProfilerStub.php" ); |
| 30 | + require_once( $IP . "/includes/ProfilerStub.php" ); |
31 | 31 | } |
32 | 32 | |
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" ); |
36 | 36 | |
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" ); |
39 | 39 | |
40 | 40 | |
41 | | -if(is_callable("updateMessages")) |
42 | | - updateMessages($verbose); |
| 41 | +if ( is_callable( "updateMessages" ) ) |
| 42 | + updateMessages( $verbose ); |
43 | 43 | 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" ); |
45 | 45 | |
46 | 46 | $mtime = microtime(); |
47 | | -$mtime = explode(" ",$mtime); |
| 47 | +$mtime = explode( " ", $mtime ); |
48 | 48 | $mtime = $mtime[1] + $mtime[0]; |
49 | 49 | $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" ); |