Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.php |
— | — | @@ -22,9 +22,12 @@ |
23 | 23 | ); |
24 | 24 | |
25 | 25 | // Use the right hook |
26 | | -$wgHooks['MessageNotInMwNs'][] = "LocalisationUpdate::FindUpdatedMessage"; |
| 26 | +$wgHooks['LocalisationCacheRecache'][] = 'LocalisationUpdate::onRecache'; // MW 1.16+ |
27 | 27 | |
28 | 28 | $dir = dirname( __FILE__ ) . '/'; |
29 | 29 | $wgExtensionMessagesFiles['LocalisationUpdate'] = $dir . 'LocalisationUpdate.i18n.php'; |
30 | 30 | $wgAutoloadClasses['LocalisationUpdate'] = $dir . 'LocalisationUpdate.class.php'; |
| 31 | +$wgAutoloadClasses['LUDependency'] = $dir . 'LocalisationUpdate.class.php'; |
| 32 | + |
31 | 33 | $wgHooks['LoadExtensionSchemaUpdates'][] = 'LocalisationUpdate::schemaUpdates'; |
| 34 | + |
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php |
— | — | @@ -1,31 +1,19 @@ |
2 | 2 | <?php |
3 | 3 | class LocalisationUpdate { |
4 | 4 | // DB Search funtion |
5 | | - public static function FindUpdatedMessage( &$message, $lckey, $langcode, $isFullKey ) { |
6 | | - // Define a cache |
7 | | - static $cache = array(); |
8 | | - $db = wfGetDB ( DB_SLAVE ); |
| 5 | + public static function onRecache( $lc, $code, &$data ) { |
| 6 | + $dbr = wfGetDB ( DB_SLAVE ); |
9 | 7 | |
10 | | - // If the key also contains the language code remove the language code from the key |
11 | | - if ( $isFullKey ) { |
12 | | - $lckey = preg_replace( "/\/" . $langcode . "/", "", $lckey ); |
13 | | - } |
| 8 | + // Get the messages from the database |
| 9 | + $res = $dbr->selectField( 'localisation', |
| 10 | + array( 'lo_key', 'lo_value' ), |
| 11 | + array( 'lo_language' => $langcode ), |
| 12 | + __METHOD__ ); |
14 | 13 | |
15 | | - // If message is in the cache, don't get an update! |
16 | | - if ( array_key_exists( $lckey . "/" . $langcode, $cache ) ) { |
17 | | - $message = $cache[$lckey . "/" . $langcode]; |
18 | | - return true; |
| 14 | + foreach ( $res as $row ) { |
| 15 | + $cache['messages'][$row->lo_key] = $row->lo_value; |
19 | 16 | } |
20 | | - |
21 | | - // Get the message from the database |
22 | | - $conds = array( 'lo_key' => $lckey, 'lo_language' => $langcode ); |
23 | | - $result = $db->selectField( 'localisation', 'lo_value', $conds, __METHOD__ ); // Check if the database has any updated message |
24 | | - if ( $result === false ) { // If no results found, exit here |
25 | | - return true; |
26 | | - } |
27 | | - |
28 | | - $message = $result; |
29 | | - $cache[$lckey . "/" . $langcode] = $result; // Update the cache |
| 17 | + $cache['deps'][] = new LUDependency; |
30 | 18 | return true; |
31 | 19 | } |
32 | 20 | |
— | — | @@ -489,5 +477,35 @@ |
490 | 478 | $vars = $ce->getVars(); |
491 | 479 | return @$vars[$varname]; |
492 | 480 | } |
| 481 | +} |
493 | 482 | |
494 | | -} |
\ No newline at end of file |
| 483 | +class LUDependency extends CacheDependency { |
| 484 | + var $hashes; |
| 485 | + |
| 486 | + function isExpired() { |
| 487 | + # FIXME: make this faster by changing the schema to include a last update timestamp |
| 488 | + $hashes = $this->getHashesFromDB(); |
| 489 | + return $hashes !== $this->hashes; |
| 490 | + } |
| 491 | + |
| 492 | + function loadDependencyValues() { |
| 493 | + $this->hashes = $this->getHashesFromDB(); |
| 494 | + } |
| 495 | + |
| 496 | + function getHashesFromDB() { |
| 497 | + $dbr = wfGetDB( DB_SLAVE ); |
| 498 | + $res = $dbr->select( |
| 499 | + 'localisation_file_hash', '*', false, __METHOD__, |
| 500 | + array( 'ORDER BY' => 'lfh_file' ) ); |
| 501 | + $hashes = ''; |
| 502 | + foreach ( $res as $row ) { |
| 503 | + $hashes .= "{$row->lfh_file} {$row->lfh_hash}\n"; |
| 504 | + } |
| 505 | + return $hashes; |
| 506 | + } |
| 507 | + |
| 508 | + function __sleep() { |
| 509 | + $this->loadDependencyValues(); |
| 510 | + return array( 'hashes' ); |
| 511 | + } |
| 512 | +} |