Using UTF-8 compatible ucfirst/lcfirst
[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 $ucfirst = get_class($wgLang) . "::ucfirst";
98 if ( !$this->mKeys ) {
99 $this->mKeys = array_map( $ucfirst, array_keys( $wgAllMessagesEn ) );
100 }
101 return $this->mKeys;
102 }
103
104 function isCacheable( $key ) {
105 global $wgAllMessagesEn, $wgLang;
106 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
107 array_key_exists( $key, $wgAllMessagesEn );
108 }
109
110 function replace( $title, $text ) {
111 global $wgMemc;
112 if ( $this->isCacheable( $title ) ) {
113 $this->lock();
114 $this->load();
115 if ( is_array( $this->mCache ) ) {
116 $this->mCache[$title] = $text;
117 $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
118 }
119 $this->unlock();
120 }
121 }
122
123 # Returns success
124 # Represents a write lock on the messages key
125 function lock() {
126 global $wgMemc;
127
128 if ( !$this->mUseCache ) {
129 return true;
130 }
131
132 $lockKey = $this->mMemcKey . "lock";
133 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
134 sleep(1);
135 }
136
137 return $i >= MSG_WAIT_TIMEOUT;
138 }
139
140 function unlock() {
141 global $wgMemc;
142
143 if ( !$this->mUseCache ) {
144 return;
145 }
146
147 $lockKey = $this->mMemcKey . "lock";
148 $wgMemc->delete( $lockKey );
149 }
150
151 function get( $key, $useDB ) {
152 global $wgLang, $wgLanguageCode;
153
154 # If uninitialised, someone is trying to call this halfway through Setup.php
155 if ( !$this->mInitialised ) {
156 return "&lt;$key&gt;";
157 }
158
159 if ( $this->mDisable ) {
160 return $wgLang->getMessage( $key );
161 }
162 $title = $wgLang->ucfirst( $key );
163
164 $message = false;
165
166 # Try the cache
167 if ( $this->mUseCache && $this->mCache ) {
168 $message = $this->mCache[$title];
169 }
170
171 # If it wasn't in the cache, load each message from the DB individually
172 if ( !$message && $useDB) {
173 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
174 " AND cur_title='$title'";
175 $res = wfQuery( $sql, DB_READ, $fname );
176
177 if ( wfNumRows( $res ) ) {
178 $obj = wfFetchObject( $res );
179 $message = $obj->cur_text;
180 wfFreeResult( $res );
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 return $message;
199 }
200 }
201 ?>