Fixed initialisation order to make it slightly more robust
[lhc/web/wiklou.git] / includes / MessageCache.php
1 <?
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;
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
25 $this->load();
26 }
27
28 # Loads messages either from memcached or the database, if not disabled
29 # On error, quietly switches to a fallback mode
30 # Returns false for a reportable error, true otherwise
31 function load() {
32 global $wgAllMessagesEn, $wgMemc;
33
34 if ( $this->mDisable ) {
35 return true;
36 }
37
38 $success = true;
39
40 if ( $this->mUseCache ) {
41 $this->mCache = $wgMemc->get( $this->mMemcKey );
42
43 # If there's nothing in memcached, load all the messages from the database
44 if ( !$this->mCache ) {
45 $this->lock();
46 # Other threads don't need to load the messages if another thread is doing it.
47 $wgMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
48 $this->loadFromDB();
49 # Save in memcached
50 if ( !$wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
51 # Hack for slabs reassignment problem
52 $wgMemc->set( $this->mMemcKey, "error" );
53 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
54 }
55 $this->unlock();
56 }
57
58 if ( !is_array( $this->mCache ) ) {
59 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
60 if ( $this->mCache == "loading" ) {
61 $this->mUseCache = false;
62 } elseif ( $this->mCache == "error" ) {
63 $this->mUseCache = false;
64 $success = false;
65 } else {
66 $this->mDisable = true;
67 $success = false;
68 }
69 $this->mCache = false;
70 }
71 }
72 return $success;
73 }
74
75 # Loads all cacheable messages from the database
76 function loadFromDB()
77 {
78 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
79 $sql .= " AND cur_title IN ('";
80 $sql .= implode( "','", $this->getKeys() ) . "')";
81 wfDebug( "$sql\n" );
82
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 wfDebug( var_export( $this->mCache, true ) );
91
92 wfFreeResult( $res );
93 }
94
95 function getKeys() {
96 global $wgAllMessagesEn;
97 if ( !$this->mKeys ) {
98 $this->mKeys = array_map( 'ucfirst', array_keys( $wgAllMessagesEn ) );
99 }
100 return $this->mKeys;
101 }
102
103 function isCacheable( $key ) {
104 global $wgAllMessagesEn;
105 return array_key_exists( lcfirst( $key ), $wgAllMessagesEn ) ||
106 array_key_exists( $key, $wgAllMessagesEn );
107 }
108
109 function replace( $title, $text ) {
110 global $wgMemc;
111 if ( $this->isCacheable( $title ) ) {
112 $this->lock();
113 $this->load();
114 if ( is_array( $this->mCache ) ) {
115 $this->mCache[$title] = $text;
116 $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
117 }
118 $this->unlock();
119 }
120 }
121
122 # Returns success
123 # Represents a write lock on the messages key
124 function lock() {
125 global $wgMemc;
126
127 if ( !$this->mUseCache ) {
128 return true;
129 }
130
131 $lockKey = $this->mMemcKey . "lock";
132 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
133 sleep(1);
134 }
135
136 return $i >= MSG_WAIT_TIMEOUT;
137 }
138
139 function unlock() {
140 global $wgMemc;
141
142 if ( !$this->mUseCache ) {
143 return;
144 }
145
146 $lockKey = $this->mMemcKey . "lock";
147 $wgMemc->delete( $lockKey );
148 }
149
150 function get( $key, $useDB ) {
151 global $wgLang, $wgLanguageCode;
152
153 # If uninitialised, someone is trying to call this halfway through Setup.php
154 if ( !$this->mInitialised ) {
155 return "&lt;$key&gt;";
156 }
157
158 if ( $this->mDisable ) {
159 return $wgLang->getMessage( $key );
160 }
161 $title = ucfirst( $key );
162
163 $message = false;
164
165 # Try the cache
166 if ( $this->mUseCache && $this->mCache ) {
167 $message = $this->mCache[$title];
168 }
169
170 # If it wasn't in the cache, load each message from the DB individually
171 if ( !$message && $useDB) {
172 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
173 " AND cur_title='$title'";
174 $res = wfQuery( $sql, DB_READ, $fname );
175
176 if ( wfNumRows( $res ) ) {
177 $obj = wfFetchObject( $res );
178 $message = $obj->cur_text;
179 wfFreeResult( $res );
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 return $message;
198 }
199 }
200
201 function lcfirst( $s ) {
202 return strtolower( $s{0} ). substr( $s, 1 );
203 }
204 ?>