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 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
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
33 global $wgAllMessagesEn, $wgMemc;
35 if ( $this->mDisable
) {
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
) {
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
);
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" );
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;
67 $this->mDisable
= true;
70 $this->mCache
= false;
76 # Loads all cacheable messages from the database
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() ) . "')";
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 ) );
97 global $wgAllMessagesEn;
98 if ( !$this->mKeys
) {
99 $this->mKeys
= array_map( 'ucfirst', array_keys( $wgAllMessagesEn ) );
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 ) {
112 if ( $this->isCacheable( $title ) ) {
115 if ( is_array( $this->mCache
) ) {
116 $this->mCache
[$title] = $text;
117 $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 ( $this->mDisable
) {
155 return $wgLang->getMessage( $key );
157 $title = ucfirst( $key );
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
181 $message = $wgLang->getMessage( $key );
184 # Try the English array
185 if ( !$message && $wgLanguageCode != "en" ) {
186 $message = Language
::getMessage( $key );
191 $message = "<$key>";
197 function lcfirst( $s ) {
198 return strtolower( $s{0} ). substr( $s, 1 );