From 4ac9fa081a7c045f6a9f1cfc529d82423f485b2e Mon Sep 17 00:00:00 2001 From: Pierre Schmitz Date: Sun, 8 Dec 2013 09:55:49 +0100 Subject: Update to MediaWiki 1.22.0 --- includes/cache/BacklinkCache.php | 134 ++++--- includes/cache/CacheDependency.php | 2 +- includes/cache/FileCacheBase.php | 6 +- includes/cache/GenderCache.php | 14 +- includes/cache/HTMLCacheUpdate.php | 11 +- includes/cache/HTMLFileCache.php | 4 +- includes/cache/LinkBatch.php | 4 +- includes/cache/LinkCache.php | 68 +++- includes/cache/LocalisationCache.php | 96 ++++- includes/cache/MessageCache.php | 729 ++++++++++++++++++++++------------- includes/cache/ResourceFileCache.php | 2 +- includes/cache/SquidUpdate.php | 197 ++++++---- includes/cache/UserCache.php | 13 + 13 files changed, 813 insertions(+), 467 deletions(-) (limited to 'includes/cache') diff --git a/includes/cache/BacklinkCache.php b/includes/cache/BacklinkCache.php index a59cc9a2..193f20fe 100644 --- a/includes/cache/BacklinkCache.php +++ b/includes/cache/BacklinkCache.php @@ -20,6 +20,7 @@ * * @file * @author Tim Starling + * @author Aaron Schulz * @copyright © 2009, Tim Starling, Domas Mituzas * @copyright © 2010, Max Sem * @copyright © 2011, Antoine Musso @@ -103,7 +104,7 @@ class BacklinkCache { self::$cache = new ProcessCacheLRU( 1 ); } $dbKey = $title->getPrefixedDBkey(); - if ( !self::$cache->has( $dbKey, 'obj' ) ) { + if ( !self::$cache->has( $dbKey, 'obj', 3600 ) ) { self::$cache->set( $dbKey, 'obj', new self( $title ) ); } return self::$cache->get( $dbKey, 'obj' ); @@ -147,71 +148,73 @@ class BacklinkCache { 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 or false - * @param $endId Integer or false + * @param $startId Integer|false + * @param $endId Integer|false + * @param $max Integer|INF * @return TitleArrayFromResult */ - public function getLinks( $table, $startId = false, $endId = false ) { + 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 ) { - // Partial range, not cached - wfDebug( __METHOD__ . ": from DB (uncacheable range)\n" ); + 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__, - array( - 'STRAIGHT_JOIN', - 'ORDER BY' => $fromField - ) ); - $ta = TitleArray::newFromResult( $res ); - - wfProfileOut( __METHOD__ ); - return $ta; - } + $options + ); - // @todo FIXME: Make this a function? - if ( !isset( $this->fullResultCache[$table] ) ) { - wfDebug( __METHOD__ . ": from DB\n" ); - $res = $this->getDB()->select( - array( $table, 'page' ), - array( 'page_namespace', 'page_title', 'page_id' ), - $this->getConditions( $table ), - __METHOD__, - array( - 'STRAIGHT_JOIN', - 'ORDER BY' => $fromField, - ) ); - $this->fullResultCache[$table] = $res; + 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" ); + } } - $ta = TitleArray::newFromResult( $this->fullResultCache[$table] ); - wfProfileOut( __METHOD__ ); - return $ta; + return $res; } /** @@ -234,7 +237,7 @@ class BacklinkCache { } else { $prefix = null; wfRunHooks( 'BacklinkCacheGetPrefix', array( $table, &$prefix ) ); - if( $prefix ) { + if ( $prefix ) { return $prefix; } else { throw new MWException( "Invalid table \"$table\" in " . __CLASS__ ); @@ -289,7 +292,7 @@ class BacklinkCache { default: $conds = null; wfRunHooks( 'BacklinkCacheGetConditions', array( $table, $this->title, &$conds ) ); - if( !$conds ) { + if ( !$conds ) { throw new MWException( "Invalid table \"$table\" in " . __CLASS__ ); } } @@ -309,7 +312,7 @@ class BacklinkCache { /** * Get the approximate number of backlinks * @param $table String - * @param $max integer Only count up to this many backlinks + * @param $max integer|INF Only count up to this many backlinks * @return integer */ public function getNumLinks( $table, $max = INF ) { @@ -335,20 +338,12 @@ class BacklinkCache { } // 4) fetch from the database ... - if ( is_infinite( $max ) ) { // full count - $count = $this->getLinks( $table )->count(); + $count = $this->getLinks( $table, false, false, $max )->count(); + if ( $count < $max ) { // full count $wgMemc->set( $memcKey, $count, self::CACHE_EXPIRY ); - } else { // with limit - $count = $this->getDB()->select( - array( $table, 'page' ), - '1', - $this->getConditions( $table ), - __METHOD__, - array( 'LIMIT' => $max ) - )->numRows(); } - return $count; + return min( $max, $count ); } /** @@ -395,8 +390,28 @@ class BacklinkCache { } // 4) ... finally fetch from the slow database :( - $this->getLinks( $table ); - $cacheEntry = $this->partitionResult( $this->fullResultCache[$table], $batchSize ); + $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 ); @@ -412,31 +427,32 @@ class BacklinkCache { * 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 @see + * @return array */ - protected function partitionResult( $res, $batchSize ) { + 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 ) { + if ( $i == 0 && $isComplete ) { $start = false; } else { - $rowNum = intval( $numRows * $i / $numBatches ); + $rowNum = $i * $batchSize; $res->seek( $rowNum ); $row = $res->fetchObject(); - $start = $row->page_id; + $start = (int)$row->page_id; } - if ( $i == $numBatches - 1 ) { + if ( $i == ( $numBatches - 1 ) && $isComplete ) { $end = false; } else { - $rowNum = intval( $numRows * ( $i + 1 ) / $numBatches ); + $rowNum = min( $numRows - 1, ( $i + 1 ) * $batchSize - 1 ); $res->seek( $rowNum ); $row = $res->fetchObject(); - $end = $row->page_id - 1; + $end = (int)$row->page_id; } # Sanity check order diff --git a/includes/cache/CacheDependency.php b/includes/cache/CacheDependency.php index 0f047e80..32bcdf7f 100644 --- a/includes/cache/CacheDependency.php +++ b/includes/cache/CacheDependency.php @@ -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 30a72174..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" @@ -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; } diff --git a/includes/cache/GenderCache.php b/includes/cache/GenderCache.php index 63e4226d..a933527a 100644 --- a/includes/cache/GenderCache.php +++ b/includes/cache/GenderCache.php @@ -66,15 +66,14 @@ class GenderCache { 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; } @@ -143,14 +143,14 @@ class GenderCache { $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 88e79281..992809ef 100644 --- a/includes/cache/HTMLCacheUpdate.php +++ b/includes/cache/HTMLCacheUpdate.php @@ -46,8 +46,6 @@ class HTMLCacheUpdate implements DeferrableUpdate { } public function doUpdate() { - global $wgMaxBacklinksInvalidate; - wfProfileIn( __METHOD__ ); $job = new HTMLCacheUpdateJob( @@ -60,13 +58,14 @@ class HTMLCacheUpdate implements DeferrableUpdate { ); $count = $this->mTitle->getBacklinkCache()->getNumLinks( $this->mTable, 200 ); - if ( $wgMaxBacklinksInvalidate !== false && $count > $wgMaxBacklinksInvalidate ) { - wfDebug( "Skipped HTML cache invalidation of {$this->mTitle->getPrefixedText()}." ); - } elseif ( $count >= 200 ) { // many backlinks + if ( $count >= 200 ) { // many backlinks JobQueueGroup::singleton()->push( $job ); JobQueueGroup::singleton()->deduplicateRootJob( $job ); } else { // few backlinks ($count might be off even if 0) - $job->run(); // just do the purge query now + $dbw = wfGetDB( DB_MASTER ); + $dbw->onTransactionIdle( function() use ( $job ) { + $job->run(); // just do the purge query now + } ); } wfProfileOut( __METHOD__ ); diff --git a/includes/cache/HTMLFileCache.php b/includes/cache/HTMLFileCache.php index 055fd685..ab379116 100644 --- a/includes/cache/HTMLFileCache.php +++ b/includes/cache/HTMLFileCache.php @@ -163,7 +163,7 @@ class HTMLFileCache extends FileCacheBase { return $text; } - wfDebug( __METHOD__ . "()\n", false); + wfDebug( __METHOD__ . "()\n", false ); $now = wfTimestampNow(); if ( $this->useGzip() ) { @@ -182,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 72a2e8e5..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; } /** diff --git a/includes/cache/LinkCache.php b/includes/cache/LinkCache.php index 0e41e265..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; } /** @@ -156,8 +182,13 @@ class LinkCache { 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 @@ -167,13 +198,26 @@ class LinkCache { */ public function addLink( $title ) { $nt = Title::newFromDBkey( $title ); - if( $nt ) { + if ( $nt ) { return $this->addLinkObj( $nt ); } else { return 0; } } + /** + * 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 * @@ -201,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 ); @@ -215,7 +263,9 @@ class LinkCache { } $f = array( 'page_id', 'page_len', 'page_is_redirect', 'page_latest' ); - if ( $wgContentHandlerUseDB ) $f[] = 'page_content_model'; + if ( $wgContentHandlerUseDB ) { + $f[] = 'page_content_model'; + } $s = $db->selectRow( 'page', $f, array( 'page_namespace' => $nt->getNamespace(), 'page_title' => $nt->getDBkey() ), diff --git a/includes/cache/LocalisationCache.php b/includes/cache/LocalisationCache.php index 009b9507..1bfd17bd 100644 --- a/includes/cache/LocalisationCache.php +++ b/includes/cache/LocalisationCache.php @@ -110,7 +110,7 @@ class LocalisationCache { 'dateFormats', 'datePreferences', 'datePreferenceMigrationMap', 'defaultDateFormat', 'extraUserToggles', 'specialPageAliases', 'imageFiles', 'preloadedMessages', 'namespaceGenderAliases', - 'digitGroupingPattern', 'pluralRules', 'compiledPluralRules', + 'digitGroupingPattern', 'pluralRules', 'pluralRuleTypes', 'compiledPluralRules', ); /** @@ -160,6 +160,20 @@ class LocalisationCache { */ 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; /** @@ -340,7 +354,6 @@ class LocalisationCache { * @param $code * @param $key * @param $subkey - * @return */ protected function loadSubitem( $code, $key, $subkey ) { if ( !in_array( $key, self::$splitKeys ) ) { @@ -370,6 +383,9 @@ class LocalisationCache { /** * Returns true if the cache identified by $code is missing or expired. + * + * @param string $code + * * @return bool */ public function isExpired( $code ) { @@ -480,9 +496,10 @@ class LocalisationCache { * @return array */ protected function readPHPFile( $_fileName, $_fileType ) { + wfProfileIn( __METHOD__ ); // Disable APC caching $_apcEnabled = ini_set( 'apc.cache_by_default', '0' ); - include( $_fileName ); + include $_fileName; ini_set( 'apc.cache_by_default', $_apcEnabled ); if ( $_fileType == 'core' || $_fileType == 'extension' ) { @@ -490,8 +507,10 @@ class LocalisationCache { } elseif ( $_fileType == 'aliases' ) { $data = compact( 'aliases' ); } else { + wfProfileOut( __METHOD__ ); throw new MWException( __METHOD__ . ": Invalid file type: $_fileType" ); } + wfProfileOut( __METHOD__ ); return $data; } @@ -506,7 +525,7 @@ class LocalisationCache { } try { $compiledRules = CLDRPluralRuleEvaluator::compile( $rules ); - } catch( CLDRPluralRuleError $e ) { + } catch ( CLDRPluralRuleError $e ) { wfDebugLog( 'l10n', $e->getMessage() . "\n" ); return array(); } @@ -519,17 +538,8 @@ class LocalisationCache { * @since 1.20 */ public function getPluralRules( $code ) { - global $IP; - if ( $this->pluralRules === null ) { - $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 ); - } + $this->loadPluralFiles(); } if ( !isset( $this->pluralRules[$code] ) ) { return null; @@ -538,6 +548,37 @@ class LocalisationCache { } } + /** + * 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. @@ -549,12 +590,16 @@ class LocalisationCache { foreach ( $rulesets as $ruleset ) { $codes = $ruleset->getAttribute( 'locales' ); $rules = array(); + $ruleTypes = array(); $ruleElements = $ruleset->getElementsByTagName( "pluralRule" ); foreach ( $ruleElements as $elt ) { + $ruleType = $elt->getAttribute( 'count' ); $rules[] = $elt->nodeValue; + $ruleTypes[] = $ruleType; } foreach ( explode( ' ', $codes ) as $code ) { $this->pluralRules[$code] = $rules; + $this->pluralRuleTypes[$code] = $ruleTypes; } } } @@ -566,9 +611,11 @@ class LocalisationCache { */ protected function readSourceFilesAndRegisterDeps( $code, &$deps ) { global $IP; + wfProfileIn( __METHOD__ ); $fileName = Language::getMessagesFileName( $code ); if ( !file_exists( $fileName ) ) { + wfProfileOut( __METHOD__ ); return false; } @@ -579,10 +626,13 @@ class LocalisationCache { $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; } @@ -673,6 +723,7 @@ class LocalisationCache { wfProfileIn( __METHOD__ ); if ( !$code ) { + wfProfileOut( __METHOD__ ); throw new MWException( "Invalid language code requested" ); } $this->recachedLangs[$code] = true; @@ -741,6 +792,7 @@ class LocalisationCache { # 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' ); @@ -761,6 +813,7 @@ class LocalisationCache { 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' ); @@ -786,6 +839,10 @@ class LocalisationCache { 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(); @@ -796,6 +853,7 @@ class LocalisationCache { 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.' ); } @@ -810,6 +868,7 @@ class LocalisationCache { } # Save to the persistent cache + wfProfileIn( __METHOD__ . '-write' ); $this->store->startWrite( $code ); foreach ( $allData as $key => $value ) { if ( in_array( $key, self::$splitKeys ) ) { @@ -821,6 +880,7 @@ class LocalisationCache { } } $this->store->finishWrite(); + wfProfileOut( __METHOD__ . '-write' ); # Clear out the MessageBlobStore # HACK: If using a null (i.e. disabled) storage backend, we @@ -868,6 +928,7 @@ class LocalisationCache { 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 ) { @@ -921,7 +982,7 @@ interface LCStore { /** * Start a write transaction. - * @param $code Language code + * @param string $code Language code */ function startWrite( $code ); @@ -933,8 +994,8 @@ interface LCStore { /** * Set a key to a given value. startWrite() must be called before this * is called, and finishWrite() must be called afterwards. - * @param $key - * @param $value + * @param string $key + * @param mixed $value */ function set( $key, $value ); } @@ -1284,5 +1345,4 @@ class LocalisationCache_BulkLoad extends LocalisationCache { $this->unload( $code ); } } - } diff --git a/includes/cache/MessageCache.php b/includes/cache/MessageCache.php index c406b5c3..a92c87f4 100644 --- a/includes/cache/MessageCache.php +++ b/includes/cache/MessageCache.php @@ -22,13 +22,28 @@ */ /** - * + * 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 @@ -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,18 +77,21 @@ class MessageCache { */ protected $mParserOptions, $mParser; - /// Variable for tracking which variables are already loaded + /** + * Variable for tracking which variables are already loaded + * @var array $mLoadedLanguages + */ protected $mLoadedLanguages = array(); /** * Singleton instance * - * @var MessageCache + * @var MessageCache $instance */ private static $instance; /** - * @var bool + * @var bool $mInParser */ protected $mInParser = false; @@ -75,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; } @@ -94,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 ); @@ -119,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 string $hash the hash of contents, to check validity. - * @param $code Mixed: Optional language code, see documenation of load(). - * @return bool on failure. + * @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"; @@ -139,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 } } @@ -192,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, "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) @@ -257,16 +227,14 @@ class MessageCache { * or false if populating empty cache fails. Also returns true if MessageCache * is disabled. * - * @param bool|String $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; - $exception = null; // deferred error - - 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" ); @@ -291,94 +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; + } - # (3) - # Nothing in caches... so we need create one and store it in caches - if ( !$success ) { - $where[] = 'cache is empty'; - $where[] = 'loading from database'; - - if ( $this->lock( $cacheKey ) ) { - $that = $this; - $osc = new ScopedCallback( function() use ( $that, $cacheKey ) { - $that->unlock( $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 ) { // acquired lock - $cache = $this->mMemc; - $isc = new ScopedCallback( function() use ( $cache, $statusKey ) { - $cache->delete( $statusKey ); - } ); - $cache = $this->loadFromDB( $code ); - $success = $this->setCache( $cache, $code ); - if ( $success ) { // messages loaded - $success = $this->saveToCaches( $cache, true, $code ); - $isc = null; // unlock - if ( !$success ) { - $this->mMemc->set( $statusKey, 'error', 60 * 5 ); - wfDebug( __METHOD__ . ": set() error: restart memcached server!\n" ); - $exception = new MWException( "Could not save cache for '$code'." ); + wfProfileOut( __METHOD__ . '-fromcache' ); + + if ( $success ) { + # 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 { - $isc = null; // unlock - $exception = new MWException( "Could not load cache from DB for '$code'." ); + $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 ); + } } - } else { - $exception = new MWException( "Could not acquire '$statusKey' lock." ); } - $osc = null; // unlock } if ( !$success ) { + $where[] = 'loading FAILED - cache is disabled'; $this->mDisable = true; $this->mCache = false; - // This used to go on, but that led to lots of nasty side - // effects like gadgets and sidebar getting cached with their - // default content - if ( $exception instanceof Exception ) { - throw $exception; - } else { - throw new MWException( "MessageCache failed to load messages" ); - } + # 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 string $code 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__ ); @@ -449,11 +484,15 @@ 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; } @@ -461,6 +500,7 @@ class MessageCache { } $cache['VERSION'] = MSG_CACHE_VERSION; + $cache['EXPIRY'] = wfTimestamp( TS_MW, time() + $this->mExpiry ); wfProfileOut( __METHOD__ ); return $cache; } @@ -468,8 +508,8 @@ class MessageCache { /** * Updates cache as necessary when message page is changed * - * @param string $title 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; @@ -502,7 +542,7 @@ 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 @@ -528,22 +568,42 @@ class MessageCache { wfProfileOut( __METHOD__ ); } + /** + * 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 array $cache cached messages with a version. - * @param bool $memc Wether to update or not memcache. - * @param string $code 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; } @@ -552,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__ ); @@ -565,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 ) { @@ -588,51 +660,62 @@ class MessageCache { /** * Get a message from either the content language or the user language. * - * @param string $key the message cache key - * @param $useDB Boolean: get the message from the DB, false to use only - * the localisation - * @param bool|string $langcode 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". * - * @throws MWException - * @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 ); @@ -641,80 +724,160 @@ class MessageCache { $uckey = $wgContLang->ucfirst( $lckey ); } - # Try the MediaWiki namespace - if( !$this->mDisable && $useDB ) { - $title = $uckey; - if( !$isFullKey && ( $langcode != $wgLanguageCode ) ) { - $title .= '/' . $langcode; - } - $message = $this->getMsgFromNamespace( $title, $langcode ); - } + // 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 array in the language object + // If we still have no message, maybe the key was in fact a full key so try that if ( $message === false ) { - $message = $lang->getMessage( $lckey ); - if ( is_null( $message ) ) { - $message = false; - } - } - - # 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. + // 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 ) ) { + if ( $message === null ) { $message = false; } } } - # Is this a custom message? Try the default language in the db... - if( ( $message === false || $message === '-' ) && - !$this->mDisable && $useDB && - !$isFullKey && ( $langcode != $wgLanguageCode ) ) { + // Post-processing if the message exists + if ( $message !== false ) { + // Fix whitespace + $message = str_replace( + array( + # Fix for trailing whitespace, removed by textarea + ' ', + # Fix for NBSP, converted to space by firefox + ' ', + ' ', + ), + 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 ); + } + } + + 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; + } + } + } + + // 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 - ' ' => ' ', - # Fix for NBSP, converted to space by firefox - ' ' => "\xc2\xa0", - ' ' => "\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 string $title Message cache key with initial uppercase letter. - * @param string $code 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 ) { $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 { @@ -734,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; @@ -752,17 +917,24 @@ class MessageCache { $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 { // 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. + // 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() . ")" ); + wfDebugLog( + 'MessageCache', + __METHOD__ . ": message content doesn't provide wikitext " + . "(content model: " . $content->getContentHandler() . ")" + ); $message = false; // negative caching } else { @@ -783,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; } @@ -835,14 +1007,16 @@ class MessageCache { } /** - * @param $text string - * @param $title Title - * @param $linestart bool - * @param $interface bool - * @param $language + * @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 ); } @@ -901,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,7 +1093,7 @@ class MessageCache { * 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 ) { @@ -929,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/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 39bf4c9f..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__ ); @@ -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,27 +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; } - wfDebug( "Squid purge: " . implode( ' ', $urlArr ) . "\n" ); + wfDebugLog( 'squid', __METHOD__ . ': ' . implode( ' ', $urlArr ) . "\n" ); - if ( $wgHTCPMulticastRouting ) { - SquidUpdate::HTCPPurge( $urlArr ); + 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; @@ -162,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 ); @@ -180,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__ ); } @@ -244,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 string $url URL to match - * @param array $rules Array of rules, see $wgHTCPMulticastRouting for format and behavior + * @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; diff --git a/includes/cache/UserCache.php b/includes/cache/UserCache.php index bfbacfaa..6085f586 100644 --- a/includes/cache/UserCache.php +++ b/includes/cache/UserCache.php @@ -58,6 +58,17 @@ class UserCache { : false; // user does not exist? } + /** + * 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 array $userIds List of user IDs @@ -70,6 +81,8 @@ class UserCache { $usersToCheck = array(); $usersToQuery = array(); + $userIds = array_unique( $userIds ); + foreach ( $userIds as $userId ) { $userId = (int)$userId; if ( $userId <= 0 ) { -- cgit v1.2.3-54-g00ecf