Merge "Use config instead of globals for OutputPage"
[lhc/web/wiklou.git] / includes / specials / SpecialCachedPage.php
index 32eb2e7..cb9b07c 100644 (file)
@@ -38,7 +38,6 @@
  * @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.
@@ -52,15 +51,28 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         * If the cache is enabled or not.
         *
         * @since 1.20
-        * @var boolean
+        * @var bool
         */
        protected $cacheEnabled = true;
 
+       /**
+        * 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 boolean $cacheEnabled
+        * @param bool $cacheEnabled
         */
        public function setCacheEnabled( $cacheEnabled ) {
                $this->cacheHelper->setCacheEnabled( $cacheEnabled );
@@ -72,25 +84,27 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         *
         * @since 1.20
         *
-        * @param integer|null $cacheExpiry Sets the cache expiry, either ttl in seconds or unix timestamp.
-        * @param boolean|null $cacheEnabled Sets if the cache should be enabled or not.
+        * @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();
+               if ( !isset( $this->cacheHelper ) ) {
+                       $this->cacheHelper = new CacheHelper();
 
-               $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
-               $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
+                       $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
+                       $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
 
-               $keyArgs = $this->getCacheKey();
+                       $keyArgs = $this->getCacheKey();
 
-               if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
-                       unset( $keyArgs['action'] );
-               }
+                       if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
+                               unset( $keyArgs['action'] );
+                       }
 
-               $this->cacheHelper->setCacheKey( $keyArgs );
+                       $this->cacheHelper->setCacheKey( $keyArgs );
 
-               if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
-                       $this->cacheHelper->rebuildOnDemand();
+                       if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
+                               $this->cacheHelper->rebuildOnDemand();
+                       }
                }
 
                $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
@@ -104,7 +118,7 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         *
         * @since 1.20
         *
-        * @param {function} $computeFunction
+        * @param callable $computeFunction
         * @param array|mixed $args
         * @param string|null $key
         *
@@ -122,12 +136,16 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         *
         * @since 1.20
         *
-        * @param {function} $computeFunction
+        * @param callable $computeFunction
         * @param array $args
         * @param string|null $key
         */
        public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
-               $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
+               $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue(
+                       $computeFunction,
+                       $args,
+                       $key
+               ) );
        }
 
        /**
@@ -137,15 +155,18 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         * @since 1.20
         */
        public function saveCache() {
-               $this->cacheHelper->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.
+        * Sets the time to live for the cache, in seconds or a unix timestamp
+        * indicating the point of expiry.
         *
         * @since 1.20
         *
-        * @param integer $cacheExpiry
+        * @param int $cacheExpiry
         */
        public function setExpiry( $cacheExpiry ) {
                $this->cacheHelper->setExpiry( $cacheExpiry );
@@ -170,12 +191,11 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         *
         * @since 1.20
         *
-        * @param boolean $hasCached
+        * @param bool $hasCached
         */
        public function onCacheInitialized( $hasCached ) {
                if ( $hasCached ) {
                        $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
                }
        }
-
 }