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, $mParserOptions, $mParser;
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;
24 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
25 $this->mParser
= new Parser
;
30 # Loads messages either from memcached or the database, if not disabled
31 # On error, quietly switches to a fallback mode
32 # Returns false for a reportable error, true otherwise
34 global $wgAllMessagesEn, $wgMemc;
36 if ( $this->mDisable
) {
42 if ( $this->mUseCache
) {
43 $this->mCache
= $wgMemc->get( $this->mMemcKey
);
45 # If there's nothing in memcached, load all the messages from the database
46 if ( !$this->mCache
) {
48 # Other threads don't need to load the messages if another thread is doing it.
49 $wgMemc->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
52 if ( !$wgMemc->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
) ) {
53 # Hack for slabs reassignment problem
54 $wgMemc->set( $this->mMemcKey
, "error" );
55 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
60 if ( !is_array( $this->mCache
) ) {
61 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
62 if ( $this->mCache
== "loading" ) {
63 $this->mUseCache
= false;
64 } elseif ( $this->mCache
== "error" ) {
65 $this->mUseCache
= false;
68 $this->mDisable
= true;
71 $this->mCache
= false;
77 # Loads all cacheable messages from the database
80 $fname = "MessageCache::loadFromDB";
81 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI
;
82 $res = wfQuery( $sql, DB_READ
, $fname );
84 $this->mCache
= array();
85 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
86 $this->mCache
[$row->cur_title
] = $row->cur_text
;
92 # Not really needed anymore
94 global $wgAllMessagesEn, $wgLang;
95 if ( !$this->mKeys
) {
96 $this->mKeys
= array();
97 foreach ( $wgAllMessagesEn as $key => $value ) {
98 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
105 function isCacheable( $key ) {
108 global $wgAllMessagesEn, $wgLang;
109 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
110 array_key_exists( $key, $wgAllMessagesEn );
114 function replace( $title, $text ) {
118 if ( is_array( $this->mCache
) ) {
119 $this->mCache
[$title] = $text;
120 $wgMemc->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
126 # Represents a write lock on the messages key
130 if ( !$this->mUseCache
) {
134 $lockKey = $this->mMemcKey
. "lock";
135 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
139 return $i >= MSG_WAIT_TIMEOUT
;
145 if ( !$this->mUseCache
) {
149 $lockKey = $this->mMemcKey
. "lock";
150 $wgMemc->delete( $lockKey );
153 function get( $key, $useDB ) {
154 global $wgLang, $wgLanguageCode;
156 # If uninitialised, someone is trying to call this halfway through Setup.php
157 if ( !$this->mInitialised
) {
158 return "<$key>";
161 if ( $this->mDisable
) {
162 return $this->transform( $wgLang->getMessage( $key ) );
164 $title = $wgLang->ucfirst( $key );
169 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
170 $message = $this->mCache
[$title];
173 # If it wasn't in the cache, load each message from the DB individually
174 if ( !$message && $useDB) {
175 $result = wfGetArray( "cur", array("cur_text"),
176 array( "cur_namespace" => NS_MEDIAWIKI
, "cur_title" => $title ),
177 "MessageCache::get" );
179 $message = $result->cur_text
;
183 # Try the array in $wgLang
185 $message = $wgLang->getMessage( $key );
188 # Try the English array
189 if ( !$message && $wgLanguageCode != "en" ) {
190 $message = Language
::getMessage( $key );
195 $message = "<$key>";
199 $message = $this->transform( $message );
204 function transform( $message ) {
205 if ( strstr( $message, "{{" ) !== false ) {
206 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
211 function disable() { $this->mDisable
= true; }
212 function enable() { $this->mDisable
= false; }