MediaWiki namespace handling, mostly consolidated from GlobalFunctions.php and Articl...
[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 function MessageCache( $useMemCached, $useDB, $expiry, $memcPrefix ) {
16 $this->initialise( $useMemCached, $useDB, $expiry, $memcPrefix );
17 }
18
19 function initialise( $useMemCached, $useDB, $expiry, $memcPrefix ) {
20 $this->mUseCache = $useMemCached;
21 $this->mDisable = !$useDB;
22 $this->mExpiry = $expiry;
23 $this->mMemcKey = "$memcPrefix:messages";
24 $this->mKeys = false; # initialised on demand
25
26 $this->load();
27 }
28
29 # Loads messages either from memcached or the database, if not disabled
30 # On error, quietly switches to a fallback mode
31 # Returns false for a reportable error, true otherwise
32 function load() {
33 global $wgAllMessagesEn, $wgMemc;
34
35 if ( $this->mDisable ) {
36 return true;
37 }
38
39 $success = true;
40
41 if ( $this->mUseCache ) {
42 $this->mCache = $wgMemc->get( $this->mMemcKey );
43
44 # If there's nothing in memcached, load all the messages from the database
45 if ( !$this->mCache ) {
46 $this->lock();
47 # Other threads don't need to load the messages if another thread is doing it.
48 $wgMemc->set( $this->mMemcKey, "loading", MSG_LOAD_TIMEOUT );
49 $this->loadFromDB();
50 # Save in memcached
51 if ( !$wgMemc->set( $this->mMemcKey, $this->mCache, $this->mExpiry ) ) {
52 # Hack for slabs reassignment problem
53 $wgMemc->set( $this->mMemcKey, "error" );
54 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
55 }
56 $this->unlock();
57 }
58
59 if ( !is_array( $this->mCache ) ) {
60 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
61 if ( $this->mCache == "loading" ) {
62 $this->mUseCache = false;
63 } elseif ( $this->mCache == "error" ) {
64 $this->mUseCache = false;
65 $success = false;
66 } else {
67 $this->mDisable = true;
68 $success = false;
69 }
70 $this->mCache = false;
71 }
72 }
73 return $success;
74 }
75
76 # Loads all cacheable messages from the database
77 function loadFromDB()
78 {
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;
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;
106 return array_key_exists( 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 ( $this->mDisable ) {
155 return $wgLang->getMessage( $key );
156 }
157 $title = ucfirst( $key );
158
159 $message = false;
160
161 # Try the cache
162 if ( $this->mUseCache && $this->mCache ) {
163 $message = $this->mCache[$title];
164 }
165
166 # If it wasn't in the cache, load each message from the DB individually
167 if ( !$message && $useDB) {
168 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI .
169 " AND cur_title='$title'";
170 $res = wfQuery( $sql, DB_READ, $fname );
171
172 if ( wfNumRows( $res ) ) {
173 $obj = wfFetchObject( $res );
174 $message = $obj->cur_text;
175 wfFreeResult( $res );
176 }
177 }
178
179 # Try the array in $wgLang
180 if ( !$message ) {
181 $message = $wgLang->getMessage( $key );
182 }
183
184 # Try the English array
185 if ( !$message && $wgLanguageCode != "en" ) {
186 $message = Language::getMessage( $key );
187 }
188
189 # Final fallback
190 if ( !$message ) {
191 $message = "&lt;$key&gt;";
192 }
193 return $message;
194 }
195 }
196
197 function lcfirst( $s ) {
198 return strtolower( $s{0} ). substr( $s, 1 );
199 }
200 ?>