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