Merge "Localisation updates from http://translatewiki.net."
[lhc/web/wiklou.git] / includes / CacheHelper.php
index b61e2ad..5209857 100644 (file)
@@ -1,5 +1,32 @@
 <?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
+ * @licence GNU GPL v2 or later
+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
+ */
 
+/**
+ * Interface for all classes implementing CacheHelper functionality.
+ *
+ * @since 1.20
+ */
 interface ICacheHelper {
 
        /**
@@ -16,24 +43,26 @@ interface 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.
         */
        function startCache( $cacheExpiry = null, $cacheEnabled = null );
 
        /**
-        * 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.
+        * 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 {function} $computeFunction
-        * @param array $args
+        * @param array|mixed $args
         * @param string|null $key
+        *
+        * @return mixed
         */
-       function addCachedHTML( $computeFunction, $args = array(), $key = null );
+       function getCachedValue( $computeFunction, $args = array(), $key = null );
 
        /**
         * Saves the HTML to the cache in case it got recomputed.
@@ -50,7 +79,7 @@ interface ICacheHelper {
         *
         * @param integer $cacheExpiry
         */
-       function setExpirey( $cacheExpiry );
+       function setExpiry( $cacheExpiry );
 
 }
 
@@ -71,12 +100,6 @@ interface ICacheHelper {
  * After adding the last HTML that should be cached, call $this->saveCache();
  *
  * @since 1.20
- *
- * @file CacheHelper.php
- * @ingroup SpecialPage
- *
- * @licence GNU GPL v2 or later
- * @author Jeroen De Dauw < jeroendedauw@gmail.com >
  */
 class CacheHelper implements ICacheHelper {
 
@@ -89,8 +112,8 @@ class CacheHelper implements ICacheHelper {
        protected $cacheExpiry = 3600;
 
        /**
-        * List of HTML chunks to be cached (if !hasCached) or that where cashed (of hasCached).
-        * If no cached already, then the newly computed chunks are added here,
+        * 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
@@ -139,7 +162,7 @@ class CacheHelper 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 ) {
@@ -163,25 +186,11 @@ class CacheHelper implements ICacheHelper {
         * @since 1.20
         *
         * @param IContextSource $context
+        * @param boolean $includePurgeLink
         *
         * @return string
         */
-       public function getCachedNotice( IContextSource $context ) {
-               $refreshArgs = $context->getRequest()->getQueryValues();
-               unset( $refreshArgs['title'] );
-               $refreshArgs['action'] = 'purge';
-
-               $subPage = $context->getTitle()->getFullText();
-               $subPage = explode( '/', $subPage, 2 );
-               $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
-
-               $refreshLink = Linker::link(
-                       $context->getTitle( $subPage ),
-                       $context->msg( 'cachedspecial-refresh-now' )->escaped(),
-                       array(),
-                       $refreshArgs
-               );
-
+       public function getCachedNotice( IContextSource $context, $includePurgeLink = true ) {
                if ( $this->cacheExpiry < 86400 * 3650 ) {
                        $message = $context->msg(
                                'cachedspecial-viewing-cached-ttl',
@@ -194,7 +203,24 @@ class CacheHelper implements ICacheHelper {
                        )->escaped();
                }
 
-               return $message . ' ' . $refreshLink;
+               if ( $includePurgeLink ) {
+                       $refreshArgs = $context->getRequest()->getQueryValues();
+                       unset( $refreshArgs['title'] );
+                       $refreshArgs['action'] = 'purge';
+
+                       $subPage = $context->getTitle()->getFullText();
+                       $subPage = explode( '/', $subPage, 2 );
+                       $subPage = count( $subPage ) > 1 ? $subPage[1] : false;
+
+                       $message .= ' ' . Linker::link(
+                               $context->getTitle( $subPage ),
+                               $context->msg( 'cachedspecial-refresh-now' )->escaped(),
+                               array(),
+                               $refreshArgs
+                       );
+               }
+
+               return $message;
        }
 
        /**
@@ -216,24 +242,6 @@ class CacheHelper implements ICacheHelper {
                }
        }
 
-
-
-       /**
-        * 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 {function} $computeFunction
-        * @param array $args
-        * @param string|null $key
-        */
-       public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
-               $this->getOutput()->addHTML( $this->getCachedValue( $computeFunction, $args, $key ) );
-       }
-
        /**
         * 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
@@ -317,7 +325,7 @@ class CacheHelper implements ICacheHelper {
         *
         * @param integer $cacheExpiry
         */
-       public function setExpirey( $cacheExpiry ) {
+       public function setExpiry( $cacheExpiry ) {
                $this->cacheExpiry = $cacheExpiry;
        }
 
@@ -333,14 +341,35 @@ class CacheHelper implements ICacheHelper {
                return call_user_func_array( 'wfMemcKey', $this->cacheKey );
        }
 
+       /**
+        * Sets the cache key that should be used.
+        *
+        * @since 1.20
+        *
+        * @param array $cacheKey
+        */
        public function setCacheKey( array $cacheKey ) {
                $this->cacheKey = $cacheKey;
        }
 
-       public function purge() {
+       /**
+        * 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 $handlerFunction
+        */
        public function setOnInitializedHandler( $handlerFunction ) {
                $this->onInitHandler = $handlerFunction;
        }