Index: trunk/extensions/Translate/scripts/createMessageIndex.php |
— | — | @@ -6,7 +6,7 @@ |
7 | 7 | * reaches a messages from somewhere else than Special:Translate. |
8 | 8 | * |
9 | 9 | * @author Niklas Laxstrom |
10 | | - * @copyright Copyright © 2008-2009, Niklas Laxström |
| 10 | + * @copyright Copyright © 2008-2011, Niklas Laxström |
11 | 11 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
12 | 12 | * @file |
13 | 13 | */ |
— | — | @@ -33,4 +33,4 @@ |
34 | 34 | } |
35 | 35 | |
36 | 36 | MessageGroups::clearCache(); |
37 | | -MessageIndexRebuilder::execute(); |
| 37 | +MessageIndex::singleton()->rebuild(); |
Index: trunk/extensions/Translate/tag/SpecialPageTranslation.php |
— | — | @@ -540,10 +540,8 @@ |
541 | 541 | $this->setupRenderJobs( $page ); |
542 | 542 | |
543 | 543 | // Re-generate caches |
544 | | - $page->getTranslationPercentages( /*re-generate*/ true ); |
545 | | - MessageGroupStats::clearGroup( $page->getMessageGroupId() ); |
546 | | - MessageIndexRebuilder::execute(); |
547 | 544 | MessageGroups::clearCache(); |
| 545 | + MessageIndexReduildJob::newJob()->insert(); |
548 | 546 | return false; |
549 | 547 | } |
550 | 548 | |
Index: trunk/extensions/Translate/tag/SpecialPageTranslationDeletePage.php |
— | — | @@ -297,8 +297,7 @@ |
298 | 298 | } |
299 | 299 | |
300 | 300 | MessageGroups::clearCache(); |
301 | | - // TODO: defer or make faster |
302 | | - //MessageIndexRebuilder::execute(); |
| 301 | + MessageIndexReduildJob::newJob()->insert(); |
303 | 302 | |
304 | 303 | global $wgOut; |
305 | 304 | $wgOut->addWikiMsg( 'pt-deletepage-started' ); |
Index: trunk/extensions/Translate/tag/SpecialPageTranslationMovePage.php |
— | — | @@ -380,8 +380,7 @@ |
381 | 381 | } |
382 | 382 | |
383 | 383 | MessageGroups::clearCache(); |
384 | | - // TODO: defer or make faster |
385 | | - MessageIndexRebuilder::execute(); |
| 384 | + MessageIndexReduildJob::newJob()->insert(); |
386 | 385 | |
387 | 386 | global $wgOut; |
388 | 387 | $wgOut->addWikiMsg( 'pt-movepage-started' ); |
Index: trunk/extensions/Translate/Translate.php |
— | — | @@ -131,6 +131,9 @@ |
132 | 132 | $wgAvailableRights[] = 'translate-import'; |
133 | 133 | $wgAvailableRights[] = 'translate-manage'; |
134 | 134 | |
| 135 | +// New jobs |
| 136 | +$wgJobClasses['MessageIndexRebuildJob'] = 'MessageIndexRebuildJob'; |
| 137 | + |
135 | 138 | $resourcePaths = array( |
136 | 139 | 'localBasePath' => dirname( __FILE__ ), |
137 | 140 | 'remoteExtPath' => 'Translate' |
— | — | @@ -484,6 +487,14 @@ |
485 | 488 | */ |
486 | 489 | $wgTranslateCacheDirectory = false; |
487 | 490 | |
| 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 | + |
488 | 499 | # ==== PHPlot ==== |
489 | 500 | |
490 | 501 | /** |
Index: trunk/extensions/Translate/README |
— | — | @@ -30,6 +30,7 @@ |
31 | 31 | |
32 | 32 | == Change log == |
33 | 33 | * 2011-10-03 |
| 34 | +- MessageIndex can now be stored in object cache (default) or in a file. |
34 | 35 | - Fixed an error that sometimes prevented translating messages that had |
35 | 36 | been renamed |
36 | 37 | * 2011-10-01 |
Index: trunk/extensions/Translate/_autoload.php |
— | — | @@ -97,7 +97,8 @@ |
98 | 98 | $wgAutoloadClasses['TranslatePreferences'] = $dir . 'utils/UserToggles.php'; |
99 | 99 | $wgAutoloadClasses['TranslateToolbox'] = $dir . 'utils/ToolBox.php'; |
100 | 100 | |
101 | | -$wgAutoloadClasses['MessageIndexRebuilder'] = $dir . 'utils/MessageIndexRebuilder.php'; |
| 101 | +$wgAutoloadClasses['MessageIndex'] = $dir . 'utils/MessageIndexRebuilder.php'; |
| 102 | +$wgAutoloadClasses['MessageIndexRebuildJob'] = $dir . 'utils/MessageIndexRebuildJob.php'; |
102 | 103 | $wgAutoloadClasses['MessageTable'] = $dir . 'utils/MessageTable.php'; |
103 | 104 | $wgAutoloadClasses['StatsTable'] = $dir . 'utils/StatsTable.php'; |
104 | 105 | $wgAutoloadClasses['JsSelectToInput'] = $dir . 'utils/JsSelectToInput.php'; |
Index: trunk/extensions/Translate/utils/MessageIndexRebuilder.php |
— | — | @@ -1,31 +1,47 @@ |
2 | 2 | <?php |
3 | 3 | /** |
4 | | - * Contains classes for rebuilding the message index. |
| 4 | + * Contains classes for handling the message index. |
5 | 5 | * |
6 | 6 | * @file |
7 | 7 | * @author Niklas Laxstrom |
8 | | - * @copyright Copyright © 2008-2010, Niklas Laxström |
| 8 | + * @copyright Copyright © 2008-2011, Niklas Laxström |
9 | 9 | * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later |
10 | 10 | */ |
11 | 11 | |
12 | 12 | /** |
13 | 13 | * 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 |
15 | 15 | * 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. |
17 | 19 | */ |
18 | | -class MessageIndexRebuilder { |
19 | | - public static function execute() { |
20 | | - $groups = MessageGroups::singleton()->getGroups(); |
| 20 | +abstract class MessageIndex { |
| 21 | + /// @var MessageIndex |
| 22 | + protected static $instance; |
21 | 23 | |
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 ); |
27 | 33 | } |
| 34 | + return self::$instance; |
| 35 | + } |
28 | 36 | |
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(); |
30 | 46 | $postponed = array(); |
31 | 47 | |
32 | 48 | STDOUT( "Working with ", 'main' ); |
— | — | @@ -41,28 +57,34 @@ |
42 | 58 | continue; |
43 | 59 | } |
44 | 60 | |
45 | | - self::checkAndAdd( $hugearray, $g ); |
| 61 | + $this->checkAndAdd( $new, $g ); |
46 | 62 | } |
47 | 63 | |
48 | 64 | foreach ( $postponed as $g ) { |
49 | | - self::checkAndAdd( $hugearray, $g, true ); |
| 65 | + $this->checkAndAdd( $new, $g, true ); |
50 | 66 | } |
51 | 67 | |
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 ) { |
54 | 76 | $changes = array(); |
55 | | - foreach ( array_diff_assoc( $hugearray, $old ) as $groups ) { |
| 77 | + foreach ( array_diff_assoc( $new, $old ) as $groups ) { |
56 | 78 | foreach ( (array) $groups as $group ) $changes[$group] = true; |
57 | 79 | } |
58 | 80 | |
59 | | - foreach ( array_diff_assoc( $old, $hugearray ) as $groups ) { |
| 81 | + foreach ( array_diff_assoc( $old, $new ) as $groups ) { |
60 | 82 | foreach ( (array) $groups as $group ) $changes[$group] = true; |
61 | 83 | } |
62 | 84 | |
63 | 85 | MessageGroupStats::clearGroup( array_keys( $changes ) ); |
64 | 86 | } |
65 | 87 | |
66 | | - protected static function checkAndAdd( &$hugearray, $g, $ignore = false ) { |
| 88 | + protected function checkAndAdd( &$hugearray, $g, $ignore = false ) { |
67 | 89 | if ( $g instanceof MessageGroupBase ) { |
68 | 90 | $cache = new MessageGroupCache( $g ); |
69 | 91 | |
— | — | @@ -116,3 +138,63 @@ |
117 | 139 | unset( $id ); // Disconnect the previous references to this $id |
118 | 140 | } |
119 | 141 | } |
| 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 |
1 | 31 | + native |
Index: trunk/extensions/Translate/TranslateUtils.php |
— | — | @@ -237,14 +237,11 @@ |
238 | 238 | * @return \list{String} Possibly empty list of group ids. |
239 | 239 | */ |
240 | 240 | public static function messageKeyToGroups( $namespace, $key ) { |
241 | | - if ( self::$mi === null ) { |
242 | | - self::messageIndex(); |
243 | | - } |
244 | | - |
| 241 | + $mi = MessageIndex::singleton()->retrieve(); |
245 | 242 | $normkey = self::normaliseKey( $namespace, $key ); |
246 | 243 | |
247 | | - if ( isset( self::$mi[$normkey] ) ) { |
248 | | - return (array) self::$mi[$normkey]; |
| 244 | + if ( isset( $mi[$normkey] ) ) { |
| 245 | + return (array) $mi[$normkey]; |
249 | 246 | } else { |
250 | 247 | return array(); |
251 | 248 | } |
— | — | @@ -260,31 +257,7 @@ |
261 | 258 | return strtr( strtolower( "$namespace:$key" ), " ", "_" ); |
262 | 259 | } |
263 | 260 | |
264 | | - |
265 | 261 | /** |
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 | | - /** |
289 | 262 | * Constructs a fieldset with contents. |
290 | 263 | * @param $legend \string Raw html. |
291 | 264 | * @param $contents \string Raw html. |