summaryrefslogtreecommitdiff
path: root/includes/cache
diff options
context:
space:
mode:
Diffstat (limited to 'includes/cache')
-rw-r--r--includes/cache/BacklinkCache.php468
-rw-r--r--includes/cache/CacheDependency.php12
-rw-r--r--includes/cache/FileCacheBase.php10
-rw-r--r--includes/cache/GenderCache.php22
-rw-r--r--includes/cache/HTMLCacheUpdate.php227
-rw-r--r--includes/cache/HTMLFileCache.php11
-rw-r--r--includes/cache/LinkBatch.php6
-rw-r--r--includes/cache/LinkCache.php98
-rw-r--r--includes/cache/LocalisationCache.php1371
-rw-r--r--includes/cache/MessageCache.php866
-rw-r--r--includes/cache/ProcessCacheLRU.php19
-rw-r--r--includes/cache/ResourceFileCache.php2
-rw-r--r--includes/cache/SquidUpdate.php208
-rw-r--r--includes/cache/UserCache.php25
14 files changed, 2603 insertions, 742 deletions
diff --git a/includes/cache/BacklinkCache.php b/includes/cache/BacklinkCache.php
new file mode 100644
index 00000000..193f20fe
--- /dev/null
+++ b/includes/cache/BacklinkCache.php
@@ -0,0 +1,468 @@
+<?php
+/**
+ * Class for fetching backlink lists, approximate backlink counts and
+ * partitions.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @author Tim Starling
+ * @author Aaron Schulz
+ * @copyright © 2009, Tim Starling, Domas Mituzas
+ * @copyright © 2010, Max Sem
+ * @copyright © 2011, Antoine Musso
+ */
+
+/**
+ * Class for fetching backlink lists, approximate backlink counts and
+ * partitions. This is a shared cache.
+ *
+ * Instances of this class should typically be fetched with the method
+ * $title->getBacklinkCache().
+ *
+ * Ideally you should only get your backlinks from here when you think
+ * there is some advantage in caching them. Otherwise it's just a waste
+ * of memory.
+ *
+ * Introduced by r47317
+ *
+ * @internal documentation reviewed on 18 Mar 2011 by hashar
+ */
+class BacklinkCache {
+ /** @var ProcessCacheLRU */
+ protected static $cache;
+
+ /**
+ * Multi dimensions array representing batches. Keys are:
+ * > (string) links table name
+ * > 'numRows' : Number of rows for this link table
+ * > 'batches' : array( $start, $end )
+ *
+ * @see BacklinkCache::partitionResult()
+ *
+ * Cleared with BacklinkCache::clear()
+ */
+ protected $partitionCache = array();
+
+ /**
+ * Contains the whole links from a database result.
+ * This is raw data that will be partitioned in $partitionCache
+ *
+ * Initialized with BacklinkCache::getLinks()
+ * Cleared with BacklinkCache::clear()
+ */
+ protected $fullResultCache = array();
+
+ /**
+ * Local copy of a database object.
+ *
+ * Accessor: BacklinkCache::getDB()
+ * Mutator : BacklinkCache::setDB()
+ * Cleared with BacklinkCache::clear()
+ */
+ protected $db;
+
+ /**
+ * Local copy of a Title object
+ */
+ protected $title;
+
+ const CACHE_EXPIRY = 3600;
+
+ /**
+ * Create a new BacklinkCache
+ *
+ * @param Title $title : Title object to create a backlink cache for
+ */
+ public function __construct( Title $title ) {
+ $this->title = $title;
+ }
+
+ /**
+ * Create a new BacklinkCache or reuse any existing one.
+ * Currently, only one cache instance can exist; callers that
+ * need multiple backlink cache objects should keep them in scope.
+ *
+ * @param Title $title : Title object to get a backlink cache for
+ * @return BacklinkCache
+ */
+ public static function get( Title $title ) {
+ if ( !self::$cache ) { // init cache
+ self::$cache = new ProcessCacheLRU( 1 );
+ }
+ $dbKey = $title->getPrefixedDBkey();
+ if ( !self::$cache->has( $dbKey, 'obj', 3600 ) ) {
+ self::$cache->set( $dbKey, 'obj', new self( $title ) );
+ }
+ return self::$cache->get( $dbKey, 'obj' );
+ }
+
+ /**
+ * Serialization handler, diasallows to serialize the database to prevent
+ * failures after this class is deserialized from cache with dead DB
+ * connection.
+ *
+ * @return array
+ */
+ function __sleep() {
+ return array( 'partitionCache', 'fullResultCache', 'title' );
+ }
+
+ /**
+ * Clear locally stored data and database object.
+ */
+ public function clear() {
+ $this->partitionCache = array();
+ $this->fullResultCache = array();
+ unset( $this->db );
+ }
+
+ /**
+ * Set the Database object to use
+ *
+ * @param $db DatabaseBase
+ */
+ public function setDB( $db ) {
+ $this->db = $db;
+ }
+
+ /**
+ * Get the slave connection to the database
+ * When non existing, will initialize the connection.
+ * @return DatabaseBase object
+ */
+ protected function getDB() {
+ if ( !isset( $this->db ) ) {
+ $this->db = wfGetDB( DB_SLAVE );
+ }
+ return $this->db;
+ }
+
+ /**
+ * Get the backlinks for a given table. Cached in process memory only.
+ * @param $table String
+ * @param $startId Integer|false
+ * @param $endId Integer|false
+ * @param $max Integer|INF
+ * @return TitleArrayFromResult
+ */
+ public function getLinks( $table, $startId = false, $endId = false, $max = INF ) {
+ return TitleArray::newFromResult( $this->queryLinks( $table, $startId, $endId, $max ) );
+ }
+
+ /**
+ * Get the backlinks for a given table. Cached in process memory only.
+ * @param $table String
+ * @param $startId Integer|false
+ * @param $endId Integer|false
+ * @param $max Integer|INF
+ * @return ResultWrapper
+ */
+ protected function queryLinks( $table, $startId, $endId, $max ) {
+ wfProfileIn( __METHOD__ );
+
+ $fromField = $this->getPrefix( $table ) . '_from';
+
+ if ( !$startId && !$endId && is_infinite( $max )
+ && isset( $this->fullResultCache[$table] ) )
+ {
+ wfDebug( __METHOD__ . ": got results from cache\n" );
+ $res = $this->fullResultCache[$table];
+ } else {
+ wfDebug( __METHOD__ . ": got results from DB\n" );
+ $conds = $this->getConditions( $table );
+ // Use the from field in the condition rather than the joined page_id,
+ // because databases are stupid and don't necessarily propagate indexes.
+ if ( $startId ) {
+ $conds[] = "$fromField >= " . intval( $startId );
+ }
+ if ( $endId ) {
+ $conds[] = "$fromField <= " . intval( $endId );
+ }
+ $options = array( 'STRAIGHT_JOIN', 'ORDER BY' => $fromField );
+ if ( is_finite( $max ) && $max > 0 ) {
+ $options['LIMIT'] = $max;
+ }
+
+ $res = $this->getDB()->select(
+ array( $table, 'page' ),
+ array( 'page_namespace', 'page_title', 'page_id' ),
+ $conds,
+ __METHOD__,
+ $options
+ );
+
+ if ( !$startId && !$endId && $res->numRows() < $max ) {
+ // The full results fit within the limit, so cache them
+ $this->fullResultCache[$table] = $res;
+ } else {
+ wfDebug( __METHOD__ . ": results from DB were uncacheable\n" );
+ }
+ }
+
+ wfProfileOut( __METHOD__ );
+ return $res;
+ }
+
+ /**
+ * Get the field name prefix for a given table
+ * @param $table String
+ * @throws MWException
+ * @return null|string
+ */
+ protected function getPrefix( $table ) {
+ static $prefixes = array(
+ 'pagelinks' => 'pl',
+ 'imagelinks' => 'il',
+ 'categorylinks' => 'cl',
+ 'templatelinks' => 'tl',
+ 'redirect' => 'rd',
+ );
+
+ if ( isset( $prefixes[$table] ) ) {
+ return $prefixes[$table];
+ } else {
+ $prefix = null;
+ wfRunHooks( 'BacklinkCacheGetPrefix', array( $table, &$prefix ) );
+ if ( $prefix ) {
+ return $prefix;
+ } else {
+ throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
+ }
+ }
+ }
+
+ /**
+ * Get the SQL condition array for selecting backlinks, with a join
+ * on the page table.
+ * @param $table String
+ * @throws MWException
+ * @return array|null
+ */
+ protected function getConditions( $table ) {
+ $prefix = $this->getPrefix( $table );
+
+ // @todo FIXME: imagelinks and categorylinks do not rely on getNamespace,
+ // they could be moved up for nicer case statements
+ switch ( $table ) {
+ case 'pagelinks':
+ case 'templatelinks':
+ $conds = array(
+ "{$prefix}_namespace" => $this->title->getNamespace(),
+ "{$prefix}_title" => $this->title->getDBkey(),
+ "page_id={$prefix}_from"
+ );
+ break;
+ case 'redirect':
+ $conds = array(
+ "{$prefix}_namespace" => $this->title->getNamespace(),
+ "{$prefix}_title" => $this->title->getDBkey(),
+ $this->getDb()->makeList( array(
+ "{$prefix}_interwiki" => '',
+ "{$prefix}_interwiki IS NULL",
+ ), LIST_OR ),
+ "page_id={$prefix}_from"
+ );
+ break;
+ case 'imagelinks':
+ $conds = array(
+ 'il_to' => $this->title->getDBkey(),
+ 'page_id=il_from'
+ );
+ break;
+ case 'categorylinks':
+ $conds = array(
+ 'cl_to' => $this->title->getDBkey(),
+ 'page_id=cl_from',
+ );
+ break;
+ default:
+ $conds = null;
+ wfRunHooks( 'BacklinkCacheGetConditions', array( $table, $this->title, &$conds ) );
+ if ( !$conds ) {
+ throw new MWException( "Invalid table \"$table\" in " . __CLASS__ );
+ }
+ }
+
+ return $conds;
+ }
+
+ /**
+ * Check if there are any backlinks
+ * @param $table String
+ * @return bool
+ */
+ public function hasLinks( $table ) {
+ return ( $this->getNumLinks( $table, 1 ) > 0 );
+ }
+
+ /**
+ * Get the approximate number of backlinks
+ * @param $table String
+ * @param $max integer|INF Only count up to this many backlinks
+ * @return integer
+ */
+ public function getNumLinks( $table, $max = INF ) {
+ global $wgMemc;
+
+ // 1) try partition cache ...
+ if ( isset( $this->partitionCache[$table] ) ) {
+ $entry = reset( $this->partitionCache[$table] );
+ return min( $max, $entry['numRows'] );
+ }
+
+ // 2) ... then try full result cache ...
+ if ( isset( $this->fullResultCache[$table] ) ) {
+ return min( $max, $this->fullResultCache[$table]->numRows() );
+ }
+
+ $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table );
+
+ // 3) ... fallback to memcached ...
+ $count = $wgMemc->get( $memcKey );
+ if ( $count ) {
+ return min( $max, $count );
+ }
+
+ // 4) fetch from the database ...
+ $count = $this->getLinks( $table, false, false, $max )->count();
+ if ( $count < $max ) { // full count
+ $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY );
+ }
+
+ return min( $max, $count );
+ }
+
+ /**
+ * Partition the backlinks into batches.
+ * Returns an array giving the start and end of each range. The first
+ * batch has a start of false, and the last batch has an end of false.
+ *
+ * @param string $table the links table name
+ * @param $batchSize Integer
+ * @return Array
+ */
+ public function partition( $table, $batchSize ) {
+ global $wgMemc;
+
+ // 1) try partition cache ...
+ if ( isset( $this->partitionCache[$table][$batchSize] ) ) {
+ wfDebug( __METHOD__ . ": got from partition cache\n" );
+ return $this->partitionCache[$table][$batchSize]['batches'];
+ }
+
+ $this->partitionCache[$table][$batchSize] = false;
+ $cacheEntry =& $this->partitionCache[$table][$batchSize];
+
+ // 2) ... then try full result cache ...
+ if ( isset( $this->fullResultCache[$table] ) ) {
+ $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize );
+ wfDebug( __METHOD__ . ": got from full result cache\n" );
+ return $cacheEntry['batches'];
+ }
+
+ $memcKey = wfMemcKey(
+ 'backlinks',
+ md5( $this->title->getPrefixedDBkey() ),
+ $table,
+ $batchSize
+ );
+
+ // 3) ... fallback to memcached ...
+ $memcValue = $wgMemc->get( $memcKey );
+ if ( is_array( $memcValue ) ) {
+ $cacheEntry = $memcValue;
+ wfDebug( __METHOD__ . ": got from memcached $memcKey\n" );
+ return $cacheEntry['batches'];
+ }
+
+ // 4) ... finally fetch from the slow database :(
+ $cacheEntry = array( 'numRows' => 0, 'batches' => array() ); // final result
+ // Do the selects in batches to avoid client-side OOMs (bug 43452).
+ // Use a LIMIT that plays well with $batchSize to keep equal sized partitions.
+ $selectSize = max( $batchSize, 200000 - ( 200000 % $batchSize ) );
+ $start = false;
+ do {
+ $res = $this->queryLinks( $table, $start, false, $selectSize );
+ $partitions = $this->partitionResult( $res, $batchSize, false );
+ // Merge the link count and range partitions for this chunk
+ $cacheEntry['numRows'] += $partitions['numRows'];
+ $cacheEntry['batches'] = array_merge( $cacheEntry['batches'], $partitions['batches'] );
+ if ( count( $partitions['batches'] ) ) {
+ list( $lStart, $lEnd ) = end( $partitions['batches'] );
+ $start = $lEnd + 1; // pick up after this inclusive range
+ }
+ } while ( $partitions['numRows'] >= $selectSize );
+ // Make sure the first range has start=false and the last one has end=false
+ if ( count( $cacheEntry['batches'] ) ) {
+ $cacheEntry['batches'][0][0] = false;
+ $cacheEntry['batches'][count( $cacheEntry['batches'] ) - 1][1] = false;
+ }
+
+ // Save partitions to memcached
+ $wgMemc->set( $memcKey, $cacheEntry, self::CACHE_EXPIRY );
+
+ // Save backlink count to memcached
+ $memcKey = wfMemcKey( 'numbacklinks', md5( $this->title->getPrefixedDBkey() ), $table );
+ $wgMemc->set( $memcKey, $cacheEntry['numRows'], self::CACHE_EXPIRY );
+
+ wfDebug( __METHOD__ . ": got from database\n" );
+ return $cacheEntry['batches'];
+ }
+
+ /**
+ * Partition a DB result with backlinks in it into batches
+ * @param $res ResultWrapper database result
+ * @param $batchSize integer
+ * @param $isComplete bool Whether $res includes all the backlinks
+ * @throws MWException
+ * @return array
+ */
+ protected function partitionResult( $res, $batchSize, $isComplete = true ) {
+ $batches = array();
+ $numRows = $res->numRows();
+ $numBatches = ceil( $numRows / $batchSize );
+
+ for ( $i = 0; $i < $numBatches; $i++ ) {
+ if ( $i == 0 && $isComplete ) {
+ $start = false;
+ } else {
+ $rowNum = $i * $batchSize;
+ $res->seek( $rowNum );
+ $row = $res->fetchObject();
+ $start = (int)$row->page_id;
+ }
+
+ if ( $i == ( $numBatches - 1 ) && $isComplete ) {
+ $end = false;
+ } else {
+ $rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 );
+ $res->seek( $rowNum );
+ $row = $res->fetchObject();
+ $end = (int)$row->page_id;
+ }
+
+ # Sanity check order
+ if ( $start && $end && $start > $end ) {
+ throw new MWException( __METHOD__ . ': Internal error: query result out of order' );
+ }
+
+ $batches[] = array( $start, $end );
+ }
+
+ return array( 'numRows' => $numRows, 'batches' => $batches );
+ }
+}
diff --git a/includes/cache/CacheDependency.php b/includes/cache/CacheDependency.php
index a3c2b52a..32bcdf7f 100644
--- a/includes/cache/CacheDependency.php
+++ b/includes/cache/CacheDependency.php
@@ -74,7 +74,7 @@ class DependencyWrapper {
/**
* Get the user-defined value
- * @return bool|\Mixed
+ * @return bool|Mixed
*/
function getValue() {
return $this->value;
@@ -98,11 +98,11 @@ class DependencyWrapper {
* calculated value will be stored to the cache in a wrapper.
*
* @param $cache BagOStuff a cache object such as $wgMemc
- * @param $key String: the cache key
+ * @param string $key the cache key
* @param $expiry Integer: the expiry timestamp or interval in seconds
* @param $callback Mixed: the callback for generating the value, or false
- * @param $callbackParams Array: the function parameters for the callback
- * @param $deps Array: the dependencies to store on a cache miss. Note: these
+ * @param array $callbackParams the function parameters for the callback
+ * @param array $deps the dependencies to store on a cache miss. Note: these
* are not the dependencies used on a cache hit! Cache hits use the stored
* dependency array.
*
@@ -153,7 +153,7 @@ class FileDependency extends CacheDependency {
/**
* Create a file dependency
*
- * @param $filename String: the name of the file, preferably fully qualified
+ * @param string $filename the name of the file, preferably fully qualified
* @param $timestamp Mixed: the unix last modified timestamp, or false if the
* file does not exist. If omitted, the timestamp will be loaded from
* the file.
@@ -404,7 +404,7 @@ class GlobalDependency extends CacheDependency {
* @return bool
*/
function isExpired() {
- if( !isset($GLOBALS[$this->name]) ) {
+ if ( !isset( $GLOBALS[$this->name] ) ) {
return true;
}
return $GLOBALS[$this->name] != $this->value;
diff --git a/includes/cache/FileCacheBase.php b/includes/cache/FileCacheBase.php
index c0c5609c..d4bf5ee6 100644
--- a/includes/cache/FileCacheBase.php
+++ b/includes/cache/FileCacheBase.php
@@ -35,7 +35,7 @@ abstract class FileCacheBase {
/* lazy loaded */
protected $mCached;
- /* @TODO: configurable? */
+ /* @todo configurable? */
const MISS_FACTOR = 15; // log 1 every MISS_FACTOR cache misses
const MISS_TTL_SEC = 3600; // how many seconds ago is "recent"
@@ -107,7 +107,7 @@ abstract class FileCacheBase {
/**
* Check if up to date cache file exists
- * @param $timestamp string MW_TS timestamp
+ * @param string $timestamp MW_TS timestamp
*
* @return bool
*/
@@ -138,7 +138,7 @@ abstract class FileCacheBase {
* @return string
*/
public function fetchText() {
- if( $this->useGzip() ) {
+ if ( $this->useGzip() ) {
$fh = gzopen( $this->cachePath(), 'rb' );
return stream_get_contents( $fh );
} else {
@@ -163,7 +163,7 @@ abstract class FileCacheBase {
$this->checkCacheDirs(); // build parent dir
if ( !file_put_contents( $this->cachePath(), $text, LOCK_EX ) ) {
- wfDebug( __METHOD__ . "() failed saving ". $this->cachePath() . "\n");
+ wfDebug( __METHOD__ . "() failed saving " . $this->cachePath() . "\n" );
$this->mCached = null;
return false;
}
@@ -229,7 +229,7 @@ abstract class FileCacheBase {
public function incrMissesRecent( WebRequest $request ) {
global $wgMemc;
if ( mt_rand( 0, self::MISS_FACTOR - 1 ) == 0 ) {
- # Get a large IP range that should include the user even if that
+ # Get a large IP range that should include the user even if that
# person's IP address changes
$ip = $request->getIP();
if ( !IP::isValid( $ip ) ) {
diff --git a/includes/cache/GenderCache.php b/includes/cache/GenderCache.php
index 2a169bb3..a933527a 100644
--- a/includes/cache/GenderCache.php
+++ b/includes/cache/GenderCache.php
@@ -59,22 +59,21 @@ class GenderCache {
/**
* Returns the gender for given username.
- * @param $username String or User: username
- * @param $caller String: the calling method
+ * @param string $username or User: username
+ * @param string $caller the calling method
* @return String
*/
public function getGenderOf( $username, $caller = '' ) {
global $wgUser;
- if( $username instanceof User ) {
+ if ( $username instanceof User ) {
$username = $username->getName();
}
$username = self::normalizeUsername( $username );
if ( !isset( $this->cache[$username] ) ) {
-
if ( $this->misses >= $this->missLimit && $wgUser->getName() !== $username ) {
- if( $this->misses === $this->missLimit ) {
+ if ( $this->misses === $this->missLimit ) {
$this->misses++;
wfDebug( __METHOD__ . ": too many misses, returning default onwards\n" );
}
@@ -84,7 +83,6 @@ class GenderCache {
$this->misses++;
$this->doQuery( $username, $caller );
}
-
}
/* Undefined if there is a valid username which for some reason doesn't
@@ -102,7 +100,9 @@ class GenderCache {
public function doLinkBatch( $data, $caller = '' ) {
$users = array();
foreach ( $data as $ns => $pagenames ) {
- if ( !MWNamespace::hasGenderDistinction( $ns ) ) continue;
+ if ( !MWNamespace::hasGenderDistinction( $ns ) ) {
+ continue;
+ }
foreach ( array_keys( $pagenames ) as $username ) {
$users[$username] = true;
}
@@ -116,7 +116,7 @@ class GenderCache {
*
* @since 1.20
* @param $titles List: array of Title objects or strings
- * @param $caller String: the calling method
+ * @param string $caller the calling method
*/
public function doTitlesArray( $titles, $caller = '' ) {
$users = array();
@@ -137,20 +137,20 @@ class GenderCache {
/**
* Preloads genders for given list of users.
* @param $users List|String: usernames
- * @param $caller String: the calling method
+ * @param string $caller the calling method
*/
public function doQuery( $users, $caller = '' ) {
$default = $this->getDefault();
$usersToCheck = array();
- foreach ( (array) $users as $value ) {
+ foreach ( (array)$users as $value ) {
$name = self::normalizeUsername( $value );
// Skip users whose gender setting we already know
if ( !isset( $this->cache[$name] ) ) {
// For existing users, this value will be overwritten by the correct value
$this->cache[$name] = $default;
// query only for valid names, which can be in the database
- if( User::isValidUserName( $name ) ) {
+ if ( User::isValidUserName( $name ) ) {
$usersToCheck[] = $name;
}
}
diff --git a/includes/cache/HTMLCacheUpdate.php b/includes/cache/HTMLCacheUpdate.php
index 0a3c0023..992809ef 100644
--- a/includes/cache/HTMLCacheUpdate.php
+++ b/includes/cache/HTMLCacheUpdate.php
@@ -23,24 +23,6 @@
/**
* Class to invalidate the HTML cache of all the pages linking to a given title.
- * Small numbers of links will be done immediately, large numbers are pushed onto
- * the job queue.
- *
- * This class is designed to work efficiently with small numbers of links, and
- * to work reasonably well with up to ~10^5 links. Above ~10^6 links, the memory
- * and time requirements of loading all backlinked IDs in doUpdate() might become
- * prohibitive. The requirements measured at Wikimedia are approximately:
- *
- * memory: 48 bytes per row
- * time: 16us per row for the query plus processing
- *
- * The reason this query is done is to support partitioning of the job
- * by backlinked ID. The memory issue could be allieviated by doing this query in
- * batches, but of course LIMIT with an offset is inefficient on the DB side.
- *
- * The class is nevertheless a vast improvement on the previous method of using
- * File::getLinksTo() and Title::touchArray(), which uses about 2KB of memory per
- * link.
*
* @ingroup Cache
*/
@@ -50,8 +32,7 @@ class HTMLCacheUpdate implements DeferrableUpdate {
*/
public $mTitle;
- public $mTable, $mPrefix, $mStart, $mEnd;
- public $mRowsPerJob, $mRowsPerQuery;
+ public $mTable;
/**
* @param $titleTo
@@ -59,202 +40,34 @@ class HTMLCacheUpdate implements DeferrableUpdate {
* @param $start bool
* @param $end bool
*/
- function __construct( $titleTo, $table, $start = false, $end = false ) {
- global $wgUpdateRowsPerJob, $wgUpdateRowsPerQuery;
-
+ function __construct( Title $titleTo, $table ) {
$this->mTitle = $titleTo;
$this->mTable = $table;
- $this->mStart = $start;
- $this->mEnd = $end;
- $this->mRowsPerJob = $wgUpdateRowsPerJob;
- $this->mRowsPerQuery = $wgUpdateRowsPerQuery;
- $this->mCache = $this->mTitle->getBacklinkCache();
}
public function doUpdate() {
- if ( $this->mStart || $this->mEnd ) {
- $this->doPartialUpdate();
- return;
- }
-
- # Get an estimate of the number of rows from the BacklinkCache
- $numRows = $this->mCache->getNumLinks( $this->mTable );
- if ( $numRows > $this->mRowsPerJob * 2 ) {
- # Do fast cached partition
- $this->insertJobs();
- } else {
- # Get the links from the DB
- $titleArray = $this->mCache->getLinks( $this->mTable );
- # Check if the row count estimate was correct
- if ( $titleArray->count() > $this->mRowsPerJob * 2 ) {
- # Not correct, do accurate partition
- wfDebug( __METHOD__.": row count estimate was incorrect, repartitioning\n" );
- $this->insertJobsFromTitles( $titleArray );
- } else {
- $this->invalidateTitles( $titleArray );
- }
- }
- }
-
- /**
- * Update some of the backlinks, defined by a page ID range
- */
- protected function doPartialUpdate() {
- $titleArray = $this->mCache->getLinks( $this->mTable, $this->mStart, $this->mEnd );
- if ( $titleArray->count() <= $this->mRowsPerJob * 2 ) {
- # This partition is small enough, do the update
- $this->invalidateTitles( $titleArray );
- } else {
- # Partitioning was excessively inaccurate. Divide the job further.
- # This can occur when a large number of links are added in a short
- # period of time, say by updating a heavily-used template.
- $this->insertJobsFromTitles( $titleArray );
- }
- }
+ wfProfileIn( __METHOD__ );
- /**
- * Partition the current range given by $this->mStart and $this->mEnd,
- * using a pre-calculated title array which gives the links in that range.
- * Queue the resulting jobs.
- *
- * @param $titleArray array
- */
- protected function insertJobsFromTitles( $titleArray ) {
- # We make subpartitions in the sense that the start of the first job
- # will be the start of the parent partition, and the end of the last
- # job will be the end of the parent partition.
- $jobs = array();
- $start = $this->mStart; # start of the current job
- $numTitles = 0;
- foreach ( $titleArray as $title ) {
- $id = $title->getArticleID();
- # $numTitles is now the number of titles in the current job not
- # including the current ID
- if ( $numTitles >= $this->mRowsPerJob ) {
- # Add a job up to but not including the current ID
- $params = array(
- 'table' => $this->mTable,
- 'start' => $start,
- 'end' => $id - 1
- );
- $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
- $start = $id;
- $numTitles = 0;
- }
- $numTitles++;
- }
- # Last job
- $params = array(
- 'table' => $this->mTable,
- 'start' => $start,
- 'end' => $this->mEnd
- );
- $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
- wfDebug( __METHOD__.": repartitioning into " . count( $jobs ) . " jobs\n" );
-
- if ( count( $jobs ) < 2 ) {
- # I don't think this is possible at present, but handling this case
- # makes the code a bit more robust against future code updates and
- # avoids a potential infinite loop of repartitioning
- wfDebug( __METHOD__.": repartitioning failed!\n" );
- $this->invalidateTitles( $titleArray );
- return;
- }
-
- Job::batchInsert( $jobs );
- }
-
- /**
- * @return mixed
- */
- protected function insertJobs() {
- $batches = $this->mCache->partition( $this->mTable, $this->mRowsPerJob );
- if ( !$batches ) {
- return;
- }
- $jobs = array();
- foreach ( $batches as $batch ) {
- $params = array(
+ $job = new HTMLCacheUpdateJob(
+ $this->mTitle,
+ array(
'table' => $this->mTable,
- 'start' => $batch[0],
- 'end' => $batch[1],
- );
- $jobs[] = new HTMLCacheUpdateJob( $this->mTitle, $params );
- }
- Job::batchInsert( $jobs );
- }
-
- /**
- * Invalidate an array (or iterator) of Title objects, right now
- * @param $titleArray array
- */
- protected function invalidateTitles( $titleArray ) {
- global $wgUseFileCache, $wgUseSquid;
-
- $dbw = wfGetDB( DB_MASTER );
- $timestamp = $dbw->timestamp();
-
- # Get all IDs in this query into an array
- $ids = array();
- foreach ( $titleArray as $title ) {
- $ids[] = $title->getArticleID();
- }
-
- if ( !$ids ) {
- return;
- }
-
- # Update page_touched
- $batches = array_chunk( $ids, $this->mRowsPerQuery );
- foreach ( $batches as $batch ) {
- $dbw->update( 'page',
- array( 'page_touched' => $timestamp ),
- array( 'page_id' => $batch ),
- __METHOD__
- );
- }
-
- # Update squid
- if ( $wgUseSquid ) {
- $u = SquidUpdate::newFromTitles( $titleArray );
- $u->doUpdate();
- }
+ ) + Job::newRootJobParams( // "overall" refresh links job info
+ "htmlCacheUpdate:{$this->mTable}:{$this->mTitle->getPrefixedText()}"
+ )
+ );
- # Update file cache
- if ( $wgUseFileCache ) {
- foreach ( $titleArray as $title ) {
- HTMLFileCache::clearFileCache( $title );
- }
+ $count = $this->mTitle->getBacklinkCache()->getNumLinks( $this->mTable, 200 );
+ if ( $count >= 200 ) { // many backlinks
+ JobQueueGroup::singleton()->push( $job );
+ JobQueueGroup::singleton()->deduplicateRootJob( $job );
+ } else { // few backlinks ($count might be off even if 0)
+ $dbw = wfGetDB( DB_MASTER );
+ $dbw->onTransactionIdle( function() use ( $job ) {
+ $job->run(); // just do the purge query now
+ } );
}
- }
-}
-
-
-/**
- * Job wrapper for HTMLCacheUpdate. Gets run whenever a related
- * job gets called from the queue.
- *
- * @ingroup JobQueue
- */
-class HTMLCacheUpdateJob extends Job {
- var $table, $start, $end;
-
- /**
- * Construct a job
- * @param $title Title: the title linked to
- * @param $params Array: job parameters (table, start and end page_ids)
- * @param $id Integer: job id
- */
- function __construct( $title, $params, $id = 0 ) {
- parent::__construct( 'htmlCacheUpdate', $title, $params, $id );
- $this->table = $params['table'];
- $this->start = $params['start'];
- $this->end = $params['end'];
- }
- public function run() {
- $update = new HTMLCacheUpdate( $this->title, $this->table, $this->start, $this->end );
- $update->doUpdate();
- return true;
+ wfProfileOut( __METHOD__ );
}
}
diff --git a/includes/cache/HTMLFileCache.php b/includes/cache/HTMLFileCache.php
index 6bfeed32..ab379116 100644
--- a/includes/cache/HTMLFileCache.php
+++ b/includes/cache/HTMLFileCache.php
@@ -33,6 +33,7 @@ class HTMLFileCache extends FileCacheBase {
* Construct an ObjectFileCache from a Title and an action
* @param $title Title|string Title object or prefixed DB key string
* @param $action string
+ * @throws MWException
* @return HTMLFileCache
*/
public static function newFromTitle( $title, $action ) {
@@ -127,7 +128,7 @@ class HTMLFileCache extends FileCacheBase {
public function loadFromFileCache( IContextSource $context ) {
global $wgMimeType, $wgLanguageCode;
- wfDebug( __METHOD__ . "()\n");
+ wfDebug( __METHOD__ . "()\n" );
$filename = $this->cachePath();
$context->getOutput()->sendCacheControl();
@@ -162,15 +163,15 @@ class HTMLFileCache extends FileCacheBase {
return $text;
}
- wfDebug( __METHOD__ . "()\n", false);
+ wfDebug( __METHOD__ . "()\n", false );
$now = wfTimestampNow();
if ( $this->useGzip() ) {
$text = str_replace(
- '</html>', '<!-- Cached/compressed '.$now." -->\n</html>", $text );
+ '</html>', '<!-- Cached/compressed ' . $now . " -->\n</html>", $text );
} else {
$text = str_replace(
- '</html>', '<!-- Cached '.$now." -->\n</html>", $text );
+ '</html>', '<!-- Cached ' . $now . " -->\n</html>", $text );
}
// Store text to FS...
@@ -181,7 +182,7 @@ class HTMLFileCache extends FileCacheBase {
// gzip output to buffer as needed and set headers...
if ( $this->useGzip() ) {
- // @TODO: ugly wfClientAcceptsGzip() function - use context!
+ // @todo Ugly wfClientAcceptsGzip() function - use context!
if ( wfClientAcceptsGzip() ) {
header( 'Content-Encoding: gzip' );
return $compressed;
diff --git a/includes/cache/LinkBatch.php b/includes/cache/LinkBatch.php
index 372f983b..48b60aa9 100644
--- a/includes/cache/LinkBatch.php
+++ b/includes/cache/LinkBatch.php
@@ -39,7 +39,7 @@ class LinkBatch {
protected $caller;
function __construct( $arr = array() ) {
- foreach( $arr as $item ) {
+ foreach ( $arr as $item ) {
$this->addObj( $item );
}
}
@@ -98,7 +98,7 @@ class LinkBatch {
* @return bool
*/
public function isEmpty() {
- return ($this->getSize() == 0);
+ return $this->getSize() == 0;
}
/**
@@ -223,7 +223,7 @@ class LinkBatch {
/**
* Construct a WHERE clause which will match all the given titles.
*
- * @param $prefix String: the appropriate table's field name prefix ('page', 'pl', etc)
+ * @param string $prefix the appropriate table's field name prefix ('page', 'pl', etc)
* @param $db DatabaseBase object to use
* @return mixed string with SQL where clause fragment, or false if no items.
*/
diff --git a/includes/cache/LinkCache.php b/includes/cache/LinkCache.php
index f759c020..54de1989 100644
--- a/includes/cache/LinkCache.php
+++ b/includes/cache/LinkCache.php
@@ -35,18 +35,44 @@ class LinkCache {
private $mGoodLinkFields = array();
private $mBadLinks = array();
private $mForUpdate = false;
+ private $useDatabase = true;
/**
- * Get an instance of this class
+ * @var LinkCache
+ */
+ protected static $instance;
+
+ /**
+ * Get an instance of this class.
*
* @return LinkCache
*/
static function &singleton() {
- static $instance;
- if ( !isset( $instance ) ) {
- $instance = new LinkCache;
+ if ( self::$instance ) {
+ return self::$instance;
}
- return $instance;
+ self::$instance = new LinkCache;
+ return self::$instance;
+ }
+
+ /**
+ * Destroy the singleton instance, a new one will be created next time
+ * singleton() is called.
+ * @since 1.22
+ */
+ static function destroySingleton() {
+ self::$instance = null;
+ }
+
+ /**
+ * Set the singleton instance to a given object.
+ * Since we do not have an interface for LinkCache, you have to be sure the
+ * given object implements all the LinkCache public methods.
+ * @param LinkCache $instance
+ * @since 1.22
+ */
+ static function setSingleton( LinkCache $instance ) {
+ self::$instance = $instance;
}
/**
@@ -74,11 +100,11 @@ class LinkCache {
* Get a field of a title object from cache.
* If this link is not good, it will return NULL.
* @param $title Title
- * @param $field String: ('length','redirect','revision')
+ * @param string $field ('length','redirect','revision','model')
* @return mixed
*/
public function getGoodLinkFieldObj( $title, $field ) {
- $dbkey = $title->getPrefixedDbKey();
+ $dbkey = $title->getPrefixedDBkey();
if ( array_key_exists( $dbkey, $this->mGoodLinkFields ) ) {
return $this->mGoodLinkFields[$dbkey][$field];
} else {
@@ -102,14 +128,16 @@ class LinkCache {
* @param $len Integer: text's length
* @param $redir Integer: whether the page is a redirect
* @param $revision Integer: latest revision's ID
+ * @param $model Integer: latest revision's content model ID
*/
- public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false ) {
- $dbkey = $title->getPrefixedDbKey();
+ public function addGoodLinkObj( $id, $title, $len = -1, $redir = null, $revision = false, $model = false ) {
+ $dbkey = $title->getPrefixedDBkey();
$this->mGoodLinks[$dbkey] = intval( $id );
$this->mGoodLinkFields[$dbkey] = array(
'length' => intval( $len ),
'redirect' => intval( $redir ),
- 'revision' => intval( $revision ) );
+ 'revision' => intval( $revision ),
+ 'model' => intval( $model ) );
}
/**
@@ -117,15 +145,16 @@ class LinkCache {
* @since 1.19
* @param $title Title
* @param $row object which has the fields page_id, page_is_redirect,
- * page_latest
+ * page_latest and page_content_model
*/
public function addGoodLinkObjFromRow( $title, $row ) {
- $dbkey = $title->getPrefixedDbKey();
+ $dbkey = $title->getPrefixedDBkey();
$this->mGoodLinks[$dbkey] = intval( $row->page_id );
$this->mGoodLinkFields[$dbkey] = array(
'length' => intval( $row->page_len ),
'redirect' => intval( $row->page_is_redirect ),
'revision' => intval( $row->page_latest ),
+ 'model' => !empty( $row->page_content_model ) ? strval( $row->page_content_model ) : null,
);
}
@@ -133,7 +162,7 @@ class LinkCache {
* @param $title Title
*/
public function addBadLinkObj( $title ) {
- $dbkey = $title->getPrefixedDbKey();
+ $dbkey = $title->getPrefixedDBkey();
if ( !$this->isBadLink( $dbkey ) ) {
$this->mBadLinks[$dbkey] = 1;
}
@@ -147,24 +176,29 @@ class LinkCache {
* @param $title Title
*/
public function clearLink( $title ) {
- $dbkey = $title->getPrefixedDbKey();
+ $dbkey = $title->getPrefixedDBkey();
unset( $this->mBadLinks[$dbkey] );
unset( $this->mGoodLinks[$dbkey] );
unset( $this->mGoodLinkFields[$dbkey] );
}
- public function getGoodLinks() { return $this->mGoodLinks; }
- public function getBadLinks() { return array_keys( $this->mBadLinks ); }
+ public function getGoodLinks() {
+ return $this->mGoodLinks;
+ }
+
+ public function getBadLinks() {
+ return array_keys( $this->mBadLinks );
+ }
/**
* Add a title to the link cache, return the page_id or zero if non-existent
*
- * @param $title String: title to add
+ * @param string $title title to add
* @return Integer
*/
public function addLink( $title ) {
$nt = Title::newFromDBkey( $title );
- if( $nt ) {
+ if ( $nt ) {
return $this->addLinkObj( $nt );
} else {
return 0;
@@ -172,13 +206,27 @@ class LinkCache {
}
/**
+ * Enable or disable database use.
+ * @since 1.22
+ * @param $value Boolean
+ * @return Boolean
+ */
+ public function useDatabase( $value = null ) {
+ if ( $value !== null ) {
+ $this->useDatabase = (bool)$value;
+ }
+ return $this->useDatabase;
+ }
+
+ /**
* Add a title to the link cache, return the page_id or zero if non-existent
*
* @param $nt Title object to add
* @return Integer
*/
public function addLinkObj( $nt ) {
- global $wgAntiLockFlags;
+ global $wgAntiLockFlags, $wgContentHandlerUseDB;
+
wfProfileIn( __METHOD__ );
$key = $nt->getPrefixedDBkey();
@@ -197,6 +245,10 @@ class LinkCache {
return 0;
}
+ if( !$this->useDatabase ) {
+ return 0;
+ }
+
# Some fields heavily used for linking...
if ( $this->mForUpdate ) {
$db = wfGetDB( DB_MASTER );
@@ -210,8 +262,12 @@ class LinkCache {
$options = array();
}
- $s = $db->selectRow( 'page',
- array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ),
+ $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' );
+ if ( $wgContentHandlerUseDB ) {
+ $f[] = 'page_content_model';
+ }
+
+ $s = $db->selectRow( 'page', $f,
array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ),
__METHOD__, $options );
# Set fields...
diff --git a/includes/cache/LocalisationCache.php b/includes/cache/LocalisationCache.php
new file mode 100644
index 00000000..25a1e196
--- /dev/null
+++ b/includes/cache/LocalisationCache.php
@@ -0,0 +1,1371 @@
+<?php
+/**
+ * Cache of the contents of localisation files.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ */
+
+define( 'MW_LC_VERSION', 2 );
+
+/**
+ * Class for caching the contents of localisation files, Messages*.php
+ * and *.i18n.php.
+ *
+ * An instance of this class is available using Language::getLocalisationCache().
+ *
+ * The values retrieved from here are merged, containing items from extension
+ * files, core messages files and the language fallback sequence (e.g. zh-cn ->
+ * zh-hans -> en ). Some common errors are corrected, for example namespace
+ * names with spaces instead of underscores, but heavyweight processing, such
+ * as grammatical transformation, is done by the caller.
+ */
+class LocalisationCache {
+ /** Configuration associative array */
+ var $conf;
+
+ /**
+ * True if recaching should only be done on an explicit call to recache().
+ * Setting this reduces the overhead of cache freshness checking, which
+ * requires doing a stat() for every extension i18n file.
+ */
+ var $manualRecache = false;
+
+ /**
+ * True to treat all files as expired until they are regenerated by this object.
+ */
+ var $forceRecache = false;
+
+ /**
+ * The cache data. 3-d array, where the first key is the language code,
+ * the second key is the item key e.g. 'messages', and the third key is
+ * an item specific subkey index. Some items are not arrays and so for those
+ * items, there are no subkeys.
+ */
+ var $data = array();
+
+ /**
+ * The persistent store object. An instance of LCStore.
+ *
+ * @var LCStore
+ */
+ var $store;
+
+ /**
+ * A 2-d associative array, code/key, where presence indicates that the item
+ * is loaded. Value arbitrary.
+ *
+ * For split items, if set, this indicates that all of the subitems have been
+ * loaded.
+ */
+ var $loadedItems = array();
+
+ /**
+ * A 3-d associative array, code/key/subkey, where presence indicates that
+ * the subitem is loaded. Only used for the split items, i.e. messages.
+ */
+ var $loadedSubitems = array();
+
+ /**
+ * An array where presence of a key indicates that that language has been
+ * initialised. Initialisation includes checking for cache expiry and doing
+ * any necessary updates.
+ */
+ var $initialisedLangs = array();
+
+ /**
+ * An array mapping non-existent pseudo-languages to fallback languages. This
+ * is filled by initShallowFallback() when data is requested from a language
+ * that lacks a Messages*.php file.
+ */
+ var $shallowFallbacks = array();
+
+ /**
+ * An array where the keys are codes that have been recached by this instance.
+ */
+ var $recachedLangs = array();
+
+ /**
+ * All item keys
+ */
+ static public $allKeys = array(
+ 'fallback', 'namespaceNames', 'bookstoreList',
+ 'magicWords', 'messages', 'rtl', 'capitalizeAllNouns', 'digitTransformTable',
+ 'separatorTransformTable', 'fallback8bitEncoding', 'linkPrefixExtension',
+ 'linkTrail', 'namespaceAliases',
+ 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap',
+ 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases',
+ 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases',
+ 'digitGroupingPattern', 'pluralRules', 'pluralRuleTypes', 'compiledPluralRules',
+ );
+
+ /**
+ * Keys for items which consist of associative arrays, which may be merged
+ * by a fallback sequence.
+ */
+ static public $mergeableMapKeys = array( 'messages', 'namespaceNames',
+ 'dateFormats', 'imageFiles', 'preloadedMessages'
+ );
+
+ /**
+ * Keys for items which are a numbered array.
+ */
+ static public $mergeableListKeys = array( 'extraUserToggles' );
+
+ /**
+ * Keys for items which contain an array of arrays of equivalent aliases
+ * for each subitem. The aliases may be merged by a fallback sequence.
+ */
+ static public $mergeableAliasListKeys = array( 'specialPageAliases' );
+
+ /**
+ * Keys for items which contain an associative array, and may be merged if
+ * the primary value contains the special array key "inherit". That array
+ * key is removed after the first merge.
+ */
+ static public $optionalMergeKeys = array( 'bookstoreList' );
+
+ /**
+ * Keys for items that are formatted like $magicWords
+ */
+ static public $magicWordKeys = array( 'magicWords' );
+
+ /**
+ * Keys for items where the subitems are stored in the backend separately.
+ */
+ static public $splitKeys = array( 'messages' );
+
+ /**
+ * Keys which are loaded automatically by initLanguage()
+ */
+ static public $preloadedKeys = array( 'dateFormats', 'namespaceNames' );
+
+ /**
+ * Associative array of cached plural rules. The key is the language code,
+ * the value is an array of plural rules for that language.
+ */
+ var $pluralRules = null;
+
+ /**
+ * Associative array of cached plural rule types. The key is the language
+ * code, the value is an array of plural rule types for that language. For
+ * example, $pluralRuleTypes['ar'] = ['zero', 'one', 'two', 'few', 'many'].
+ * The index for each rule type matches the index for the rule in
+ * $pluralRules, thus allowing correlation between the two. The reason we
+ * don't just use the type names as the keys in $pluralRules is because
+ * Language::convertPlural applies the rules based on numeric order (or
+ * explicit numeric parameter), not based on the name of the rule type. For
+ * example, {{plural:count|wordform1|wordform2|wordform3}}, rather than
+ * {{plural:count|one=wordform1|two=wordform2|many=wordform3}}.
+ */
+ var $pluralRuleTypes = null;
+
+ var $mergeableKeys = null;
+
+ /**
+ * Constructor.
+ * For constructor parameters, see the documentation in DefaultSettings.php
+ * for $wgLocalisationCacheConf.
+ *
+ * @param $conf Array
+ * @throws MWException
+ */
+ function __construct( $conf ) {
+ global $wgCacheDirectory;
+
+ $this->conf = $conf;
+ $storeConf = array();
+ if ( !empty( $conf['storeClass'] ) ) {
+ $storeClass = $conf['storeClass'];
+ } else {
+ switch ( $conf['store'] ) {
+ case 'files':
+ case 'file':
+ $storeClass = 'LCStore_CDB';
+ break;
+ case 'db':
+ $storeClass = 'LCStore_DB';
+ break;
+ case 'accel':
+ $storeClass = 'LCStore_Accel';
+ break;
+ case 'detect':
+ $storeClass = $wgCacheDirectory ? 'LCStore_CDB' : 'LCStore_DB';
+ break;
+ default:
+ throw new MWException(
+ 'Please set $wgLocalisationCacheConf[\'store\'] to something sensible.' );
+ }
+ }
+
+ wfDebug( get_class( $this ) . ": using store $storeClass\n" );
+ if ( !empty( $conf['storeDirectory'] ) ) {
+ $storeConf['directory'] = $conf['storeDirectory'];
+ }
+
+ $this->store = new $storeClass( $storeConf );
+ foreach ( array( 'manualRecache', 'forceRecache' ) as $var ) {
+ if ( isset( $conf[$var] ) ) {
+ $this->$var = $conf[$var];
+ }
+ }
+ }
+
+ /**
+ * Returns true if the given key is mergeable, that is, if it is an associative
+ * array which can be merged through a fallback sequence.
+ * @param $key
+ * @return bool
+ */
+ public function isMergeableKey( $key ) {
+ if ( $this->mergeableKeys === null ) {
+ $this->mergeableKeys = array_flip( array_merge(
+ self::$mergeableMapKeys,
+ self::$mergeableListKeys,
+ self::$mergeableAliasListKeys,
+ self::$optionalMergeKeys,
+ self::$magicWordKeys
+ ) );
+ }
+ return isset( $this->mergeableKeys[$key] );
+ }
+
+ /**
+ * Get a cache item.
+ *
+ * Warning: this may be slow for split items (messages), since it will
+ * need to fetch all of the subitems from the cache individually.
+ * @param $code
+ * @param $key
+ * @return mixed
+ */
+ public function getItem( $code, $key ) {
+ if ( !isset( $this->loadedItems[$code][$key] ) ) {
+ wfProfileIn( __METHOD__ . '-load' );
+ $this->loadItem( $code, $key );
+ wfProfileOut( __METHOD__ . '-load' );
+ }
+
+ if ( $key === 'fallback' && isset( $this->shallowFallbacks[$code] ) ) {
+ return $this->shallowFallbacks[$code];
+ }
+
+ return $this->data[$code][$key];
+ }
+
+ /**
+ * Get a subitem, for instance a single message for a given language.
+ * @param $code
+ * @param $key
+ * @param $subkey
+ * @return null
+ */
+ public function getSubitem( $code, $key, $subkey ) {
+ if ( !isset( $this->loadedSubitems[$code][$key][$subkey] ) &&
+ !isset( $this->loadedItems[$code][$key] ) ) {
+ wfProfileIn( __METHOD__ . '-load' );
+ $this->loadSubitem( $code, $key, $subkey );
+ wfProfileOut( __METHOD__ . '-load' );
+ }
+
+ if ( isset( $this->data[$code][$key][$subkey] ) ) {
+ return $this->data[$code][$key][$subkey];
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Get the list of subitem keys for a given item.
+ *
+ * This is faster than array_keys($lc->getItem(...)) for the items listed in
+ * self::$splitKeys.
+ *
+ * Will return null if the item is not found, or false if the item is not an
+ * array.
+ * @param $code
+ * @param $key
+ * @return bool|null|string
+ */
+ public function getSubitemList( $code, $key ) {
+ if ( in_array( $key, self::$splitKeys ) ) {
+ return $this->getSubitem( $code, 'list', $key );
+ } else {
+ $item = $this->getItem( $code, $key );
+ if ( is_array( $item ) ) {
+ return array_keys( $item );
+ } else {
+ return false;
+ }
+ }
+ }
+
+ /**
+ * Load an item into the cache.
+ * @param $code
+ * @param $key
+ */
+ protected function loadItem( $code, $key ) {
+ if ( !isset( $this->initialisedLangs[$code] ) ) {
+ $this->initLanguage( $code );
+ }
+
+ // Check to see if initLanguage() loaded it for us
+ if ( isset( $this->loadedItems[$code][$key] ) ) {
+ return;
+ }
+
+ if ( isset( $this->shallowFallbacks[$code] ) ) {
+ $this->loadItem( $this->shallowFallbacks[$code], $key );
+ return;
+ }
+
+ if ( in_array( $key, self::$splitKeys ) ) {
+ $subkeyList = $this->getSubitem( $code, 'list', $key );
+ foreach ( $subkeyList as $subkey ) {
+ if ( isset( $this->data[$code][$key][$subkey] ) ) {
+ continue;
+ }
+ $this->data[$code][$key][$subkey] = $this->getSubitem( $code, $key, $subkey );
+ }
+ } else {
+ $this->data[$code][$key] = $this->store->get( $code, $key );
+ }
+
+ $this->loadedItems[$code][$key] = true;
+ }
+
+ /**
+ * Load a subitem into the cache
+ * @param $code
+ * @param $key
+ * @param $subkey
+ */
+ protected function loadSubitem( $code, $key, $subkey ) {
+ if ( !in_array( $key, self::$splitKeys ) ) {
+ $this->loadItem( $code, $key );
+ return;
+ }
+
+ if ( !isset( $this->initialisedLangs[$code] ) ) {
+ $this->initLanguage( $code );
+ }
+
+ // Check to see if initLanguage() loaded it for us
+ if ( isset( $this->loadedItems[$code][$key] ) ||
+ isset( $this->loadedSubitems[$code][$key][$subkey] ) ) {
+ return;
+ }
+
+ if ( isset( $this->shallowFallbacks[$code] ) ) {
+ $this->loadSubitem( $this->shallowFallbacks[$code], $key, $subkey );
+ return;
+ }
+
+ $value = $this->store->get( $code, "$key:$subkey" );
+ $this->data[$code][$key][$subkey] = $value;
+ $this->loadedSubitems[$code][$key][$subkey] = true;
+ }
+
+ /**
+ * Returns true if the cache identified by $code is missing or expired.
+ *
+ * @param string $code
+ *
+ * @return bool
+ */
+ public function isExpired( $code ) {
+ if ( $this->forceRecache && !isset( $this->recachedLangs[$code] ) ) {
+ wfDebug( __METHOD__ . "($code): forced reload\n" );
+ return true;
+ }
+
+ $deps = $this->store->get( $code, 'deps' );
+ $keys = $this->store->get( $code, 'list' );
+ $preload = $this->store->get( $code, 'preload' );
+ // Different keys may expire separately, at least in LCStore_Accel
+ if ( $deps === null || $keys === null || $preload === null ) {
+ wfDebug( __METHOD__ . "($code): cache missing, need to make one\n" );
+ return true;
+ }
+
+ foreach ( $deps as $dep ) {
+ // Because we're unserializing stuff from cache, we
+ // could receive objects of classes that don't exist
+ // anymore (e.g. uninstalled extensions)
+ // When this happens, always expire the cache
+ if ( !$dep instanceof CacheDependency || $dep->isExpired() ) {
+ wfDebug( __METHOD__ . "($code): cache for $code expired due to " .
+ get_class( $dep ) . "\n" );
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * Initialise a language in this object. Rebuild the cache if necessary.
+ * @param $code
+ * @throws MWException
+ */
+ protected function initLanguage( $code ) {
+ if ( isset( $this->initialisedLangs[$code] ) ) {
+ return;
+ }
+
+ $this->initialisedLangs[$code] = true;
+
+ # If the code is of the wrong form for a Messages*.php file, do a shallow fallback
+ if ( !Language::isValidBuiltInCode( $code ) ) {
+ $this->initShallowFallback( $code, 'en' );
+ return;
+ }
+
+ # Recache the data if necessary
+ if ( !$this->manualRecache && $this->isExpired( $code ) ) {
+ if ( file_exists( Language::getMessagesFileName( $code ) ) ) {
+ $this->recache( $code );
+ } elseif ( $code === 'en' ) {
+ throw new MWException( 'MessagesEn.php is missing.' );
+ } else {
+ $this->initShallowFallback( $code, 'en' );
+ }
+ return;
+ }
+
+ # Preload some stuff
+ $preload = $this->getItem( $code, 'preload' );
+ if ( $preload === null ) {
+ if ( $this->manualRecache ) {
+ // No Messages*.php file. Do shallow fallback to en.
+ if ( $code === 'en' ) {
+ throw new MWException( 'No localisation cache found for English. ' .
+ 'Please run maintenance/rebuildLocalisationCache.php.' );
+ }
+ $this->initShallowFallback( $code, 'en' );
+ return;
+ } else {
+ throw new MWException( 'Invalid or missing localisation cache.' );
+ }
+ }
+ $this->data[$code] = $preload;
+ foreach ( $preload as $key => $item ) {
+ if ( in_array( $key, self::$splitKeys ) ) {
+ foreach ( $item as $subkey => $subitem ) {
+ $this->loadedSubitems[$code][$key][$subkey] = true;
+ }
+ } else {
+ $this->loadedItems[$code][$key] = true;
+ }
+ }
+ }
+
+ /**
+ * Create a fallback from one language to another, without creating a
+ * complete persistent cache.
+ * @param $primaryCode
+ * @param $fallbackCode
+ */
+ public function initShallowFallback( $primaryCode, $fallbackCode ) {
+ $this->data[$primaryCode] =& $this->data[$fallbackCode];
+ $this->loadedItems[$primaryCode] =& $this->loadedItems[$fallbackCode];
+ $this->loadedSubitems[$primaryCode] =& $this->loadedSubitems[$fallbackCode];
+ $this->shallowFallbacks[$primaryCode] = $fallbackCode;
+ }
+
+ /**
+ * Read a PHP file containing localisation data.
+ * @param $_fileName
+ * @param $_fileType
+ * @throws MWException
+ * @return array
+ */
+ protected function readPHPFile( $_fileName, $_fileType ) {
+ wfProfileIn( __METHOD__ );
+ // Disable APC caching
+ $_apcEnabled = ini_set( 'apc.cache_by_default', '0' );
+ include $_fileName;
+ ini_set( 'apc.cache_by_default', $_apcEnabled );
+
+ if ( $_fileType == 'core' || $_fileType == 'extension' ) {
+ $data = compact( self::$allKeys );
+ } elseif ( $_fileType == 'aliases' ) {
+ $data = compact( 'aliases' );
+ } else {
+ wfProfileOut( __METHOD__ );
+ throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" );
+ }
+ wfProfileOut( __METHOD__ );
+ return $data;
+ }
+
+ /**
+ * Get the compiled plural rules for a given language from the XML files.
+ * @since 1.20
+ */
+ public function getCompiledPluralRules( $code ) {
+ $rules = $this->getPluralRules( $code );
+ if ( $rules === null ) {
+ return null;
+ }
+ try {
+ $compiledRules = CLDRPluralRuleEvaluator::compile( $rules );
+ } catch ( CLDRPluralRuleError $e ) {
+ wfDebugLog( 'l10n', $e->getMessage() . "\n" );
+ return array();
+ }
+ return $compiledRules;
+ }
+
+ /**
+ * Get the plural rules for a given language from the XML files.
+ * Cached.
+ * @since 1.20
+ */
+ public function getPluralRules( $code ) {
+ if ( $this->pluralRules === null ) {
+ $this->loadPluralFiles();
+ }
+ if ( !isset( $this->pluralRules[$code] ) ) {
+ return null;
+ } else {
+ return $this->pluralRules[$code];
+ }
+ }
+
+ /**
+ * Get the plural rule types for a given language from the XML files.
+ * Cached.
+ * @since 1.22
+ */
+ public function getPluralRuleTypes( $code ) {
+ if ( $this->pluralRuleTypes === null ) {
+ $this->loadPluralFiles();
+ }
+ if ( !isset( $this->pluralRuleTypes[$code] ) ) {
+ return null;
+ } else {
+ return $this->pluralRuleTypes[$code];
+ }
+ }
+
+ /**
+ * Load the plural XML files.
+ */
+ protected function loadPluralFiles() {
+ global $IP;
+ $cldrPlural = "$IP/languages/data/plurals.xml";
+ $mwPlural = "$IP/languages/data/plurals-mediawiki.xml";
+ // Load CLDR plural rules
+ $this->loadPluralFile( $cldrPlural );
+ if ( file_exists( $mwPlural ) ) {
+ // Override or extend
+ $this->loadPluralFile( $mwPlural );
+ }
+ }
+
+ /**
+ * Load a plural XML file with the given filename, compile the relevant
+ * rules, and save the compiled rules in a process-local cache.
+ */
+ protected function loadPluralFile( $fileName ) {
+ $doc = new DOMDocument;
+ $doc->load( $fileName );
+ $rulesets = $doc->getElementsByTagName( "pluralRules" );
+ foreach ( $rulesets as $ruleset ) {
+ $codes = $ruleset->getAttribute( 'locales' );
+ $rules = array();
+ $ruleTypes = array();
+ $ruleElements = $ruleset->getElementsByTagName( "pluralRule" );
+ foreach ( $ruleElements as $elt ) {
+ $ruleType = $elt->getAttribute( 'count' );
+ if ( $ruleType === 'other' ) {
+ // Don't record "other" rules, which have an empty condition
+ continue;
+ }
+ $rules[] = $elt->nodeValue;
+ $ruleTypes[] = $ruleType;
+ }
+ foreach ( explode( ' ', $codes ) as $code ) {
+ $this->pluralRules[$code] = $rules;
+ $this->pluralRuleTypes[$code] = $ruleTypes;
+ }
+ }
+ }
+
+ /**
+ * Read the data from the source files for a given language, and register
+ * the relevant dependencies in the $deps array. If the localisation
+ * exists, the data array is returned, otherwise false is returned.
+ */
+ protected function readSourceFilesAndRegisterDeps( $code, &$deps ) {
+ global $IP;
+ wfProfileIn( __METHOD__ );
+
+ $fileName = Language::getMessagesFileName( $code );
+ if ( !file_exists( $fileName ) ) {
+ wfProfileOut( __METHOD__ );
+ return false;
+ }
+
+ $deps[] = new FileDependency( $fileName );
+ $data = $this->readPHPFile( $fileName, 'core' );
+
+ # Load CLDR plural rules for JavaScript
+ $data['pluralRules'] = $this->getPluralRules( $code );
+ # And for PHP
+ $data['compiledPluralRules'] = $this->getCompiledPluralRules( $code );
+ # Load plural rule types
+ $data['pluralRuleTypes'] = $this->getPluralRuleTypes( $code );
+
+ $deps['plurals'] = new FileDependency( "$IP/languages/data/plurals.xml" );
+ $deps['plurals-mw'] = new FileDependency( "$IP/languages/data/plurals-mediawiki.xml" );
+
+ wfProfileOut( __METHOD__ );
+ return $data;
+ }
+
+ /**
+ * Merge two localisation values, a primary and a fallback, overwriting the
+ * primary value in place.
+ * @param $key
+ * @param $value
+ * @param $fallbackValue
+ */
+ protected function mergeItem( $key, &$value, $fallbackValue ) {
+ if ( !is_null( $value ) ) {
+ if ( !is_null( $fallbackValue ) ) {
+ if ( in_array( $key, self::$mergeableMapKeys ) ) {
+ $value = $value + $fallbackValue;
+ } elseif ( in_array( $key, self::$mergeableListKeys ) ) {
+ $value = array_unique( array_merge( $fallbackValue, $value ) );
+ } elseif ( in_array( $key, self::$mergeableAliasListKeys ) ) {
+ $value = array_merge_recursive( $value, $fallbackValue );
+ } elseif ( in_array( $key, self::$optionalMergeKeys ) ) {
+ if ( !empty( $value['inherit'] ) ) {
+ $value = array_merge( $fallbackValue, $value );
+ }
+
+ if ( isset( $value['inherit'] ) ) {
+ unset( $value['inherit'] );
+ }
+ } elseif ( in_array( $key, self::$magicWordKeys ) ) {
+ $this->mergeMagicWords( $value, $fallbackValue );
+ }
+ }
+ } else {
+ $value = $fallbackValue;
+ }
+ }
+
+ /**
+ * @param $value
+ * @param $fallbackValue
+ */
+ protected function mergeMagicWords( &$value, $fallbackValue ) {
+ foreach ( $fallbackValue as $magicName => $fallbackInfo ) {
+ if ( !isset( $value[$magicName] ) ) {
+ $value[$magicName] = $fallbackInfo;
+ } else {
+ $oldSynonyms = array_slice( $fallbackInfo, 1 );
+ $newSynonyms = array_slice( $value[$magicName], 1 );
+ $synonyms = array_values( array_unique( array_merge(
+ $newSynonyms, $oldSynonyms ) ) );
+ $value[$magicName] = array_merge( array( $fallbackInfo[0] ), $synonyms );
+ }
+ }
+ }
+
+ /**
+ * Given an array mapping language code to localisation value, such as is
+ * found in extension *.i18n.php files, iterate through a fallback sequence
+ * to merge the given data with an existing primary value.
+ *
+ * Returns true if any data from the extension array was used, false
+ * otherwise.
+ * @param $codeSequence
+ * @param $key
+ * @param $value
+ * @param $fallbackValue
+ * @return bool
+ */
+ protected function mergeExtensionItem( $codeSequence, $key, &$value, $fallbackValue ) {
+ $used = false;
+ foreach ( $codeSequence as $code ) {
+ if ( isset( $fallbackValue[$code] ) ) {
+ $this->mergeItem( $key, $value, $fallbackValue[$code] );
+ $used = true;
+ }
+ }
+
+ return $used;
+ }
+
+ /**
+ * Load localisation data for a given language for both core and extensions
+ * and save it to the persistent cache store and the process cache
+ * @param $code
+ * @throws MWException
+ */
+ public function recache( $code ) {
+ global $wgExtensionMessagesFiles;
+ wfProfileIn( __METHOD__ );
+
+ if ( !$code ) {
+ wfProfileOut( __METHOD__ );
+ throw new MWException( "Invalid language code requested" );
+ }
+ $this->recachedLangs[$code] = true;
+
+ # Initial values
+ $initialData = array_combine(
+ self::$allKeys,
+ array_fill( 0, count( self::$allKeys ), null ) );
+ $coreData = $initialData;
+ $deps = array();
+
+ # Load the primary localisation from the source file
+ $data = $this->readSourceFilesAndRegisterDeps( $code, $deps );
+ if ( $data === false ) {
+ wfDebug( __METHOD__ . ": no localisation file for $code, using fallback to en\n" );
+ $coreData['fallback'] = 'en';
+ } else {
+ wfDebug( __METHOD__ . ": got localisation for $code from source\n" );
+
+ # Merge primary localisation
+ foreach ( $data as $key => $value ) {
+ $this->mergeItem( $key, $coreData[$key], $value );
+ }
+
+ }
+
+ # Fill in the fallback if it's not there already
+ if ( is_null( $coreData['fallback'] ) ) {
+ $coreData['fallback'] = $code === 'en' ? false : 'en';
+ }
+ if ( $coreData['fallback'] === false ) {
+ $coreData['fallbackSequence'] = array();
+ } else {
+ $coreData['fallbackSequence'] = array_map( 'trim', explode( ',', $coreData['fallback'] ) );
+ $len = count( $coreData['fallbackSequence'] );
+
+ # Ensure that the sequence ends at en
+ if ( $coreData['fallbackSequence'][$len - 1] !== 'en' ) {
+ $coreData['fallbackSequence'][] = 'en';
+ }
+
+ # Load the fallback localisation item by item and merge it
+ foreach ( $coreData['fallbackSequence'] as $fbCode ) {
+ # Load the secondary localisation from the source file to
+ # avoid infinite cycles on cyclic fallbacks
+ $fbData = $this->readSourceFilesAndRegisterDeps( $fbCode, $deps );
+ if ( $fbData === false ) {
+ continue;
+ }
+
+ foreach ( self::$allKeys as $key ) {
+ if ( !isset( $fbData[$key] ) ) {
+ continue;
+ }
+
+ if ( is_null( $coreData[$key] ) || $this->isMergeableKey( $key ) ) {
+ $this->mergeItem( $key, $coreData[$key], $fbData[$key] );
+ }
+ }
+ }
+ }
+
+ $codeSequence = array_merge( array( $code ), $coreData['fallbackSequence'] );
+
+ # Load the extension localisations
+ # This is done after the core because we know the fallback sequence now.
+ # But it has a higher precedence for merging so that we can support things
+ # like site-specific message overrides.
+ wfProfileIn( __METHOD__ . '-extensions' );
+ $allData = $initialData;
+ foreach ( $wgExtensionMessagesFiles as $fileName ) {
+ $data = $this->readPHPFile( $fileName, 'extension' );
+ $used = false;
+
+ foreach ( $data as $key => $item ) {
+ if ( $this->mergeExtensionItem( $codeSequence, $key, $allData[$key], $item ) ) {
+ $used = true;
+ }
+ }
+
+ if ( $used ) {
+ $deps[] = new FileDependency( $fileName );
+ }
+ }
+
+ # Merge core data into extension data
+ foreach ( $coreData as $key => $item ) {
+ $this->mergeItem( $key, $allData[$key], $item );
+ }
+ wfProfileOut( __METHOD__ . '-extensions' );
+
+ # Add cache dependencies for any referenced globals
+ $deps['wgExtensionMessagesFiles'] = new GlobalDependency( 'wgExtensionMessagesFiles' );
+ $deps['version'] = new ConstantDependency( 'MW_LC_VERSION' );
+
+ # Add dependencies to the cache entry
+ $allData['deps'] = $deps;
+
+ # Replace spaces with underscores in namespace names
+ $allData['namespaceNames'] = str_replace( ' ', '_', $allData['namespaceNames'] );
+
+ # And do the same for special page aliases. $page is an array.
+ foreach ( $allData['specialPageAliases'] as &$page ) {
+ $page = str_replace( ' ', '_', $page );
+ }
+ # Decouple the reference to prevent accidental damage
+ unset( $page );
+
+ # If there were no plural rules, return an empty array
+ if ( $allData['pluralRules'] === null ) {
+ $allData['pluralRules'] = array();
+ }
+ if ( $allData['compiledPluralRules'] === null ) {
+ $allData['compiledPluralRules'] = array();
+ }
+ # If there were no plural rule types, return an empty array
+ if ( $allData['pluralRuleTypes'] === null ) {
+ $allData['pluralRuleTypes'] = array();
+ }
+
+ # Set the list keys
+ $allData['list'] = array();
+ foreach ( self::$splitKeys as $key ) {
+ $allData['list'][$key] = array_keys( $allData[$key] );
+ }
+ # Run hooks
+ wfRunHooks( 'LocalisationCacheRecache', array( $this, $code, &$allData ) );
+
+ if ( is_null( $allData['namespaceNames'] ) ) {
+ wfProfileOut( __METHOD__ );
+ throw new MWException( __METHOD__ . ': Localisation data failed sanity check! ' .
+ 'Check that your languages/messages/MessagesEn.php file is intact.' );
+ }
+
+ # Set the preload key
+ $allData['preload'] = $this->buildPreload( $allData );
+
+ # Save to the process cache and register the items loaded
+ $this->data[$code] = $allData;
+ foreach ( $allData as $key => $item ) {
+ $this->loadedItems[$code][$key] = true;
+ }
+
+ # Save to the persistent cache
+ wfProfileIn( __METHOD__ . '-write' );
+ $this->store->startWrite( $code );
+ foreach ( $allData as $key => $value ) {
+ if ( in_array( $key, self::$splitKeys ) ) {
+ foreach ( $value as $subkey => $subvalue ) {
+ $this->store->set( "$key:$subkey", $subvalue );
+ }
+ } else {
+ $this->store->set( $key, $value );
+ }
+ }
+ $this->store->finishWrite();
+ wfProfileOut( __METHOD__ . '-write' );
+
+ # Clear out the MessageBlobStore
+ # HACK: If using a null (i.e. disabled) storage backend, we
+ # can't write to the MessageBlobStore either
+ if ( !$this->store instanceof LCStore_Null ) {
+ MessageBlobStore::clear();
+ }
+
+ wfProfileOut( __METHOD__ );
+ }
+
+ /**
+ * Build the preload item from the given pre-cache data.
+ *
+ * The preload item will be loaded automatically, improving performance
+ * for the commonly-requested items it contains.
+ * @param $data
+ * @return array
+ */
+ protected function buildPreload( $data ) {
+ $preload = array( 'messages' => array() );
+ foreach ( self::$preloadedKeys as $key ) {
+ $preload[$key] = $data[$key];
+ }
+
+ foreach ( $data['preloadedMessages'] as $subkey ) {
+ if ( isset( $data['messages'][$subkey] ) ) {
+ $subitem = $data['messages'][$subkey];
+ } else {
+ $subitem = null;
+ }
+ $preload['messages'][$subkey] = $subitem;
+ }
+
+ return $preload;
+ }
+
+ /**
+ * Unload the data for a given language from the object cache.
+ * Reduces memory usage.
+ * @param $code
+ */
+ public function unload( $code ) {
+ unset( $this->data[$code] );
+ unset( $this->loadedItems[$code] );
+ unset( $this->loadedSubitems[$code] );
+ unset( $this->initialisedLangs[$code] );
+ unset( $this->shallowFallbacks[$code] );
+
+ foreach ( $this->shallowFallbacks as $shallowCode => $fbCode ) {
+ if ( $fbCode === $code ) {
+ $this->unload( $shallowCode );
+ }
+ }
+ }
+
+ /**
+ * Unload all data
+ */
+ public function unloadAll() {
+ foreach ( $this->initialisedLangs as $lang => $unused ) {
+ $this->unload( $lang );
+ }
+ }
+
+ /**
+ * Disable the storage backend
+ */
+ public function disableBackend() {
+ $this->store = new LCStore_Null;
+ $this->manualRecache = false;
+ }
+
+ /**
+ * Return an array with initialised languages.
+ *
+ * @return array
+ */
+ public function getInitialisedLanguages() {
+ return $this->initialisedLangs;
+ }
+
+ /**
+ * Set initialised languages.
+ *
+ * @param array $languages Optional array of initialised languages.
+ */
+ public function setInitialisedLanguages( $languages = array() ) {
+ $this->initialisedLangs = $languages;
+ }
+
+}
+
+/**
+ * Interface for the persistence layer of LocalisationCache.
+ *
+ * The persistence layer is two-level hierarchical cache. The first level
+ * is the language, the second level is the item or subitem.
+ *
+ * Since the data for a whole language is rebuilt in one operation, it needs
+ * to have a fast and atomic method for deleting or replacing all of the
+ * current data for a given language. The interface reflects this bulk update
+ * operation. Callers writing to the cache must first call startWrite(), then
+ * will call set() a couple of thousand times, then will call finishWrite()
+ * to commit the operation. When finishWrite() is called, the cache is
+ * expected to delete all data previously stored for that language.
+ *
+ * The values stored are PHP variables suitable for serialize(). Implementations
+ * of LCStore are responsible for serializing and unserializing.
+ */
+interface LCStore {
+ /**
+ * Get a value.
+ * @param string $code Language code
+ * @param string $key Cache key
+ */
+ function get( $code, $key );
+
+ /**
+ * Start a write transaction.
+ * @param string $code Language code
+ */
+ function startWrite( $code );
+
+ /**
+ * Finish a write transaction.
+ */
+ function finishWrite();
+
+ /**
+ * Set a key to a given value. startWrite() must be called before this
+ * is called, and finishWrite() must be called afterwards.
+ * @param string $key
+ * @param mixed $value
+ */
+ function set( $key, $value );
+}
+
+/**
+ * LCStore implementation which uses PHP accelerator to store data.
+ * This will work if one of XCache, WinCache or APC cacher is configured.
+ * (See ObjectCache.php)
+ */
+class LCStore_Accel implements LCStore {
+ var $currentLang;
+ var $keys;
+
+ public function __construct() {
+ $this->cache = wfGetCache( CACHE_ACCEL );
+ }
+
+ public function get( $code, $key ) {
+ $k = wfMemcKey( 'l10n', $code, 'k', $key );
+ $r = $this->cache->get( $k );
+ return $r === false ? null : $r;
+ }
+
+ public function startWrite( $code ) {
+ $k = wfMemcKey( 'l10n', $code, 'l' );
+ $keys = $this->cache->get( $k );
+ if ( $keys ) {
+ foreach ( $keys as $k ) {
+ $this->cache->delete( $k );
+ }
+ }
+ $this->currentLang = $code;
+ $this->keys = array();
+ }
+
+ public function finishWrite() {
+ if ( $this->currentLang ) {
+ $k = wfMemcKey( 'l10n', $this->currentLang, 'l' );
+ $this->cache->set( $k, array_keys( $this->keys ) );
+ }
+ $this->currentLang = null;
+ $this->keys = array();
+ }
+
+ public function set( $key, $value ) {
+ if ( $this->currentLang ) {
+ $k = wfMemcKey( 'l10n', $this->currentLang, 'k', $key );
+ $this->keys[$k] = true;
+ $this->cache->set( $k, $value );
+ }
+ }
+}
+
+/**
+ * LCStore implementation which uses the standard DB functions to store data.
+ * This will work on any MediaWiki installation.
+ */
+class LCStore_DB implements LCStore {
+ var $currentLang;
+ var $writesDone = false;
+
+ /**
+ * @var DatabaseBase
+ */
+ var $dbw;
+ var $batch;
+ var $readOnly = false;
+
+ public function get( $code, $key ) {
+ if ( $this->writesDone ) {
+ $db = wfGetDB( DB_MASTER );
+ } else {
+ $db = wfGetDB( DB_SLAVE );
+ }
+ $row = $db->selectRow( 'l10n_cache', array( 'lc_value' ),
+ array( 'lc_lang' => $code, 'lc_key' => $key ), __METHOD__ );
+ if ( $row ) {
+ return unserialize( $row->lc_value );
+ } else {
+ return null;
+ }
+ }
+
+ public function startWrite( $code ) {
+ if ( $this->readOnly ) {
+ return;
+ }
+
+ if ( !$code ) {
+ throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
+ }
+
+ $this->dbw = wfGetDB( DB_MASTER );
+ try {
+ $this->dbw->begin( __METHOD__ );
+ $this->dbw->delete( 'l10n_cache', array( 'lc_lang' => $code ), __METHOD__ );
+ } catch ( DBQueryError $e ) {
+ if ( $this->dbw->wasReadOnlyError() ) {
+ $this->readOnly = true;
+ $this->dbw->rollback( __METHOD__ );
+ return;
+ } else {
+ throw $e;
+ }
+ }
+
+ $this->currentLang = $code;
+ $this->batch = array();
+ }
+
+ public function finishWrite() {
+ if ( $this->readOnly ) {
+ return;
+ }
+
+ if ( $this->batch ) {
+ $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
+ }
+
+ $this->dbw->commit( __METHOD__ );
+ $this->currentLang = null;
+ $this->dbw = null;
+ $this->batch = array();
+ $this->writesDone = true;
+ }
+
+ public function set( $key, $value ) {
+ if ( $this->readOnly ) {
+ return;
+ }
+
+ if ( is_null( $this->currentLang ) ) {
+ throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
+ }
+
+ $this->batch[] = array(
+ 'lc_lang' => $this->currentLang,
+ 'lc_key' => $key,
+ 'lc_value' => serialize( $value ) );
+
+ if ( count( $this->batch ) >= 100 ) {
+ $this->dbw->insert( 'l10n_cache', $this->batch, __METHOD__ );
+ $this->batch = array();
+ }
+ }
+}
+
+/**
+ * LCStore implementation which stores data as a collection of CDB files in the
+ * directory given by $wgCacheDirectory. If $wgCacheDirectory is not set, this
+ * will throw an exception.
+ *
+ * Profiling indicates that on Linux, this implementation outperforms MySQL if
+ * the directory is on a local filesystem and there is ample kernel cache
+ * space. The performance advantage is greater when the DBA extension is
+ * available than it is with the PHP port.
+ *
+ * See Cdb.php and http://cr.yp.to/cdb.html
+ */
+class LCStore_CDB implements LCStore {
+ var $readers, $writer, $currentLang, $directory;
+
+ function __construct( $conf = array() ) {
+ global $wgCacheDirectory;
+
+ if ( isset( $conf['directory'] ) ) {
+ $this->directory = $conf['directory'];
+ } else {
+ $this->directory = $wgCacheDirectory;
+ }
+ }
+
+ public function get( $code, $key ) {
+ if ( !isset( $this->readers[$code] ) ) {
+ $fileName = $this->getFileName( $code );
+
+ if ( !file_exists( $fileName ) ) {
+ $this->readers[$code] = false;
+ } else {
+ $this->readers[$code] = CdbReader::open( $fileName );
+ }
+ }
+
+ if ( !$this->readers[$code] ) {
+ return null;
+ } else {
+ $value = $this->readers[$code]->get( $key );
+
+ if ( $value === false ) {
+ return null;
+ }
+ return unserialize( $value );
+ }
+ }
+
+ public function startWrite( $code ) {
+ if ( !file_exists( $this->directory ) ) {
+ if ( !wfMkdirParents( $this->directory, null, __METHOD__ ) ) {
+ throw new MWException( "Unable to create the localisation store " .
+ "directory \"{$this->directory}\"" );
+ }
+ }
+
+ // Close reader to stop permission errors on write
+ if ( !empty( $this->readers[$code] ) ) {
+ $this->readers[$code]->close();
+ }
+
+ $this->writer = CdbWriter::open( $this->getFileName( $code ) );
+ $this->currentLang = $code;
+ }
+
+ public function finishWrite() {
+ // Close the writer
+ $this->writer->close();
+ $this->writer = null;
+ unset( $this->readers[$this->currentLang] );
+ $this->currentLang = null;
+ }
+
+ public function set( $key, $value ) {
+ if ( is_null( $this->writer ) ) {
+ throw new MWException( __CLASS__ . ': must call startWrite() before calling set()' );
+ }
+ $this->writer->set( $key, serialize( $value ) );
+ }
+
+ protected function getFileName( $code ) {
+ if ( strval( $code ) === '' || strpos( $code, '/' ) !== false ) {
+ throw new MWException( __METHOD__ . ": Invalid language \"$code\"" );
+ }
+ return "{$this->directory}/l10n_cache-$code.cdb";
+ }
+}
+
+/**
+ * Null store backend, used to avoid DB errors during install
+ */
+class LCStore_Null implements LCStore {
+ public function get( $code, $key ) {
+ return null;
+ }
+
+ public function startWrite( $code ) {}
+ public function finishWrite() {}
+ public function set( $key, $value ) {}
+}
+
+/**
+ * A localisation cache optimised for loading large amounts of data for many
+ * languages. Used by rebuildLocalisationCache.php.
+ */
+class LocalisationCache_BulkLoad extends LocalisationCache {
+ /**
+ * A cache of the contents of data files.
+ * Core files are serialized to avoid using ~1GB of RAM during a recache.
+ */
+ var $fileCache = array();
+
+ /**
+ * Most recently used languages. Uses the linked-list aspect of PHP hashtables
+ * to keep the most recently used language codes at the end of the array, and
+ * the language codes that are ready to be deleted at the beginning.
+ */
+ var $mruLangs = array();
+
+ /**
+ * Maximum number of languages that may be loaded into $this->data
+ */
+ var $maxLoadedLangs = 10;
+
+ /**
+ * @param $fileName
+ * @param $fileType
+ * @return array|mixed
+ */
+ protected function readPHPFile( $fileName, $fileType ) {
+ $serialize = $fileType === 'core';
+ if ( !isset( $this->fileCache[$fileName][$fileType] ) ) {
+ $data = parent::readPHPFile( $fileName, $fileType );
+
+ if ( $serialize ) {
+ $encData = serialize( $data );
+ } else {
+ $encData = $data;
+ }
+
+ $this->fileCache[$fileName][$fileType] = $encData;
+
+ return $data;
+ } elseif ( $serialize ) {
+ return unserialize( $this->fileCache[$fileName][$fileType] );
+ } else {
+ return $this->fileCache[$fileName][$fileType];
+ }
+ }
+
+ /**
+ * @param $code
+ * @param $key
+ * @return mixed
+ */
+ public function getItem( $code, $key ) {
+ unset( $this->mruLangs[$code] );
+ $this->mruLangs[$code] = true;
+ return parent::getItem( $code, $key );
+ }
+
+ /**
+ * @param $code
+ * @param $key
+ * @param $subkey
+ * @return
+ */
+ public function getSubitem( $code, $key, $subkey ) {
+ unset( $this->mruLangs[$code] );
+ $this->mruLangs[$code] = true;
+ return parent::getSubitem( $code, $key, $subkey );
+ }
+
+ /**
+ * @param $code
+ */
+ public function recache( $code ) {
+ parent::recache( $code );
+ unset( $this->mruLangs[$code] );
+ $this->mruLangs[$code] = true;
+ $this->trimCache();
+ }
+
+ /**
+ * @param $code
+ */
+ public function unload( $code ) {
+ unset( $this->mruLangs[$code] );
+ parent::unload( $code );
+ }
+
+ /**
+ * Unload cached languages until there are less than $this->maxLoadedLangs
+ */
+ protected function trimCache() {
+ while ( count( $this->data ) > $this->maxLoadedLangs && count( $this->mruLangs ) ) {
+ reset( $this->mruLangs );
+ $code = key( $this->mruLangs );
+ wfDebug( __METHOD__ . ": unloading $code\n" );
+ $this->unload( $code );
+ }
+ }
+}
diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php
index b854a2ec..a92c87f4 100644
--- a/includes/cache/MessageCache.php
+++ b/includes/cache/MessageCache.php
@@ -22,14 +22,29 @@
*/
/**
- *
+ * MediaWiki message cache structure version.
+ * Bump this whenever the message cache format has changed.
*/
-define( 'MSG_LOAD_TIMEOUT', 60 );
-define( 'MSG_LOCK_TIMEOUT', 10 );
-define( 'MSG_WAIT_TIMEOUT', 10 );
define( 'MSG_CACHE_VERSION', 1 );
/**
+ * Memcached timeout when loading a key.
+ * See MessageCache::load()
+ */
+define( 'MSG_LOAD_TIMEOUT', 60 );
+
+/**
+ * Memcached timeout when locking a key for a writing operation.
+ * See MessageCache::lock()
+ */
+define( 'MSG_LOCK_TIMEOUT', 30 );
+/**
+ * Number of times we will try to acquire a lock from Memcached.
+ * This comes in addition to MSG_LOCK_TIMEOUT.
+ */
+define( 'MSG_WAIT_TIMEOUT', 30 );
+
+/**
* Message cache
* Performs various MediaWiki namespace-related functions
* @ingroup Cache
@@ -44,10 +59,16 @@ class MessageCache {
*/
protected $mCache;
- // Should mean that database cannot be used, but check
+ /**
+ * Should mean that database cannot be used, but check
+ * @var bool $mDisable
+ */
protected $mDisable;
- /// Lifetime for cache, used by object caching
+ /**
+ * Lifetime for cache, used by object caching.
+ * Set on construction, see __construct().
+ */
protected $mExpiry;
/**
@@ -56,38 +77,21 @@ class MessageCache {
*/
protected $mParserOptions, $mParser;
- /// Variable for tracking which variables are already loaded
- protected $mLoadedLanguages = array();
-
- /**
- * Used for automatic detection of most used messages.
- */
- protected $mRequestedMessages = array();
-
/**
- * How long the message request counts are stored. Longer period gives
- * better sample, but also takes longer to adapt changes. The counts
- * are aggregrated per day, regardless of the value of this variable.
+ * Variable for tracking which variables are already loaded
+ * @var array $mLoadedLanguages
*/
- protected static $mAdaptiveDataAge = 604800; // Is 7*24*3600
-
- /**
- * Filter the tail of less used messages that are requested more seldom
- * than this factor times the number of request of most requested message.
- * These messages are not loaded in the default set, but are still cached
- * individually on demand with the normal cache expiry time.
- */
- protected static $mAdaptiveInclusionThreshold = 0.05;
+ protected $mLoadedLanguages = array();
/**
* Singleton instance
*
- * @var MessageCache
+ * @var MessageCache $instance
*/
private static $instance;
/**
- * @var bool
+ * @var bool $mInParser
*/
protected $mInParser = false;
@@ -95,12 +99,16 @@ class MessageCache {
* Get the signleton instance of this class
*
* @since 1.18
- * @return MessageCache object
+ * @return MessageCache
*/
public static function singleton() {
if ( is_null( self::$instance ) ) {
global $wgUseDatabaseMessages, $wgMsgCacheExpiry;
- self::$instance = new self( wfGetMessageCacheStorage(), $wgUseDatabaseMessages, $wgMsgCacheExpiry );
+ self::$instance = new self(
+ wfGetMessageCacheStorage(),
+ $wgUseDatabaseMessages,
+ $wgMsgCacheExpiry
+ );
}
return self::$instance;
}
@@ -114,6 +122,11 @@ class MessageCache {
self::$instance = null;
}
+ /**
+ * @param ObjectCache $memCached A cache instance. If none, fall back to CACHE_NONE.
+ * @param bool $useDB
+ * @param int $expiry Lifetime for cache. @see $mExpiry.
+ */
function __construct( $memCached, $useDB, $expiry ) {
if ( !$memCached ) {
$memCached = wfGetCache( CACHE_NONE );
@@ -139,15 +152,13 @@ class MessageCache {
/**
* Try to load the cache from a local file.
- * Actual format of the file depends on the $wgLocalMessageCacheSerialized
- * setting.
*
- * @param $hash String: the hash of contents, to check validity.
- * @param $code Mixed: Optional language code, see documenation of load().
- * @return bool on failure.
+ * @param string $hash the hash of contents, to check validity.
+ * @param Mixed $code Optional language code, see documenation of load().
+ * @return array The cache array
*/
- function loadFromLocal( $hash, $code ) {
- global $wgCacheDirectory, $wgLocalMessageCacheSerialized;
+ function getLocalCache( $hash, $code ) {
+ global $wgCacheDirectory;
$filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
@@ -159,31 +170,19 @@ class MessageCache {
return false; // No cache file
}
- if ( $wgLocalMessageCacheSerialized ) {
- // Check to see if the file has the hash specified
- $localHash = fread( $file, 32 );
- if ( $hash === $localHash ) {
- // All good, get the rest of it
- $serialized = '';
- while ( !feof( $file ) ) {
- $serialized .= fread( $file, 100000 );
- }
- fclose( $file );
- return $this->setCache( unserialize( $serialized ), $code );
- } else {
- fclose( $file );
- return false; // Wrong hash
+ // Check to see if the file has the hash specified
+ $localHash = fread( $file, 32 );
+ if ( $hash === $localHash ) {
+ // All good, get the rest of it
+ $serialized = '';
+ while ( !feof( $file ) ) {
+ $serialized .= fread( $file, 100000 );
}
+ fclose( $file );
+ return unserialize( $serialized );
} else {
- $localHash = substr( fread( $file, 40 ), 8 );
fclose( $file );
- if ( $hash != $localHash ) {
- return false; // Wrong hash
- }
-
- # Require overwrites the member variable or just shadows it?
- require( $filename );
- return $this->setCache( $this->mCache, $code );
+ return false; // Wrong hash
}
}
@@ -212,55 +211,6 @@ class MessageCache {
wfRestoreWarnings();
}
- function saveToScript( $array, $hash, $code ) {
- global $wgCacheDirectory;
-
- $filename = "$wgCacheDirectory/messages-" . wfWikiID() . "-$code";
- $tempFilename = $filename . '.tmp';
- wfMkdirParents( $wgCacheDirectory, null, __METHOD__ ); // might fail
-
- wfSuppressWarnings();
- $file = fopen( $tempFilename, 'w' );
- wfRestoreWarnings();
-
- if ( !$file ) {
- wfDebug( "Unable to open local cache file for writing\n" );
- return;
- }
-
- fwrite( $file, "<?php\n//$hash\n\n \$this->mCache = array(" );
-
- foreach ( $array as $key => $message ) {
- $key = $this->escapeForScript( $key );
- $message = $this->escapeForScript( $message );
- fwrite( $file, "'$key' => '$message',\n" );
- }
-
- fwrite( $file, ");\n?>" );
- fclose( $file);
- rename( $tempFilename, $filename );
- }
-
- function escapeForScript( $string ) {
- $string = str_replace( '\\', '\\\\', $string );
- $string = str_replace( '\'', '\\\'', $string );
- return $string;
- }
-
- /**
- * Set the cache to $cache, if it is valid. Otherwise set the cache to false.
- *
- * @return bool
- */
- function setCache( $cache, $code ) {
- if ( isset( $cache['VERSION'] ) && $cache['VERSION'] == MSG_CACHE_VERSION ) {
- $this->mCache[$code] = $cache;
- return true;
- } else {
- return false;
- }
- }
-
/**
* Loads messages from caches or from database in this order:
* (1) local message cache (if $wgUseLocalMessageCache is enabled)
@@ -277,13 +227,14 @@ class MessageCache {
* or false if populating empty cache fails. Also returns true if MessageCache
* is disabled.
*
- * @param $code String: language to which load messages
+ * @param bool|String $code Language to which load messages
+ * @throws MWException
* @return bool
*/
function load( $code = false ) {
global $wgUseLocalMessageCache;
- if( !is_string( $code ) ) {
+ if ( !is_string( $code ) ) {
# This isn't really nice, so at least make a note about it and try to
# fall back
wfDebug( __METHOD__ . " called without providing a language code\n" );
@@ -308,77 +259,161 @@ class MessageCache {
# Loading code starts
wfProfileIn( __METHOD__ );
$success = false; # Keep track of success
+ $staleCache = false; # a cache array with expired data, or false if none has been loaded
$where = array(); # Debug info, delayed to avoid spamming debug log too much
$cacheKey = wfMemcKey( 'messages', $code ); # Key in memc for messages
- # (1) local cache
+ # Local cache
# Hash of the contents is stored in memcache, to detect if local cache goes
- # out of date (due to update in other thread?)
+ # out of date (e.g. due to replace() on some other server)
if ( $wgUseLocalMessageCache ) {
wfProfileIn( __METHOD__ . '-fromlocal' );
$hash = $this->mMemc->get( wfMemcKey( 'messages', $code, 'hash' ) );
if ( $hash ) {
- $success = $this->loadFromLocal( $hash, $code );
- if ( $success ) $where[] = 'got from local cache';
+ $cache = $this->getLocalCache( $hash, $code );
+ if ( !$cache ) {
+ $where[] = 'local cache is empty or has the wrong hash';
+ } elseif ( $this->isCacheExpired( $cache ) ) {
+ $where[] = 'local cache is expired';
+ $staleCache = $cache;
+ } else {
+ $where[] = 'got from local cache';
+ $success = true;
+ $this->mCache[$code] = $cache;
+ }
}
wfProfileOut( __METHOD__ . '-fromlocal' );
}
- # (2) memcache
- # Fails if nothing in cache, or in the wrong version.
if ( !$success ) {
- wfProfileIn( __METHOD__ . '-fromcache' );
- $cache = $this->mMemc->get( $cacheKey );
- $success = $this->setCache( $cache, $code );
- if ( $success ) {
- $where[] = 'got from global cache';
- $this->saveToCaches( $cache, false, $code );
- }
- wfProfileOut( __METHOD__ . '-fromcache' );
- }
+ # Try the global cache. If it is empty, try to acquire a lock. If
+ # the lock can't be acquired, wait for the other thread to finish
+ # and then try the global cache a second time.
+ for ( $failedAttempts = 0; $failedAttempts < 2; $failedAttempts++ ) {
+ wfProfileIn( __METHOD__ . '-fromcache' );
+ $cache = $this->mMemc->get( $cacheKey );
+ if ( !$cache ) {
+ $where[] = 'global cache is empty';
+ } elseif ( $this->isCacheExpired( $cache ) ) {
+ $where[] = 'global cache is expired';
+ $staleCache = $cache;
+ } else {
+ $where[] = 'got from global cache';
+ $this->mCache[$code] = $cache;
+ $this->saveToCaches( $cache, 'local-only', $code );
+ $success = true;
+ }
+
+ wfProfileOut( __METHOD__ . '-fromcache' );
- # (3)
- # Nothing in caches... so we need create one and store it in caches
- if ( !$success ) {
- $where[] = 'cache is empty';
- $where[] = 'loading from database';
-
- $this->lock( $cacheKey );
-
- # Limit the concurrency of loadFromDB to a single process
- # This prevents the site from going down when the cache expires
- $statusKey = wfMemcKey( 'messages', $code, 'status' );
- $success = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
- if ( $success ) {
- $cache = $this->loadFromDB( $code );
- $success = $this->setCache( $cache, $code );
- }
- if ( $success ) {
- $success = $this->saveToCaches( $cache, true, $code );
if ( $success ) {
- $this->mMemc->delete( $statusKey );
+ # Done, no need to retry
+ break;
+ }
+
+ # We need to call loadFromDB. Limit the concurrency to a single
+ # process. This prevents the site from going down when the cache
+ # expires.
+ $statusKey = wfMemcKey( 'messages', $code, 'status' );
+ $acquired = $this->mMemc->add( $statusKey, 'loading', MSG_LOAD_TIMEOUT );
+ if ( $acquired ) {
+ # Unlock the status key if there is an exception
+ $that = $this;
+ $statusUnlocker = new ScopedCallback( function () use ( $that, $statusKey ) {
+ $that->mMemc->delete( $statusKey );
+ } );
+
+ # Now let's regenerate
+ $where[] = 'loading from database';
+
+ # Lock the cache to prevent conflicting writes
+ # If this lock fails, it doesn't really matter, it just means the
+ # write is potentially non-atomic, e.g. the results of a replace()
+ # may be discarded.
+ if ( $this->lock( $cacheKey ) ) {
+ $mainUnlocker = new ScopedCallback( function () use ( $that, $cacheKey ) {
+ $that->unlock( $cacheKey );
+ } );
+ } else {
+ $mainUnlocker = null;
+ $where[] = 'could not acquire main lock';
+ }
+
+ $cache = $this->loadFromDB( $code );
+ $this->mCache[$code] = $cache;
+ $success = true;
+ $saveSuccess = $this->saveToCaches( $cache, 'all', $code );
+
+ # Unlock
+ ScopedCallback::consume( $mainUnlocker );
+ ScopedCallback::consume( $statusUnlocker );
+
+ if ( !$saveSuccess ) {
+ # Cache save has failed.
+ # There are two main scenarios where this could be a problem:
+ #
+ # - The cache is more than the maximum size (typically
+ # 1MB compressed).
+ #
+ # - Memcached has no space remaining in the relevant slab
+ # class. This is unlikely with recent versions of
+ # memcached.
+ #
+ # Either way, if there is a local cache, nothing bad will
+ # happen. If there is no local cache, disabling the message
+ # cache for all requests avoids incurring a loadFromDB()
+ # overhead on every request, and thus saves the wiki from
+ # complete downtime under moderate traffic conditions.
+ if ( !$wgUseLocalMessageCache ) {
+ $this->mMemc->set( $statusKey, 'error', 60 * 5 );
+ $where[] = 'could not save cache, disabled globally for 5 minutes';
+ } else {
+ $where[] = "could not save global cache";
+ }
+ }
+
+ # Load from DB complete, no need to retry
+ break;
+ } elseif ( $staleCache ) {
+ # Use the stale cache while some other thread constructs the new one
+ $where[] = 'using stale cache';
+ $this->mCache[$code] = $staleCache;
+ $success = true;
+ break;
+ } elseif ( $failedAttempts > 0 ) {
+ # Already retried once, still failed, so don't do another lock/unlock cycle
+ # This case will typically be hit if memcached is down, or if
+ # loadFromDB() takes longer than MSG_WAIT_TIMEOUT
+ $where[] = "could not acquire status key.";
+ break;
} else {
- $this->mMemc->set( $statusKey, 'error', 60 * 5 );
- wfDebug( "MemCached set error in MessageCache: restart memcached server!\n" );
+ $status = $this->mMemc->get( $statusKey );
+ if ( $status === 'error' ) {
+ # Disable cache
+ break;
+ } else {
+ # Wait for the other thread to finish, then retry
+ $where[] = 'waited for other thread to complete';
+ $this->lock( $cacheKey );
+ $this->unlock( $cacheKey );
+ }
}
}
- $this->unlock($cacheKey);
}
if ( !$success ) {
- # Bad luck... this should not happen
$where[] = 'loading FAILED - cache is disabled';
- $info = implode( ', ', $where );
- wfDebug( __METHOD__ . ": Loading $code... $info\n" );
$this->mDisable = true;
$this->mCache = false;
+ # This used to throw an exception, but that led to nasty side effects like
+ # the whole wiki being instantly down if the memcached server died
} else {
# All good, just record the success
- $info = implode( ', ', $where );
- wfDebug( __METHOD__ . ": Loading $code... $info\n" );
$this->mLoadedLanguages[$code] = true;
}
+ $info = implode( ', ', $where );
+ wfDebug( __METHOD__ . ": Loading $code... $info\n" );
wfProfileOut( __METHOD__ );
return $success;
}
@@ -388,8 +423,8 @@ class MessageCache {
* $wgMaxMsgCacheEntrySize are assigned a special value, and are loaded
* on-demand from the database later.
*
- * @param $code String: language code.
- * @return Array: loaded messages for storing in caches.
+ * @param string $code Language code.
+ * @return array Loaded messages for storing in caches.
*/
function loadFromDB( $code ) {
wfProfileIn( __METHOD__ );
@@ -404,19 +439,20 @@ class MessageCache {
);
$mostused = array();
- if ( $wgAdaptiveMessageCache ) {
- $mostused = $this->getMostUsedMessages();
- if ( $code !== $wgLanguageCode ) {
- foreach ( $mostused as $key => $value ) {
- $mostused[$key] = "$value/$code";
- }
+ if ( $wgAdaptiveMessageCache && $code !== $wgLanguageCode ) {
+ if ( !isset( $this->mCache[$wgLanguageCode] ) ) {
+ $this->load( $wgLanguageCode );
+ }
+ $mostused = array_keys( $this->mCache[$wgLanguageCode] );
+ foreach ( $mostused as $key => $value ) {
+ $mostused[$key] = "$value/$code";
}
}
if ( count( $mostused ) ) {
$conds['page_title'] = $mostused;
} elseif ( $code !== $wgLanguageCode ) {
- $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), "/$code" );
+ $conds[] = 'page_title' . $dbr->buildLike( $dbr->anyString(), '/', $code );
} else {
# Effectively disallows use of '/' character in NS_MEDIAWIKI for uses
# other than language code.
@@ -448,24 +484,23 @@ class MessageCache {
foreach ( $res as $row ) {
$text = Revision::getRevisionText( $row );
- if( $text === false ) {
+ if ( $text === false ) {
// Failed to fetch data; possible ES errors?
// Store a marker to fetch on-demand as a workaround...
$entry = '!TOO BIG';
- wfDebugLog( 'MessageCache', __METHOD__ . ": failed to load message page text for {$row->page_title} ($code)" );
+ wfDebugLog(
+ 'MessageCache',
+ __METHOD__
+ . ": failed to load message page text for {$row->page_title} ($code)"
+ );
} else {
$entry = ' ' . $text;
}
$cache[$row->page_title] = $entry;
}
- foreach ( $mostused as $key ) {
- if ( !isset( $cache[$key] ) ) {
- $cache[$key] = '!NONEXISTENT';
- }
- }
-
$cache['VERSION'] = MSG_CACHE_VERSION;
+ $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry );
wfProfileOut( __METHOD__ );
return $cache;
}
@@ -473,8 +508,8 @@ class MessageCache {
/**
* Updates cache as necessary when message page is changed
*
- * @param $title String: name of the page changed.
- * @param $text Mixed: new contents of the page.
+ * @param string $title Name of the page changed.
+ * @param mixed $text New contents of the page.
*/
public function replace( $title, $text ) {
global $wgMaxMsgCacheEntrySize;
@@ -507,12 +542,12 @@ class MessageCache {
}
# Update caches
- $this->saveToCaches( $this->mCache[$code], true, $code );
+ $this->saveToCaches( $this->mCache[$code], 'all', $code );
$this->unlock( $cacheKey );
// Also delete cached sidebar... just in case it is affected
$codes = array( $code );
- if ( $code === 'en' ) {
+ if ( $code === 'en' ) {
// Delete all sidebars, like for example on action=purge on the
// sidebar messages
$codes = array_keys( Language::fetchLanguageNames() );
@@ -534,21 +569,41 @@ class MessageCache {
}
/**
+ * Is the given cache array expired due to time passing or a version change?
+ *
+ * @param $cache
+ * @return bool
+ */
+ protected function isCacheExpired( $cache ) {
+ if ( !isset( $cache['VERSION'] ) || !isset( $cache['EXPIRY'] ) ) {
+ return true;
+ }
+ if ( $cache['VERSION'] != MSG_CACHE_VERSION ) {
+ return true;
+ }
+ if ( wfTimestampNow() >= $cache['EXPIRY'] ) {
+ return true;
+ }
+ return false;
+ }
+
+ /**
* Shortcut to update caches.
*
- * @param $cache Array: cached messages with a version.
- * @param $memc Bool: Wether to update or not memcache.
- * @param $code String: Language code.
- * @return bool on somekind of error.
+ * @param array $cache Cached messages with a version.
+ * @param string $dest Either "local-only" to save to local caches only
+ * or "all" to save to all caches.
+ * @param string|bool $code Language code (default: false)
+ * @return bool
*/
- protected function saveToCaches( $cache, $memc = true, $code = false ) {
+ protected function saveToCaches( $cache, $dest, $code = false ) {
wfProfileIn( __METHOD__ );
- global $wgUseLocalMessageCache, $wgLocalMessageCacheSerialized;
+ global $wgUseLocalMessageCache;
$cacheKey = wfMemcKey( 'messages', $code );
- if ( $memc ) {
- $success = $this->mMemc->set( $cacheKey, $cache, $this->mExpiry );
+ if ( $dest === 'all' ) {
+ $success = $this->mMemc->set( $cacheKey, $cache );
} else {
$success = true;
}
@@ -557,12 +612,8 @@ class MessageCache {
if ( $wgUseLocalMessageCache ) {
$serialized = serialize( $cache );
$hash = md5( $serialized );
- $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash, $this->mExpiry );
- if ($wgLocalMessageCacheSerialized) {
- $this->saveToLocal( $serialized, $hash, $code );
- } else {
- $this->saveToScript( $cache, $hash, $code );
- }
+ $this->mMemc->set( wfMemcKey( 'messages', $code, 'hash' ), $hash );
+ $this->saveToLocal( $serialized, $hash, $code );
}
wfProfileOut( __METHOD__ );
@@ -570,19 +621,35 @@ class MessageCache {
}
/**
- * Represents a write lock on the messages key
+ * Represents a write lock on the messages key.
*
- * @param $key string
+ * Will retry MessageCache::MSG_WAIT_TIMEOUT times, each operations having
+ * a timeout of MessageCache::MSG_LOCK_TIMEOUT.
*
+ * @param string $key
* @return Boolean: success
*/
function lock( $key ) {
$lockKey = $key . ':lock';
- for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT ); $i++ ) {
+ $acquired = false;
+ $testDone = false;
+ for ( $i = 0; $i < MSG_WAIT_TIMEOUT && !$acquired; $i++ ) {
+ $acquired = $this->mMemc->add( $lockKey, 1, MSG_LOCK_TIMEOUT );
+ if ( $acquired ) {
+ break;
+ }
+
+ # Fail fast if memcached is totally down
+ if ( !$testDone ) {
+ $testDone = true;
+ if ( !$this->mMemc->set( wfMemcKey( 'test' ), 'test', 1 ) ) {
+ break;
+ }
+ }
sleep( 1 );
}
- return $i >= MSG_WAIT_TIMEOUT;
+ return $acquired;
}
function unlock( $key ) {
@@ -593,50 +660,62 @@ class MessageCache {
/**
* Get a message from either the content language or the user language.
*
- * @param $key String: the message cache key
- * @param $useDB Boolean: get the message from the DB, false to use only
- * the localisation
- * @param $langcode String: code of the language to get the message for, if
- * it is a valid code create a language for that language,
- * if it is a string but not a valid code then make a basic
- * language object, if it is a false boolean then use the
- * current users language (as a fallback for the old
- * parameter functionality), or if it is a true boolean
- * then use the wikis content language (also as a
- * fallback).
- * @param $isFullKey Boolean: specifies whether $key is a two part key
+ * First, assemble a list of languages to attempt getting the message from. This
+ * chain begins with the requested language and its fallbacks and then continues with
+ * the content language and its fallbacks. For each language in the chain, the following
+ * process will occur (in this order):
+ * 1. If a language-specific override, i.e., [[MW:msg/lang]], is available, use that.
+ * Note: for the content language, there is no /lang subpage.
+ * 2. Fetch from the static CDB cache.
+ * 3. If available, check the database for fallback language overrides.
+ *
+ * This process provides a number of guarantees. When changing this code, make sure all
+ * of these guarantees are preserved.
+ * * If the requested language is *not* the content language, then the CDB cache for that
+ * specific language will take precedence over the root database page ([[MW:msg]]).
+ * * Fallbacks will be just that: fallbacks. A fallback language will never be reached if
+ * the message is available *anywhere* in the language for which it is a fallback.
+ *
+ * @param string $key the message key
+ * @param bool $useDB If true, look for the message in the DB, false
+ * to use only the compiled l10n cache.
+ * @param bool|string|object $langcode Code of the language to get the message for.
+ * - If string and a valid code, will create a standard language object
+ * - If string but not a valid code, will create a basic language object
+ * - If boolean and false, create object from the current users language
+ * - If boolean and true, create object from the wikis content language
+ * - If language object, use it as given
+ * @param bool $isFullKey specifies whether $key is a two part key
* "msg/lang".
*
- * @return string|bool
+ * @throws MWException when given an invalid key
+ * @return string|bool False if the message doesn't exist, otherwise the message (which can be empty)
*/
function get( $key, $useDB = true, $langcode = true, $isFullKey = false ) {
- global $wgLanguageCode, $wgContLang;
+ global $wgContLang;
- if ( is_int( $key ) ) {
- // "Non-string key given" exception sometimes happens for numerical strings that become ints somewhere on their way here
- $key = strval( $key );
- }
+ $section = new ProfileSection( __METHOD__ );
- if ( !is_string( $key ) ) {
+ if ( is_int( $key ) ) {
+ // Fix numerical strings that somehow become ints
+ // on their way here
+ $key = (string)$key;
+ } elseif ( !is_string( $key ) ) {
throw new MWException( 'Non-string key given' );
- }
-
- if ( strval( $key ) === '' ) {
- # Shortcut: the empty key is always missing
+ } elseif ( $key === '' ) {
+ // Shortcut: the empty key is always missing
return false;
}
- $lang = wfGetLangObj( $langcode );
- if ( !$lang ) {
- throw new MWException( "Bad lang code $langcode given" );
+ // For full keys, get the language code from the key
+ $pos = strrpos( $key, '/' );
+ if ( $isFullKey && $pos !== false ) {
+ $langcode = substr( $key, $pos + 1 );
+ $key = substr( $key, 0, $pos );
}
- $langcode = $lang->getCode();
-
- $message = false;
-
- # Normalise title-case input (with some inlining)
- $lckey = str_replace( ' ', '_', $key );
+ // Normalise title-case input (with some inlining)
+ $lckey = strtr( $key, ' ', '_' );
if ( ord( $key ) < 128 ) {
$lckey[0] = strtolower( $lckey[0] );
$uckey = ucfirst( $lckey );
@@ -645,89 +724,160 @@ class MessageCache {
$uckey = $wgContLang->ucfirst( $lckey );
}
- /**
- * Record each message request, but only once per request.
- * This information is not used unless $wgAdaptiveMessageCache
- * is enabled.
- */
- $this->mRequestedMessages[$uckey] = true;
+ // Loop through each language in the fallback list until we find something useful
+ $lang = wfGetLangObj( $langcode );
+ $message = $this->getMessageFromFallbackChain( $lang, $lckey, $uckey, !$this->mDisable && $useDB );
- # Try the MediaWiki namespace
- if( !$this->mDisable && $useDB ) {
- $title = $uckey;
- if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
- $title .= '/' . $langcode;
+ // If we still have no message, maybe the key was in fact a full key so try that
+ if ( $message === false ) {
+ $parts = explode( '/', $lckey );
+ // We may get calls for things that are http-urls from sidebar
+ // Let's not load nonexistent languages for those
+ // They usually have more than one slash.
+ if ( count( $parts ) == 2 && $parts[1] !== '' ) {
+ $message = Language::getMessageFor( $parts[0], $parts[1] );
+ if ( $message === null ) {
+ $message = false;
+ }
}
- $message = $this->getMsgFromNamespace( $title, $langcode );
}
- # Try the array in the language object
- if ( $message === false ) {
- $message = $lang->getMessage( $lckey );
- if ( is_null( $message ) ) {
- $message = false;
+ // Post-processing if the message exists
+ if ( $message !== false ) {
+ // Fix whitespace
+ $message = str_replace(
+ array(
+ # Fix for trailing whitespace, removed by textarea
+ '&#32;',
+ # Fix for NBSP, converted to space by firefox
+ '&nbsp;',
+ '&#160;',
+ ),
+ array(
+ ' ',
+ "\xc2\xa0",
+ "\xc2\xa0"
+ ),
+ $message
+ );
+ }
+
+ return $message;
+ }
+
+ /**
+ * Given a language, try and fetch a message from that language, then the
+ * fallbacks of that language, then the site language, then the fallbacks for the
+ * site language.
+ *
+ * @param Language $lang Requested language
+ * @param string $lckey Lowercase key for the message
+ * @param string $uckey Uppercase key for the message
+ * @param bool $useDB Whether to use the database
+ *
+ * @see MessageCache::get
+ * @return string|bool The message, or false if not found
+ */
+ protected function getMessageFromFallbackChain( $lang, $lckey, $uckey, $useDB ) {
+ global $wgLanguageCode, $wgContLang;
+
+ $langcode = $lang->getCode();
+ $message = false;
+
+ // First try the requested language.
+ if ( $useDB ) {
+ if ( $langcode === $wgLanguageCode ) {
+ // Messages created in the content language will not have the /lang extension
+ $message = $this->getMsgFromNamespace( $uckey, $langcode );
+ } else {
+ $message = $this->getMsgFromNamespace( "$uckey/$langcode", $langcode );
}
}
- # Try the array of another language
- if( $message === false ) {
- $parts = explode( '/', $lckey );
- # We may get calls for things that are http-urls from sidebar
- # Let's not load nonexistent languages for those
- # They usually have more than one slash.
- if ( count( $parts ) == 2 && $parts[1] !== '' ) {
- $message = Language::getMessageFor( $parts[0], $parts[1] );
- if ( is_null( $message ) ) {
- $message = false;
+ if ( $message !== false ) {
+ return $message;
+ }
+
+ // Check the CDB cache
+ $message = $lang->getMessage( $lckey );
+ if ( $message !== null ) {
+ return $message;
+ }
+
+ list( $fallbackChain, $siteFallbackChain ) = Language::getFallbacksIncludingSiteLanguage( $langcode );
+
+ // Next try checking the database for all of the fallback languages of the requested language.
+ if ( $useDB ) {
+ foreach ( $fallbackChain as $code ) {
+ if ( $code === $wgLanguageCode ) {
+ // Messages created in the content language will not have the /lang extension
+ $message = $this->getMsgFromNamespace( $uckey, $code );
+ } else {
+ $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
+ }
+
+ if ( $message !== false ) {
+ // Found the message.
+ return $message;
}
}
}
- # Is this a custom message? Try the default language in the db...
- if( ( $message === false || $message === '-' ) &&
- !$this->mDisable && $useDB &&
- !$isFullKey && ( $langcode != $wgLanguageCode ) ) {
+ // Now try checking the site language.
+ if ( $useDB ) {
$message = $this->getMsgFromNamespace( $uckey, $wgLanguageCode );
+ if ( $message !== false ) {
+ return $message;
+ }
}
- # Final fallback
- if( $message === false ) {
- return false;
+ $message = $wgContLang->getMessage( $lckey );
+ if ( $message !== null ) {
+ return $message;
}
- # Fix whitespace
- $message = strtr( $message,
- array(
- # Fix for trailing whitespace, removed by textarea
- '&#32;' => ' ',
- # Fix for NBSP, converted to space by firefox
- '&nbsp;' => "\xc2\xa0",
- '&#160;' => "\xc2\xa0",
- ) );
+ // Finally try the DB for the site language's fallbacks.
+ if ( $useDB ) {
+ foreach ( $siteFallbackChain as $code ) {
+ $message = $this->getMsgFromNamespace( "$uckey/$code", $code );
+ if ( $message === false && $code === $wgLanguageCode ) {
+ // Messages created in the content language will not have the /lang extension
+ $message = $this->getMsgFromNamespace( $uckey, $code );
+ }
- return $message;
+ if ( $message !== false ) {
+ // Found the message.
+ return $message;
+ }
+ }
+ }
+
+ return false;
}
/**
* Get a message from the MediaWiki namespace, with caching. The key must
* first be converted to two-part lang/msg form if necessary.
*
- * @param $title String: Message cache key with initial uppercase letter.
- * @param $code String: code denoting the language to try.
+ * Unlike self::get(), this function doesn't resolve fallback chains, and
+ * some callers require this behavior. LanguageConverter::parseCachedTable()
+ * and self::get() are some examples in core.
*
- * @return string|bool False on failure
+ * @param string $title Message cache key with initial uppercase letter.
+ * @param string $code Code denoting the language to try.
+ * @return string|bool The message, or false if it does not exist or on error
*/
function getMsgFromNamespace( $title, $code ) {
- global $wgAdaptiveMessageCache;
-
$this->load( $code );
if ( isset( $this->mCache[$code][$title] ) ) {
$entry = $this->mCache[$code][$title];
if ( substr( $entry, 0, 1 ) === ' ' ) {
- return substr( $entry, 1 );
+ // The message exists, so make sure a string
+ // is returned.
+ return (string)substr( $entry, 1 );
} elseif ( $entry === '!NONEXISTENT' ) {
return false;
- } elseif( $entry === '!TOO BIG' ) {
+ } elseif ( $entry === '!TOO BIG' ) {
// Fall through and try invididual message cache below
}
} else {
@@ -738,15 +888,7 @@ class MessageCache {
return $message;
}
- /**
- * If message cache is in normal mode, it is guaranteed
- * (except bugs) that there is always entry (or placeholder)
- * in the cache if message exists. Thus we can do minor
- * performance improvement and return false early.
- */
- if ( !$wgAdaptiveMessageCache ) {
- return false;
- }
+ return false;
}
# Try the individual message cache
@@ -755,7 +897,9 @@ class MessageCache {
if ( $entry ) {
if ( substr( $entry, 0, 1 ) === ' ' ) {
$this->mCache[$code][$title] = $entry;
- return substr( $entry, 1 );
+ // The message exists, so make sure a string
+ // is returned.
+ return (string)substr( $entry, 1 );
} elseif ( $entry === '!NONEXISTENT' ) {
$this->mCache[$code][$title] = '!NONEXISTENT';
return false;
@@ -770,16 +914,39 @@ class MessageCache {
Title::makeTitle( NS_MEDIAWIKI, $title ), false, Revision::READ_LATEST
);
if ( $revision ) {
- $message = $revision->getText();
- if ($message === false) {
+ $content = $revision->getContent();
+ if ( !$content ) {
// A possibly temporary loading failure.
- wfDebugLog( 'MessageCache', __METHOD__ . ": failed to load message page text for {$title} ($code)" );
+ wfDebugLog(
+ 'MessageCache',
+ __METHOD__ . ": failed to load message page text for {$title} ($code)"
+ );
+ $message = null; // no negative caching
} else {
- $this->mCache[$code][$title] = ' ' . $message;
- $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+ // XXX: Is this the right way to turn a Content object into a message?
+ // NOTE: $content is typically either WikitextContent, JavaScriptContent or
+ // CssContent. MessageContent is *not* used for storing messages, it's
+ // only used for wrapping them when needed.
+ $message = $content->getWikitextForTransclusion();
+
+ if ( $message === false || $message === null ) {
+ wfDebugLog(
+ 'MessageCache',
+ __METHOD__ . ": message content doesn't provide wikitext "
+ . "(content model: " . $content->getContentHandler() . ")"
+ );
+
+ $message = false; // negative caching
+ } else {
+ $this->mCache[$code][$title] = ' ' . $message;
+ $this->mMemc->set( $titleKey, ' ' . $message, $this->mExpiry );
+ }
}
} else {
- $message = false;
+ $message = false; // negative caching
+ }
+
+ if ( $message === false ) { // negative caching
$this->mCache[$code][$title] = '!NONEXISTENT';
$this->mMemc->set( $titleKey, '!NONEXISTENT', $this->mExpiry );
}
@@ -788,15 +955,15 @@ class MessageCache {
}
/**
- * @param $message string
- * @param $interface bool
- * @param $language
- * @param $title Title
+ * @param string $message
+ * @param bool $interface
+ * @param string $language Language code
+ * @param Title $title
* @return string
*/
function transform( $message, $interface = false, $language = null, $title = null ) {
// Avoid creating parser if nothing to transform
- if( strpos( $message, '{{' ) === false ) {
+ if ( strpos( $message, '{{' ) === false ) {
return $message;
}
@@ -840,14 +1007,16 @@ class MessageCache {
}
/**
- * @param $text string
- * @param $title Title
- * @param $linestart bool
- * @param $interface bool
- * @param $language
- * @return ParserOutput
+ * @param string $text
+ * @param Title $title
+ * @param bool $linestart Whether or not this is at the start of a line
+ * @param bool $interface Whether this is an interface message
+ * @param string $language Language code
+ * @return ParserOutput|string
*/
- public function parse( $text, $title = null, $linestart = true, $interface = false, $language = null ) {
+ public function parse( $text, $title = null, $linestart = true,
+ $interface = false, $language = null
+ ) {
if ( $this->mInParser ) {
return htmlspecialchars( $text );
}
@@ -890,7 +1059,7 @@ class MessageCache {
*/
function clear() {
$langs = Language::fetchLanguageNames( null, 'mw' );
- foreach ( array_keys($langs) as $code ) {
+ foreach ( array_keys( $langs ) as $code ) {
# Global cache
$this->mMemc->delete( wfMemcKey( 'messages', $code ) );
# Invalidate all local caches
@@ -906,12 +1075,12 @@ class MessageCache {
public function figureMessage( $key ) {
global $wgLanguageCode;
$pieces = explode( '/', $key );
- if( count( $pieces ) < 2 ) {
+ if ( count( $pieces ) < 2 ) {
return array( $key, $wgLanguageCode );
}
$lang = array_pop( $pieces );
- if( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
+ if ( !Language::fetchLanguageName( $lang, null, 'mw' ) ) {
return array( $key, $wgLanguageCode );
}
@@ -919,88 +1088,12 @@ class MessageCache {
return array( $message, $lang );
}
- public static function logMessages() {
- wfProfileIn( __METHOD__ );
- global $wgAdaptiveMessageCache;
- if ( !$wgAdaptiveMessageCache || !self::$instance instanceof MessageCache ) {
- wfProfileOut( __METHOD__ );
- return;
- }
-
- $cachekey = wfMemckey( 'message-profiling' );
- $cache = wfGetCache( CACHE_DB );
- $data = $cache->get( $cachekey );
-
- if ( !$data ) {
- $data = array();
- }
-
- $age = self::$mAdaptiveDataAge;
- $filterDate = substr( wfTimestamp( TS_MW, time() - $age ), 0, 8 );
- foreach ( array_keys( $data ) as $key ) {
- if ( $key < $filterDate ) {
- unset( $data[$key] );
- }
- }
-
- $index = substr( wfTimestampNow(), 0, 8 );
- if ( !isset( $data[$index] ) ) {
- $data[$index] = array();
- }
-
- foreach ( self::$instance->mRequestedMessages as $message => $_ ) {
- if ( !isset( $data[$index][$message] ) ) {
- $data[$index][$message] = 0;
- }
- $data[$index][$message]++;
- }
-
- $cache->set( $cachekey, $data );
- wfProfileOut( __METHOD__ );
- }
-
- /**
- * @return array
- */
- public function getMostUsedMessages() {
- wfProfileIn( __METHOD__ );
- $cachekey = wfMemcKey( 'message-profiling' );
- $cache = wfGetCache( CACHE_DB );
- $data = $cache->get( $cachekey );
- if ( !$data ) {
- wfProfileOut( __METHOD__ );
- return array();
- }
-
- $list = array();
-
- foreach( $data as $messages ) {
- foreach( $messages as $message => $count ) {
- $key = $message;
- if ( !isset( $list[$key] ) ) {
- $list[$key] = 0;
- }
- $list[$key] += $count;
- }
- }
-
- $max = max( $list );
- foreach ( $list as $message => $count ) {
- if ( $count < intval( $max * self::$mAdaptiveInclusionThreshold ) ) {
- unset( $list[$message] );
- }
- }
-
- wfProfileOut( __METHOD__ );
- return array_keys( $list );
- }
-
/**
* Get all message keys stored in the message cache for a given language.
* If $code is the content language code, this will return all message keys
* for which MediaWiki:msgkey exists. If $code is another language code, this
* will ONLY return message keys for which MediaWiki:msgkey/$code exists.
- * @param $code string
+ * @param string $code Language code
* @return array of message keys (strings)
*/
public function getAllMessageKeys( $code ) {
@@ -1010,9 +1103,12 @@ class MessageCache {
// Apparently load() failed
return null;
}
- $cache = $this->mCache[$code]; // Copy the cache
- unset( $cache['VERSION'] ); // Remove the VERSION key
- $cache = array_diff( $cache, array( '!NONEXISTENT' ) ); // Remove any !NONEXISTENT keys
+ // Remove administrative keys
+ $cache = $this->mCache[$code];
+ unset( $cache['VERSION'] );
+ unset( $cache['EXPIRY'] );
+ // Remove any !NONEXISTENT keys
+ $cache = array_diff( $cache, array( '!NONEXISTENT' ) );
// Keys may appear with a capital first letter. lcfirst them.
return array_map( array( $wgContLang, 'lcfirst' ), array_keys( $cache ) );
}
diff --git a/includes/cache/ProcessCacheLRU.php b/includes/cache/ProcessCacheLRU.php
index f215ebd8..76c76f37 100644
--- a/includes/cache/ProcessCacheLRU.php
+++ b/includes/cache/ProcessCacheLRU.php
@@ -28,6 +28,8 @@
class ProcessCacheLRU {
/** @var Array */
protected $cache = array(); // (key => prop => value)
+ /** @var Array */
+ protected $cacheTimes = array(); // (key => prop => UNIX timestamp)
protected $maxCacheKeys; // integer; max entries
@@ -44,7 +46,7 @@ class ProcessCacheLRU {
/**
* Set a property field for a cache entry.
- * This will prune the cache if it gets too large.
+ * This will prune the cache if it gets too large based on LRU.
* If the item is already set, it will be pushed to the top of the cache.
*
* @param $key string
@@ -57,9 +59,12 @@ class ProcessCacheLRU {
$this->ping( $key ); // push to top
} elseif ( count( $this->cache ) >= $this->maxCacheKeys ) {
reset( $this->cache );
- unset( $this->cache[key( $this->cache )] );
+ $evictKey = key( $this->cache );
+ unset( $this->cache[$evictKey] );
+ unset( $this->cacheTimes[$evictKey] );
}
$this->cache[$key][$prop] = $value;
+ $this->cacheTimes[$key][$prop] = time();
}
/**
@@ -67,10 +72,14 @@ class ProcessCacheLRU {
*
* @param $key string
* @param $prop string
+ * @param $maxAge integer Ignore items older than this many seconds (since 1.21)
* @return bool
*/
- public function has( $key, $prop ) {
- return isset( $this->cache[$key][$prop] );
+ public function has( $key, $prop, $maxAge = 0 ) {
+ if ( isset( $this->cache[$key][$prop] ) ) {
+ return ( $maxAge <= 0 || ( time() - $this->cacheTimes[$key][$prop] ) <= $maxAge );
+ }
+ return false;
}
/**
@@ -100,9 +109,11 @@ class ProcessCacheLRU {
public function clear( $keys = null ) {
if ( $keys === null ) {
$this->cache = array();
+ $this->cacheTimes = array();
} else {
foreach ( (array)$keys as $key ) {
unset( $this->cache[$key] );
+ unset( $this->cacheTimes[$key] );
}
}
}
diff --git a/includes/cache/ResourceFileCache.php b/includes/cache/ResourceFileCache.php
index 61f1e8c3..2ad7b853 100644
--- a/includes/cache/ResourceFileCache.php
+++ b/includes/cache/ResourceFileCache.php
@@ -29,7 +29,7 @@
class ResourceFileCache extends FileCacheBase {
protected $mCacheWorthy;
- /* @TODO: configurable? */
+ /* @todo configurable? */
const MISS_THRESHOLD = 360; // 6/min * 60 min
/**
diff --git a/includes/cache/SquidUpdate.php b/includes/cache/SquidUpdate.php
index 423e3884..71afeba9 100644
--- a/includes/cache/SquidUpdate.php
+++ b/includes/cache/SquidUpdate.php
@@ -26,32 +26,42 @@
* @ingroup Cache
*/
class SquidUpdate {
- var $urlArr, $mMaxTitles;
/**
- * @param $urlArr array
- * @param $maxTitles bool|int
+ * Collection of URLs to purge.
+ * @var array
*/
- function __construct( $urlArr = array(), $maxTitles = false ) {
+ protected $urlArr;
+
+ /**
+ * @param array $urlArr Collection of URLs to purge
+ * @param bool|int $maxTitles Maximum number of unique URLs to purge
+ */
+ public function __construct( $urlArr = array(), $maxTitles = false ) {
global $wgMaxSquidPurgeTitles;
if ( $maxTitles === false ) {
- $this->mMaxTitles = $wgMaxSquidPurgeTitles;
- } else {
- $this->mMaxTitles = $maxTitles;
+ $maxTitles = $wgMaxSquidPurgeTitles;
}
- $urlArr = array_unique( $urlArr ); // Remove duplicates
- if ( count( $urlArr ) > $this->mMaxTitles ) {
- $urlArr = array_slice( $urlArr, 0, $this->mMaxTitles );
+
+ // Remove duplicate URLs from list
+ $urlArr = array_unique( $urlArr );
+ if ( count( $urlArr ) > $maxTitles ) {
+ // Truncate to desired maximum URL count
+ $urlArr = array_slice( $urlArr, 0, $maxTitles );
}
$this->urlArr = $urlArr;
}
/**
- * @param $title Title
+ * Create a SquidUpdate from the given Title object.
*
+ * The resulting SquidUpdate will purge the given Title's URLs as well as
+ * the pages that link to it. Capped at $wgMaxSquidPurgeTitles total URLs.
+ *
+ * @param Title $title
* @return SquidUpdate
*/
- static function newFromLinksTo( &$title ) {
+ public static function newFromLinksTo( Title $title ) {
global $wgMaxSquidPurgeTitles;
wfProfileIn( __METHOD__ );
@@ -61,13 +71,13 @@ class SquidUpdate {
array( 'page_namespace', 'page_title' ),
array(
'pl_namespace' => $title->getNamespace(),
- 'pl_title' => $title->getDBkey(),
+ 'pl_title' => $title->getDBkey(),
'pl_from=page_id' ),
__METHOD__ );
$blurlArr = $title->getSquidURLs();
- if ( $dbr->numRows( $res ) <= $wgMaxSquidPurgeTitles ) {
+ if ( $res->numRows() <= $wgMaxSquidPurgeTitles ) {
foreach ( $res as $BL ) {
- $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title ) ;
+ $tobj = Title::makeTitle( $BL->page_namespace, $BL->page_title );
$blurlArr[] = $tobj->getInternalURL();
}
}
@@ -79,12 +89,11 @@ class SquidUpdate {
/**
* Create a SquidUpdate from an array of Title objects, or a TitleArray object
*
- * @param $titles array
- * @param $urlArr array
- *
+ * @param array $titles
+ * @param array $urlArr
* @return SquidUpdate
*/
- static function newFromTitles( $titles, $urlArr = array() ) {
+ public static function newFromTitles( $titles, $urlArr = array() ) {
global $wgMaxSquidPurgeTitles;
$i = 0;
foreach ( $titles as $title ) {
@@ -97,20 +106,19 @@ class SquidUpdate {
}
/**
- * @param $title Title
- *
+ * @param Title $title
* @return SquidUpdate
*/
- static function newSimplePurge( &$title ) {
+ public static function newSimplePurge( Title $title ) {
$urlArr = $title->getSquidURLs();
return new SquidUpdate( $urlArr );
}
/**
- * Purges the list of URLs passed to the constructor
+ * Purges the list of URLs passed to the constructor.
*/
- function doUpdate() {
- SquidUpdate::purge( $this->urlArr );
+ public function doUpdate() {
+ self::purge( $this->urlArr );
}
/**
@@ -119,25 +127,30 @@ class SquidUpdate {
* (example: $urlArr[] = 'http://my.host/something')
* XXX report broken Squids per mail or log
*
- * @param $urlArr array
- * @return void
+ * @param array $urlArr List of full URLs to purge
*/
- static function purge( $urlArr ) {
- global $wgSquidServers, $wgHTCPMulticastRouting;
+ public static function purge( $urlArr ) {
+ global $wgSquidServers, $wgHTCPRouting;
- if( !$urlArr ) {
+ if ( !$urlArr ) {
return;
}
- if ( $wgHTCPMulticastRouting ) {
- SquidUpdate::HTCPPurge( $urlArr );
+ wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) . "\n" );
+
+ if ( $wgHTCPRouting ) {
+ self::HTCPPurge( $urlArr );
}
wfProfileIn( __METHOD__ );
- $urlArr = array_unique( $urlArr ); // Remove duplicates
- $maxSocketsPerSquid = 8; // socket cap per Squid
- $urlsPerSocket = 400; // 400 seems to be a good tradeoff, opening a socket takes a while
+ // Remove duplicate URLs
+ $urlArr = array_unique( $urlArr );
+ // Maximum number of parallel connections per squid
+ $maxSocketsPerSquid = 8;
+ // Number of requests to send per socket
+ // 400 seems to be a good tradeoff, opening a socket takes a while
+ $urlsPerSocket = 400;
$socketsPerSquid = ceil( count( $urlArr ) / $urlsPerSocket );
if ( $socketsPerSquid > $maxSocketsPerSquid ) {
$socketsPerSquid = $maxSocketsPerSquid;
@@ -160,17 +173,20 @@ class SquidUpdate {
}
/**
+ * Send Hyper Text Caching Protocol (HTCP) CLR requests.
+ *
* @throws MWException
- * @param $urlArr array
+ * @param array $urlArr Collection of URLs to purge
*/
- static function HTCPPurge( $urlArr ) {
- global $wgHTCPMulticastRouting, $wgHTCPMulticastTTL;
+ public static function HTCPPurge( $urlArr ) {
+ global $wgHTCPRouting, $wgHTCPMulticastTTL;
wfProfileIn( __METHOD__ );
- $htcpOpCLR = 4; // HTCP CLR
+ // HTCP CLR operation
+ $htcpOpCLR = 4;
// @todo FIXME: PHP doesn't support these socket constants (include/linux/in.h)
- if( !defined( "IPPROTO_IP" ) ) {
+ if ( !defined( "IPPROTO_IP" ) ) {
define( "IPPROTO_IP", 0 );
define( "IP_MULTICAST_LOOP", 34 );
define( "IP_MULTICAST_TTL", 33 );
@@ -178,55 +194,73 @@ class SquidUpdate {
// pfsockopen doesn't work because we need set_sock_opt
$conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
- if ( $conn ) {
- // Set socket options
- socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
- if ( $wgHTCPMulticastTTL != 1 )
- socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
- $wgHTCPMulticastTTL );
-
- $urlArr = array_unique( $urlArr ); // Remove duplicates
- foreach ( $urlArr as $url ) {
- if( !is_string( $url ) ) {
- throw new MWException( 'Bad purge URL' );
- }
- $url = SquidUpdate::expand( $url );
- $conf = self::getRuleForURL( $url, $wgHTCPMulticastRouting );
- if ( !$conf ) {
- wfDebug( "No HTCP rule configured for URL $url , skipping\n" );
- continue;
- }
- if ( !isset( $conf['host'] ) || !isset( $conf['port'] ) ) {
+ if ( ! $conn ) {
+ $errstr = socket_strerror( socket_last_error() );
+ wfDebugLog( 'squid', __METHOD__ .
+ ": Error opening UDP socket: $errstr\n" );
+ wfProfileOut( __METHOD__ );
+ return;
+ }
+
+ // Set socket options
+ socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_LOOP, 0 );
+ if ( $wgHTCPMulticastTTL != 1 ) {
+ // Set multicast time to live (hop count) option on socket
+ socket_set_option( $conn, IPPROTO_IP, IP_MULTICAST_TTL,
+ $wgHTCPMulticastTTL );
+ }
+
+ // Remove duplicate URLs from collection
+ $urlArr = array_unique( $urlArr );
+ foreach ( $urlArr as $url ) {
+ if ( !is_string( $url ) ) {
+ wfProfileOut( __METHOD__ );
+ throw new MWException( 'Bad purge URL' );
+ }
+ $url = self::expand( $url );
+ $conf = self::getRuleForURL( $url, $wgHTCPRouting );
+ if ( !$conf ) {
+ wfDebugLog( 'squid', __METHOD__ .
+ "No HTCP rule configured for URL {$url} , skipping\n" );
+ continue;
+ }
+
+ if ( isset( $conf['host'] ) && isset( $conf['port'] ) ) {
+ // Normalize single entries
+ $conf = array( $conf );
+ }
+ foreach ( $conf as $subconf ) {
+ if ( !isset( $subconf['host'] ) || !isset( $subconf['port'] ) ) {
+ wfProfileOut( __METHOD__ );
throw new MWException( "Invalid HTCP rule for URL $url\n" );
}
+ }
- // Construct a minimal HTCP request diagram
- // as per RFC 2756
- // Opcode 'CLR', no response desired, no auth
- $htcpTransID = rand();
+ // Construct a minimal HTCP request diagram
+ // as per RFC 2756
+ // Opcode 'CLR', no response desired, no auth
+ $htcpTransID = rand();
- $htcpSpecifier = pack( 'na4na*na8n',
- 4, 'HEAD', strlen( $url ), $url,
- 8, 'HTTP/1.0', 0 );
+ $htcpSpecifier = pack( 'na4na*na8n',
+ 4, 'HEAD', strlen( $url ), $url,
+ 8, 'HTTP/1.0', 0 );
- $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
- $htcpLen = 4 + $htcpDataLen + 2;
+ $htcpDataLen = 8 + 2 + strlen( $htcpSpecifier );
+ $htcpLen = 4 + $htcpDataLen + 2;
- // Note! Squid gets the bit order of the first
- // word wrong, wrt the RFC. Apparently no other
- // implementation exists, so adapt to Squid
- $htcpPacket = pack( 'nxxnCxNxxa*n',
- $htcpLen, $htcpDataLen, $htcpOpCLR,
- $htcpTransID, $htcpSpecifier, 2);
+ // Note! Squid gets the bit order of the first
+ // word wrong, wrt the RFC. Apparently no other
+ // implementation exists, so adapt to Squid
+ $htcpPacket = pack( 'nxxnCxNxxa*n',
+ $htcpLen, $htcpDataLen, $htcpOpCLR,
+ $htcpTransID, $htcpSpecifier, 2 );
- // Send out
- wfDebug( "Purging URL $url via HTCP\n" );
+ wfDebugLog( 'squid', __METHOD__ .
+ "Purging URL $url via HTCP\n" );
+ foreach ( $conf as $subconf ) {
socket_sendto( $conn, $htcpPacket, $htcpLen, 0,
- $conf['host'], $conf['port'] );
+ $subconf['host'], $subconf['port'] );
}
- } else {
- $errstr = socket_strerror( socket_last_error() );
- wfDebug( __METHOD__ . "(): Error opening UDP socket: $errstr\n" );
}
wfProfileOut( __METHOD__ );
}
@@ -242,21 +276,20 @@ class SquidUpdate {
*
* Client functions should not need to call this.
*
- * @param $url string
- *
+ * @param string $url
* @return string
*/
- static function expand( $url ) {
+ public static function expand( $url ) {
return wfExpandUrl( $url, PROTO_INTERNAL );
}
-
+
/**
* Find the HTCP routing rule to use for a given URL.
- * @param $url string URL to match
- * @param $rules array Array of rules, see $wgHTCPMulticastRouting for format and behavior
+ * @param string $url URL to match
+ * @param array $rules Array of rules, see $wgHTCPRouting for format and behavior
* @return mixed Element of $rules that matched, or false if nothing matched
*/
- static function getRuleForURL( $url, $rules ) {
+ private static function getRuleForURL( $url, $rules ) {
foreach ( $rules as $regex => $routing ) {
if ( $regex === '' || preg_match( $regex, $url ) ) {
return $routing;
@@ -264,5 +297,4 @@ class SquidUpdate {
}
return false;
}
-
}
diff --git a/includes/cache/UserCache.php b/includes/cache/UserCache.php
index 6ec23669..6085f586 100644
--- a/includes/cache/UserCache.php
+++ b/includes/cache/UserCache.php
@@ -45,7 +45,7 @@ class UserCache {
* Get a property of a user based on their user ID
*
* @param $userId integer User ID
- * @param $prop string User property
+ * @param string $prop User property
* @return mixed The property or false if the user does not exist
*/
public function getProp( $userId, $prop ) {
@@ -59,10 +59,21 @@ class UserCache {
}
/**
+ * Get the name of a user or return $ip if the user ID is 0
+ *
+ * @param integer $userId
+ * @param string $ip
+ * @since 1.22
+ */
+ public function getUserName( $userId, $ip ) {
+ return $userId > 0 ? $this->getProp( $userId, 'name' ) : $ip;
+ }
+
+ /**
* Preloads user names for given list of users.
- * @param $userIds Array List of user IDs
- * @param $options Array Option flags; include 'userpage' and 'usertalk'
- * @param $caller String: the calling method
+ * @param array $userIds List of user IDs
+ * @param array $options Option flags; include 'userpage' and 'usertalk'
+ * @param string $caller the calling method
*/
public function doQuery( array $userIds, $options = array(), $caller = '' ) {
wfProfileIn( __METHOD__ );
@@ -70,6 +81,8 @@ class UserCache {
$usersToCheck = array();
$usersToQuery = array();
+ $userIds = array_unique( $userIds );
+
foreach ( $userIds as $userId ) {
$userId = (int)$userId;
if ( $userId <= 0 ) {
@@ -124,8 +137,8 @@ class UserCache {
* Check if a cache type is in $options and was not loaded for this user
*
* @param $uid integer user ID
- * @param $type string Cache type
- * @param $options Array Requested cache types
+ * @param string $type Cache type
+ * @param array $options Requested cache types
* @return bool
*/
protected function queryNeeded( $uid, $type, array $options ) {