r98798 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r98797‎ | r98798 | r98799 >
Date:19:06, 3 October 2011
Author:nikerabbit
Status:ok
Tags:
Comment:
Renamed file to match class name, followup r98797
Modified paths:
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/utils/MessageIndex.php (added) (history)
  • /trunk/extensions/Translate/utils/MessageIndexRebuilder.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/Translate/_autoload.php
@@ -97,7 +97,7 @@
9898 $wgAutoloadClasses['TranslatePreferences'] = $dir . 'utils/UserToggles.php';
9999 $wgAutoloadClasses['TranslateToolbox'] = $dir . 'utils/ToolBox.php';
100100
101 -$wgAutoloadClasses['MessageIndex'] = $dir . 'utils/MessageIndexRebuilder.php';
 101+$wgAutoloadClasses['MessageIndex'] = $dir . 'utils/MessageIndex.php';
102102 $wgAutoloadClasses['MessageIndexRebuildJob'] = $dir . 'utils/MessageIndexRebuildJob.php';
103103 $wgAutoloadClasses['MessageTable'] = $dir . 'utils/MessageTable.php';
104104 $wgAutoloadClasses['StatsTable'] = $dir . 'utils/StatsTable.php';
Index: trunk/extensions/Translate/utils/MessageIndexRebuilder.php
@@ -1,200 +0,0 @@
2 -<?php
3 -/**
4 - * Contains classes for handling the message index.
5 - *
6 - * @file
7 - * @author Niklas Laxstrom
8 - * @copyright Copyright © 2008-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 - * Creates a database of keys in all groups, so that namespace and key can be
14 - * used to get the groups they belong to. This is used as a fallback when
15 - * loadgroup parameter is not provided in the request, which happens if someone
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.
19 - */
20 -abstract class MessageIndex {
21 - /// @var MessageIndex
22 - protected static $instance;
23 -
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 );
33 - }
34 - return self::$instance;
35 - }
36 -
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();
46 - $postponed = array();
47 -
48 - STDOUT( "Working with ", 'main' );
49 -
50 - foreach ( $groups as $g ) {
51 - if ( !$g->exists() ) {
52 - continue;
53 - }
54 -
55 - # Skip meta thingies
56 - if ( $g->isMeta() ) {
57 - $postponed[] = $g;
58 - continue;
59 - }
60 -
61 - $this->checkAndAdd( $new, $g );
62 - }
63 -
64 - foreach ( $postponed as $g ) {
65 - $this->checkAndAdd( $new, $g, true );
66 - }
67 -
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 ) {
76 - $changes = array();
77 - foreach ( array_diff_assoc( $new, $old ) as $groups ) {
78 - foreach ( (array) $groups as $group ) $changes[$group] = true;
79 - }
80 -
81 - foreach ( array_diff_assoc( $old, $new ) as $groups ) {
82 - foreach ( (array) $groups as $group ) $changes[$group] = true;
83 - }
84 -
85 - MessageGroupStats::clearGroup( array_keys( $changes ) );
86 - }
87 -
88 - protected function checkAndAdd( &$hugearray, $g, $ignore = false ) {
89 - if ( $g instanceof MessageGroupBase ) {
90 - $cache = new MessageGroupCache( $g );
91 -
92 - if ( $cache->exists() ) {
93 - $keys = $cache->getKeys();
94 - } else {
95 - $keys = array_keys( $g->load( 'en' ) );
96 - }
97 - } else {
98 - $messages = $g->getDefinitions();
99 -
100 - if ( !is_array( $messages ) ) {
101 - return;
102 - }
103 -
104 - $keys = array_keys( $messages );
105 - }
106 -
107 - $id = $g->getId();
108 -
109 - STDOUT( "$id ", 'main' );
110 -
111 - $namespace = $g->getNamespace();
112 -
113 - foreach ( $keys as $key ) {
114 - # Force all keys to lower case, because the case doesn't matter and it is
115 - # easier to do comparing when the case of first letter is unknown, because
116 - # mediawiki forces it to upper case
117 - $key = TranslateUtils::normaliseKey( $namespace, $key );
118 - if ( isset( $hugearray[$key] ) ) {
119 - if ( !$ignore ) {
120 - $to = implode( ', ', (array)$hugearray[$key] );
121 - STDERR( "Key $key already belongs to $to, conflict with $id" );
122 - }
123 -
124 - if ( is_array( $hugearray[$key] ) ) {
125 - // Hard work is already done, just add a new reference
126 - $hugearray[$key][] = &$id;
127 - } else {
128 - // Store the actual reference, then remove it from array, to not
129 - // replace the references value, but to store a array of new
130 - // references instead. References are hard!
131 - $value = &$hugearray[$key];
132 - unset( $hugearray[$key] );
133 - $hugearray[$key] = array( &$value, &$id );
134 - }
135 - } else {
136 - $hugearray[$key] = &$id;
137 - }
138 - }
139 - unset( $id ); // Disconnect the previous references to this $id
140 - }
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/MessageIndex.php
@@ -0,0 +1,200 @@
 2+<?php
 3+/**
 4+ * Contains classes for handling the message index.
 5+ *
 6+ * @file
 7+ * @author Niklas Laxstrom
 8+ * @copyright Copyright © 2008-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+ * Creates a database of keys in all groups, so that namespace and key can be
 14+ * used to get the groups they belong to. This is used as a fallback when
 15+ * loadgroup parameter is not provided in the request, which happens if someone
 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.
 19+ */
 20+abstract class MessageIndex {
 21+ /// @var MessageIndex
 22+ protected static $instance;
 23+
 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 );
 33+ }
 34+ return self::$instance;
 35+ }
 36+
 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();
 46+ $postponed = array();
 47+
 48+ STDOUT( "Working with ", 'main' );
 49+
 50+ foreach ( $groups as $g ) {
 51+ if ( !$g->exists() ) {
 52+ continue;
 53+ }
 54+
 55+ # Skip meta thingies
 56+ if ( $g->isMeta() ) {
 57+ $postponed[] = $g;
 58+ continue;
 59+ }
 60+
 61+ $this->checkAndAdd( $new, $g );
 62+ }
 63+
 64+ foreach ( $postponed as $g ) {
 65+ $this->checkAndAdd( $new, $g, true );
 66+ }
 67+
 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 ) {
 76+ $changes = array();
 77+ foreach ( array_diff_assoc( $new, $old ) as $groups ) {
 78+ foreach ( (array) $groups as $group ) $changes[$group] = true;
 79+ }
 80+
 81+ foreach ( array_diff_assoc( $old, $new ) as $groups ) {
 82+ foreach ( (array) $groups as $group ) $changes[$group] = true;
 83+ }
 84+
 85+ MessageGroupStats::clearGroup( array_keys( $changes ) );
 86+ }
 87+
 88+ protected function checkAndAdd( &$hugearray, $g, $ignore = false ) {
 89+ if ( $g instanceof MessageGroupBase ) {
 90+ $cache = new MessageGroupCache( $g );
 91+
 92+ if ( $cache->exists() ) {
 93+ $keys = $cache->getKeys();
 94+ } else {
 95+ $keys = array_keys( $g->load( 'en' ) );
 96+ }
 97+ } else {
 98+ $messages = $g->getDefinitions();
 99+
 100+ if ( !is_array( $messages ) ) {
 101+ return;
 102+ }
 103+
 104+ $keys = array_keys( $messages );
 105+ }
 106+
 107+ $id = $g->getId();
 108+
 109+ STDOUT( "$id ", 'main' );
 110+
 111+ $namespace = $g->getNamespace();
 112+
 113+ foreach ( $keys as $key ) {
 114+ # Force all keys to lower case, because the case doesn't matter and it is
 115+ # easier to do comparing when the case of first letter is unknown, because
 116+ # mediawiki forces it to upper case
 117+ $key = TranslateUtils::normaliseKey( $namespace, $key );
 118+ if ( isset( $hugearray[$key] ) ) {
 119+ if ( !$ignore ) {
 120+ $to = implode( ', ', (array)$hugearray[$key] );
 121+ STDERR( "Key $key already belongs to $to, conflict with $id" );
 122+ }
 123+
 124+ if ( is_array( $hugearray[$key] ) ) {
 125+ // Hard work is already done, just add a new reference
 126+ $hugearray[$key][] = &$id;
 127+ } else {
 128+ // Store the actual reference, then remove it from array, to not
 129+ // replace the references value, but to store a array of new
 130+ // references instead. References are hard!
 131+ $value = &$hugearray[$key];
 132+ unset( $hugearray[$key] );
 133+ $hugearray[$key] = array( &$value, &$id );
 134+ }
 135+ } else {
 136+ $hugearray[$key] = &$id;
 137+ }
 138+ }
 139+ unset( $id ); // Disconnect the previous references to this $id
 140+ }
 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+}
Property changes on: trunk/extensions/Translate/utils/MessageIndex.php
___________________________________________________________________
Added: svn:eol-style
1202 + native

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r98797Rewrote MessageIndexRebuilder to more generic class with different storage ba...nikerabbit19:05, 3 October 2011

Status & tagging log