MediaWiki  1.23.1
CachedAction Class Reference

Abstract action class with scaffolding for caching HTML and other values in a single blob. More...

Inheritance diagram for CachedAction:
Collaboration diagram for CachedAction:

Public Member Functions

 addCachedHTML ( $computeFunction, $args=array(), $key=null)
 Add some HTML to be cached. More...
 
 getCachedValue ( $computeFunction, $args=array(), $key=null)
 Get a cached value if available or compute it if not and then cache it if possible. More...
 
 onCacheInitialized ( $hasCached)
 Gets called after the cache got initialized. More...
 
 saveCache ()
 Saves the HTML to the cache in case it got recomputed. More...
 
 setCacheEnabled ( $cacheEnabled)
 Sets if the cache should be enabled or not. More...
 
 setExpiry ( $cacheExpiry)
 Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry. More...
 
 startCache ( $cacheExpiry=null, $cacheEnabled=null)
 Initializes the caching. More...
 
- Public Member Functions inherited from FormlessAction
 execute (array $data=null, $captureErrors=true)
 Execute the action silently, not giving any output. More...
 
 onSubmit ( $data)
 
 onSuccess ()
 
 onView ()
 Show something on GET request. More...
 
 show ()
 The main action entry point. More...
 
- Public Member Functions inherited from Action
 __construct (Page $page, IContextSource $context=null)
 Constructor. More...
 
 execute ()
 Execute the action in a silent fashion: do not display anything or release any errors. More...
 
 getContext ()
 Get the IContextSource in use here. More...
 
 getLang ()
 Shortcut to get the user Language being used for this instance. More...
 
 getLanguage ()
 Shortcut to get the user Language being used for this instance. More...
 
 getName ()
 Return the name of the action this object responds to. More...
 
 getOutput ()
 Get the OutputPage being used for this instance. More...
 
 getRequest ()
 Get the WebRequest being used for this instance. More...
 
 getRestriction ()
 Get the permission required to perform this action. More...
 
 getSkin ()
 Shortcut to get the Skin being used for this instance. More...
 
 getTitle ()
 Shortcut to get the Title object from the page. More...
 
 getUser ()
 Shortcut to get the User being used for this instance. More...
 
 msg ()
 Get a Message object with context set Parameters are the same as wfMessage() More...
 
 requiresUnblock ()
 Whether this action can still be executed by a blocked user. More...
 
 requiresWrite ()
 Whether this action requires the wiki not to be locked. More...
 

Protected Member Functions

 getCacheKey ()
 Returns the variables used to constructed the cache key in an array. More...
 
- Protected Member Functions inherited from FormlessAction
 getFormFields ()
 We don't want an HTMLForm. More...
 
- Protected Member Functions inherited from Action
 checkCanExecute (User $user)
 Checks if the given user (identified by an object) can perform this action. More...
 
 getDescription ()
 Returns the description that goes below the <h1> tag. More...
 
 getPageTitle ()
 Returns the name that goes in the <h1> page title. More...
 
 setHeaders ()
 Set output headers for noindexing etc. More...
 

Protected Attributes

bool $cacheEnabled = true
 If the cache is enabled or not. More...
 
CacheHelper $cacheHelper
 CacheHelper object to which we forward the non-SpecialPage specific caching work. More...
 
- Protected Attributes inherited from Action
IContextSource $context
 IContextSource if specified; otherwise we'll use the Context from the Page $context. More...
 
array $fields
 The fields used to create the HTMLForm $fields. More...
 
WikiPage Article ImagePage CategoryPage Page $page
 Page on which we're performing the action $page. More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Action
static exists ( $name)
 Check if a given action is recognised, even if it's disabled. More...
 
static factory ( $action, Page $page, IContextSource $context=null)
 Get an appropriate Action subclass for the given action. More...
 
static getActionName (IContextSource $context)
 Get the action that will be executed, not necessarily the one passed passed through the "action" request parameter. More...
 

Detailed Description

Abstract action class with scaffolding for caching HTML and other values in a single blob.

Before using any of the caching functionality, call startCache. After the last call to either getCachedValue or addCachedHTML, call saveCache.

To get a cached value or compute it, use getCachedValue like this: $this->getCachedValue( $callback );

To add HTML that should be cached, use addCachedHTML like this: $this->addCachedHTML( $callback );

The callback function is only called when needed, so do all your expensive computations here. This function should returns the HTML to be cached. It should not add anything to the PageOutput object!

Definition at line 46 of file CachedAction.php.

Member Function Documentation

◆ addCachedHTML()

CachedAction::addCachedHTML (   $computeFunction,
  $args = array(),
  $key = null 
)

Add some HTML to be cached.

This is done by providing a callback function that should return the HTML to be added. It will only be called if the item is not in the cache yet or when the cache has been invalidated.

Since
1.20
Parameters
{function}$computeFunction
array$args
string | null$key

Definition at line 133 of file CachedAction.php.

References $args, $html, and Action\getOutput().

◆ getCachedValue()

CachedAction::getCachedValue (   $computeFunction,
  $args = array(),
  $key = null 
)

Get a cached value if available or compute it if not and then cache it if possible.

The provided $computeFunction is only called when the computation needs to happen and should return a result value. $args are arguments that will be passed to the compute function when called.

Since
1.20
Parameters
{function}$computeFunction
array | mixed$args
string | null$key
Returns
mixed

Implements ICacheHelper.

Definition at line 117 of file CachedAction.php.

References $args.

◆ getCacheKey()

CachedAction::getCacheKey ( )
protected

Returns the variables used to constructed the cache key in an array.

Since
1.20
Returns
array

Definition at line 167 of file CachedAction.php.

References array(), Action\getLanguage(), Action\getName(), and page.

Referenced by startCache().

◆ onCacheInitialized()

CachedAction::onCacheInitialized (   $hasCached)

Gets called after the cache got initialized.

Since
1.20
Parameters
bool$hasCached

Definition at line 182 of file CachedAction.php.

References Action\getOutput().

◆ saveCache()

CachedAction::saveCache ( )

Saves the HTML to the cache in case it got recomputed.

Should be called after the last time anything is added via addCachedHTML.

Since
1.20

Implements ICacheHelper.

Definition at line 144 of file CachedAction.php.

◆ setCacheEnabled()

CachedAction::setCacheEnabled (   $cacheEnabled)

Sets if the cache should be enabled or not.

Since
1.20
Parameters
bool$cacheEnabled

Implements ICacheHelper.

Definition at line 69 of file CachedAction.php.

References $cacheEnabled.

◆ setExpiry()

CachedAction::setExpiry (   $cacheExpiry)

Sets the time to live for the cache, in seconds or a unix timestamp indicating the point of expiry.

Since
1.20
Parameters
int$cacheExpiry

Implements ICacheHelper.

Definition at line 156 of file CachedAction.php.

◆ startCache()

CachedAction::startCache (   $cacheExpiry = null,
  $cacheEnabled = null 
)

Initializes the caching.

Should be called before the first time anything is added via addCachedHTML.

Since
1.20
Parameters
int | null$cacheExpirySets the cache expiry, either ttl in seconds or unix timestamp.
bool | null$cacheEnabledSets if the cache should be enabled or not.

Implements ICacheHelper.

Definition at line 82 of file CachedAction.php.

References $cacheEnabled, array(), getCacheKey(), and Action\getRequest().

Member Data Documentation

◆ $cacheEnabled

bool CachedAction::$cacheEnabled = true
protected

If the cache is enabled or not.

Since
1.20

Definition at line 61 of file CachedAction.php.

Referenced by setCacheEnabled(), and startCache().

◆ $cacheHelper

CacheHelper CachedAction::$cacheHelper
protected

CacheHelper object to which we forward the non-SpecialPage specific caching work.

Initialized in startCache.

Since
1.20

Definition at line 54 of file CachedAction.php.


The documentation for this class was generated from the following file: