Index: trunk/extensions/Translate/SpecialManageGroups.php |
— | — | @@ -96,7 +96,7 @@ |
97 | 97 | $wgLang->time( $timestamp ) |
98 | 98 | ); |
99 | 99 | |
100 | | - if ( $this->changedSinceCached( $group ) ) { |
| 100 | + if ( !$cache->isValid() ) { |
101 | 101 | $out = '<span style="color:red">!!</span> ' . $out; |
102 | 102 | } |
103 | 103 | |
— | — | @@ -365,7 +365,9 @@ |
366 | 366 | continue; |
367 | 367 | } |
368 | 368 | |
369 | | - if ( !$this->changedSinceCached( $group, $code ) ) { |
| 369 | + $cache = new MessageGroupCache( $group, $code ); |
| 370 | + |
| 371 | + if ( $cache->isValid() ) { |
370 | 372 | continue; |
371 | 373 | } |
372 | 374 | |
— | — | @@ -376,7 +378,6 @@ |
377 | 379 | array( 'group' => $group->getId(), 'language' => $code ) |
378 | 380 | ); |
379 | 381 | |
380 | | - $cache = new MessageGroupCache( $group, $code ); |
381 | 382 | if ( !$cache->exists() ) { |
382 | 383 | $modified[] = wfMsgHtml( 'translate-manage-modlang-new', $link ); |
383 | 384 | } else { |
— | — | @@ -457,32 +458,4 @@ |
458 | 459 | $this->out->setSubtitle( implode( ' > ', $links ) ); |
459 | 460 | } |
460 | 461 | |
461 | | - /** |
462 | | - * Checks if the source file has changed since last check. |
463 | | - * Uses modification timestamps and file hashes to check. |
464 | | - */ |
465 | | - protected function changedSinceCached( $group, $code = 'en' ) { |
466 | | - $cache = new MessageGroupCache( $group, $code ); |
467 | | - $filename = $group->getSourceFilePath( $code ); |
468 | | - |
469 | | - $mtime = file_exists( $filename ) ? filemtime( $filename ) : false; |
470 | | - $cachetime = $cache->exists() ? $cache->getTimestamp() : false; |
471 | | - |
472 | | - // No such language at all, or cache is up to date |
473 | | - if ( $mtime <= $cachetime ) { |
474 | | - return false; |
475 | | - } |
476 | | - |
477 | | - // Timestamps differ (or either cache or the file does not exists) |
478 | | - $oldhash = $cache->exists() ? $cache->getHash() : false; |
479 | | - $newhash = file_exists( $filename ) ? md5( file_get_contents( $filename ) ) : false; |
480 | | - wfDebugLog( 'translate-manage', "$mtime === $cachetime | $code | $oldhash !== $newhash\n" ); |
481 | | - if ( $newhash === $oldhash ) { |
482 | | - // Update cache so that we don't need to compare hashes next time |
483 | | - $cache->create(); |
484 | | - return false; |
485 | | - } |
486 | | - |
487 | | - return true; |
488 | | - } |
489 | 462 | } |
Index: trunk/extensions/Translate/utils/MessageGroupCache.php |
— | — | @@ -1,14 +1,17 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * @todo Needs documentation. |
| 4 | + * Code for caching the messages of file based message groups. |
5 | 5 | * @file |
6 | 6 | * @author Niklas Laxström |
7 | | - * @copyright Copyright © 2009 Niklas Laxström |
| 7 | + * @copyright Copyright © 2009-2010 Niklas Laxström |
8 | 8 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
9 | 9 | */ |
10 | 10 | |
11 | 11 | /** |
12 | | - * @todo Needs documentation. |
| 12 | + * Caches messages of file based message group source file. Can also track |
| 13 | + * that the cache is up to date. Parsing the source files can be slow, so |
| 14 | + * constructing CDB cache makes accessing that data constant speed regardless |
| 15 | + * of the actual format. |
13 | 16 | */ |
14 | 17 | class MessageGroupCache { |
15 | 18 | /// \string |
— | — | @@ -32,27 +35,43 @@ |
33 | 36 | $this->code = $code; |
34 | 37 | } |
35 | 38 | |
| 39 | + /** |
| 40 | + * Returns whether cache exists for this language and group. |
| 41 | + * @return \bool |
| 42 | + */ |
36 | 43 | public function exists() { |
37 | 44 | return file_exists( $this->getCacheFileName() ); |
38 | 45 | } |
39 | 46 | |
| 47 | + /** |
| 48 | + * Returns list of message keys that are stored. |
| 49 | + * @return \List{String} Message keys that can be passed one-by-one to get() method. |
| 50 | + */ |
40 | 51 | public function getKeys() { |
41 | | - return unserialize( $this->open()->get( $this->specialKey( 'keys' ) ) ); |
| 52 | + return unserialize( $this->open()->get( '#keys' ) ); |
42 | 53 | } |
43 | 54 | |
| 55 | + /** |
| 56 | + * Returns timestamp in unix-format about when this cache was first created. |
| 57 | + * @return \string Unix timestamp. |
| 58 | + */ |
44 | 59 | public function getTimestamp() { |
45 | | - return $this->open()->get( $this->specialKey( 'timestamp' ) ); |
| 60 | + return $this->open()->get( '#created' ); |
46 | 61 | } |
47 | 62 | |
48 | | - public function getHash() { |
49 | | - return $this->open()->get( $this->specialKey( 'hash' ) ); |
50 | | - } |
51 | | - |
| 63 | + /** |
| 64 | + * Get an item from the cache. |
| 65 | + * @return \string |
| 66 | + */ |
52 | 67 | public function get( $key ) { |
53 | 68 | return $this->open()->get( $key ); |
54 | 69 | } |
55 | 70 | |
56 | | - public function create() { |
| 71 | + /** |
| 72 | + * Populates the cache from current state of the source file. |
| 73 | + * @param $created \string Unix timestamp when the cache is created (for automatic updates). |
| 74 | + */ |
| 75 | + public function create( $created = false ) { |
57 | 76 | $this->close(); // Close the reader instance just to be sure |
58 | 77 | |
59 | 78 | $group = MessageGroups::getGroup( $this->group ); |
— | — | @@ -64,24 +83,88 @@ |
65 | 84 | |
66 | 85 | $cache = CdbWriter::open( $this->getCacheFileName() ); |
67 | 86 | $keys = array_keys( $messages ); |
68 | | - $cache->set( $this->specialKey( 'keys' ), serialize( $keys ) ); |
| 87 | + $cache->set( '#keys', serialize( $keys ) ); |
69 | 88 | |
70 | 89 | foreach ( $messages as $key => $value ) { |
71 | 90 | $cache->set( $key, $value ); |
72 | 91 | } |
73 | 92 | |
74 | | - $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() ); |
75 | | - $cache->set( $this->specialKey( 'hash' ), $hash ); |
| 93 | + $cache->set( '#created', $created ? $created : wfTimestamp() ); |
| 94 | + $cache->set( '#updated', wfTimestamp() ); |
| 95 | + $cache->set( '#filehash', $hash ); |
| 96 | + $cache->set( '#msgcount', count( $messages ) ); |
| 97 | + $cache->set( '#msghash', md5( serialize( ksort( $messages ) ) ) ); |
| 98 | + $cache->set( '#version', '3' ); |
76 | 99 | $cache->close(); |
77 | 100 | } |
78 | 101 | |
| 102 | + /** |
| 103 | + * Checks whether the cache still reflects the source file. |
| 104 | + * It uses multiple conditions to speed up the checking from file |
| 105 | + * modification timestamps to hashing. |
| 106 | + * @return \bool Wether the cache is up to date. |
| 107 | + */ |
| 108 | + public function isValid() { |
| 109 | + $group = MessageGroups::getGroup( $this->group ); |
| 110 | + $filename = $group->getSourceFilePath( $this->code ); |
| 111 | + |
| 112 | + // Timestamp and existence checks |
| 113 | + if ( !$this->exists() ) { |
| 114 | + return !file_exists( $filename ); |
| 115 | + } elseif ( !file_exists( $filename ) ) { |
| 116 | + //$this->delete(); |
| 117 | + return false; |
| 118 | + } else { |
| 119 | + // Cache is up-to-date if created after file was last modified |
| 120 | + return filemtime( $filename ) <= $this->get( '#updated' ); |
| 121 | + } |
| 122 | + // From now on cache and source file exists, but source file mtime is newer |
| 123 | + $created = $this->get( '#created' ); |
| 124 | + |
| 125 | + // File hash check |
| 126 | + $newhash = md5( file_get_contents( $filename ) ); |
| 127 | + if ( $this->get( '#filehash' === $oldhash ) ) { |
| 128 | + // Update cache so that we don't need to compare hashes next time |
| 129 | + $cache->create( $created ); |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + // Message count check |
| 134 | + $messages = $group->load( $this->code ); |
| 135 | + if ( $this->get( '#msgcount' ) !== count( $messages ) ) { |
| 136 | + // Number of messsages has changed |
| 137 | + return false; |
| 138 | + } |
| 139 | + |
| 140 | + // Content hash check |
| 141 | + if ( $this->get( '#msghash' ) === md5( serialize( ksort( $messages ) ) ) ) { |
| 142 | + // Update cache so that we don't need to do slow checks next time |
| 143 | + $cache->create( $createdat ); |
| 144 | + return true; |
| 145 | + } |
| 146 | + |
| 147 | + return false; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Open the cache for reading. |
| 152 | + * @return MessageGroupCache |
| 153 | + */ |
79 | 154 | protected function open() { |
80 | 155 | if ( $this->cache === null ) { |
81 | 156 | $this->cache = CdbReader::open( $this->getCacheFileName() ); |
| 157 | + if ( $this->cache->get( '#version' ) !== '3' ) { |
| 158 | + $this->updateCacheFormat( $this->cache ); |
| 159 | + $this->close(); |
| 160 | + return $this->open(); |
| 161 | + } |
82 | 162 | } |
83 | 163 | return $this->cache; |
84 | 164 | } |
85 | 165 | |
| 166 | + /** |
| 167 | + * Close the cache from reading. |
| 168 | + */ |
86 | 169 | protected function close() { |
87 | 170 | if ( $this->cache !== null ) { |
88 | 171 | $this->cache->close(); |
— | — | @@ -89,11 +172,45 @@ |
90 | 173 | } |
91 | 174 | } |
92 | 175 | |
| 176 | + /** |
| 177 | + * Returns full path the the cache file. |
| 178 | + */ |
93 | 179 | protected function getCacheFileName() { |
94 | 180 | return TranslateUtils::cacheFile( "translate_groupcache-{$this->group}-{$this->code}.cdb" ); |
95 | 181 | } |
96 | 182 | |
97 | | - protected function specialKey( $key ) { |
98 | | - return "<|$key#>"; |
| 183 | + /** |
| 184 | + * Updates cache to cache format 2. |
| 185 | + */ |
| 186 | + protected function updateCacheFormat( $oldcache ) { |
| 187 | + // Read the data from the old format |
| 188 | + $conv = array( |
| 189 | + '#keys' => $oldcache->get( '<|keys#>' ), |
| 190 | + '#created' => $oldcache->get( '<|timestamp#>' ), |
| 191 | + '#updated' => wfTimestamp(), |
| 192 | + '#filehash' => $oldcache->get( '<|hash#>' ), |
| 193 | + '#version' => '3', |
| 194 | + ); |
| 195 | + $conv['#msgcount'] = count( $conv['#keys'] ); |
| 196 | + |
| 197 | + $messages = array(); |
| 198 | + foreach ( unserialize( $conv['#keys'] ) as $key ) { |
| 199 | + $messages[$key] = $oldcache->get( $key ); |
| 200 | + } |
| 201 | + |
| 202 | + $conv['#msghash'] = md5( serialize( ksort( $messages ) ) ); |
| 203 | + $oldcache->close(); |
| 204 | + |
| 205 | + // Store the data in new format |
| 206 | + $cache = CdbWriter::open( $this->getCacheFileName() ); |
| 207 | + foreach ( $conv as $key => $value ) { |
| 208 | + $cache->set( $key, $value ); |
| 209 | + } |
| 210 | + foreach ( $messages as $key => $value ) { |
| 211 | + $cache->set( $key, $value ); |
| 212 | + } |
| 213 | + $cache->close(); |
| 214 | + |
99 | 215 | } |
| 216 | + |
100 | 217 | } |