Introducing special page modular extensions, making the board vote special page the...
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?php
2
3 # Message cache
4 # Performs various useful MediaWiki namespace-related functions
5
6 define( "MSG_LOAD_TIMEOUT", 60);
7 define( "MSG_LOCK_TIMEOUT", 10);
8 define( "MSG_WAIT_TIMEOUT", 10);
9
10 class MessageCache
11 {
12 var $mCache, $mUseCache, $mDisable, $mExpiry;
13 var $mMemcKey, $mKeys, $mParserOptions, $mParser;
14 var $mExtensionMessages;
15
16 var $mInitialised = false;
17
18 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
19 $this->mUseCache = !is_null( $memCached );
20 $this->mMemc = &$memCached;
21 $this->mDisable = !$useDB;
22 $this->mExpiry = $expiry;
23 $this->mDisableTransform = false;
24 $this->mMemcKey = "$memcPrefix:messages";
25 $this->mKeys = false; # initialised on demand
26 $this->mInitialised = true;
27 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
28 $this->mParser = new Parser;
29
30 $this->load();
31 }
32
33 # Loads messages either from memcached or the database, if not disabled
34 # On error, quietly switches to a fallback mode
35 # Returns false for a reportable error, true otherwise
36 function load() {
37 global $wgAllMessagesEn;
38
39 if ( $this->mDisable ) {
40 return true;
41 }
42
43 $success = true;
44
45 if ( $this->mUseCache ) {
46 $this->mCache = $this->mMemc->get( $this->mMemcKey );
47
48 # If there's nothing in memcached, load all the messages from the database
49 if ( !$this->mCache ) {
50 $this->lock();
51 # Other threads don't need to load the messages if another thread is doing it.
52 $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
53 $this->loadFromDB();
54 # Save in memcached
55 if ( !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
56 # Hack for slabs reassignment problem
57 $this->mMemc->set( $this->mMemcKey, "error" );
58 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
59 }
60 $this->unlock();
61 }
62
63 if ( !is_array( $this->mCache ) ) {
64 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
65 if ( $this->mCache == "loading" ) {
66 $this->mUseCache = false;
67 } elseif ( $this->mCache == "error" ) {
68 $this->mUseCache = false;
69 $success = false;
70 } else {
71 $this->mDisable = true;
72 $success = false;
73 }
74 $this->mCache = false;
75 }
76 }
77 return $success;
78 }
79
80 # Loads all cacheable messages from the database
81 function loadFromDB()
82 {
83 $fname = "MessageCache::loadFromDB";
84 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
85 $res = wfQuery( $sql, DB_READ, $fname );
86
87 $this->mCache = array();
88 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
89 $this->mCache[$row->cur_title] = $row->cur_text;
90 }
91
92 wfFreeResult( $res );
93 }
94
95 # Not really needed anymore
96 function getKeys() {
97 global $wgAllMessagesEn, $wgLang;
98 if ( !$this->mKeys ) {
99 $this->mKeys = array();
100 foreach ( $wgAllMessagesEn as $key => $value ) {
101 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
102 }
103 }
104 return $this->mKeys;
105 }
106
107 # Obsolete
108 function isCacheable( $key ) {
109 return true;
110 /*
111 global $wgAllMessagesEn, $wgLang;
112 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
113 array_key_exists( $key, $wgAllMessagesEn );
114 */
115 }
116
117 function replace( $title, $text ) {
118 $this->lock();
119 $this->load();
120 if ( is_array( $this->mCache ) ) {
121 $this->mCache[$title] = $text;
122 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
123 }
124 $this->unlock();
125 }
126
127 # Returns success
128 # Represents a write lock on the messages key
129 function lock() {
130 if ( !$this->mUseCache ) {
131 return true;
132 }
133
134 $lockKey = $this->mMemcKey . "lock";
135 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
136 sleep(1);
137 }
138
139 return $i >= MSG_WAIT_TIMEOUT;
140 }
141
142 function unlock() {
143 if ( !$this->mUseCache ) {
144 return;
145 }
146
147 $lockKey = $this->mMemcKey . "lock";
148 $this->mMemc->delete( $lockKey );
149 }
150
151 function get( $key, $useDB ) {
152 global $wgLang, $wgLanguageCode;
153
154 # If uninitialised, someone is trying to call this halfway through Setup.php
155 if ( !$this->mInitialised ) {
156 return "&lt;$key&gt;";
157 }
158
159 $message = false;
160 if ( !$this->mDisable ) {
161 $title = $wgLang->ucfirst( $key );
162
163
164 # Try the cache
165 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
166 $message = $this->mCache[$title];
167 }
168
169 # If it wasn't in the cache, load each message from the DB individually
170 if ( !$message && $useDB) {
171 $result = wfGetArray( "cur", array("cur_text"),
172 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
173 "MessageCache::get" );
174 if ( $result ) {
175 $message = $result->cur_text;
176 }
177 }
178 }
179 # Try the extension array
180 if ( !$message ) {
181 $message = @$this->mExtensionMessages[$key];
182 }
183
184 # Try the array in $wgLang
185 if ( !$message ) {
186 $message = $wgLang->getMessage( $key );
187 }
188
189 # Try the English array
190 if ( !$message && $wgLanguageCode != "en" ) {
191 $message = Language::getMessage( $key );
192 }
193
194 # Final fallback
195 if ( !$message ) {
196 $message = "&lt;$key&gt;";
197 }
198
199 # Replace brace tags
200 $message = $this->transform( $message );
201 return $message;
202 }
203
204 function transform( $message ) {
205 if( !$this->mDisableTransform ) {
206 if ( strstr( $message, "{{" ) !== false ) {
207 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
208 }
209 }
210 return $message;
211 }
212
213 function disable() { $this->mDisable = true; }
214 function enable() { $this->mDisable = false; }
215 function disableTransform() { $this->mDisableTransform = true; }
216
217 function addMessage( $key, $value ) {
218 $this->mExtensionMessages[$key] = $value;
219 }
220
221 function addMessages( $messages ) {
222 foreach ( $messages as $key => $value ) {
223 $this->mExtensionMessages[$key] = $value;
224 }
225 }
226 }
227 ?>