table definitions for board vote
[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
15 var $mInitialised = false;
16
17 function initialise( $useMemCached, $useDB, $expiry, $memcPrefix ) {
18 $this->mUseCache = $useMemCached;
19 $this->mDisable = !$useDB;
20 $this->mExpiry = $expiry;
21 $this->mDisableTransform = false;
22 $this->mMemcKey = "$memcPrefix:messages";
23 $this->mKeys = false; # initialised on demand
24 $this->mInitialised = true;
25 $this->mParserOptions = ParserOptions::newFromUser( $u=NULL );
26 $this->mParser = new Parser;
27
28 $this->load();
29 }
30
31 # Loads messages either from memcached or the database, if not disabled
32 # On error, quietly switches to a fallback mode
33 # Returns false for a reportable error, true otherwise
34 function load() {
35 global $wgAllMessagesEn, $wgMemc;
36
37 if ( $this->mDisable ) {
38 return true;
39 }
40
41 $success = true;
42
43 if ( $this->mUseCache ) {
44 $this->mCache = $wgMemc->get( $this->mMemcKey );
45
46 # If there's nothing in memcached, load all the messages from the database
47 if ( !$this->mCache ) {
48 $this->lock();
49 # Other threads don't need to load the messages if another thread is doing it.
50 $wgMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
51 $this->loadFromDB();
52 # Save in memcached
53 if ( !$wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
54 # Hack for slabs reassignment problem
55 $wgMemc->set( $this->mMemcKey, "error" );
56 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
57 }
58 $this->unlock();
59 }
60
61 if ( !is_array( $this->mCache ) ) {
62 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
63 if ( $this->mCache == "loading" ) {
64 $this->mUseCache = false;
65 } elseif ( $this->mCache == "error" ) {
66 $this->mUseCache = false;
67 $success = false;
68 } else {
69 $this->mDisable = true;
70 $success = false;
71 }
72 $this->mCache = false;
73 }
74 }
75 return $success;
76 }
77
78 # Loads all cacheable messages from the database
79 function loadFromDB()
80 {
81 $fname = "MessageCache::loadFromDB";
82 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
83 $res = wfQuery( $sql, DB_READ, $fname );
84
85 $this->mCache = array();
86 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
87 $this->mCache[$row->cur_title] = $row->cur_text;
88 }
89
90 wfFreeResult( $res );
91 }
92
93 # Not really needed anymore
94 function getKeys() {
95 global $wgAllMessagesEn, $wgLang;
96 if ( !$this->mKeys ) {
97 $this->mKeys = array();
98 foreach ( $wgAllMessagesEn as $key => $value ) {
99 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
100 }
101 }
102 return $this->mKeys;
103 }
104
105 # Obsolete
106 function isCacheable( $key ) {
107 return true;
108 /*
109 global $wgAllMessagesEn, $wgLang;
110 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
111 array_key_exists( $key, $wgAllMessagesEn );
112 */
113 }
114
115 function replace( $title, $text ) {
116 global $wgMemc;
117 $this->lock();
118 $this->load();
119 if ( is_array( $this->mCache ) ) {
120 $this->mCache[$title] = $text;
121 $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
122 }
123 $this->unlock();
124 }
125
126 # Returns success
127 # Represents a write lock on the messages key
128 function lock() {
129 global $wgMemc;
130
131 if ( !$this->mUseCache ) {
132 return true;
133 }
134
135 $lockKey = $this->mMemcKey . "lock";
136 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
137 sleep(1);
138 }
139
140 return $i >= MSG_WAIT_TIMEOUT;
141 }
142
143 function unlock() {
144 global $wgMemc;
145
146 if ( !$this->mUseCache ) {
147 return;
148 }
149
150 $lockKey = $this->mMemcKey . "lock";
151 $wgMemc->delete( $lockKey );
152 }
153
154 function get( $key, $useDB ) {
155 global $wgLang, $wgLanguageCode;
156
157 # If uninitialised, someone is trying to call this halfway through Setup.php
158 if ( !$this->mInitialised ) {
159 return "&lt;$key&gt;";
160 }
161
162 if ( $this->mDisable ) {
163 return $this->transform( $wgLang->getMessage( $key ) );
164 }
165 $title = $wgLang->ucfirst( $key );
166
167 $message = false;
168
169 # Try the cache
170 if ( $this->mUseCache && $this->mCache && array_key_exists( $title, $this->mCache ) ) {
171 $message = $this->mCache[$title];
172 }
173
174 # If it wasn't in the cache, load each message from the DB individually
175 if ( !$message && $useDB) {
176 $result = wfGetArray( "cur", array("cur_text"),
177 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
178 "MessageCache::get" );
179 if ( $result ) {
180 $message = $result->cur_text;
181 }
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 }
218 ?>