merging latest master
[lhc/web/wiklou.git] / includes / specials / SpecialCachedPage.php
index eb8e6b9..b3f6c72 100644 (file)
@@ -4,7 +4,7 @@
  * Abstract special page class with scaffolding for caching HTML and other values
  * in a single blob.
  *
- * Before using any of the cahing functionality, call startCache.
+ * 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:
  * computations here. This function should returns the HTML to be cached.
  * It should not add anything to the PageOutput object!
  *
- * @since 1.20
+ * 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.
  *
- * @file SpecialCachedPage.php
- * @ingroup SpecialPage
+ * 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
  *
- * @licence GNU GPL v2 or later
+ * @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 foreward the non-SpecialPage specific caching work.
+        * CacheHelper object to which we forward the non-SpecialPage specific caching work.
         * Initialized in startCache.
         *
         * @since 1.20
@@ -36,6 +48,27 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         */
        protected $cacheHelper;
 
+       /**
+        * If the cache is enabled or not.
+        *
+        * @since 1.20
+        * @var boolean
+        */
+       protected $cacheEnabled = true;
+
+       /**
+        * Gets called after @see SpecialPage::execute.
+        *
+        * @since 1.20
+        *
+        * @param $subPage string|null
+        */
+       protected function afterExecute( $subPage ) {
+               $this->saveCache();
+
+               parent::afterExecute( $subPage );
+       }
+
        /**
         * Sets if the cache should be enabled or not.
         *
@@ -52,24 +85,27 @@ abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
         *
         * @since 1.20
         *
-        * @param integer|null $cacheExpiry Sets the cache expirty, either ttl in seconds or unix timestamp.
+        * @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.
         */
        public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
-               $this->cacheHelper = new CacheHelper( $this->get );
+               if ( !isset( $this->cacheHelper ) ) {
+                       $this->cacheHelper = new CacheHelper();
 
-               $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->purge();
+                       if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
+                               $this->cacheHelper->rebuildOnDemand();
+                       }
                }
 
                $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
@@ -116,18 +152,20 @@ 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
         */
-       public function setExpirey( $cacheExpiry ) {
-               $this->cacheHelper->setExpirey( $cacheExpiry );
+       public function setExpiry( $cacheExpiry ) {
+               $this->cacheHelper->setExpiry( $cacheExpiry );
        }
 
        /**