OOP calling convention for database functions. DBMS abstraction implemented by means...
[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 wfDebug( "MessageCache::load(): disabled\n" );
41 return true;
42 }
43
44 $success = true;
45
46 if ( $this->mUseCache ) {
47 $this->mCache = $this->mMemc->get( $this->mMemcKey );
48
49 # If there's nothing in memcached, load all the messages from the database
50 if ( !$this->mCache ) {
51 wfDebug( "MessageCache::load(): loading all messages\n" );
52 $this->lock();
53 # Other threads don't need to load the messages if another thread is doing it.
54 $this->mMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
55 $this->loadFromDB();
56 # Save in memcached
57 # Keep trying if it fails, this is kind of important
58 for ( $i=0; $i<20 && !$this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ); $i++ ) {
59 usleep(mt_rand(500000,1500000));
60 }
61 if ( $i == 20 ) {
62 $this->mMemc->set( $this->mMemcKey, "error", 86400 );
63 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
64 }
65 $this->unlock();
66 }
67
68 if ( !is_array( $this->mCache ) ) {
69 wfMsg( "MessageCache::load(): individual message mode\n" );
70 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
71 # Causing too much DB load, disabling -- TS
72 $this->mDisable = true;
73 /*
74 if ( $this->mCache == "loading" ) {
75 $this->mUseCache = false;
76 } elseif ( $this->mCache == "error" ) {
77 $this->mUseCache = false;
78 $success = false;
79 } else {
80 $this->mDisable = true;
81 $success = false;
82 }*/
83 $this->mCache = false;
84 }
85 }
86 return $success;
87 }
88
89 # Loads all cacheable messages from the database
90 function loadFromDB()
91 {
92 $fname = "MessageCache::loadFromDB";
93 $dbr =& wfGetDB( DB_READ );
94 $res = $dbr->select( 'cur',
95 array( 'cur_title', 'cur_text' ),
96 array( 'cur_is_redirect' => 0, 'cur_namespace' => NS_MEDIAWIKI ),
97 $fname
98 );
99
100 $this->mCache = array();
101 for ( $row = $dbr->fetchObject( $res ); $row; $row = $dbr->fetchObject( $res ) ) {
102 $this->mCache[$row->cur_title] = $row->cur_text;
103 }
104
105 $dbr->freeResult( $res );
106 }
107
108 # Not really needed anymore
109 function getKeys() {
110 global $wgAllMessagesEn, $wgLang;
111 if ( !$this->mKeys ) {
112 $this->mKeys = array();
113 foreach ( $wgAllMessagesEn as $key => $value ) {
114 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
115 }
116 }
117 return $this->mKeys;
118 }
119
120 # Obsolete
121 function isCacheable( $key ) {
122 return true;
123 /*
124 global $wgAllMessagesEn, $wgLang;
125 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
126 array_key_exists( $key, $wgAllMessagesEn );
127 */
128 }
129
130 function replace( $title, $text ) {
131 $this->lock();
132 $this->load();
133 if ( is_array( $this->mCache ) ) {
134 $this->mCache[$title] = $text;
135 $this->mMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
136 }
137 $this->unlock();
138 }
139
140 # Returns success
141 # Represents a write lock on the messages key
142 function lock() {
143 if ( !$this->mUseCache ) {
144 return true;
145 }
146
147 $lockKey = $this->mMemcKey . "lock";
148 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
149 sleep(1);
150 }
151
152 return $i >= MSG_WAIT_TIMEOUT;
153 }
154
155 function unlock() {
156 if ( !$this->mUseCache ) {
157 return;
158 }
159
160 $lockKey = $this->mMemcKey . "lock";
161 $this->mMemc->delete( $lockKey );
162 }
163
164 function get( $key, $useDB ) {
165 global $wgLang, $wgLanguageCode;
166
167 # If uninitialised, someone is trying to call this halfway through Setup.php
168 if ( !$this->mInitialised ) {
169 return "&lt;$key&gt;";
170 }
171
172 $message = false;
173 if ( !$this->mDisable ) {
174 $title = $wgLang->ucfirst( $key );
175
176
177 # Try the cache
178 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
179 $message = $this->mCache[$title];
180 }
181
182 # If it wasn't in the cache, load each message from the DB individually
183 if ( !$message && $useDB) {
184 $dbr =& wfGetDB( DB_READ );
185 $result = $dbr->getArray( "cur", array("cur_text"),
186 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
187 "MessageCache::get" );
188 if ( $result ) {
189 $message = $result->cur_text;
190 }
191 }
192 }
193 # Try the extension array
194 if ( !$message ) {
195 $message = @$this->mExtensionMessages[$key];
196 }
197
198 # Try the array in $wgLang
199 if ( !$message ) {
200 $message = $wgLang->getMessage( $key );
201 }
202
203 # Try the English array
204 if ( !$message && $wgLanguageCode != "en" ) {
205 $message = Language::getMessage( $key );
206 }
207
208 # Final fallback
209 if ( !$message ) {
210 $message = "&lt;$key&gt;";
211 }
212
213 # Replace brace tags
214 $message = $this->transform( $message );
215 return $message;
216 }
217
218 function transform( $message ) {
219 if( !$this->mDisableTransform ) {
220 if ( strstr( $message, "{{" ) !== false ) {
221 $message = $this->mParser->transformMsg( $message, $this->mParserOptions );
222 }
223 }
224 return $message;
225 }
226
227 function disable() { $this->mDisable = true; }
228 function enable() { $this->mDisable = false; }
229 function disableTransform() { $this->mDisableTransform = true; }
230
231 function addMessage( $key, $value ) {
232 $this->mExtensionMessages[$key] = $value;
233 }
234
235 function addMessages( $messages ) {
236 foreach ( $messages as $key => $value ) {
237 $this->mExtensionMessages[$key] = $value;
238 }
239 }
240 }
241 ?>