Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php |
— | — | @@ -1,4 +1,10 @@ |
2 | 2 | <?php |
| 3 | + |
| 4 | +/** |
| 5 | + * Class for localization updates. |
| 6 | + * |
| 7 | + * TODO: refactor code to remove duplication |
| 8 | + */ |
3 | 9 | class LocalisationUpdate { |
4 | 10 | |
5 | 11 | private static $newHashes = null; |
— | — | @@ -212,10 +218,45 @@ |
213 | 219 | // Return the cleaned up file. |
214 | 220 | return $contents; |
215 | 221 | } |
| 222 | + |
| 223 | + /** |
| 224 | + * Removes all unneeded content from a file and returns it. |
| 225 | + * |
| 226 | + * FIXME: duplicated cleanupFile code |
| 227 | + * |
| 228 | + * @param $contents String |
| 229 | + * |
| 230 | + * @return string |
| 231 | + */ |
| 232 | + public static function cleanupExtensionFile( $contents ) { |
| 233 | + // We don't want PHP tags. |
| 234 | + $contents = preg_replace( "/<\?php/", "", $contents ); |
| 235 | + $contents = preg_replace( "/\?" . ">/", "", $contents ); |
| 236 | + |
| 237 | + $results = array(); |
| 238 | + |
| 239 | + // And we only want message arrays. |
| 240 | + preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results ); |
| 241 | + |
| 242 | + // But we want them all in one string. |
| 243 | + if( !empty( $results[0] ) && is_array( $results[0] ) ) { |
| 244 | + $contents = implode( "\n\n", $results[0] ); |
| 245 | + } else { |
| 246 | + $contents = ''; |
| 247 | + } |
216 | 248 | |
| 249 | + // And we hate the windows vs linux linebreaks. |
| 250 | + $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents ); |
| 251 | + |
| 252 | + return $contents; |
| 253 | + } |
| 254 | + |
217 | 255 | /** |
| 256 | + * Returns the contents of a file or false on failiure. |
218 | 257 | * |
219 | 258 | * @param $basefile String |
| 259 | + * |
| 260 | + * @return string or false |
220 | 261 | */ |
221 | 262 | public static function getFileContents( $basefile ) { |
222 | 263 | global $wgLocalisationUpdateRetryAttempts; |
— | — | @@ -249,24 +290,39 @@ |
250 | 291 | return $basefilecontents; |
251 | 292 | } |
252 | 293 | |
253 | | - public static function compareFiles( $basefile, $comparefile, $verbose, $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) { |
| 294 | + /** |
| 295 | + * Returns an array containing the differences between the files. |
| 296 | + * |
| 297 | + * @param $basefile String |
| 298 | + * @param $comparefile String |
| 299 | + * @param $verbose Boolean |
| 300 | + * @param $forbiddenKeys Array |
| 301 | + * @param $alwaysGetResult Boolean |
| 302 | + * @param $saveResults Boolean |
| 303 | + * |
| 304 | + * @return array |
| 305 | + */ |
| 306 | + public static function compareFiles( $basefile, $comparefile, $verbose, array $forbiddenKeys = array(), $alwaysGetResult = true, $saveResults = false ) { |
254 | 307 | $compare_messages = array(); |
255 | 308 | $base_messages = array(); |
256 | 309 | |
257 | | - // Get the languagecode |
| 310 | + // Get the languagecode. |
258 | 311 | $langcode = Language::getCodeFromFileName( $basefile, 'Messages' ); |
259 | 312 | |
260 | 313 | $basefilecontents = self::getFileContents( $basefile ); |
261 | | - if ( $basefilecontents === false || $basefilecontents === "" ) return array(); // Failed |
| 314 | + |
| 315 | + if ( $basefilecontents === false || $basefilecontents === '' ) { |
| 316 | + return array(); // Failed |
| 317 | + } |
262 | 318 | |
263 | | - // Only get the part we need |
| 319 | + // Only get the part we need. |
264 | 320 | $basefilecontents = self::cleanupFile( $basefilecontents ); |
265 | 321 | |
266 | | - // Change the variable name |
| 322 | + // Change the variable name. |
267 | 323 | $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents ); |
268 | | - |
269 | 324 | $basehash = md5( $basefilecontents ); |
270 | | - // Check if the file has changed since our last update |
| 325 | + |
| 326 | + // Check if the file has changed since our last update. |
271 | 327 | if ( !$alwaysGetResult ) { |
272 | 328 | if ( !self::checkHash( $basefile, $basehash ) ) { |
273 | 329 | self::myLog( "Skipping {$langcode} since the remote file hasn't changed since our last update" ); |
— | — | @@ -274,52 +330,57 @@ |
275 | 331 | } |
276 | 332 | } |
277 | 333 | |
278 | | - // Get the array with messages |
| 334 | + // Get the array with messages. |
279 | 335 | $base_messages = self::parsePHP( $basefilecontents, 'base_messages' ); |
280 | 336 | |
281 | 337 | $comparefilecontents = self::getFileContents( $comparefile ); |
282 | | - if ( $comparefilecontents === false || $comparefilecontents === "" ) return array(); // Failed |
| 338 | + |
| 339 | + if ( $comparefilecontents === false || $comparefilecontents === '' ) { |
| 340 | + return array(); // Failed |
| 341 | + } |
283 | 342 | |
284 | | - // only get the stuff we need |
| 343 | + // Only get the stuff we need. |
285 | 344 | $comparefilecontents = self::cleanupFile( $comparefilecontents ); |
286 | 345 | |
287 | | - // rename the array |
| 346 | + // Rename the array. |
288 | 347 | $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents ); |
289 | | - |
290 | 348 | $comparehash = md5( $comparefilecontents ); |
291 | | - // If this is the remote file check if the file has changed since our last update |
| 349 | + |
| 350 | + // If this is the remote file check if the file has changed since our last update. |
292 | 351 | if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) { |
293 | 352 | if ( !self::checkHash( $comparefile, $comparehash ) ) { |
294 | 353 | self::myLog( "Skipping {$langcode} since the remote file has not changed since our last update" ); |
295 | 354 | return array(); |
296 | 355 | } |
297 | 356 | } |
298 | | - // Get the array |
| 357 | + |
| 358 | + // Get the array. |
299 | 359 | $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' ); |
300 | 360 | |
301 | | - // if the localfile and the remote file are the same, skip them! |
| 361 | + // If the localfile and the remote file are the same, skip them! |
302 | 362 | if ( $basehash == $comparehash && !$alwaysGetResult ) { |
303 | 363 | self::myLog( "Skipping {$langcode} since the remote file is the same as the local file" ); |
304 | 364 | return array(); |
305 | 365 | } |
306 | 366 | |
307 | | - // Add the messages we got with our previous update(s) to the local array (as we already got these as well) |
308 | | - $compare_messages = array_merge( $compare_messages, |
309 | | - self::readFile( $langcode ) ); |
| 367 | + // Add the messages we got with our previous update(s) to the local array (as we already got these as well). |
| 368 | + $compare_messages = array_merge( |
| 369 | + $compare_messages, |
| 370 | + self::readFile( $langcode ) |
| 371 | + ); |
310 | 372 | |
311 | | - // Compare the remote and local message arrays |
| 373 | + // Compare the remote and local message arrays. |
312 | 374 | $changedStrings = array_diff_assoc( $base_messages, $compare_messages ); |
313 | 375 | |
314 | | - // If we want to save the differences |
315 | | - if ( $saveResults && !empty($changedStrings) && is_array($changedStrings)) { |
| 376 | + // If we want to save the differences. |
| 377 | + if ( $saveResults && !empty( $changedStrings ) && is_array( $changedStrings ) ) { |
316 | 378 | self::myLog( "--Checking languagecode {$langcode}--" ); |
317 | | - // The save them |
| 379 | + // Save the differences. |
318 | 380 | $updates = self::saveChanges( $changedStrings, $forbiddenKeys, $compare_messages, $base_messages, $langcode, $verbose ); |
319 | 381 | self::myLog( "{$updates} messages updated for {$langcode}." ); |
320 | 382 | } elseif ( $saveResults ) { |
321 | 383 | self::myLog( "--{$langcode} hasn't changed--" ); |
322 | 384 | } |
323 | | - |
324 | 385 | |
325 | 386 | self::saveHash( $basefile, $basehash ); |
326 | 387 | |
— | — | @@ -329,10 +390,13 @@ |
330 | 391 | } |
331 | 392 | |
332 | 393 | /** |
333 | | - * Checks whether a messages file has a certain hash |
| 394 | + * Checks whether a messages file has a certain hash. |
| 395 | + * |
334 | 396 | * TODO: Swap return values, this is insane |
| 397 | + * |
335 | 398 | * @param $file string Filename |
336 | 399 | * @param $hash string Hash |
| 400 | + * |
337 | 401 | * @return bool True if $file does NOT have hash $hash, false if it does |
338 | 402 | */ |
339 | 403 | public static function checkHash( $file, $hash ) { |
— | — | @@ -341,8 +405,10 @@ |
342 | 406 | } |
343 | 407 | |
344 | 408 | public static function saveHash( $file, $hash ) { |
345 | | - if ( is_null( self::$newHashes ) ) |
| 409 | + if ( is_null( self::$newHashes ) ) { |
346 | 410 | self::$newHashes = self::readFile( 'hashes' ); |
| 411 | + } |
| 412 | + |
347 | 413 | self::$newHashes[$file] = $hash; |
348 | 414 | } |
349 | 415 | |
— | — | @@ -350,68 +416,78 @@ |
351 | 417 | self::writeFile( 'hashes', self::$newHashes ); |
352 | 418 | } |
353 | 419 | |
354 | | - public static function saveChanges( $changedStrings, $forbiddenKeys, $compare_messages, $base_messages, $langcode, $verbose ) { |
355 | | - // Count the updates |
| 420 | + /** |
| 421 | + * |
| 422 | + * |
| 423 | + * @param $changedStrings Array |
| 424 | + * @param $forbiddenKeys Array |
| 425 | + * @param $compare_messages Array |
| 426 | + * @param $base_messages Array |
| 427 | + * @param $langcode String |
| 428 | + * @param $verbose Boolean |
| 429 | + * |
| 430 | + * @return Integer: the amount of updated messages |
| 431 | + */ |
| 432 | + public static function saveChanges( $changedStrings, array $forbiddenKeys, array $compare_messages, array $base_messages, $langcode, $verbose ) { |
| 433 | + // Count the updates. |
356 | 434 | $updates = 0; |
357 | | - if(!is_array($changedStrings)) { |
358 | | - self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file ".(__FILE__)." at line ".(__LINE__)); |
| 435 | + |
| 436 | + if( !is_array( $changedStrings ) ) { |
| 437 | + self::myLog("CRITICAL ERROR: \$changedStrings is not an array in file " . (__FILE__) . ' at line ' .( __LINE__ ) ); |
359 | 438 | return 0; |
360 | 439 | } |
361 | 440 | |
362 | 441 | $new_messages = self::readFile( $langcode ); |
| 442 | + |
363 | 443 | foreach ( $changedStrings as $key => $value ) { |
364 | | - // If this message wasn't changed in English |
| 444 | + // If this message wasn't changed in English. |
365 | 445 | if ( !isset( $forbiddenKeys[$key] ) ) { |
366 | 446 | $new_messages[$key] = $base_messages[$key]; |
367 | 447 | |
368 | | - // Output extra logmessages when needed |
| 448 | + // Output extra logmessages when needed. |
369 | 449 | if ( $verbose ) { |
370 | 450 | $oldmsg = isset( $compare_messages[$key] ) ? "'{$compare_messages[$key]}'" : 'not set'; |
371 | 451 | self::myLog( "Updated message {$key} from $oldmsg to '{$base_messages[$key]}'" ); |
372 | 452 | } |
373 | 453 | |
374 | | - // Update the counter |
| 454 | + // Update the counter. |
375 | 455 | $updates++; |
376 | 456 | } |
377 | 457 | } |
378 | 458 | self::writeFile( $langcode, $new_messages ); |
| 459 | + |
379 | 460 | return $updates; |
380 | 461 | } |
381 | 462 | |
382 | | - public static function cleanupExtensionFile( $contents ) { |
383 | | - // We don't want PHP tags |
384 | | - $contents = preg_replace( "/<\?php/", "", $contents ); |
385 | | - $contents = preg_replace( "/\?" . ">/", "", $contents ); |
386 | | - $results = array(); |
387 | | - // And we only want message arrays |
388 | | - preg_match_all( "/\\\$messages(.*\s)*?\);/", $contents, $results ); |
389 | | - // But we want them all in one string |
390 | | - if(!empty($results[0]) && is_array($results[0])) { |
391 | | - $contents = implode( "\n\n", $results[0] ); |
392 | | - } else { |
393 | | - $contents = ""; |
394 | | - } |
395 | | - |
396 | | - // And we hate the windows vs linux linebreaks |
397 | | - $contents = preg_replace( "/\\\r\\\n?/", "\n", $contents ); |
398 | | - return $contents; |
399 | | - } |
400 | | - |
| 463 | + /** |
| 464 | + * |
| 465 | + * @param $extension String |
| 466 | + * @param $basefile String |
| 467 | + * @param $comparefile String |
| 468 | + * @param $verbose Boolean |
| 469 | + * @param $alwaysGetResult Boolean |
| 470 | + * @param $saveResults Boolean |
| 471 | + * |
| 472 | + * @return Integer: the amount of updated messages |
| 473 | + */ |
401 | 474 | public static function compareExtensionFiles( $extension, $basefile, $comparefile, $verbose, $alwaysGetResult = true, $saveResults = false ) { |
402 | 475 | // FIXME: Factor out duplicated code? |
403 | 476 | $compare_messages = array(); |
404 | 477 | $base_messages = array(); |
405 | 478 | |
406 | 479 | $basefilecontents = self::getFileContents( $basefile ); |
407 | | - if ( $basefilecontents === false || $basefilecontents === "" ) return 0; // Failed |
| 480 | + |
| 481 | + if ( $basefilecontents === false || $basefilecontents === '' ) { |
| 482 | + return 0; // Failed |
| 483 | + } |
408 | 484 | |
409 | | - // Cleanup the file where needed |
| 485 | + // Cleanup the file where needed. |
410 | 486 | $basefilecontents = self::cleanupExtensionFile( $basefilecontents ); |
411 | 487 | |
412 | | - // Rename the arrays |
| 488 | + // Rename the arrays. |
413 | 489 | $basefilecontents = preg_replace( "/\\\$messages/", "\$base_messages", $basefilecontents ); |
414 | | - |
415 | 490 | $basehash = md5( $basefilecontents ); |
| 491 | + |
416 | 492 | // If this is the remote file |
417 | 493 | if ( preg_match( "/^http/", $basefile ) && !$alwaysGetResult ) { |
418 | 494 | // Check if the hash has changed |
— | — | @@ -425,14 +501,18 @@ |
426 | 502 | $base_messages = self::parsePHP( $basefilecontents, 'base_messages' ); |
427 | 503 | |
428 | 504 | $comparefilecontents = self::getFileContents( $comparefile ); |
429 | | - if ( $comparefilecontents === false || $comparefilecontents === "" ) return 0; // Failed |
| 505 | + |
| 506 | + if ( $comparefilecontents === false || $comparefilecontents === '' ) { |
| 507 | + return 0; // Failed |
| 508 | + } |
430 | 509 | |
431 | | - // Only get what we need |
| 510 | + // Only get what we need. |
432 | 511 | $comparefilecontents = self::cleanupExtensionFile( $comparefilecontents ); |
433 | 512 | |
434 | | - // Rename the array |
| 513 | + // Rename the array. |
435 | 514 | $comparefilecontents = preg_replace( "/\\\$messages/", "\$compare_messages", $comparefilecontents ); |
436 | 515 | $comparehash = md5( $comparefilecontents ); |
| 516 | + |
437 | 517 | if ( preg_match( "/^http/", $comparefile ) && !$alwaysGetResult ) { |
438 | 518 | // Check if the remote file has changed |
439 | 519 | if ( !self::checkHash( $comparefile, $comparehash ) ) { |
— | — | @@ -440,16 +520,17 @@ |
441 | 521 | return 0; |
442 | 522 | } |
443 | 523 | } |
444 | | - // Get the real array |
| 524 | + |
| 525 | + // Get the real array. |
445 | 526 | $compare_messages = self::parsePHP( $comparefilecontents, 'compare_messages' ); |
446 | 527 | |
447 | | - // If both files are the same, they can be skipped |
| 528 | + // If both files are the same, they can be skipped. |
448 | 529 | if ( $basehash == $comparehash && !$alwaysGetResult ) { |
449 | 530 | self::myLog( "Skipping {$extension} since the remote file is the same as the local file" ); |
450 | 531 | return 0; |
451 | 532 | } |
452 | 533 | |
453 | | - // Update counter |
| 534 | + // Update counter. |
454 | 535 | $updates = 0; |
455 | 536 | |
456 | 537 | if ( !is_array( $base_messages ) ) { |
— | — | @@ -468,28 +549,29 @@ |
469 | 550 | $compare_messages['en'] = array(); |
470 | 551 | } |
471 | 552 | |
472 | | - // Find the changed english strings |
| 553 | + // Find the changed english strings. |
473 | 554 | $forbiddenKeys = array_diff_assoc( $base_messages['en'], $compare_messages['en'] ); |
474 | 555 | |
475 | | - // Do an update for each language |
| 556 | + // Do an update for each language. |
476 | 557 | foreach ( $base_messages as $language => $messages ) { |
477 | | - if ( $language == "en" ) { // Skip english |
| 558 | + if ( $language == 'en' ) { // Skip english. |
478 | 559 | continue; |
479 | 560 | } |
480 | 561 | |
481 | | - // Add the already known messages to the array so we will only find new changes |
| 562 | + // Add the already known messages to the array so we will only find new changes. |
482 | 563 | $compare_messages[$language] = array_merge( |
483 | 564 | $compare_messages[$language], |
484 | | - self::readFile( $language ) ); |
| 565 | + self::readFile( $language ) |
| 566 | + ); |
485 | 567 | |
486 | 568 | if ( empty( $compare_messages[$language] ) || !is_array( $compare_messages[$language] ) ) { |
487 | 569 | $compare_messages[$language] = array(); |
488 | 570 | } |
489 | 571 | |
490 | | - // Get the array of changed strings |
| 572 | + // Get the array of changed strings. |
491 | 573 | $changedStrings = array_diff_assoc( $messages, $compare_messages[$language] ); |
492 | 574 | |
493 | | - // If we want to save the changes |
| 575 | + // If we want to save the changes. |
494 | 576 | if ( $saveResults === true && !empty( $changedStrings ) && is_array( $changedStrings ) ) { |
495 | 577 | self::myLog( "--Checking languagecode {$language}--" ); |
496 | 578 | // The save them |
— | — | @@ -500,7 +582,7 @@ |
501 | 583 | } |
502 | 584 | } |
503 | 585 | |
504 | | - // And log some stuff |
| 586 | + // And log some stuff. |
505 | 587 | self::myLog( "Updated " . $updates . " messages for the '{$extension}' extension" ); |
506 | 588 | |
507 | 589 | self::saveHash( $basefile, $basehash ); |
— | — | @@ -510,6 +592,11 @@ |
511 | 593 | return $updates; |
512 | 594 | } |
513 | 595 | |
| 596 | + /** |
| 597 | + * Logs a message. |
| 598 | + * |
| 599 | + * @param $log String |
| 600 | + */ |
514 | 601 | public static function myLog( $log ) { |
515 | 602 | if ( isset( $_SERVER ) && array_key_exists( 'REQUEST_METHOD', $_SERVER ) ) { |
516 | 603 | wfDebug( $log . "\n" ); |