Printable mode cleanup. Now done through stylesheets, <link>ed so that the
[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;
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 $res = wfQuery( $sql, DB_READ, $fname );
81
82 $this->mCache = array();
83 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
84 $this->mCache[$row->cur_title] = $row->cur_text;
85 }
86
87 wfFreeResult( $res );
88 }
89
90 # Not really needed anymore
91 function getKeys() {
92 global $wgAllMessagesEn, $wgLang;
93 if ( !$this->mKeys ) {
94 $this->mKeys = array();
95 foreach ( $wgAllMessagesEn as $key => $value ) {
96 array_push( $this->mKeys, $wgLang->ucfirst( $key ) );
97 }
98 }
99 return $this->mKeys;
100 }
101
102 # Obsolete
103 function isCacheable( $key ) {
104 return true;
105 /*
106 global $wgAllMessagesEn, $wgLang;
107 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
108 array_key_exists( $key, $wgAllMessagesEn );
109 */
110 }
111
112 function replace( $title, $text ) {
113 global $wgMemc;
114 $this->lock();
115 $this->load();
116 if ( is_array( $this->mCache ) ) {
117 $this->mCache[$title] = $text;
118 $wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry );
119 }
120 $this->unlock();
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 $result = wfGetArray( "cur", array("cur_text"),
174 array( "cur_namespace" => NS_MEDIAWIKI, "cur_title" => $title ),
175 "MessageCache::get" );
176 if ( $result ) {
177 $message = $result->cur_text;
178 }
179 }
180
181 # Try the array in $wgLang
182 if ( !$message ) {
183 $message = $wgLang->getMessage( $key );
184 }
185
186 # Try the English array
187 if ( !$message && $wgLanguageCode != "en" ) {
188 $message = Language::getMessage( $key );
189 }
190
191 # Final fallback
192 if ( !$message ) {
193 $message = "&lt;$key&gt;";
194 }
195 return $message;
196 }
197
198 function disable() { $this->mDisable = true; }
199 function enable() { $this->mDisable = false; }
200
201 }
202 ?>