r98797 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98796‎ | r98797 | r98798 >
Date:19:05, 3 October 2011
Author:nikerabbit
Status:deferred
Tags:
Comment:
Rewrote MessageIndexRebuilder to more generic class with different storage backends
Modified paths:
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/TranslateUtils.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/scripts/createMessageIndex.php (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslation.php (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslationDeletePage.php (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslationMovePage.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageIndexRebuildJob.php (added) (history)
  • /trunk/extensions/Translate/utils/MessageIndexRebuilder.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/createMessageIndex.php
@@ -6,7 +6,7 @@
77 * reaches a messages from somewhere else than Special:Translate.
88 *
99 * @author Niklas Laxstrom
10 - * @copyright Copyright © 2008-2009, Niklas Laxström
 10+ * @copyright Copyright © 2008-2011, Niklas Laxström
1111 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1212 * @file
1313 */
@@ -33,4 +33,4 @@
3434 }
3535
3636 MessageGroups::clearCache();
37 -MessageIndexRebuilder::execute();
 37+MessageIndex::singleton()->rebuild();
Index: trunk/extensions/Translate/tag/SpecialPageTranslation.php
@@ -540,10 +540,8 @@
541541 $this->setupRenderJobs( $page );
542542
543543 // Re-generate caches
544 - $page->getTranslationPercentages( /*re-generate*/ true );
545 - MessageGroupStats::clearGroup( $page->getMessageGroupId() );
546 - MessageIndexRebuilder::execute();
547544 MessageGroups::clearCache();
 545+ MessageIndexReduildJob::newJob()->insert();
548546 return false;
549547 }
550548
Index: trunk/extensions/Translate/tag/SpecialPageTranslationDeletePage.php
@@ -297,8 +297,7 @@
298298 }
299299
300300 MessageGroups::clearCache();
301 - // TODO: defer or make faster
302 - //MessageIndexRebuilder::execute();
 301+ MessageIndexReduildJob::newJob()->insert();
303302
304303 global $wgOut;
305304 $wgOut->addWikiMsg( 'pt-deletepage-started' );
Index: trunk/extensions/Translate/tag/SpecialPageTranslationMovePage.php
@@ -380,8 +380,7 @@
381381 }
382382
383383 MessageGroups::clearCache();
384 - // TODO: defer or make faster
385 - MessageIndexRebuilder::execute();
 384+ MessageIndexReduildJob::newJob()->insert();
386385
387386 global $wgOut;
388387 $wgOut->addWikiMsg( 'pt-movepage-started' );
Index: trunk/extensions/Translate/Translate.php
@@ -131,6 +131,9 @@
132132 $wgAvailableRights[] = 'translate-import';
133133 $wgAvailableRights[] = 'translate-manage';
134134
 135+// New jobs
 136+$wgJobClasses['MessageIndexRebuildJob'] = 'MessageIndexRebuildJob';
 137+
135138 $resourcePaths = array(
136139 'localBasePath' => dirname( __FILE__ ),
137140 'remoteExtPath' => 'Translate'
@@ -484,6 +487,14 @@
485488 */
486489 $wgTranslateCacheDirectory = false;
487490
 491+/**
 492+ * Configures where message index is stored.
 493+ * Available classes are CachedMessageIndex and FileCachedMessageIndex.
 494+ * FileCachedMessageIndex needs $wgCacheDirectory to be functional.
 495+ */
 496+$wgTranslateMessageIndex = array( 'CachedMessageIndex' );
 497+# $wgTranslateMessageIndex = array( 'FileCachedMessageIndex' );
 498+
488499 # ==== PHPlot ====
489500
490501 /**
Index: trunk/extensions/Translate/README
@@ -30,6 +30,7 @@
3131
3232 == Change log ==
3333 * 2011-10-03
 34+- MessageIndex can now be stored in object cache (default) or in a file.
3435 - Fixed an error that sometimes prevented translating messages that had
3536 been renamed
3637 * 2011-10-01
Index: trunk/extensions/Translate/_autoload.php
@@ -97,7 +97,8 @@
9898 $wgAutoloadClasses['TranslatePreferences'] = $dir . 'utils/UserToggles.php';
9999 $wgAutoloadClasses['TranslateToolbox'] = $dir . 'utils/ToolBox.php';
100100
101 -$wgAutoloadClasses['MessageIndexRebuilder'] = $dir . 'utils/MessageIndexRebuilder.php';
 101+$wgAutoloadClasses['MessageIndex'] = $dir . 'utils/MessageIndexRebuilder.php';
 102+$wgAutoloadClasses['MessageIndexRebuildJob'] = $dir . 'utils/MessageIndexRebuildJob.php';
102103 $wgAutoloadClasses['MessageTable'] = $dir . 'utils/MessageTable.php';
103104 $wgAutoloadClasses['StatsTable'] = $dir . 'utils/StatsTable.php';
104105 $wgAutoloadClasses['JsSelectToInput'] = $dir . 'utils/JsSelectToInput.php';
Index: trunk/extensions/Translate/utils/MessageIndexRebuilder.php
@@ -1,31 +1,47 @@
22 <?php
33 /**
4 - * Contains classes for rebuilding the message index.
 4+ * Contains classes for handling the message index.
55 *
66 * @file
77 * @author Niklas Laxstrom
8 - * @copyright Copyright © 2008-2010, Niklas Laxström
 8+ * @copyright Copyright © 2008-2011, Niklas Laxström
99 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1010 */
1111
1212 /**
1313 * Creates a database of keys in all groups, so that namespace and key can be
14 - * used to get the group they belong to. This is used as a fallback when
 14+ * used to get the groups they belong to. This is used as a fallback when
1515 * loadgroup parameter is not provided in the request, which happens if someone
16 - * reaches a messages from somewhere else than Special:Translate.
 16+ * reaches a messages from somewhere else than Special:Translate. Also used
 17+ * by Special:TranslationStats and alike which need to map lots of titles
 18+ * to message groups.
1719 */
18 -class MessageIndexRebuilder {
19 - public static function execute() {
20 - $groups = MessageGroups::singleton()->getGroups();
 20+abstract class MessageIndex {
 21+ /// @var MessageIndex
 22+ protected static $instance;
2123
22 - $filename = TranslateUtils::cacheFile( 'translate_messageindex.ser' );
23 - if ( file_exists( $filename ) ) {
24 - $old = unserialize( file_get_contents( $filename ) );
25 - } else {
26 - $old = array();
 24+ /// @var array
 25+ protected $index;
 26+
 27+ public static function singleton() {
 28+ if ( self::$instance === null ) {
 29+ global $wgTranslateMessageIndex;
 30+ $params = $wgTranslateMessageIndex;
 31+ $class = array_shift( $params );
 32+ self::$instance = new $class( $params );
2733 }
 34+ return self::$instance;
 35+ }
2836
29 - $hugearray = array();
 37+ /** @return array */
 38+ abstract public function retrieve();
 39+ abstract protected function store( array $array );
 40+
 41+ public function rebuild() {
 42+ $groups = MessageGroups::singleton()->getGroups();
 43+
 44+ $old = $this->retrieve();
 45+ $new = array();
3046 $postponed = array();
3147
3248 STDOUT( "Working with ", 'main' );
@@ -41,28 +57,34 @@
4258 continue;
4359 }
4460
45 - self::checkAndAdd( $hugearray, $g );
 61+ $this->checkAndAdd( $new, $g );
4662 }
4763
4864 foreach ( $postponed as $g ) {
49 - self::checkAndAdd( $hugearray, $g, true );
 65+ $this->checkAndAdd( $new, $g, true );
5066 }
5167
52 - file_put_contents( $filename, serialize( $hugearray ) );
53 -
 68+ $this->store( $new );
 69+ $this->clearMessageGroupStats( $old, $new );
 70+ }
 71+
 72+ /**
 73+ * Purge message group stats when set of keys have changed.
 74+ */
 75+ protected function clearMessageGroupStats( array $old, array $new ) {
5476 $changes = array();
55 - foreach ( array_diff_assoc( $hugearray, $old ) as $groups ) {
 77+ foreach ( array_diff_assoc( $new, $old ) as $groups ) {
5678 foreach ( (array) $groups as $group ) $changes[$group] = true;
5779 }
5880
59 - foreach ( array_diff_assoc( $old, $hugearray ) as $groups ) {
 81+ foreach ( array_diff_assoc( $old, $new ) as $groups ) {
6082 foreach ( (array) $groups as $group ) $changes[$group] = true;
6183 }
6284
6385 MessageGroupStats::clearGroup( array_keys( $changes ) );
6486 }
6587
66 - protected static function checkAndAdd( &$hugearray, $g, $ignore = false ) {
 88+ protected function checkAndAdd( &$hugearray, $g, $ignore = false ) {
6789 if ( $g instanceof MessageGroupBase ) {
6890 $cache = new MessageGroupCache( $g );
6991
@@ -116,3 +138,63 @@
117139 unset( $id ); // Disconnect the previous references to this $id
118140 }
119141 }
 142+
 143+/**
 144+ * Storage on serialized file.
 145+ */
 146+class FileCachedMessageIndex extends MessageIndex {
 147+ protected $filename = 'translate_messageindex.ser';
 148+
 149+ /** @return array */
 150+ public function retrieve() {
 151+ if ( $this->index !== null ) {
 152+ return $this->index;
 153+ }
 154+
 155+ $file = TranslateUtils::cacheFile( $this->filename );
 156+ if ( file_exists( $file ) ) {
 157+ return $this->index = unserialize( file_get_contents( $file ) );
 158+ } else {
 159+ return $this->index = array();
 160+ }
 161+ }
 162+
 163+ protected function store( array $array ) {
 164+ $file = TranslateUtils::cacheFile( $this->filename );
 165+ file_put_contents( $file, serialize( $array ) );
 166+ }
 167+
 168+}
 169+
 170+/**
 171+ * Storage on ObjectCache.
 172+ */
 173+class CachedMessageIndex extends MessageIndex {
 174+ protected $key = 'translate-messageindex';
 175+ protected $cache;
 176+
 177+ protected function __construct( array $params ) {
 178+ $this->cache = wfGetCache( CACHE_ANYTHING );
 179+ }
 180+
 181+ /** @return array */
 182+ public function retrieve() {
 183+ if ( $this->index !== null ) {
 184+ return $this->index;
 185+ }
 186+
 187+ $key = wfMemckey( $this->key );
 188+ $data = $this->cache->get( $key );
 189+ if ( is_array( $data ) ) {
 190+ return $this->index = $data;
 191+ } else {
 192+ return $this->index = array();
 193+ }
 194+ }
 195+
 196+ protected function store( array $array ) {
 197+ $key = wfMemckey( $this->key );
 198+ $data = $this->cache->set( $key, $array );
 199+ }
 200+
 201+}
Index: trunk/extensions/Translate/utils/MessageIndexRebuildJob.php
@@ -0,0 +1,29 @@
 2+<?php
 3+/**
 4+ * Contains class with job for rebuilding message index.
 5+ *
 6+ * @file
 7+ * @author Niklas Laxström
 8+ * @copyright Copyright © 2011, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ */
 11+
 12+/**
 13+ * Job for rebuilding message index.
 14+ *
 15+ * @ingroup JobQueue
 16+ */
 17+class MessageIndexRebuildJob extends Job {
 18+ public static function newJob() {
 19+ $job = new self( Title::newMainPage() );
 20+ return $job;
 21+ }
 22+
 23+ function __construct( $title, $params = array(), $id = 0 ) {
 24+ parent::__construct( __CLASS__, $title, $params, $id );
 25+ }
 26+
 27+ function run() {
 28+ MessageIndex::singleton()->rebuild();
 29+ }
 30+}
Property changes on: trunk/extensions/Translate/utils/MessageIndexRebuildJob.php
___________________________________________________________________
Added: svn:eol-style
131 + native
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -237,14 +237,11 @@
238238 * @return \list{String} Possibly empty list of group ids.
239239 */
240240 public static function messageKeyToGroups( $namespace, $key ) {
241 - if ( self::$mi === null ) {
242 - self::messageIndex();
243 - }
244 -
 241+ $mi = MessageIndex::singleton()->retrieve();
245242 $normkey = self::normaliseKey( $namespace, $key );
246243
247 - if ( isset( self::$mi[$normkey] ) ) {
248 - return (array) self::$mi[$normkey];
 244+ if ( isset( $mi[$normkey] ) ) {
 245+ return (array) $mi[$normkey];
249246 } else {
250247 return array();
251248 }
@@ -260,31 +257,7 @@
261258 return strtr( strtolower( "$namespace:$key" ), " ", "_" );
262259 }
263260
264 -
265261 /**
266 - * Opens and returns the message index.
267 - * @return \array or \type{false}
268 - */
269 - public static function messageIndex() {
270 - wfDebug( __METHOD__ . ": loading from file...\n" );
271 - $filename = self::cacheFile( 'translate_messageindex.ser' );
272 -
273 - if ( !file_exists( $filename ) ) {
274 - MessageIndexRebuilder::execute();
275 - }
276 -
277 - if ( file_exists( $filename ) ) {
278 - $keyToGroup = unserialize( file_get_contents( $filename ) );
279 - } else {
280 - throw new MWException( 'Unable to get message index' );
281 - }
282 -
283 - self::$mi = $keyToGroup;
284 -
285 - return $keyToGroup;
286 - }
287 -
288 - /**
289262 * Constructs a fieldset with contents.
290263 * @param $legend \string Raw html.
291264 * @param $contents \string Raw html.

Follow-up revisions

RevisionCommit summaryAuthorDate
r98798Renamed file to match class name, followup r98797nikerabbit19:06, 3 October 2011
r98939Follow-up r98894 (introduced in r98797): fix Finnish method names (should hav...siebrand22:16, 4 October 2011

Status & tagging log