Using UTF-8 compatible ucfirst/lcfirst -- 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 $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, $wgLang;
97 if ( !$this->mKeys ) {
98 $this->mKeys = array();
99 foreach ( $wgAllMessagesEn as $key => $value ) {
100 array_push( $wgLang->ucfirst( $key ), $this->mKeys );
101 }
102 }
103 return $this->mKeys;
104 }
105
106 function isCacheable( $key ) {
107 global $wgAllMessagesEn, $wgLang;
108 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
109 array_key_exists( $key, $wgAllMessagesEn );
110 }
111
112 function replace( $title, $text ) {
113 global $wgMemc;
114 if ( $this->isCacheable( $title ) ) {
115 $this->lock();
116 $this->load();
117 if ( is_array( $this->mCache ) ) {
118 $this->mCache[$title] = $text;
119 $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
120 }
121 $this->unlock();
122 }
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 $wgLang->getMessage( $key );
163 }
164 $title = $wgLang->ucfirst( $key );
165
166 $message = false;
167
168 # Try the cache
169 if ( $this->mUseCache && $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 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
176 " AND cur_title='$title'";
177 $res = wfQuery( $sql, DB_READ, $fname );
178
179 if ( wfNumRows( $res ) ) {
180 $obj = wfFetchObject( $res );
181 $message = $obj->cur_text;
182 wfFreeResult( $res );
183 }
184 }
185
186 # Try the array in $wgLang
187 if ( !$message ) {
188 $message = $wgLang->getMessage( $key );
189 }
190
191 # Try the English array
192 if ( !$message && $wgLanguageCode != "en" ) {
193 $message = Language::getMessage( $key );
194 }
195
196 # Final fallback
197 if ( !$message ) {
198 $message = "&lt;$key&gt;";
199 }
200 return $message;
201 }
202 }
203 ?>