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;
14 var $mExtensionMessages;
16 var $mInitialised = false;
18 function initialise( &$memCached, $useDB, $expiry, $memcPrefix ) {
19 $this->mUseCache
= !is_null( $memCached );
20 $this->mMemc
= &$memCached;
21 $this->mDisable
= !$useDB;
22 $this->mExpiry
= $expiry;
23 $this->mDisableTransform
= false;
24 $this->mMemcKey
= "$memcPrefix:messages";
25 $this->mKeys
= false; # initialised on demand
26 $this->mInitialised
= true;
27 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
28 $this->mParser
= new Parser
;
33 # Loads messages either from memcached or the database, if not disabled
34 # On error, quietly switches to a fallback mode
35 # Returns false for a reportable error, true otherwise
37 global $wgAllMessagesEn;
39 if ( $this->mDisable
) {
45 if ( $this->mUseCache
) {
46 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
48 # If there's nothing in memcached, load all the messages from the database
49 if ( !$this->mCache
) {
51 # Other threads don't need to load the messages if another thread is doing it.
52 $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
55 if ( !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
) ) {
56 # Hack for slabs reassignment problem
57 $this->mMemc
->set( $this->mMemcKey
, "error" );
58 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
63 if ( !is_array( $this->mCache
) ) {
64 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
65 if ( $this->mCache
== "loading" ) {
66 $this->mUseCache
= false;
67 } elseif ( $this->mCache
== "error" ) {
68 $this->mUseCache
= false;
71 $this->mDisable
= true;
74 $this->mCache
= false;
80 # Loads all cacheable messages from the database
83 $fname = "MessageCache::loadFromDB";
84 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI
;
85 $res = wfQuery( $sql, DB_READ
, $fname );
87 $this->mCache
= array();
88 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
89 $this->mCache
[$row->cur_title
] = $row->cur_text
;
95 # Not really needed anymore
97 global $wgAllMessagesEn, $wgLang;
98 if ( !$this->mKeys
) {
99 $this->mKeys
= array();
100 foreach ( $wgAllMessagesEn as $key => $value ) {
101 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
108 function isCacheable( $key ) {
111 global $wgAllMessagesEn, $wgLang;
112 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
113 array_key_exists( $key, $wgAllMessagesEn );
117 function replace( $title, $text ) {
120 if ( is_array( $this->mCache
) ) {
121 $this->mCache
[$title] = $text;
122 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
128 # 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
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
139 return $i >= MSG_WAIT_TIMEOUT
;
143 if ( !$this->mUseCache
) {
147 $lockKey = $this->mMemcKey
. "lock";
148 $this->mMemc
->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>";
160 if ( !$this->mDisable
) {
161 $title = $wgLang->ucfirst( $key );
165 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
166 $message = $this->mCache
[$title];
169 # If it wasn't in the cache, load each message from the DB individually
170 if ( !$message && $useDB) {
171 $result = wfGetArray( "cur", array("cur_text"),
172 array( "cur_namespace" => NS_MEDIAWIKI
, "cur_title" => $title ),
173 "MessageCache::get" );
175 $message = $result->cur_text
;
179 # Try the extension array
181 $message = @$this->mExtensionMessages
[$key];
184 # Try the array in $wgLang
186 $message = $wgLang->getMessage( $key );
189 # Try the English array
190 if ( !$message && $wgLanguageCode != "en" ) {
191 $message = Language
::getMessage( $key );
196 $message = "<$key>";
200 $message = $this->transform( $message );
204 function transform( $message ) {
205 if( !$this->mDisableTransform
) {
206 if ( strstr( $message, "{{" ) !== false ) {
207 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
213 function disable() { $this->mDisable
= true; }
214 function enable() { $this->mDisable
= false; }
215 function disableTransform() { $this->mDisableTransform
= true; }
217 function addMessage( $key, $value ) {
218 $this->mExtensionMessages
[$key] = $value;
221 function addMessages( $messages ) {
222 foreach ( $messages as $key => $value ) {
223 $this->mExtensionMessages
[$key] = $value;