X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2FCacheHelper.php;h=cfa4160e4c194614c07b202e55a8439b97eddfb1;hb=0929a1fba8ce5914848b7516bcf1dfb44c853e11;hp=8199cb4a4b188fd6228ce22ca8f1ed9c7d631bd1;hpb=67e23ec357d2265bc487bdd4b63e30fcaf5871c7;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/CacheHelper.php b/includes/CacheHelper.php index 8199cb4a4b..cfa4160e4c 100644 --- a/includes/CacheHelper.php +++ b/includes/CacheHelper.php @@ -18,7 +18,7 @@ * http://www.gnu.org/copyleft/gpl.html * * @file - * @licence GNU GPL v2 or later + * @license GNU GPL v2 or later * @author Jeroen De Dauw < jeroendedauw@gmail.com > */ @@ -28,7 +28,6 @@ * @since 1.20 */ interface ICacheHelper { - /** * Sets if the cache should be enabled or not. * @@ -81,7 +80,6 @@ interface ICacheHelper { * @param integer $cacheExpiry */ function setExpiry( $cacheExpiry ); - } /** @@ -103,7 +101,6 @@ interface ICacheHelper { * @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. * @@ -143,10 +140,18 @@ class CacheHelper implements ICacheHelper { * Function that gets called when initialization is done. * * @since 1.20 - * @var function + * @var callable */ protected $onInitHandler = false; + /** + * Elements to build a cache key with. + * + * @since 1.20 + * @var array + */ + protected $cacheKey = array(); + /** * Sets if the cache should be enabled or not. * @@ -197,8 +202,7 @@ class CacheHelper implements ICacheHelper { 'cachedspecial-viewing-cached-ttl', $context->getLanguage()->formatDuration( $this->cacheExpiry ) )->escaped(); - } - else { + } else { $message = $context->msg( 'cachedspecial-viewing-cached-ts' )->escaped(); @@ -268,26 +272,22 @@ class CacheHelper implements ICacheHelper { $itemKey = array_shift( $itemKey ); if ( !is_integer( $itemKey ) ) { - wfWarn( "Attempted to get item with non-numeric key while the next item in the queue has a key ($itemKey) in " . __METHOD__ ); - } - elseif ( is_null( $itemKey ) ) { + wfWarn( "Attempted to get item with non-numeric key while " . + "the next item in the queue has a key ($itemKey) in " . __METHOD__ ); + } elseif ( is_null( $itemKey ) ) { wfWarn( "Attempted to get an item while the queue is empty in " . __METHOD__ ); - } - else { + } else { $value = array_shift( $this->cachedChunks ); } - } - else { + } else { if ( array_key_exists( $key, $this->cachedChunks ) ) { $value = $this->cachedChunks[$key]; unset( $this->cachedChunks[$key] ); - } - else { + } else { wfWarn( "There is no item with key '$key' in this->cachedChunks in " . __METHOD__ ); } } - } - else { + } else { if ( !is_array( $args ) ) { $args = array( $args ); } @@ -297,8 +297,7 @@ class CacheHelper implements ICacheHelper { if ( $this->cacheEnabled ) { if ( is_null( $key ) ) { $this->cachedChunks[] = $value; - } - else { + } else { $this->cachedChunks[$key] = $value; } } @@ -315,7 +314,11 @@ class CacheHelper implements ICacheHelper { */ public function saveCache() { if ( $this->cacheEnabled && $this->hasCached === false && !empty( $this->cachedChunks ) ) { - wfGetCache( CACHE_ANYTHING )->set( $this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry ); + wfGetCache( CACHE_ANYTHING )->set( + $this->getCacheKeyString(), + $this->cachedChunks, + $this->cacheExpiry + ); } } @@ -338,8 +341,13 @@ class CacheHelper implements ICacheHelper { * @since 1.20 * * @return string + * @throws MWException */ protected function getCacheKeyString() { + if ( $this->cacheKey === array() ) { + throw new MWException( 'No cache key set, so cannot obtain or save the CacheHelper values.' ); + } + return call_user_func_array( 'wfMemcKey', $this->cacheKey ); } @@ -375,5 +383,4 @@ class CacheHelper implements ICacheHelper { public function setOnInitializedHandler( $handlerFunction ) { $this->onInitHandler = $handlerFunction; } - }