Somewhat improved
[mediawiki.git] / includes / MessageCache.php
blob3226653e53d0f01bee8035f81bcfc53d068b1673
1 <?
3 # Message cache
4 # Performs various useful MediaWiki namespace-related functions
6 define( "MSG_LOAD_TIMEOUT", 60);
7 define( "MSG_LOCK_TIMEOUT", 10);
8 define( "MSG_WAIT_TIMEOUT", 10);
10 class MessageCache
12 var $mCache, $mUseCache, $mDisable, $mExpiry;
13 var $mMemcKey, $mKeys;
15 function MessageCache( $useMemCached, $useDB, $expiry, $memcPrefix ) {
16 $this->initialise( $useMemCached, $useDB, $expiry, $memcPrefix );
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
26 $this->load();
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;
35 if ( $this->mDisable ) {
36 return true;
39 $success = true;
41 if ( $this->mUseCache ) {
42 $this->mCache = $wgMemc->get( $this->mMemcKey );
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" );
56 $this->unlock();
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;
70 $this->mCache = false;
73 return $success;
76 # Loads all cacheable messages from the database
77 function 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" );
84 $res = wfQuery( $sql, DB_READ, $fname );
86 $this->mCache = array();
87 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
88 $this->mCache[$row->cur_title] = $row->cur_text;
91 wfDebug( var_export( $this->mCache, true ) );
93 wfFreeResult( $res );
96 function getKeys() {
97 global $wgAllMessagesEn;
98 if ( !$this->mKeys ) {
99 $this->mKeys = array_map( 'ucfirst', array_keys( $wgAllMessagesEn ) );
101 return $this->mKeys;
104 function isCacheable( $key ) {
105 global $wgAllMessagesEn;
106 return array_key_exists( lcfirst( $key ), $wgAllMessagesEn ) ||
107 array_key_exists( $key, $wgAllMessagesEn );
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 );
119 $this->unlock();
123 # Returns success
124 # Represents a write lock on the messages key
125 function lock() {
126 global $wgMemc;
128 if ( !$this->mUseCache ) {
129 return true;
132 $lockKey = $this->mMemcKey . "lock";
133 for ($i=0; $i < MSG_WAIT_TIMEOUT && !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
134 sleep(1);
137 return $i >= MSG_WAIT_TIMEOUT;
140 function unlock() {
141 global $wgMemc;
143 if ( !$this->mUseCache ) {
144 return;
147 $lockKey = $this->mMemcKey . "lock";
148 $wgMemc->delete( $lockKey );
151 function get( $key, $useDB ) {
152 global $wgLang, $wgLanguageCode;
154 if ( $this->mDisable ) {
155 return $wgLang->getMessage( $key );
157 $title = ucfirst( $key );
159 $message = false;
161 # Try the cache
162 if ( $this->mUseCache && $this->mCache ) {
163 $message = $this->mCache[$title];
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 );
172 if ( wfNumRows( $res ) ) {
173 $obj = wfFetchObject( $res );
174 $message = $obj->cur_text;
175 wfFreeResult( $res );
179 # Try the array in $wgLang
180 if ( !$message ) {
181 $message = $wgLang->getMessage( $key );
184 # Try the English array
185 if ( !$message && $wgLanguageCode != "en" ) {
186 $message = Language::getMessage( $key );
189 # Final fallback
190 if ( !$message ) {
191 $message = "&lt;$key&gt;";
193 return $message;
197 function lcfirst( $s ) {
198 return strtolower( $s{0} ). substr( $s, 1 );