r71939 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r71938‎ | r71939 | r71940 >
Date:11:05, 30 August 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Rework MessageGroupCache to more sane interface
Modified paths:
  • /trunk/extensions/Translate/Groups.php (modified) (history)
  • /trunk/extensions/Translate/SpecialManageGroups.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageGroupCache.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Groups.php
@@ -222,9 +222,9 @@
223223 }
224224
225225 public function getMessage( $key, $code ) {
226 - $cache = new MessageGroupCache( $this );
227 - if ( $cache->exists( $code ) ) {
228 - $msg = $cache->get( $key, $code );
 226+ $cache = new MessageGroupCache( $this, $code );
 227+ if ( $cache->exists() ) {
 228+ $msg = $cache->get( $key );
229229
230230 if ( $msg !== false ) {
231231 return $msg;
@@ -232,7 +232,7 @@
233233
234234 // Try harder
235235 $nkey = str_replace( ' ', '_', strtolower( $key ) );
236 - $keys = $cache->getKeys( $code );
 236+ $keys = $cache->getKeys();
237237
238238 foreach ( $keys as $k ) {
239239 if ( $nkey === str_replace( ' ', '_', strtolower( $k ) ) ) {
Index: trunk/extensions/Translate/utils/MessageGroupCache.php
@@ -11,59 +11,58 @@
1212 * @todo Needs documentation.
1313 */
1414 class MessageGroupCache {
 15+ /// \string
1516 protected $group;
 17+ /// CdbReader
1618 protected $cache;
17 -
18 - // Implementation detail
 19+ /// \string
1920 protected $code;
2021
21 - // id or instance of MessageGroup
22 - public function __construct( $group ) {
 22+ /**
 23+ * Contructs a new cache object for given group and language code.
 24+ * @param $group \types{String,FileBasedMessageGroup} Group object or id.
 25+ * @param $code \string Language code. Default value 'en'.
 26+ */
 27+ public function __construct( $group, $code = 'en' ) {
2328 if ( is_object( $group ) ) {
2429 $this->group = $group->getId();
2530 } else {
2631 $this->group = $group;
2732 }
 33+ $this->code = $code;
2834 }
2935
30 - public function exists( $code = 'en' ) {
31 - return file_exists( $this->getCacheFileName( $code ) );
 36+ public function exists() {
 37+ return file_exists( $this->getCacheFileName() );
3238 }
3339
34 - public function getKeys( $code = 'en' ) {
35 - $cache = $this->open( $code );
36 -
37 - return unserialize( $cache->get( $this->specialKey( 'keys' ) ) );
 40+ public function getKeys() {
 41+ return unserialize( $this->open()->get( $this->specialKey( 'keys' ) ) );
3842 }
3943
40 - public function getTimestamp( $code = 'en' ) {
41 - $cache = $this->open( $code );
42 -
43 - return $cache->get( $this->specialKey( 'timestamp' ) );
 44+ public function getTimestamp() {
 45+ return $this->open()->get( $this->specialKey( 'timestamp' ) );
4446 }
4547
46 - public function updateTimestamp( $code = 'en' ) {
47 - $cache = CdbWriter::open( $this->getCacheFileName( $code ) );
48 - $cache->set( $this->specialKey( 'timestamp' ), wfTimestamp() );
49 - $cache->close();
 48+ public function getHash() {
 49+ return $this->open()->get( $this->specialKey( 'hash' ) );
5050 }
5151
52 - public function getHash( $code = 'en' ) {
53 - $cache = $this->open( $code );
54 - return $cache->get( $this->specialKey( 'hash' ) );
 52+ public function get( $key ) {
 53+ return $this->open()->get( $key );
5554 }
5655
57 - public function get( $key, $code = 'en' ) {
58 - $cache = $this->open( $code );
 56+ public function create() {
 57+ $this->close(); // Close the reader instance just to be sure
5958
60 - return $cache->get( $key );
61 - }
 59+ $group = MessageGroups::getGroup( $this->group );
 60+ $messages = $group->load( $this->code );
 61+ if ( !count( $messages ) ) {
 62+ return; // Don't create empty caches
 63+ }
 64+ $hash = md5( file_get_contents( $group->getSourceFilePath( $this->code ) ) );
6265
63 - public function create( $messages, $code = 'en', $hash = false ) {
64 - $this->cache = null; // Needed?
65 -
66 - $cache = CdbWriter::open( $this->getCacheFileName( $code ) );
67 -
 66+ $cache = CdbWriter::open( $this->getCacheFileName() );
6867 $keys = array_keys( $messages );
6968 $cache->set( $this->specialKey( 'keys' ), serialize( $keys ) );
7069
@@ -76,22 +75,24 @@
7776 $cache->close();
7877 }
7978
80 - protected function open( $code ) {
81 - if ( $code !== $this->code || !$this->cache ) {
82 - if ( $this->cache ) {
83 - $this->cache->close();
84 - }
85 -
86 - $this->cache = CdbReader::open( $this->getCacheFileName( $code ) );
 79+ protected function open() {
 80+ if ( $this->cache === null ) {
 81+ $this->cache = CdbReader::open( $this->getCacheFileName() );
8782 }
88 -
8983 return $this->cache;
9084 }
9185
92 - protected function getCacheFileName( $code ) {
93 - return TranslateUtils::cacheFile( "translate_groupcache-{$this->group}-$code.cdb" );
 86+ protected function close() {
 87+ if ( $this->cache !== null ) {
 88+ $this->cache->close();
 89+ $this->cache = null;
 90+ }
9491 }
9592
 93+ protected function getCacheFileName() {
 94+ return TranslateUtils::cacheFile( "translate_groupcache-{$this->group}-{$this->code}.cdb" );
 95+ }
 96+
9697 protected function specialKey( $key ) {
9798 return "<|$key#>";
9899 }
Index: trunk/extensions/Translate/SpecialManageGroups.php
@@ -57,31 +57,21 @@
5858 $this->user->isAllowed( 'translate-manage' ) &&
5959 $this->user->matchEditToken( $wgRequest->getVal( 'token' ) )
6060 ) {
61 - $cache = new MessageGroupCache( $group );
6261 $languages = explode( ',', $wgRequest->getText( 'codes' ) );
6362 foreach ( $languages as $code ) {
64 - $messages = $group->load( $code );
65 - if ( count( $messages ) ) {
66 - $filename = $group->getSourceFilePath( $code );
67 - $hash = md5( file_get_contents( $filename ) );
68 - $cache->create( $messages, $code, $hash );
69 - } else {
70 - ///@todo delete stale caches?
71 - }
 63+ $cache = new MessageGroupCache( $group, $code );
 64+ $cache->create();
7265 }
7366 }
7467
75 - $cache = new MessageGroupCache( $group );
7668 $code = $wgRequest->getText( 'language', 'en' );
77 -
78 -
7969 // Go to English for undefined codes.
8070 $codes = array_keys( Language::getLanguageNames( false ) );
8171 if ( !in_array( $code, $codes ) ) {
8272 $code = 'en';
8373 }
8474
85 - $this->importForm( $cache, $group, $code );
 75+ $this->importForm( $group, $code );
8676 } else {
8777 global $wgLang, $wgOut;
8878
@@ -99,7 +89,6 @@
10090 $out = $link . $separator;
10191
10292 $cache = new MessageGroupCache( $group );
103 -
10493 if ( $cache->exists() ) {
10594 $timestamp = wfTimestamp( TS_MW, $cache->getTimestamp() );
10695 $out .= wfMsg( 'translate-manage-cacheat',
@@ -144,7 +133,7 @@
145134 /**
146135 * @todo Very long code block; split up.
147136 */
148 - public function importForm( $cache, $group, $code ) {
 137+ public function importForm( $group, $code ) {
149138 $this->setSubtitle( $group, $code );
150139
151140 $formParams = array(
@@ -174,12 +163,9 @@
175164 );
176165
177166 // BEGIN
178 - $messages = $group->load( $code );
179 -
 167+ $cache = new MessageGroupCache( $group, $code );
180168 if ( !$cache->exists() && $code === 'en' ) {
181 - $filename = $group->getSourceFilePath( $code );
182 - $hash = md5( file_get_contents( $filename ) );
183 - $cache->create( $messages, $code, $hash );
 169+ $cache->create();
184170 }
185171
186172 $collection = $group->initCollection( $code );
@@ -194,6 +180,7 @@
195181 $ignoredMessages = array();
196182 }
197183
 184+ $messages = $group->load( $code );
198185 $changed = array();
199186 foreach ( $messages as $key => $value ) {
200187 // ignored? ignore!
@@ -329,9 +316,7 @@
330317 $changed[] = '<ul>';
331318 }
332319
333 - $filename = $group->getSourceFilePath( $code );
334 - $hash = md5( file_get_contents( $filename ) );
335 - $cache->create( $messages, $code, $hash );
 320+ $cache->create();
336321 $message = wfMsgExt( 'translate-manage-import-rebuild', 'parseinline' );
337322 $changed[] = "<li>$message</li>";
338323 $message = wfMsgExt( 'translate-manage-import-done', 'parseinline' );
@@ -350,6 +335,7 @@
351336 $this->out->addHTML( implode( "\n", $changed ) );
352337 $this->out->addHTML( Xml::submitButton( wfMsg( 'translate-manage-submit' ) ) );
353338 } else {
 339+ $cache->create(); // Update timestamp
354340 $this->out->addWikiMsg( 'translate-manage-nochanges' );
355341 }
356342 }
@@ -357,7 +343,7 @@
358344 $this->out->addHTML( '</form>' );
359345
360346 if ( $code === 'en' ) {
361 - $this->doModLangs( $cache, $group );
 347+ $this->doModLangs( $group );
362348 } else {
363349 $this->out->addHTML( '<p>' . $this->skin->link(
364350 $this->getTitle(),
@@ -368,7 +354,7 @@
369355 }
370356 }
371357
372 - public function doModLangs( $cache, $group ) {
 358+ public function doModLangs( $group ) {
373359 global $wgLang;
374360
375361 $languages = array_keys( Language::getLanguageNames( false ) );
@@ -390,12 +376,9 @@
391377 array( 'group' => $group->getId(), 'language' => $code )
392378 );
393379
394 - $filename = $group->getSourceFilePath( $code );
395 - $mtime = file_exists( $filename ) ? filemtime( $filename ) : false;
396 - $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false;
397 - if ( $mtime === false ) {
 380+ if ( !$cache->exists() ) {
398381 $modified[] = wfMsgHtml( 'translate-manage-modlang-new', $link );
399 - } elseif ( $mtime > $cachetime ) {
 382+ } else {
400383 $modified[] = $link;
401384 }
402385
@@ -478,11 +461,11 @@
479462 * Uses modification timestamps and file hashes to check.
480463 */
481464 protected function changedSinceCached( $group, $code = 'en' ) {
482 - $cache = new MessageGroupCache( $group );
 465+ $cache = new MessageGroupCache( $group, $code );
483466 $filename = $group->getSourceFilePath( $code );
484467
485468 $mtime = file_exists( $filename ) ? filemtime( $filename ) : false;
486 - $cachetime = $cache->exists( $code ) ? $cache->getTimestamp( $code ) : false;
 469+ $cachetime = $cache->exists() ? $cache->getTimestamp() : false;
487470
488471 // No such language at all, or cache is up to date
489472 if ( $mtime <= $cachetime ) {
@@ -490,12 +473,12 @@
491474 }
492475
493476 // Timestamps differ (or either cache or the file does not exists)
494 - $oldhash = $cache->exists( $code ) ? $cache->getHash( $code ) : false;
 477+ $oldhash = $cache->exists() ? $cache->getHash() : false;
495478 $newhash = file_exists( $filename ) ? md5( file_get_contents( $filename ) ) : false;
496479 wfDebugLog( 'translate-manage', "$mtime === $cachetime | $code | $oldhash !== $newhash\n" );
497480 if ( $newhash === $oldhash ) {
498481 // Update cache so that we don't need to compare hashes next time
499 - $cache->updateTimestamp( $code );
 482+ $cache->create();
500483 return false;
501484 }
502485

Status & tagging log