MediaWiki  master
CachedAction.php
Go to the documentation of this file.
00001 <?php
00002 
00040 abstract class CachedAction extends FormlessAction implements ICacheHelper {
00041 
00049         protected $cacheHelper;
00050 
00057         protected $cacheEnabled = true;
00058 
00065         public function setCacheEnabled( $cacheEnabled ) {
00066                 $this->cacheHelper->setCacheEnabled( $cacheEnabled );
00067         }
00068 
00078         public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
00079                 $this->cacheHelper = new CacheHelper();
00080 
00081                 $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
00082                 $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
00083 
00084                 $keyArgs = $this->getCacheKey();
00085 
00086                 if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
00087                         unset( $keyArgs['action'] );
00088                 }
00089 
00090                 $this->cacheHelper->setCacheKey( $keyArgs );
00091 
00092                 if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
00093                         $this->cacheHelper->rebuildOnDemand();
00094                 }
00095 
00096                 $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
00097         }
00098 
00113         public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
00114                 return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
00115         }
00116 
00129         public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
00130                 $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
00131         }
00132 
00139         public function saveCache() {
00140                 $this->cacheHelper->saveCache();
00141         }
00142 
00150         public function setExpiry( $cacheExpiry ) {
00151                 $this->cacheHelper->setExpiry( $cacheExpiry );
00152         }
00153 
00161         protected function getCacheKey() {
00162                 return array(
00163                         get_class( $this->page ),
00164                         $this->getName(),
00165                         $this->getLanguage()->getCode()
00166                 );
00167         }
00168 
00176         public function onCacheInitialized( $hasCached ) {
00177                 if ( $hasCached ) {
00178                         $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
00179                 }
00180         }
00181 
00182 }