diff options
Diffstat (limited to 'MLEB/Translate/MessageGroups.php')
-rw-r--r-- | MLEB/Translate/MessageGroups.php | 134 |
1 files changed, 81 insertions, 53 deletions
diff --git a/MLEB/Translate/MessageGroups.php b/MLEB/Translate/MessageGroups.php index 92ba97fa..caaf815c 100644 --- a/MLEB/Translate/MessageGroups.php +++ b/MLEB/Translate/MessageGroups.php @@ -8,6 +8,7 @@ * @copyright Copyright © 2008-2013, Niklas Laxström, Siebrand Mazeland * @license GPL-2.0-or-later */ +use \MediaWiki\MediaWikiServices; /** * Factory class for accessing message groups individually by id or @@ -26,7 +27,7 @@ class MessageGroups { protected $groups; /** - * @var BagOStuff|null + * @var WANObjectCache|null */ protected $cache; @@ -34,25 +35,72 @@ class MessageGroups { * Initialises the list of groups */ protected function init() { - global $wgAutoloadClasses; - if ( is_array( $this->groups ) ) { return; } - $key = wfMemcKey( 'translate-groups' ); - $value = DependencyWrapper::getValueFromCache( $this->getCache(), $key ); + $value = $this->getCachedGroupDefinitions(); + $groups = $value['cc']; - if ( $value === null ) { - wfDebug( __METHOD__ . "-nocache\n" ); - $groups = $this->loadGroupDefinitions(); - } else { - wfDebug( __METHOD__ . "-withcache\n" ); - $groups = $value['cc']; - self::appendAutoloader( $value['autoload'], $wgAutoloadClasses ); + $this->postInit( $groups ); + } + + /** + * @param bool|string $recache Either "recache" or false + * @return array + */ + protected function getCachedGroupDefinitions( $recache = false ) { + global $wgAutoloadClasses, $wgVersion; + + $regenerator = function () { + global $wgAutoloadClasses; + + $groups = $deps = $autoload = []; + // This constructs the list of all groups from multiple different sources. + // When possible, a cache dependency is created to automatically recreate + // the cache when configuration changes. + Hooks::run( 'TranslatePostInitGroups', [ &$groups, &$deps, &$autoload ] ); + // Register autoloaders for this request, both values modified by reference + self::appendAutoloader( $autoload, $wgAutoloadClasses ); + + $value = [ + 'ts' => wfTimestamp( TS_MW ), + 'cc' => $groups, + 'autoload' => $autoload + ]; + $wrapper = new DependencyWrapper( $value, $deps ); + $wrapper->initialiseDeps(); + + return $wrapper; // save the new value to cache + }; + + $cache = $this->getCache(); + /** @var DependencyWrapper $wrapper */ + $wrapper = $cache->getWithSetCallback( + $cache->makeKey( 'translate-groups' ), + $cache::TTL_DAY, + $regenerator, + [ + 'lockTSE' => 30, // avoid stampedes + 'touchedCallback' => function ( $value ) { + return ( $value instanceof DependencyWrapper && $value->isExpired() ) + ? time() // treat value as if it just expired (for "lockTSE") + : null; + }, + 'minAsOf' => $recache ? INF : $cache::MIN_TIMESTAMP_NONE, // "miss" on recache + ] + ); + + // B/C for "touchedCallback" param not existing + if ( version_compare( $wgVersion, '1.33', '<' ) && $wrapper->isExpired() ) { + $wrapper = $regenerator(); + $cache->set( $cache->makeKey( 'translate-groups' ), $wrapper, $cache::TTL_DAY ); } - $this->postInit( $groups ); + $value = $wrapper->getValue(); + self::appendAutoloader( $value['autoload'], $wgAutoloadClasses ); + + return $value; } /** @@ -75,7 +123,9 @@ class MessageGroups { * @since 2015.04 */ public function recache() { - $groups = $this->loadGroupDefinitions(); + $value = $this->getCachedGroupDefinitions( 'recache' ); + $groups = $value['cc']; + $this->postInit( $groups ); } @@ -86,7 +136,10 @@ class MessageGroups { */ public static function clearCache() { $self = self::singleton(); - $self->getCache()->delete( wfMemcKey( 'translate-groups' ) ); + + $cache = $self->getCache(); + $cache->delete( $cache->makeKey( 'translate-groups' ), 1 ); + $self->clearProcessCache(); } @@ -104,11 +157,11 @@ class MessageGroups { /** * Returns a cacher object. * - * @return BagOStuff + * @return WANObjectCache */ protected function getCache() { if ( $this->cache === null ) { - return wfGetCache( CACHE_ANYTHING ); + return MediaWikiServices::getInstance()->getMainWANObjectCache(); } else { return $this->cache; } @@ -117,9 +170,9 @@ class MessageGroups { /** * Override cache, for example during tests. * - * @param BagOStuff|null $cache + * @param WANObjectCache|null $cache */ - public function setCache( BagOStuff $cache = null ) { + public function setCache( WANObjectCache $cache = null ) { $this->cache = $cache; } @@ -142,35 +195,6 @@ class MessageGroups { } /** - * This constructs the list of all groups from multiple different - * sources. When possible, a cache dependency is created to automatically - * recreate the cache when configuration changes. - * @return array - */ - protected function loadGroupDefinitions() { - global $wgAutoloadClasses; - - $groups = $deps = $autoload = []; - - Hooks::run( 'TranslatePostInitGroups', [ &$groups, &$deps, &$autoload ] ); - - // Register autoloaders for this request, both values modified by reference - self::appendAutoloader( $autoload, $wgAutoloadClasses ); - - $key = wfMemcKey( 'translate-groups' ); - $value = [ - 'ts' => wfTimestamp( TS_MW ), - 'cc' => $groups, - 'autoload' => $autoload, - ]; - - $wrapper = new DependencyWrapper( $value, $deps ); - $wrapper->storeToCache( $this->getCache(), $key, 60 * 60 * 2 ); - - return $groups; - } - - /** * Hook: TranslatePostInitGroups * @param array &$groups * @param array &$deps @@ -273,6 +297,10 @@ class MessageGroups { public static function getCCGroups( array &$groups, array &$deps, array &$autoload ) { global $wgTranslateCC; + if ( $wgTranslateCC !== [] ) { + wfDeprecated( '$wgTranslateCC' ); + } + $deps[] = new GlobalDependency( 'wgTranslateCC' ); $groups += $wgTranslateCC; @@ -340,6 +368,7 @@ class MessageGroups { public static function labelExists( $name ) { $groups = self::loadAggregateGroups(); $labels = array_map( function ( $g ) { + /** @var MessageGroup $g */ return $g->getLabel(); }, $groups ); return (bool)in_array( $name, $labels, true ); @@ -387,7 +416,6 @@ class MessageGroups { /** * Sets the message group priority. - * @see MessageGroups::getPriority * * @param MessageGroup|string $group Message group * @param string $priority Priority (empty string to unset) @@ -538,7 +566,7 @@ class MessageGroups { /** * Constructor function. - * @return MessageGroups + * @return self */ public static function singleton() { static $instance; @@ -739,8 +767,8 @@ class MessageGroups { /** * Sorts groups by label value - * @param string $a - * @param string $b + * @param MessageGroup $a + * @param MessageGroup $b * @return int */ public static function groupLabelSort( $a, $b ) { @@ -818,7 +846,7 @@ class MessageGroups { /** * Get all the aggregate messages groups defined in translate_metadata table. * - * @return array + * @return MessageGroup[] */ protected static function loadAggregateGroups() { $dbw = TranslateUtils::getSafeReadDB(); |