MediaWiki  REL1_22
CachedAction.php
Go to the documentation of this file.
00001 <?php
00046 abstract class CachedAction extends FormlessAction implements ICacheHelper {
00047 
00055     protected $cacheHelper;
00056 
00063     protected $cacheEnabled = true;
00064 
00071     public function setCacheEnabled( $cacheEnabled ) {
00072         $this->cacheHelper->setCacheEnabled( $cacheEnabled );
00073     }
00074 
00084     public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
00085         $this->cacheHelper = new CacheHelper();
00086 
00087         $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
00088         $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
00089 
00090         $keyArgs = $this->getCacheKey();
00091 
00092         if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
00093             unset( $keyArgs['action'] );
00094         }
00095 
00096         $this->cacheHelper->setCacheKey( $keyArgs );
00097 
00098         if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
00099             $this->cacheHelper->rebuildOnDemand();
00100         }
00101 
00102         $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
00103     }
00104 
00119     public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
00120         return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
00121     }
00122 
00135     public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
00136         $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
00137     }
00138 
00145     public function saveCache() {
00146         $this->cacheHelper->saveCache();
00147     }
00148 
00156     public function setExpiry( $cacheExpiry ) {
00157         $this->cacheHelper->setExpiry( $cacheExpiry );
00158     }
00159 
00167     protected function getCacheKey() {
00168         return array(
00169             get_class( $this->page ),
00170             $this->getName(),
00171             $this->getLanguage()->getCode()
00172         );
00173     }
00174 
00182     public function onCacheInitialized( $hasCached ) {
00183         if ( $hasCached ) {
00184             $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
00185         }
00186     }
00187 
00188 }