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