MediaWiki  REL1_24
SpecialCachedPage.php
Go to the documentation of this file.
00001 <?php
00002 
00040 abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
00048     protected $cacheHelper;
00049 
00056     protected $cacheEnabled = true;
00057 
00065     protected function afterExecute( $subPage ) {
00066         $this->saveCache();
00067 
00068         parent::afterExecute( $subPage );
00069     }
00070 
00077     public function setCacheEnabled( $cacheEnabled ) {
00078         $this->cacheHelper->setCacheEnabled( $cacheEnabled );
00079     }
00080 
00090     public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
00091         if ( !isset( $this->cacheHelper ) ) {
00092             $this->cacheHelper = new CacheHelper();
00093 
00094             $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
00095             $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
00096 
00097             $keyArgs = $this->getCacheKey();
00098 
00099             if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
00100                 unset( $keyArgs['action'] );
00101             }
00102 
00103             $this->cacheHelper->setCacheKey( $keyArgs );
00104 
00105             if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
00106                 $this->cacheHelper->rebuildOnDemand();
00107             }
00108         }
00109 
00110         $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
00111     }
00112 
00127     public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
00128         return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
00129     }
00130 
00143     public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
00144         $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue(
00145             $computeFunction,
00146             $args,
00147             $key
00148         ) );
00149     }
00150 
00157     public function saveCache() {
00158         if ( isset( $this->cacheHelper ) ) {
00159             $this->cacheHelper->saveCache();
00160         }
00161     }
00162 
00171     public function setExpiry( $cacheExpiry ) {
00172         $this->cacheHelper->setExpiry( $cacheExpiry );
00173     }
00174 
00182     protected function getCacheKey() {
00183         return array(
00184             $this->mName,
00185             $this->getLanguage()->getCode()
00186         );
00187     }
00188 
00196     public function onCacheInitialized( $hasCached ) {
00197         if ( $hasCached ) {
00198             $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
00199         }
00200     }
00201 }