bug fix
[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 $fname = "MessageCache::loadFromDB";
79 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI;
80 $sql .= " AND cur_title IN ('";
81 $sql .= implode( "','", $this->getKeys() ) . "')";
82 wfDebug( "$sql\n" );
83
84 $res = wfQuery( $sql, DB_READ, $fname );
85
86 $this->mCache = array();
87 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
88 $this->mCache[$row->cur_title] = $row->cur_text;
89 }
90
91 wfDebug( var_export( $this->mCache, true ) );
92
93 wfFreeResult( $res );
94 }
95
96 function getKeys() {
97 global $wgAllMessagesEn, $wgLang;
98 if ( !$this->mKeys ) {
99 $this->mKeys = array();
100 foreach ( $wgAllMessagesEn as $key => $value ) {
101 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
102 }
103 }
104 return $this->mKeys;
105 }
106
107 function isCacheable( $key ) {
108 global $wgAllMessagesEn, $wgLang;
109 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
110 array_key_exists( $key, $wgAllMessagesEn );
111 }
112
113 function replace( $title, $text ) {
114 global $wgMemc;
115 if ( $this->isCacheable( $title ) ) {
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
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 $wgLang->getMessage( $key );
164 }
165 $title = $wgLang->ucfirst( $key );
166
167 $message = false;
168
169 # Try the cache
170 if ( $this->mUseCache && $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 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
177 " AND cur_title='$title'";
178 $res = wfQuery( $sql, DB_READ, $fname );
179
180 if ( wfNumRows( $res ) ) {
181 $obj = wfFetchObject( $res );
182 $message = $obj->cur_text;
183 wfFreeResult( $res );
184 }
185 }
186
187 # Try the array in $wgLang
188 if ( !$message ) {
189 $message = $wgLang->getMessage( $key );
190 }
191
192 # Try the English array
193 if ( !$message && $wgLanguageCode != "en" ) {
194 $message = Language::getMessage( $key );
195 }
196
197 # Final fallback
198 if ( !$message ) {
199 $message = "&lt;$key&gt;";
200 }
201 return $message;
202 }
203 }
204 ?>