MediaWiki  REL1_22
SpecialCachedPage.php
Go to the documentation of this file.
00001 <?php
00002 
00040 abstract class SpecialCachedPage extends SpecialPage implements ICacheHelper {
00041 
00049     protected $cacheHelper;
00050 
00057     protected $cacheEnabled = true;
00058 
00066     protected function afterExecute( $subPage ) {
00067         $this->saveCache();
00068 
00069         parent::afterExecute( $subPage );
00070     }
00071 
00078     public function setCacheEnabled( $cacheEnabled ) {
00079         $this->cacheHelper->setCacheEnabled( $cacheEnabled );
00080     }
00081 
00091     public function startCache( $cacheExpiry = null, $cacheEnabled = null ) {
00092         if ( !isset( $this->cacheHelper ) ) {
00093             $this->cacheHelper = new CacheHelper();
00094 
00095             $this->cacheHelper->setCacheEnabled( $this->cacheEnabled );
00096             $this->cacheHelper->setOnInitializedHandler( array( $this, 'onCacheInitialized' ) );
00097 
00098             $keyArgs = $this->getCacheKey();
00099 
00100             if ( array_key_exists( 'action', $keyArgs ) && $keyArgs['action'] === 'purge' ) {
00101                 unset( $keyArgs['action'] );
00102             }
00103 
00104             $this->cacheHelper->setCacheKey( $keyArgs );
00105 
00106             if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
00107                 $this->cacheHelper->rebuildOnDemand();
00108             }
00109         }
00110 
00111         $this->cacheHelper->startCache( $cacheExpiry, $cacheEnabled );
00112     }
00113 
00128     public function getCachedValue( $computeFunction, $args = array(), $key = null ) {
00129         return $this->cacheHelper->getCachedValue( $computeFunction, $args, $key );
00130     }
00131 
00144     public function addCachedHTML( $computeFunction, $args = array(), $key = null ) {
00145         $this->getOutput()->addHTML( $this->cacheHelper->getCachedValue( $computeFunction, $args, $key ) );
00146     }
00147 
00154     public function saveCache() {
00155         if ( isset( $this->cacheHelper ) ) {
00156             $this->cacheHelper->saveCache();
00157         }
00158     }
00159 
00167     public function setExpiry( $cacheExpiry ) {
00168         $this->cacheHelper->setExpiry( $cacheExpiry );
00169     }
00170 
00178     protected function getCacheKey() {
00179         return array(
00180             $this->mName,
00181             $this->getLanguage()->getCode()
00182         );
00183     }
00184 
00192     public function onCacheInitialized( $hasCached ) {
00193         if ( $hasCached ) {
00194             $this->getOutput()->setSubtitle( $this->cacheHelper->getCachedNotice( $this->getContext() ) );
00195         }
00196     }
00197 }