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( &$memCached, $useDB, $expiry, $memcPrefix ) {
18 $this->mUseCache
= !is_null( $memCached );
19 $this->mMemc
= &$memCached;
20 $this->mDisable
= !$useDB;
21 $this->mExpiry
= $expiry;
22 $this->mDisableTransform
= false;
23 $this->mMemcKey
= "$memcPrefix:messages";
24 $this->mKeys
= false; # initialised on demand
25 $this->mInitialised
= true;
26 $this->mParserOptions
= ParserOptions
::newFromUser( $u=NULL );
27 $this->mParser
= new Parser
;
32 # Loads messages either from memcached or the database, if not disabled
33 # On error, quietly switches to a fallback mode
34 # Returns false for a reportable error, true otherwise
36 global $wgAllMessagesEn;
38 if ( $this->mDisable
) {
44 if ( $this->mUseCache
) {
45 $this->mCache
= $this->mMemc
->get( $this->mMemcKey
);
47 # If there's nothing in memcached, load all the messages from the database
48 if ( !$this->mCache
) {
50 # Other threads don't need to load the messages if another thread is doing it.
51 $this->mMemc
->set( $this->mMemcKey
, "loading", MSG_LOAD_TIMEOUT
);
54 if ( !$this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
) ) {
55 # Hack for slabs reassignment problem
56 $this->mMemc
->set( $this->mMemcKey
, "error" );
57 wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
62 if ( !is_array( $this->mCache
) ) {
63 # If it is 'loading' or 'error', switch to individual message mode, otherwise disable
64 if ( $this->mCache
== "loading" ) {
65 $this->mUseCache
= false;
66 } elseif ( $this->mCache
== "error" ) {
67 $this->mUseCache
= false;
70 $this->mDisable
= true;
73 $this->mCache
= false;
79 # Loads all cacheable messages from the database
82 $fname = "MessageCache::loadFromDB";
83 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI
;
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
;
94 # Not really needed anymore
96 global $wgAllMessagesEn, $wgLang;
97 if ( !$this->mKeys
) {
98 $this->mKeys
= array();
99 foreach ( $wgAllMessagesEn as $key => $value ) {
100 array_push( $this->mKeys
, $wgLang->ucfirst( $key ) );
107 function isCacheable( $key ) {
110 global $wgAllMessagesEn, $wgLang;
111 return array_key_exists( $wgLang->lcfirst( $key ), $wgAllMessagesEn ) ||
112 array_key_exists( $key, $wgAllMessagesEn );
116 function replace( $title, $text ) {
119 if ( is_array( $this->mCache
) ) {
120 $this->mCache
[$title] = $text;
121 $this->mMemc
->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
127 # Represents a write lock on the messages key
129 if ( !$this->mUseCache
) {
133 $lockKey = $this->mMemcKey
. "lock";
134 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$this->mMemc
->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
138 return $i >= MSG_WAIT_TIMEOUT
;
142 if ( !$this->mUseCache
) {
146 $lockKey = $this->mMemcKey
. "lock";
147 $this->mMemc
->delete( $lockKey );
150 function get( $key, $useDB ) {
151 global $wgLang, $wgLanguageCode;
153 # If uninitialised, someone is trying to call this halfway through Setup.php
154 if ( !$this->mInitialised
) {
155 return "<$key>";
158 if ( $this->mDisable
) {
159 return $this->transform( $wgLang->getMessage( $key ) );
161 $title = $wgLang->ucfirst( $key );
166 if ( $this->mUseCache
&& $this->mCache
&& array_key_exists( $title, $this->mCache
) ) {
167 $message = $this->mCache
[$title];
170 # If it wasn't in the cache, load each message from the DB individually
171 if ( !$message && $useDB) {
172 $result = wfGetArray( "cur", array("cur_text"),
173 array( "cur_namespace" => NS_MEDIAWIKI
, "cur_title" => $title ),
174 "MessageCache::get" );
176 $message = $result->cur_text
;
180 # Try the array in $wgLang
182 $message = $wgLang->getMessage( $key );
185 # Try the English array
186 if ( !$message && $wgLanguageCode != "en" ) {
187 $message = Language
::getMessage( $key );
192 $message = "<$key>";
196 $message = $this->transform( $message );
200 function transform( $message ) {
201 if( !$this->mDisableTransform
) {
202 if ( strstr( $message, "{{" ) !== false ) {
203 $message = $this->mParser
->transformMsg( $message, $this->mParserOptions
);
209 function disable() { $this->mDisable
= true; }
210 function enable() { $this->mDisable
= false; }
211 function disableTransform() { $this->mDisableTransform
= true; }