Index: trunk/extensions/LocalisationUpdate/patch-lfh_timestamp.sql |
— | — | @@ -0,0 +1,4 @@ |
| 2 | +-- Patch that adds lfh_timestamp field and index
|
| 3 | +
|
| 4 | +ALTER TABLE /*_*/localisation_file_hash ADD lfh_timestamp varchar(14) NOT NULL;
|
| 5 | +CREATE INDEX /*i*/lfh_timestamp ON /*_*/localisation_file_hash (lfh_timestamp);
|
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.i18n.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | * @file |
7 | 7 | * @ingroup Extensions |
8 | 8 | */ |
9 | | - |
| 9 | + |
10 | 10 | $messages = array(); |
11 | 11 | |
12 | 12 | /** English |
Index: trunk/extensions/LocalisationUpdate/LocalisationUpdate.class.php |
— | — | @@ -5,7 +5,7 @@ |
6 | 6 | $dbr = wfGetDB ( DB_SLAVE ); |
7 | 7 | |
8 | 8 | // Get the messages from the database |
9 | | - $res = $dbr->selectField( 'localisation', |
| 9 | + $res = $dbr->select( 'localisation', |
10 | 10 | array( 'lo_key', 'lo_value' ), |
11 | 11 | array( 'lo_language' => $langcode ), |
12 | 12 | __METHOD__ ); |
— | — | @@ -236,12 +236,12 @@ |
237 | 237 | $changedStrings = array_diff_assoc( $base_messages, $compare_messages ); |
238 | 238 | |
239 | 239 | // If we want to save the differences |
240 | | - if ( $saveResults === true && !empty($changedStrings) && is_array($changedStrings)) { |
| 240 | + if ( $saveResults && !empty($changedStrings) && is_array($changedStrings)) { |
241 | 241 | self::myLog( "--Checking languagecode {$langcode}--" ); |
242 | 242 | // The save them |
243 | 243 | $updates = self::saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode, $verbose ); |
244 | 244 | self::myLog( "{$updates} messages updated for {$langcode}." ); |
245 | | - } elseif($saveResults === true) { |
| 245 | + } elseif ( $saveResults ) { |
246 | 246 | self::myLog( "--{$langcode} hasn't changed--" ); |
247 | 247 | } |
248 | 248 | |
— | — | @@ -257,6 +257,13 @@ |
258 | 258 | return $changedStrings; |
259 | 259 | } |
260 | 260 | |
| 261 | + /** |
| 262 | + * Checks whether a messages file has a certain hash |
| 263 | + * TODO: Swap return values, this is insane |
| 264 | + * @param $file string Filename |
| 265 | + * @param $hash string Hash |
| 266 | + * @return bool True if $file does NOT have hash $hash, false if it does |
| 267 | + */ |
261 | 268 | public static function checkHash( $file, $hash ) { |
262 | 269 | $db = wfGetDB( DB_MASTER ); |
263 | 270 | |
— | — | @@ -271,10 +278,15 @@ |
272 | 279 | |
273 | 280 | public static function saveHash ($file, $hash) { |
274 | 281 | $db = wfGetDB ( DB_MASTER ); |
275 | | - $hashConds = array( 'lfh_file' => $file, 'lfh_hash' => $hash ); |
276 | | - $conds = array( 'lfh_file' => $file ); |
277 | | - $db->delete( 'localisation_file_hash', $conds , __METHOD__ ); |
278 | | - $db->insert( 'localisation_file_hash', $hashConds, __METHOD__ ); |
| 282 | + // Double query sucks but we wanna make sure we don't update |
| 283 | + // the timestamp when the hash hasn't changed |
| 284 | + if ( self::checkHash( $file, $hash ) ) |
| 285 | + $db->replace( 'localisation_file_hash', array( 'lfh_file' ), array( |
| 286 | + 'lfh_file' => $file, |
| 287 | + 'lfh_hash' => $hash, |
| 288 | + 'lfh_timestamp' => $db->timestamp( wfTimestamp() ) |
| 289 | + ), __METHOD__ |
| 290 | + ); |
279 | 291 | } |
280 | 292 | |
281 | 293 | public static function saveChanges( $changedStrings, $forbiddenKeys, $base_messages, $langcode, $verbose ) { |
— | — | @@ -458,9 +470,10 @@ |
459 | 471 | } |
460 | 472 | |
461 | 473 | public static function schemaUpdates() { |
462 | | - global $wgExtNewTables; |
| 474 | + global $wgExtNewTables, $wgExtNewFields; |
463 | 475 | $dir = dirname( __FILE__ ); |
464 | 476 | $wgExtNewTables[] = array( 'localisation', "$dir/schema.sql" ); |
| 477 | + $wgExtNewFields[] = array( 'localisation_file_hash', 'lfh_timestamp', "$dir/patch-lfh_timestamp.sql" ); |
465 | 478 | return true; |
466 | 479 | } |
467 | 480 | |
— | — | @@ -480,32 +493,26 @@ |
481 | 494 | } |
482 | 495 | |
483 | 496 | class LUDependency extends CacheDependency { |
484 | | - var $hashes; |
| 497 | + var $timestamp; |
485 | 498 | |
486 | 499 | 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; |
| 500 | + $timestamp = $this->getTimestamp(); |
| 501 | + return $timestamp !== $this->timestamp; |
490 | 502 | } |
491 | 503 | |
492 | 504 | function loadDependencyValues() { |
493 | | - $this->hashes = $this->getHashesFromDB(); |
| 505 | + $this->timestamp = $this->getTimestamp(); |
494 | 506 | } |
495 | 507 | |
496 | | - function getHashesFromDB() { |
| 508 | + function getTimestamp() { |
497 | 509 | $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; |
| 510 | + return $dbr->selectField( |
| 511 | + 'localisation_file_hash', 'MAX(lfh_timestamp)', '', |
| 512 | + __METHOD__ ); |
506 | 513 | } |
507 | 514 | |
508 | 515 | function __sleep() { |
509 | 516 | $this->loadDependencyValues(); |
510 | | - return array( 'hashes' ); |
| 517 | + return array( 'timestamp' ); |
511 | 518 | } |
512 | 519 | } |
Index: trunk/extensions/LocalisationUpdate/schema.sql |
— | — | @@ -9,10 +9,13 @@ |
10 | 10 | |
11 | 11 | CREATE TABLE /*$wgDBprefix*/localisation_file_hash ( |
12 | 12 | lfh_file varchar(250) NOT NULL, |
13 | | - lfh_hash varchar(50) NOT NULL |
| 13 | + lfh_hash varchar(50) NOT NULL, |
| 14 | + lfh_timestamp varchar(14) NOT NULL |
14 | 15 | ) /*$wgDBTableOptions*/; |
15 | 16 | |
16 | 17 | CREATE UNIQUE INDEX /*i*/lfh_file ON |
17 | 18 | /*$wgDBprefix*/localisation_file_hash (lfh_file); |
18 | 19 | CREATE INDEX /*i*/lfh_file_hash ON |
19 | | - /*$wgDBprefix*/localisation_file_hash (lfh_file, lfh_hash); |
\ No newline at end of file |
| 20 | + /*$wgDBprefix*/localisation_file_hash (lfh_file, lfh_hash); |
| 21 | +CREATE INDEX /*i*/lfh_timestamp ON |
| 22 | + /*$wgDBprefix*/localisation_file_hash (lfh_timestamp); |
\ No newline at end of file |