diff options
author | Petr Pchelko <ppchelko@wikimedia.org> | 2021-04-20 04:41:54 -0700 |
---|---|---|
committer | Ppchelko <ppchelko@wikimedia.org> | 2021-04-20 12:56:01 +0000 |
commit | ab9ecc0268843fdaac350271b70b1c9b53437d35 (patch) | |
tree | d7a21e4d6555b6f7db685313529a3ed952e777de | |
parent | db888db7ff4b76203a86bf96a058273fdb340d33 (diff) | |
download | mediawikicore-ab9ecc0268843fdaac350271b70b1c9b53437d35.tar.gz mediawikicore-ab9ecc0268843fdaac350271b70b1c9b53437d35.zip |
Drop hard-deprecated CacheHelper classes
Bug: T249230
Change-Id: I8fb43e862a19031520dda5a147e533becd7eb89e
-rw-r--r-- | RELEASE-NOTES-1.37 | 2 | ||||
-rw-r--r-- | autoload.php | 4 | ||||
-rw-r--r-- | includes/actions/CachedAction.php | 197 | ||||
-rw-r--r-- | includes/cache/CacheHelper.php | 328 | ||||
-rw-r--r-- | includes/cache/ICacheHelper.php | 84 | ||||
-rw-r--r-- | includes/specials/SpecialCachedPage.php | 209 |
6 files changed, 2 insertions, 822 deletions
diff --git a/RELEASE-NOTES-1.37 b/RELEASE-NOTES-1.37 index 26acc4dd2414..a75e5ab24d3b 100644 --- a/RELEASE-NOTES-1.37 +++ b/RELEASE-NOTES-1.37 @@ -89,6 +89,8 @@ because of Phabricator reports. Use WatchlistManager::clearTitleUserNotification() instead. * Autopromote class, deprecated since 1.35, was removed. Use UserGroupManager instead. +* The following classes, hard-deprecated since 1.36, have been removed: + CachedAction, SpecialCachedPage, CacheHelper, ICacheHelper. === Deprecations in 1.37 === * JobQueue::getWiki, deprecated in 1.33, now emits deprecation warnings. diff --git a/autoload.php b/autoload.php index ece09e5b32e3..487b74880657 100644 --- a/autoload.php +++ b/autoload.php @@ -215,9 +215,7 @@ $wgAutoloadLocalClasses = [ 'BufferingStatsdDataFactory' => __DIR__ . '/includes/libs/stats/BufferingStatsdDataFactory.php', 'CLIParser' => __DIR__ . '/maintenance/parse.php', 'CacheDependency' => __DIR__ . '/includes/cache/dependency/CacheDependency.php', - 'CacheHelper' => __DIR__ . '/includes/cache/CacheHelper.php', 'CacheTime' => __DIR__ . '/includes/parser/CacheTime.php', - 'CachedAction' => __DIR__ . '/includes/actions/CachedAction.php', 'CachedBagOStuff' => __DIR__ . '/includes/libs/objectcache/CachedBagOStuff.php', 'CachingSiteStore' => __DIR__ . '/includes/site/CachingSiteStore.php', 'CannotCreateActorException' => __DIR__ . '/includes/exception/CannotCreateActorException.php', @@ -637,7 +635,6 @@ $wgAutoloadLocalClasses = [ 'HttpStatus' => __DIR__ . '/includes/libs/HttpStatus.php', 'IApiMessage' => __DIR__ . '/includes/api/IApiMessage.php', 'IBufferingStatsdDataFactory' => __DIR__ . '/includes/libs/stats/IBufferingStatsdDataFactory.php', - 'ICacheHelper' => __DIR__ . '/includes/cache/ICacheHelper.php', 'IContextSource' => __DIR__ . '/includes/context/IContextSource.php', 'IDBAccessObject' => __DIR__ . '/includes/dao/IDBAccessObject.php', 'IDatabase' => __DIR__ . '/includes/libs/rdbms/database/IDatabase.php', @@ -1493,7 +1490,6 @@ $wgAutoloadLocalClasses = [ 'SpecialBookSources' => __DIR__ . '/includes/specials/SpecialBookSources.php', 'SpecialBotPasswords' => __DIR__ . '/includes/specials/SpecialBotPasswords.php', 'SpecialBrokenRedirects' => __DIR__ . '/includes/specials/SpecialBrokenRedirects.php', - 'SpecialCachedPage' => __DIR__ . '/includes/specials/SpecialCachedPage.php', 'SpecialCategories' => __DIR__ . '/includes/specials/SpecialCategories.php', 'SpecialChangeContentModel' => __DIR__ . '/includes/specials/SpecialChangeContentModel.php', 'SpecialChangeCredentials' => __DIR__ . '/includes/specials/SpecialChangeCredentials.php', diff --git a/includes/actions/CachedAction.php b/includes/actions/CachedAction.php deleted file mode 100644 index 5345d60806e9..000000000000 --- a/includes/actions/CachedAction.php +++ /dev/null @@ -1,197 +0,0 @@ -<?php -/** - * Abstract action class with scaffolding for caching HTML and other values - * in a single blob. - * - * 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 - * @ingroup Actions - * @author Jeroen De Dauw < jeroendedauw@gmail.com > - * @since 1.20 - */ - -/** - * Abstract action class with scaffolding for caching HTML and other values - * in a single blob. - * - * Before using any of the caching functionality, call startCache. - * After the last call to either getCachedValue or addCachedHTML, call saveCache. - * - * To get a cached value or compute it, use getCachedValue like this: - * $this->getCachedValue( $callback ); - * - * To add HTML that should be cached, use addCachedHTML like this: - * $this->addCachedHTML( $callback ); - * - * The callback function is only called when needed, so do all your expensive - * computations here. This function should returns the HTML to be cached. - * It should not add anything to the PageOutput object! - * - * @ingroup Actions - */ -abstract class CachedAction extends FormlessAction implements ICacheHelper { - - /** - * CacheHelper object to which we forward the non-SpecialPage specific caching work. - * Initialized in startCache. - * - * @since 1.20 - * @var CacheHelper - */ - protected $cacheHelper; - - /** - * If the cache is enabled or not. - * - * @since 1.20 - * @var bool - */ - protected $cacheEnabled = true; - - public function __construct( - Page $page, - IContextSource $context = null - ) { - parent::__construct( $page, $context ); - wfDeprecated( __CLASS__, '1.36' ); - } - - /** - * Sets if the cache should be enabled or not. - * - * @since 1.20 - * @param bool $cacheEnabled - */ - public function setCacheEnabled( $cacheEnabled ) { - $this->cacheHelper->setCacheEnabled( $cacheEnabled ); - } - - /** - * Initializes the caching. - * Should be called before the first time anything is added via addCachedHTML. - * - * @since 1.20 - * - * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp. - * @param bool|null $cacheEnabled Sets if the cache should be enabled or not. - */ - public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { - $this->cacheHelper = new CacheHelper(); - - $this->cacheHelper->setCacheEnabled( $this->cacheEnabled ); - $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] ); - - $keyArgs = $this->getCacheKey(); - - if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { - unset( $keyArgs['action'] ); - } - - $this->cacheHelper->setCacheKey( $keyArgs ); - - if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { - $this->cacheHelper->rebuildOnDemand(); - } - - $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled ); - } - - /** - * Get a cached value if available or compute it if not and then cache it if possible. - * The provided $computeFunction is only called when the computation needs to happen - * and should return a result value. $args are arguments that will be passed to the - * compute function when called. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array|mixed $args - * @param string|null $key - * - * @return mixed - */ - public function getCachedValue( $computeFunction, $args = [], $key = null ) { - return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ); - } - - /** - * Add some HTML to be cached. - * This is done by providing a callback function that should - * return the HTML to be added. It will only be called if the - * item is not in the cache yet or when the cache has been invalidated. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array $args - * @param string|null $key - */ - public function addCachedHTML( $computeFunction, $args = [], $key = null ) { - $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ); - $this->getOutput()->addHTML( $html ); - } - - /** - * Saves the HTML to the cache in case it got recomputed. - * Should be called after the last time anything is added via addCachedHTML. - * - * @since 1.20 - */ - public function saveCache() { - $this->cacheHelper->saveCache(); - } - - /** - * Sets the time to live for the cache, in seconds or a unix timestamp - * indicating the point of expiry. - * - * @since 1.20 - * - * @param int $cacheExpiry - */ - public function setExpiry( $cacheExpiry ) { - $this->cacheHelper->setExpiry( $cacheExpiry ); - } - - /** - * Returns the variables used to constructed the cache key in an array. - * - * @since 1.20 - * - * @return array - */ - protected function getCacheKey() { - return [ - get_class( $this->getArticle() ), - $this->getName(), - $this->getLanguage()->getCode() - ]; - } - - /** - * Gets called after the cache got initialized. - * - * @since 1.20 - * - * @param bool $hasCached - */ - public function onCacheInitialized( $hasCached ) { - if ( $hasCached ) { - $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) ); - } - } -} diff --git a/includes/cache/CacheHelper.php b/includes/cache/CacheHelper.php deleted file mode 100644 index ada950eb256d..000000000000 --- a/includes/cache/CacheHelper.php +++ /dev/null @@ -1,328 +0,0 @@ -<?php -/** - * Cache of various elements in a single cache entry. - * - * 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 - * @license GPL-2.0-or-later - * @author Jeroen De Dauw < jeroendedauw@gmail.com > - */ - -use MediaWiki\MediaWikiServices; - -/** - * Helper class for caching various elements in a single cache entry. - * - * To get a cached value or compute it, use getCachedValue like this: - * $this->getCachedValue( $callback ); - * - * To add HTML that should be cached, use addCachedHTML like this: - * $this->addCachedHTML( $callback ); - * - * The callback function is only called when needed, so do all your expensive - * computations here. This function should returns the HTML to be cached. - * It should not add anything to the PageOutput object! - * - * Before the first addCachedHTML call, you should call $this->startCache(); - * After adding the last HTML that should be cached, call $this->saveCache(); - * - * @since 1.20 - */ -class CacheHelper implements ICacheHelper { - /** - * The time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. - * - * @since 1.20 - * @var int - */ - protected $cacheExpiry = 3600; - - /** - * List of HTML chunks to be cached (if !hasCached) or that where cached (of hasCached). - * If not cached already, then the newly computed chunks are added here, - * if it as cached already, chunks are removed from this list as they are needed. - * - * @since 1.20 - * @var array - */ - protected $cachedChunks; - - /** - * Indicates if the to be cached content was already cached. - * Null if this information is not available yet. - * - * @since 1.20 - * @var bool|null - */ - protected $hasCached = null; - - /** - * If the cache is enabled or not. - * - * @since 1.20 - * @var bool - */ - protected $cacheEnabled = true; - - /** - * Function that gets called when initialization is done. - * - * @since 1.20 - * @var callable|null - */ - protected $onInitHandler; - - /** - * Elements to build a cache key with. - * - * @since 1.20 - * @var array - */ - protected $cacheKey = []; - - public function __construct() { - wfDeprecated( __CLASS__, '1.36' ); - } - - /** - * Sets if the cache should be enabled or not. - * - * @since 1.20 - * @param bool $cacheEnabled - */ - public function setCacheEnabled( $cacheEnabled ) { - $this->cacheEnabled = $cacheEnabled; - } - - /** - * Initializes the caching. - * Should be called before the first time anything is added via addCachedHTML. - * - * @since 1.20 - * - * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp. - * @param bool|null $cacheEnabled Sets if the cache should be enabled or not. - */ - public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { - if ( $this->hasCached === null ) { - if ( $cacheExpiry !== null ) { - $this->cacheExpiry = $cacheExpiry; - } - - if ( $cacheEnabled !== null ) { - $this->setCacheEnabled( $cacheEnabled ); - } - - $this->initCaching(); - } - } - - /** - * Returns a message that notifies the user he/she is looking at - * a cached version of the page, including a refresh link. - * - * @since 1.20 - * - * @param IContextSource $context - * @param bool $includePurgeLink - * - * @return string - */ - public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) { - if ( $this->cacheExpiry < 86400 * 3650 ) { - $message = $context->msg( - 'cachedspecial-viewing-cached-ttl', - $context->getLanguage()->formatDuration( $this->cacheExpiry ) - )->escaped(); - } else { - $message = $context->msg( - 'cachedspecial-viewing-cached-ts' - )->escaped(); - } - - if ( $includePurgeLink ) { - $refreshArgs = $context->getRequest()->getQueryValues(); - unset( $refreshArgs['title'] ); - $refreshArgs['action'] = 'purge'; - - $message .= ' ' . MediaWikiServices::getInstance()->getLinkRenderer()->makeLink( - $context->getTitle(), - $context->msg( 'cachedspecial-refresh-now' )->text(), - [], - $refreshArgs - ); - } - - return $message; - } - - /** - * Initializes the caching if not already done so. - * Should be called before any of the caching functionality is used. - * - * @since 1.20 - */ - protected function initCaching() { - if ( $this->cacheEnabled && $this->hasCached === null ) { - $cachedChunks = ObjectCache::getInstance( CACHE_ANYTHING )->get( $this->getCacheKeyString() ); - - $this->hasCached = is_array( $cachedChunks ); - $this->cachedChunks = $this->hasCached ? $cachedChunks : []; - - if ( $this->onInitHandler !== null ) { - call_user_func( $this->onInitHandler, $this->hasCached ); - } - } - } - - /** - * Get a cached value if available or compute it if not and then cache it if possible. - * The provided $computeFunction is only called when the computation needs to happen - * and should return a result value. $args are arguments that will be passed to the - * compute function when called. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array|mixed $args - * @param string|null $key - * - * @return mixed - */ - public function getCachedValue( $computeFunction, $args = [], $key = null ) { - $this->initCaching(); - - if ( $this->cacheEnabled && $this->hasCached ) { - $value = null; - - if ( $key === null ) { - reset( $this->cachedChunks ); - $itemKey = key( $this->cachedChunks ); - - if ( $itemKey === null ) { - wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ ); - } elseif ( !is_int( $itemKey ) ) { - wfWarn( "Attempted to get item with non-numeric key while " . - "the next item in the queue has a key ($itemKey) in " . __METHOD__ ); - } else { - $value = array_shift( $this->cachedChunks ); - } - } elseif ( array_key_exists( $key, $this->cachedChunks ) ) { - $value = $this->cachedChunks[$key]; - unset( $this->cachedChunks[$key] ); - } else { - wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ ); - } - } else { - if ( !is_array( $args ) ) { - $args = [ $args ]; - } - - $value = $computeFunction( ...$args ); - - if ( $this->cacheEnabled ) { - if ( $key === null ) { - $this->cachedChunks[] = $value; - } else { - $this->cachedChunks[$key] = $value; - } - } - } - - return $value; - } - - /** - * Saves the HTML to the cache in case it got recomputed. - * Should be called after the last time anything is added via addCachedHTML. - * - * @since 1.20 - */ - public function saveCache() { - if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) { - ObjectCache::getInstance( CACHE_ANYTHING )->set( - $this->getCacheKeyString(), - $this->cachedChunks, - $this->cacheExpiry - ); - } - } - - /** - * Sets the time to live for the cache, in seconds or a unix timestamp - * indicating the point of expiry... - * - * @since 1.20 - * - * @param int $cacheExpiry - */ - public function setExpiry( $cacheExpiry ) { - $this->cacheExpiry = $cacheExpiry; - } - - /** - * Returns the cache key to use to cache this page's HTML output. - * Is constructed from the special page name and language code. - * - * @since 1.20 - * - * @return string - * @throws MWException - */ - protected function getCacheKeyString() { - if ( $this->cacheKey === [] ) { - throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' ); - } - - return ObjectCache::getLocalClusterInstance()->makeKey( - ...array_values( $this->cacheKey ) - ); - } - - /** - * Sets the cache key that should be used. - * - * @since 1.20 - * - * @param array $cacheKey - */ - public function setCacheKey( array $cacheKey ) { - $this->cacheKey = $cacheKey; - } - - /** - * Rebuild the content, even if it's already cached. - * This effectively has the same effect as purging the cache, - * since it will be overridden with the new value on the next request. - * - * @since 1.20 - */ - public function rebuildOnDemand() { - $this->hasCached = false; - } - - /** - * Sets a function that gets called when initialization of the cache is done. - * - * @since 1.20 - * - * @param callable $handlerFunction - */ - public function setOnInitializedHandler( $handlerFunction ) { - $this->onInitHandler = $handlerFunction; - } -} diff --git a/includes/cache/ICacheHelper.php b/includes/cache/ICacheHelper.php deleted file mode 100644 index bdac12c6443c..000000000000 --- a/includes/cache/ICacheHelper.php +++ /dev/null @@ -1,84 +0,0 @@ -<?php -/** - * Cache of various elements in a single cache entry. - * - * 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 - * @license GPL-2.0-or-later - * @author Jeroen De Dauw < jeroendedauw@gmail.com > - */ - -/** - * Interface for all classes implementing CacheHelper functionality. - * - * @deprecated since 1.36 - * @since 1.20 - */ -interface ICacheHelper { - /** - * Sets if the cache should be enabled or not. - * - * @since 1.20 - * @param bool $cacheEnabled - */ - public function setCacheEnabled( $cacheEnabled ); - - /** - * Initializes the caching. - * Should be called before the first time anything is added via addCachedHTML. - * - * @since 1.20 - * - * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp. - * @param bool|null $cacheEnabled Sets if the cache should be enabled or not. - */ - public function startCache( $cacheExpiry = null, $cacheEnabled = null ); - - /** - * Get a cached value if available or compute it if not and then cache it if possible. - * The provided $computeFunction is only called when the computation needs to happen - * and should return a result value. $args are arguments that will be passed to the - * compute function when called. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array|mixed $args - * @param string|null $key - * - * @return mixed - */ - public function getCachedValue( $computeFunction, $args = [], $key = null ); - - /** - * Saves the HTML to the cache in case it got recomputed. - * Should be called after the last time anything is added via addCachedHTML. - * - * @since 1.20 - */ - public function saveCache(); - - /** - * Sets the time to live for the cache, in seconds or a unix timestamp - * indicating the point of expiry... - * - * @since 1.20 - * - * @param int $cacheExpiry - */ - public function setExpiry( $cacheExpiry ); -} diff --git a/includes/specials/SpecialCachedPage.php b/includes/specials/SpecialCachedPage.php deleted file mode 100644 index 8be22b9c6b73..000000000000 --- a/includes/specials/SpecialCachedPage.php +++ /dev/null @@ -1,209 +0,0 @@ -<?php - -/** - * Abstract special page class with scaffolding for caching HTML and other values - * in a single blob. - * - * Before using any of the caching functionality, call startCache. - * After the last call to either getCachedValue or addCachedHTML, call saveCache. - * - * To get a cached value or compute it, use getCachedValue like this: - * $this->getCachedValue( $callback ); - * - * To add HTML that should be cached, use addCachedHTML like this: - * $this->addCachedHTML( $callback ); - * - * The callback function is only called when needed, so do all your expensive - * computations here. This function should returns the HTML to be cached. - * It should not add anything to the PageOutput object! - * - * 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 - * @ingroup SpecialPage - * @author Jeroen De Dauw < jeroendedauw@gmail.com > - * @since 1.20 - */ -abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper { - /** - * CacheHelper object to which we forward the non-SpecialPage specific caching work. - * Initialized in startCache. - * - * @since 1.20 - * @var CacheHelper - */ - protected $cacheHelper; - - /** - * If the cache is enabled or not. - * - * @since 1.20 - * @var bool - */ - protected $cacheEnabled = true; - - public function __construct( - $name = '', $restriction = '', $listed = true, - $function = false, $file = '', $includable = false - ) { - parent::__construct( $name, $restriction, $listed, $function, $file, $includable ); - wfDeprecated( __CLASS__, '1.36' ); - } - - /** - * Gets called after @see SpecialPage::execute. - * - * @since 1.20 - * - * @param string|null $subPage - */ - protected function afterExecute( $subPage ) { - $this->saveCache(); - - parent::afterExecute( $subPage ); - } - - /** - * Sets if the cache should be enabled or not. - * - * @since 1.20 - * @param bool $cacheEnabled - */ - public function setCacheEnabled( $cacheEnabled ) { - $this->cacheHelper->setCacheEnabled( $cacheEnabled ); - } - - /** - * Initializes the caching. - * Should be called before the first time anything is added via addCachedHTML. - * - * @since 1.20 - * - * @param int|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp. - * @param bool|null $cacheEnabled Sets if the cache should be enabled or not. - */ - public function startCache( $cacheExpiry = null, $cacheEnabled = null ) { - if ( !isset( $this->cacheHelper ) ) { - $this->cacheHelper = new CacheHelper(); - - $this->cacheHelper->setCacheEnabled( $this->cacheEnabled ); - $this->cacheHelper->setOnInitializedHandler( [ $this, 'onCacheInitialized' ] ); - - $keyArgs = $this->getCacheKey(); - - if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) { - unset( $keyArgs['action'] ); - } - - $this->cacheHelper->setCacheKey( $keyArgs ); - - if ( $this->getRequest()->getText( 'action' ) === 'purge' ) { - $this->cacheHelper->rebuildOnDemand(); - } - } - - $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled ); - } - - /** - * Get a cached value if available or compute it if not and then cache it if possible. - * The provided $computeFunction is only called when the computation needs to happen - * and should return a result value. $args are arguments that will be passed to the - * compute function when called. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array|mixed $args - * @param string|null $key - * - * @return mixed - */ - public function getCachedValue( $computeFunction, $args = [], $key = null ) { - return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ); - } - - /** - * Add some HTML to be cached. - * This is done by providing a callback function that should - * return the HTML to be added. It will only be called if the - * item is not in the cache yet or when the cache has been invalidated. - * - * @since 1.20 - * - * @param callable $computeFunction - * @param array $args - * @param string|null $key - */ - public function addCachedHTML( $computeFunction, $args = [], $key = null ) { - $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( - $computeFunction, - $args, - $key - ) ); - } - - /** - * Saves the HTML to the cache in case it got recomputed. - * Should be called after the last time anything is added via addCachedHTML. - * - * @since 1.20 - */ - public function saveCache() { - if ( isset( $this->cacheHelper ) ) { - $this->cacheHelper->saveCache(); - } - } - - /** - * Sets the time to live for the cache, in seconds or a unix timestamp - * indicating the point of expiry. - * - * @since 1.20 - * - * @param int $cacheExpiry - */ - public function setExpiry( $cacheExpiry ) { - $this->cacheHelper->setExpiry( $cacheExpiry ); - } - - /** - * Returns the variables used to constructed the cache key in an array. - * - * @since 1.20 - * - * @return array - */ - protected function getCacheKey() { - return [ - $this->mName, - $this->getLanguage()->getCode() - ]; - } - - /** - * Gets called after the cache got initialized. - * - * @since 1.20 - * - * @param bool $hasCached - */ - public function onCacheInitialized( $hasCached ) { - if ( $hasCached ) { - $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) ); - } - } -} |