r53925 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r53924‎ | r53925 | r53926 >
Date:08:03, 29 July 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
* Move message index to cdb (standard location, permissions, etc)
* Make it a class function that can be called from anywhere, and remove now useless MessageIndex which was causing bugs
Modified paths:
  • /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/PageTranslationHooks.php (modified) (history)
  • /trunk/extensions/Translate/tag/SpecialPageTranslation.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageIndex.php (deleted) (history)
  • /trunk/extensions/Translate/utils/MessageIndexRebuilder.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/createMessageIndex.php
@@ -7,89 +7,11 @@
88 *
99 * @author Niklas Laxstrom
1010 *
11 - * @copyright Copyright © 2008, Niklas Laxström
 11+ * @copyright Copyright © 2008-2009, Niklas Laxström
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1313 * @file
1414 */
1515
1616 require( dirname( __FILE__ ) . '/cli.inc' );
1717
18 -$groups = MessageGroups::singleton()->getGroups();
19 -
20 -$hugearray = array();
21 -$postponed = array();
22 -
23 -STDOUT( "Working with ", 'main' );
24 -
25 -foreach ( $groups as $g ) {
26 - if ( !$g->exists() ) continue;
27 - # Skip meta thingies
28 - if ( $g->isMeta() ) {
29 - $postponed[] = $g;
30 - continue;
31 - }
32 -
33 - checkAndAdd( $g );
34 -}
35 -
36 -foreach ( $postponed as $g ) {
37 - checkAndAdd( $g, true );
38 -}
39 -
40 -wfMkdirParents( dirname( TRANSLATE_INDEXFILE ) );
41 -file_put_contents( TRANSLATE_INDEXFILE, serialize( $hugearray ) );
42 -
43 -var_dump( unserialize( serialize( $hugearray ) ) );
44 -
45 -function checkAndAdd( $g, $ignore = false ) {
46 - global $hugearray;
47 -
48 - if ( $g instanceof MessageGroupBase ) {
49 - $cache = new MessageGroupCache( $g );
50 - if ( $cache->exists() ) {
51 - $keys = $cache->getKeys();
52 - } else {
53 - $keys = array_keys( $g->load( 'en' ) );
54 - }
55 - } else {
56 - $messages = $g->getDefinitions();
57 - if ( !is_array( $messages ) ) continue;
58 - $keys = array_keys( $messages );
59 - }
60 -
61 - $id = $g->getId();
62 -
63 - STDOUT( "$id ", 'main' );
64 -
65 - $namespace = $g->getNamespace();
66 -
67 - foreach ( $keys as $key ) {
68 - # Force all keys to lower case, because the case doesn't matter and it is
69 - # easier to do comparing when the case of first letter is unknown, because
70 - # mediawiki forces it to upper case
71 - $key = TranslateUtils::normaliseKey( $namespace, $key );
72 - if ( isset( $hugearray[$key] ) ) {
73 - if ( !$ignore ) {
74 - $to = implode( ', ', (array)$hugearray[$key] );
75 - STDERR( "Key $key already belongs to $to, conflict with $id" );
76 - }
77 -
78 - if ( is_array($hugearray[$key]) ) {
79 - // Hard work is already done, just add a new reference
80 - $hugearray[$key][] = &$id;
81 - } else {
82 - // Store the actual reference, then remove it from array, to not
83 - // replace the references value, but to store a array of new
84 - // references instead. References are hard!
85 - $value = &$hugearray[$key];
86 - unset($hugearray[$key]);
87 - $hugearray[$key] = array( &$value, &$id );
88 - #var_dump( $hugearray ); die();
89 - }
90 - } else {
91 - $hugearray[$key] = &$id;
92 - }
93 - }
94 - unset( $id ); // Disconnect the previous references to this $id
95 -
96 -}
\ No newline at end of file
 18+MessageIndexRebuilder::execute();
\ No newline at end of file
Index: trunk/extensions/Translate/tag/SpecialPageTranslation.php
@@ -400,7 +400,7 @@
401401 $this->setupRenderJobs( $page );
402402
403403 // Re-generate caches
404 - MessageIndex::cache( NS_TRANSLATIONS );
 404+ MessageIndexRebuilder::execute();
405405 $page->getTranslationPercentages( /*re-generate*/ true );
406406
407407 return false;
Index: trunk/extensions/Translate/tag/PageTranslationHooks.php
@@ -32,6 +32,13 @@
3333 return true;
3434 }
3535
 36+ public static function titleToGroup( Title $title ) {
 37+ $namespace = $title->getNamespace();
 38+ $text = $title->getDBkey();
 39+ list( $key, ) = TranslateUtils::figureMessage( $text );
 40+ return TranslateUtils::messageKeyToGroup( $namespace, $key );
 41+ }
 42+
3643 public static function onSectionSave( $article, $user, $text, $summary, $minor,
3744 $_, $_, $flags, $revision ) {
3845 $title = $article->getTitle();
@@ -44,7 +51,7 @@
4552 if ( strpos( $text, TRANSLATE_FUZZY ) !== false ) return true;
4653
4754 // Figure out the group
48 - $groupKey = MessageIndex::titleToGroup( $title );
 55+ $groupKey = self::titleToGroup( $title );
4956 $group = MessageGroups::getGroup( $groupKey );
5057 if ( !$group instanceof WikiPageMessageGroup ) return;
5158
@@ -244,7 +251,7 @@
245252 public static function translationsCheck( $title, $user, $action, &$result ) {
246253 // Case 1: Unknown section translations
247254 if ( $title->getNamespace() == NS_TRANSLATIONS && $action === 'edit' ) {
248 - $group = MessageIndex::titleToGroup( $title );
 255+ $group = self::titleToGroup( $title );
249256 if ( $group === null ) {
250257 // No group means that the page is currently not
251258 // registered to any page translation message groups
Index: trunk/extensions/Translate/_autoload.php
@@ -75,10 +75,11 @@
7676 $wgAutoloadClasses['TranslatePreferences'] = $dir . 'utils/UserToggles.php';
7777 $wgAutoloadClasses['TranslateToolbox'] = $dir . 'utils/ToolBox.php';
7878
79 -$wgAutoloadClasses['MessageIndex'] = $dir . 'utils/MessageIndex.php';
 79+$wgAutoloadClasses['MessageIndexRebuilder'] = $dir . 'utils/MessageIndexRebuilder.php';
8080 $wgAutoloadClasses['MessageTable'] = $dir . 'utils/MessageTable.php';
8181 $wgAutoloadClasses['JsSelectToInput'] = $dir . 'utils/JsSelectToInput.php';
8282 $wgAutoloadClasses['HTMLJsSelectToInputField'] = $dir . 'utils/HTMLJsSelectToInputField.php';
 83+$wgAutoloadClasses['MessageGroupCache'] = $dir . 'utils/MessageGroupCache.php';
8384
8485
8586 # predefined groups
@@ -108,7 +109,6 @@
109110
110111 $wgAutoloadClasses['TranslateSpyc'] = $dir . 'spyc/loader.php';
111112 $wgAutoloadClasses['SpecialManageGroups'] = $dir . 'SpecialManageGroups.php';
112 -$wgAutoloadClasses['MessageGroupCache'] = $dir . 'utils/MessageGroupCache.php';
113113
114114 $wgAutoloadClasses['FFS'] = $dir . 'FFS.php';
115115 $wgAutoloadClasses['SimpleFFS'] = $dir . 'FFS.php';
Index: trunk/extensions/Translate/utils/MessageIndex.php
@@ -1,125 +0,0 @@
2 -<?php
3 -/**
4 - * Only used for page translation currently.
5 - *
6 - * @author Niklas Laxstrom
7 - *
8 - * @copyright Copyright © 2008-2009, Niklas Laxström
9 - * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
10 - * @file
11 - */
12 -
13 -class MessageIndex {
14 - static $cache = array();
15 -
16 - // Nice shortcut
17 - public static function titleToGroup( Title $title ) {
18 - $namespace = $title->getNamespace();
19 - $text = $title->getDBkey();
20 - list( $key, ) = TranslateUtils::figureMessage( $text );
21 - return self::messageToGroup( $namespace, $key );
22 - }
23 -
24 - public static function messageToGroup( $namespace, $key ) {
25 - $namepace = MWNamespace::getCanonicalName( $namespace );
26 - if ( $namespace === false ) return null;
27 -
28 - $key = self::normaliseKey( $key );
29 - $index = self::index( $namespace );
30 - return @$index[$key];
31 - }
32 -
33 - public static function cache( $namespace = null ) {
34 - $namespace = self::checkNs( $namespace );
35 - if ( $namespace === null ) return null;
36 -
37 - $groups = MessageGroups::singleton()->getGroups();
38 -
39 - $hugearray = array();
40 - $postponed = array();
41 -
42 - STDOUT( "Working with ", 'main' );
43 -
44 - foreach ( $groups as $g ) {
45 - # the cache is split per namespace for efficieny
46 - if ( $namespace !== null && $g->getNamespace() !== $namespace )
47 - continue;
48 -
49 - # Skip meta thingies
50 - if ( $g->isMeta() ) {
51 - $postponed[] = $g;
52 - continue;
53 - }
54 -
55 - self::checkAndAdd( $hugearray, $g );
56 - }
57 -
58 - foreach ( $postponed as $g ) {
59 - self::checkAndAdd( $hugearray, $g, true );
60 - }
61 -
62 - global $wgMemc;
63 - foreach ( $hugearray as $ns => $array ) {
64 - $memcKey = wfMemcKey( 'messageindex', $ns );
65 - $wgMemc->set( $memcKey, serialize( $array ), 60*60*12 );
66 - $cache = $wgMemc->get( $memcKey );
67 - }
68 - }
69 -
70 - protected static function checkAndAdd( &$hugearray, $g, $ignore = false ) {
71 - $messages = $g->getDefinitions();
72 - $id = $g->getId();
73 -
74 - if ( !is_array( $messages ) ) continue;
75 -
76 - STDOUT( "$id ", 'main' );
77 -
78 - $namespace = $g->getNamespace();
79 -
80 - foreach ( $messages as $key => $data ) {
81 - # Force all keys to lower case, because the case doesn't matter and it is
82 - # easier to do comparing when the case of first letter is unknown, because
83 - # mediawiki forces it to upper case
84 - $key = self::normaliseKey( $key );
85 - if ( isset( $hugearray[$namespace][$key] ) ) {
86 - if ( !$ignore )
87 - STDERR( "Key $key already belongs to $hugearray[$namespace][$key], conflict with $id" );
88 - } else {
89 - $hugearray[$namespace][$key] = &$id;
90 - }
91 - }
92 - unset( $id ); // Disconnect the previous references to this $id
93 -
94 - }
95 -
96 - protected static function normaliseKey( $key ) {
97 - global $wgContLang;
98 - return $wgContLang->ucfirst( str_replace( " ", "_", $key ) );
99 - }
100 -
101 - protected static function index( $namespace ) {
102 - $namespace = self::checkNs( $namespace );
103 - if ( $namespace === null ) return null;
104 -
105 - global $wgMemc;
106 - $memcKey = wfMemcKey( 'messageindex', $namespace );
107 - $cache = unserialize( $wgMemc->get($memcKey) );
108 -
109 - // Missing? Update it
110 - if ( !is_array($cache) ) self::cache( $namespace );
111 - $cache = unserialize( $wgMemc->get($memcKey) );
112 - if ( !is_array($cache) ) throw new MWException( "Caching failed: $namespace" );
113 -
114 - return $cache;
115 -
116 - }
117 -
118 - protected static function checkNs( $namespace ) {
119 - if ( $namespace !== null && $namespace == NS_TRANSLATIONS ) {
120 - $namepace = MWNamespace::getCanonicalName( $namespace );
121 - if ( $namespace === false ) return null;
122 - return $namespace;
123 - }
124 - return null;
125 - }
126 -}
\ No newline at end of file
Index: trunk/extensions/Translate/utils/MessageIndexRebuilder.php
@@ -0,0 +1,95 @@
 2+<?php
 3+/**
 4+ * Creates a database of keys in all groups, so that namespace and key can be
 5+ * used to get the group they belong to. This is used as a fallback when
 6+ * loadgroup parameter is not provided in the request, which happens if someone
 7+ * reaches a messages from somewhere else than Special:Translate.
 8+ *
 9+ * @author Niklas Laxstrom
 10+ *
 11+ * @copyright Copyright © 2008-2009, Niklas Laxström
 12+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 13+ * @file
 14+ */
 15+
 16+class MessageIndexRebuilder {
 17+ public static function execute() {
 18+
 19+ $groups = MessageGroups::singleton()->getGroups();
 20+
 21+ $hugearray = array();
 22+ $postponed = array();
 23+
 24+ STDOUT( "Working with ", 'main' );
 25+
 26+ foreach ( $groups as $g ) {
 27+ if ( !$g->exists() ) continue;
 28+ # Skip meta thingies
 29+ if ( $g->isMeta() ) {
 30+ $postponed[] = $g;
 31+ continue;
 32+ }
 33+
 34+ self::checkAndAdd( $hugearray, $g );
 35+ }
 36+
 37+ foreach ( $postponed as $g ) {
 38+ self::checkAndAdd( $hugearray, $g, true );
 39+ }
 40+
 41+ global $wgCacheDirectory;
 42+ $filename = "$wgCacheDirectory/translate_messageindex.cdb";
 43+ $writer = CdbWriter::open( $filename );
 44+ $writer->set( 'map', serialize($hugearray) );
 45+ $writer->close();
 46+ }
 47+
 48+ protected static function checkAndAdd( &$hugearray, $g, $ignore = false ) {
 49+ if ( $g instanceof MessageGroupBase ) {
 50+ $cache = new MessageGroupCache( $g );
 51+ if ( $cache->exists() ) {
 52+ $keys = $cache->getKeys();
 53+ } else {
 54+ $keys = array_keys( $g->load( 'en' ) );
 55+ }
 56+ } else {
 57+ $messages = $g->getDefinitions();
 58+ if ( !is_array( $messages ) ) continue;
 59+ $keys = array_keys( $messages );
 60+ }
 61+
 62+ $id = $g->getId();
 63+
 64+ STDOUT( "$id ", 'main' );
 65+
 66+ $namespace = $g->getNamespace();
 67+
 68+ foreach ( $keys as $key ) {
 69+ # Force all keys to lower case, because the case doesn't matter and it is
 70+ # easier to do comparing when the case of first letter is unknown, because
 71+ # mediawiki forces it to upper case
 72+ $key = TranslateUtils::normaliseKey( $namespace, $key );
 73+ if ( isset( $hugearray[$key] ) ) {
 74+ if ( !$ignore ) {
 75+ $to = implode( ', ', (array)$hugearray[$key] );
 76+ STDERR( "Key $key already belongs to $to, conflict with $id" );
 77+ }
 78+
 79+ if ( is_array($hugearray[$key]) ) {
 80+ // Hard work is already done, just add a new reference
 81+ $hugearray[$key][] = &$id;
 82+ } else {
 83+ // Store the actual reference, then remove it from array, to not
 84+ // replace the references value, but to store a array of new
 85+ // references instead. References are hard!
 86+ $value = &$hugearray[$key];
 87+ unset($hugearray[$key]);
 88+ $hugearray[$key] = array( &$value, &$id );
 89+ }
 90+ } else {
 91+ $hugearray[$key] = &$id;
 92+ }
 93+ }
 94+ unset( $id ); // Disconnect the previous references to this $id
 95+ }
 96+}
\ No newline at end of file
Property changes on: trunk/extensions/Translate/utils/MessageIndexRebuilder.php
___________________________________________________________________
Name: svn:eol-style
197 + native
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -172,11 +172,6 @@
173173 $index = self::messageIndex();
174174 $group = @$index[$normkey];
175175 if ( is_array($group) ) $group = $group[0];
176 -
177 - global $wgEnablePageTranslation;
178 - if ( $wgEnablePageTranslation && !$group ) {
179 - $group = MessageIndex::messageToGroup( $namespace, $key );
180 - }
181176 return $group;
182177 }
183178
@@ -193,9 +188,16 @@
194189 public static function messageIndex() {
195190 static $keyToGroup = null;
196191 if ( $keyToGroup !== null ) return $keyToGroup;
197 - if ( file_exists( TRANSLATE_INDEXFILE ) ) {
198 - $keyToGroup = unserialize( file_get_contents( TRANSLATE_INDEXFILE ) );
 192+
 193+ global $wgCacheDirectory;
 194+ $filename = "$wgCacheDirectory/translate_messageindex.cdb";
 195+ if ( !file_exists($filename) ) MessageIndexRebuilder::execute();
 196+
 197+ if ( file_exists($filename) ) {
 198+ $reader = CdbReader::open( $filename );
 199+ $keyToGroup = unserialize( $reader->get( 'map' ) );
199200 } else {
 201+ $keyToGroup = false;
200202 wfDebug( __METHOD__ . ": Message index missing." );
201203 }
202204

Status & tagging log