MediaWiki  REL1_24
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         $html = $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
00137         $this->getOutput()->addHTML( $html );
00138     }
00139 
00146     public function saveCache() {
00147         $this->cacheHelper->saveCache();
00148     }
00149 
00158     public function setExpiry( $cacheExpiry ) {
00159         $this->cacheHelper->setExpiry( $cacheExpiry );
00160     }
00161 
00169     protected function getCacheKey() {
00170         return array(
00171             get_class( $this->page ),
00172             $this->getName(),
00173             $this->getLanguage()->getCode()
00174         );
00175     }
00176 
00184     public function onCacheInitialized( $hasCached ) {
00185         if ( $hasCached ) {
00186             $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
00187         }
00188     }
00189 }