https://www.mediawiki.org/wiki/Special:Code/MediaWiki/114164
Revision: 114164
Author: jeroendedauw
Date: 2012-03-19 18:40:54 +0000 (Mon, 19 Mar 2012)
Log Message:
-----------
some refactoring to allow for nicer usage in deriving classes
Modified Paths:
--------------
trunk/phase3/includes/specials/SpecialCachedPage.php
Modified: trunk/phase3/includes/specials/SpecialCachedPage.php
===================================================================
--- trunk/phase3/includes/specials/SpecialCachedPage.php 2012-03-19
18:40:22 UTC (rev 114163)
+++ trunk/phase3/includes/specials/SpecialCachedPage.php 2012-03-19
18:40:54 UTC (rev 114164)
@@ -4,11 +4,12 @@
* Abstract special page class with scaffolding for caching the HTML output.
*
* To enable the caching functionality, the cacheExpiry field should be set
- * in the constructor.
+ * before parent::execute is called (and it should be called from execute).
*
* To add HTML that should be cached, use addCachedHTML like this:
* $this->addCachedHTML( array( $this, 'displayCachedContent' ) );
*
+ * Before the first addCachedHTML call, you should call $this->startCache();
* After adding the last HTML that should be cached, call $this->saveCache();
*
* @since 1.20
@@ -25,9 +26,9 @@
* The time to live for the cache, in seconds or a unix timestamp
indicating the point of expiry.
*
* @since 1.20
- * @var integer|null
+ * @var integer
*/
- protected $cacheExpiry = null;
+ protected $cacheExpiry = 3600;
/**
* List of HTML chunks to be cached (if !hasCached) or that where
cashed (of hasCached).
@@ -49,24 +50,46 @@
protected $hasCached = null;
/**
- * Main method.
+ * If the cache is enabled or not.
*
* @since 1.20
+ * @var boolean
+ */
+ protected $cacheEnabled = true;
+
+ /**
+ * Sets if the cache should be enabled or not.
*
- * @param string|null $subPage
+ * @since 1.20
+ * @param boolean $cacheEnabled
*/
- public function execute( $subPage ) {
+ public function setCacheEnabled( $cacheEnabled ) {
+ $this->cacheEnabled = $cacheEnabled;
+ }
+
+ /**
+ * Initializes the caching.
+ * Should be called before the first time anything is added via
addCachedHTML.
+ *
+ * @since 1.20
+ *
+ * @param integer|null $cacheExpiry Sets the cache expirty, either ttl
in seconds or unix timestamp.
+ * @param boolean|null $cacheEnabled Sets if the cache should be
enabled or not.
+ */
+ public function startCache( $cacheExpiry = null, $cacheEnabled = null )
{
+ if ( !is_null( $cacheExpiry ) ) {
+ $this->cacheExpiry = $cacheExpiry;
+ }
+
+ if ( !is_null( $cacheEnabled ) ) {
+ $this->setCacheEnabled( $cacheEnabled );
+ }
+
if ( $this->getRequest()->getText( 'action' ) === 'purge' ) {
$this->hasCached = false;
}
- if ( !is_null( $this->cacheExpiry ) ) {
- $this->initCaching();
-
- if ( $this->hasCached === true ) {
- $this->getOutput()->setSubtitle(
$this->getCachedNotice( $subPage ) );
- }
- }
+ $this->initCaching();
}
/**
@@ -75,17 +98,15 @@
*
* @since 1.20
*
- * @param string|null $subPage
- *
* @return string
*/
- protected function getCachedNotice( $subPage ) {
+ protected function getCachedNotice() {
$refreshArgs = $this->getRequest()->getQueryValues();
unset( $refreshArgs['title'] );
$refreshArgs['action'] = 'purge';
$refreshLink = Linker::link(
- $this->getTitle( $subPage ),
+ $this->getTitle( $this->getTitle()->getSubpageText() ),
$this->msg( 'cachedspecial-refresh-now' )->escaped(),
array(),
$refreshArgs
@@ -114,13 +135,21 @@
*/
protected function initCaching() {
if ( is_null( $this->hasCached ) ) {
- $cachedChunks = wfGetCache( CACHE_ANYTHING )->get(
$this->getCacheKey() );
+ $cachedChunks = wfGetCache( CACHE_ANYTHING )->get(
$this->getCacheKeyString() );
$this->hasCached = is_array( $cachedChunks );
$this->cachedChunks = $this->hasCached ? $cachedChunks
: array();
+
+ $this->onCacheInitialized();
}
}
+ protected function onCacheInitialized() {
+ if ( $this->hasCached ) {
+ $this->getOutput()->setSubtitle(
$this->getCachedNotice() );
+ }
+ }
+
/**
* Add some HTML to be cached.
* This is done by providing a callback function that should
@@ -136,7 +165,7 @@
public function addCachedHTML( $callback, $args = array(), $key = null
) {
$this->initCaching();
- if ( $this->hasCached ) {
+ if ( $this->cacheEnabled && $this->hasCached ) {
$html = '';
if ( is_null( $key ) ) {
@@ -166,12 +195,14 @@
else {
$html = call_user_func_array( $callback, $args );
- if ( is_null( $key ) ) {
- $this->cachedChunks[] = $html;
+ if ( $this->cacheEnabled ) {
+ if ( is_null( $key ) ) {
+ $this->cachedChunks[] = $html;
+ }
+ else {
+ $this->cachedChunks[$key] = $html;
+ }
}
- else {
- $this->cachedChunks[$key] = $html;
- }
}
$this->getOutput()->addHTML( $html );
@@ -184,8 +215,8 @@
* @since 1.20
*/
public function saveCache() {
- if ( $this->hasCached === false && !empty( $this->cachedChunks
) ) {
- wfGetCache( CACHE_ANYTHING )->set(
$this->getCacheKey(), $this->cachedChunks, $this->cacheExpiry );
+ if ( $this->cacheEnabled && $this->hasCached === false &&
!empty( $this->cachedChunks ) ) {
+ wfGetCache( CACHE_ANYTHING )->set(
$this->getCacheKeyString(), $this->cachedChunks, $this->cacheExpiry );
}
}
@@ -208,8 +239,22 @@
*
* @return string
*/
+ protected function getCacheKeyString() {
+ return call_user_func_array( 'wfMemcKey', $this->getCacheKey()
);
+ }
+
+ /**
+ * Returns the variables used to constructed the cache key in an array.
+ *
+ * @since 1.20
+ *
+ * @return array
+ */
protected function getCacheKey() {
- return wfMemcKey( $this->mName, $this->getLanguage()->getCode()
);
+ return array(
+ $this->mName,
+ $this->getLanguage()->getCode()
+ );
}
}
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs