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);
12 var $mCache, $mUseCache, $mDisable, $mExpiry;
13 var $mMemcKey, $mKeys;
15 var $mInitialised = false;
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;
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
32 global $wgAllMessagesEn, $wgMemc;
34 if ( $this->mDisable
) {
40 if ( $this->mUseCache
) {
41 $this->mCache
= $wgMemc->get( $this->mMemcKey
);
43 # If there's nothing in memcached, load all the messages from the database
44 if ( !$this->mCache
) {
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
);
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" );
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;
66 $this->mDisable
= true;
69 $this->mCache
= false;
75 # Loads all cacheable messages from the database
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 );
82 $this->mCache
= array();
83 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
84 $this->mCache
[$row->cur_title
] = $row->cur_text
;
90 # Not really needed anymore
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 ) );
103 function isCacheable( $key ) {
106 global $wgAllMessagesEn, $wgLang;
107 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
108 array_key_exists( $key, $wgAllMessagesEn );
112 function replace( $title, $text ) {
116 if ( is_array( $this->mCache
) ) {
117 $this->mCache
[$title] = $text;
118 $wgMemc->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
124 # Represents a write lock on the messages key
128 if ( !$this->mUseCache
) {
132 $lockKey = $this->mMemcKey
. "lock";
133 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
137 return $i >= MSG_WAIT_TIMEOUT
;
143 if ( !$this->mUseCache
) {
147 $lockKey = $this->mMemcKey
. "lock";
148 $wgMemc->delete( $lockKey );
151 function get( $key, $useDB ) {
152 global $wgLang, $wgLanguageCode;
154 # If uninitialised, someone is trying to call this halfway through Setup.php
155 if ( !$this->mInitialised
) {
156 return "<$key>";
159 if ( $this->mDisable
) {
160 return $wgLang->getMessage( $key );
162 $title = $wgLang->ucfirst( $key );
167 if ( $this->mUseCache
&& $this->mCache
) {
168 $message = $this->mCache
[$title];
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" );
177 $message = $result->cur_text
;
181 # Try the array in $wgLang
183 $message = $wgLang->getMessage( $key );
186 # Try the English array
187 if ( !$message && $wgLanguageCode != "en" ) {
188 $message = Language
::getMessage( $key );
193 $message = "<$key>";
198 function disable() { $this->mDisable
= true; }
199 function enable() { $this->mDisable
= false; }