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 $sql = "SELECT cur_title,cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI
;
79 $sql .= " AND cur_title IN ('";
80 $sql .= implode( "','", $this->getKeys() ) . "')";
83 $res = wfQuery( $sql, DB_READ
, $fname );
85 $this->mCache
= array();
86 for ( $row = wfFetchObject( $res ); $row; $row = wfFetchObject( $res ) ) {
87 $this->mCache
[$row->cur_title
] = $row->cur_text
;
90 wfDebug( var_export( $this->mCache
, true ) );
96 global $wgAllMessagesEn;
97 if ( !$this->mKeys
) {
98 $this->mKeys
= array_map( 'ucfirst', array_keys( $wgAllMessagesEn ) );
103 function isCacheable( $key ) {
104 global $wgAllMessagesEn;
105 return array_key_exists( lcfirst( $key ), $wgAllMessagesEn ) ||
106 array_key_exists( $key, $wgAllMessagesEn );
109 function replace( $title, $text ) {
111 if ( $this->isCacheable( $title ) ) {
114 if ( is_array( $this->mCache
) ) {
115 $this->mCache
[$title] = $text;
116 $wgMemc->set( $this->mMemcKey
, $this->mCache
, $this->mExpiry
);
123 # Represents a write lock on the messages key
127 if ( !$this->mUseCache
) {
131 $lockKey = $this->mMemcKey
. "lock";
132 for ($i=0; $i < MSG_WAIT_TIMEOUT
&& !$wgMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT
); $i++
) {
136 return $i >= MSG_WAIT_TIMEOUT
;
142 if ( !$this->mUseCache
) {
146 $lockKey = $this->mMemcKey
. "lock";
147 $wgMemc->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 $wgLang->getMessage( $key );
161 $title = ucfirst( $key );
166 if ( $this->mUseCache
&& $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 $sql = "SELECT cur_text FROM cur WHERE cur_namespace=" . NS_MEDIAWIKI
.
173 " AND cur_title='$title'";
174 $res = wfQuery( $sql, DB_READ
, $fname );
176 if ( wfNumRows( $res ) ) {
177 $obj = wfFetchObject( $res );
178 $message = $obj->cur_text
;
179 wfFreeResult( $res );
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>";
201 function lcfirst( $s ) {
202 return strtolower( $s{0} ). substr( $s, 1 );