Extension:CacheManager

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
CacheManager

Release status: beta

Implementation Page action
Description Disables caching for a page
Author(s) Ludovic MOUTON (LeLorrainTalk)
Last version 1.0
MediaWiki 1.10.0+
License GPL
Download This page
Added rights

managecache

Hooks used
SkinTemplateContentActions

UnknownAction
IsFileCacheable

Check usage (experimental)

Cache Manager is an enhancement of the default file caching . The default system caches aggressively: pages are cached unconditionally even if they contain variables, extensions and other changeable output. With this extension, you can specify each page that should not be cached: it uses a special tab on top of each page to specify not to cache it.

Contents

[edit] Installation

The extension was designed for MediaWiki 1.13 but it should be compatible with later version beginning with 1.10.

[edit] Download

Cut and paste the code found below and place it in $IP/extensions/CacheManager/CacheManager.php and CacheManager.i18n.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Install

[edit] Database

To install this extension, create a new table in your mediawiki database :

CREATE TABLE `page_uncache` (
  `page_id` INT(10) UNSIGNED NOT NULL,
  PRIMARY KEY  (`page_id`)
);

[edit] Files

To install this extension, add the following to LocalSettings.php:

$wgCacheEpoch = max( $wgCacheEpoch, gmdate( 'YmdHis', @filemtime( __FILE__ ) ) );
$wgUseFileCache = true;
$wgFileCacheDirectory = "$IP/cache";
$wgShowIPinHeader = false; 
require_once("$IP/extensions/CacheManager/CacheManager.php");

[edit] Configuration

[edit] User rights

The extension introduces a new user rights managecache. You can configure the rights in LocalSettings.php:

Defaults from DeletePagePermanently.php:

$wgGroupPermissions['*']         ['managecache'] = false;
$wgGroupPermissions['user']      ['managecache'] = true;
$wgGroupPermissions['bureaucrat']['managecache'] = true;
$wgGroupPermissions['sysop']     ['managecache'] = true;

[edit] Code

<?php
/**
 * CacheManager.php
 * Version 1.0 BETA from Ludovic MOUTON (2008)
 * GPL
*/
 
if ( !defined( 'MEDIAWIKI' ) ) {
        exit( 1 );
}
 
$wgExtensionCredits['other'][] = array(
        'name' => 'CacheManager',
        'version' => '1.0',
        'author' => array('Ludovic MOUTON'),
        'description' => 'Adds a new cache tab to each page.',
        'url' => 'http://www.mediawiki.org/wiki/Extension:CacheManager',
);
 
# Default settings
$wgGroupPermissions['*']         ['managecache'] = false;
$wgGroupPermissions['user']      ['managecache'] = true;
$wgGroupPermissions['bureaucrat']['managecache'] = true;
$wgGroupPermissions['sysop']     ['managecache'] = true;
 
$wgExtensionMessagesFiles['CacheManager'] = dirname( __FILE__ ) . '/CacheManager.i18n.php';
 
$wgExtensionFunctions[] = 'wfCacheManager';
 
function wfCacheManager(){
        new wfCacheManager;
        return true;
}
 
class wfCacheManager {
        function wfCacheManager(){
                global $wgHooks, $wgMessageCache, $wgUser;
 
                wfLoadExtensionMessages( 'CacheManager');
 
                $wgMessageCache->addMessage( 'page_cache', wfMsg('tab_label_cache') );
                $wgMessageCache->addMessage( 'page_uncache', wfMsg('tab_label_uncache') );
                $wgHooks['SkinTemplateContentActions'][] = array(&$this, 'ManageTab');
                $wgHooks['UnknownAction'][] = array(&$this, 'Action');
                $wgHooks['IsFileCacheable'][] = array(&$this, 'ManageCache');
        }
 
        function ManageTab( &$content_actions ){
                global $wgRequest, $wgRequest, $wgTitle, $wgUser;
 
                if( !$wgUser->isAllowed( 'managecache' ) )
                        return false;
 
                $action = $wgRequest->getText( 'action' );
 
                # Special pages can not be deleted (special pages have no article id anyway).
         $id = $wgTitle->getArticleID();
                $ns = $wgTitle->getNamespace();
 
                if ( $id != 0 || $ns != NS_SPECIAL){
                        wfLoadExtensionMessages( 'CacheManager' );
 
                        $dbw = wfGetDB( DB_MASTER );
                        $res = $dbw->select( 'page_uncache', 'page_id', 'page_id='.$id );
                        if($dbw->fetchObject( $res ) === false)
                        {
                                $content_actions['ask_uncache'] = array(
                                        'class' => $action == 'ask_uncache' ? 'selected' : false,
                                        'text' => wfMsg( 'page_uncache' ),
                                        'href' => $wgTitle->getLocalUrl( 'action=ask_uncache' )
                                );
                        }
                        else
                        {
                                $content_actions['ask_cache'] = array(
                                        'class' => $action == 'ask_cache' ? 'selected' : false,
                                        'text' => wfMsg( 'page_cache' ),
                                        'href' => $wgTitle->getLocalUrl( 'action=ask_cache' )
                                );
                        }
                }
 
                return true;
        }
 
        function Action( $action, &$wgArticle ) {
                global $wgOut, $wgUser;
 
                wfLoadExtensionMessages( 'CacheManager' );
 
                if( !$wgUser->isAllowed( 'managecache' ) ){
                        $wgOut->permissionRequired( 'managecache' );
                        return false;
                }
 
                # Print a form to approve xxxx
         if( $action == 'ask_cache' ){
 
                        $action = $wgArticle->getTitle()->escapeLocalUrl()."?action=page_cache";          
                        $wgOut->addHTML("<form id='ask_cachemanager' method='post' action=\"$action\">
<table>
        <tr>
                <td>" . wfMsg('ask_cache') . "</td>
        </tr>
        <tr>
                <td><input type='submit' name='submit' value=\"" . wfMsg('yes') . "\" /></td>
        </tr>
</table></form>");
 
                        return false;
 
                } elseif($action == 'ask_uncache') {
 
                        $action = $wgArticle->getTitle()->escapeLocalUrl()."?action=page_uncache";                
                        $wgOut->addHTML("<form id='ask_cachemanager' method='post' action=\"$action\">
<table>
        <tr>
                <td>" . wfMsg('ask_uncache') . "</td>
        </tr>
        <tr>
                <td><input type='submit' name='submit' value=\"" . wfMsg('yes') . "\" /></td>
        </tr>
</table></form>");
 
                        return false;
                }
                elseif( $action == 'page_cache' ){
 
                        # Cache the page : delete the id of the page in the table page_uncache
                 $dbw = wfGetDB( DB_MASTER );
                        $ns  = $wgArticle->mTitle->getNamespace();
                        $t   = $wgArticle->mTitle->getDBkey();
                        $id  = $wgArticle->mTitle->getArticleID();
 
                        if ( $t == '' || $id == 0 || $ns == NS_SPECIAL ){
                                $wgOut->addHTML( wfMsgHtml('caching_impossible') );
                                return false;
                        }
 
                        if($dbw->delete('page_uncache', array('page_id'=>$id))) 
                                $wgOut->addHTML( wfMsgHtml('caching_done') );
                        else
                                $wgOut->addHTML( wfMsgHtml('caching_impossible') );
 
                        return false;
                }
                elseif( $action == 'page_uncache' ){
 
                        # Uncache the page : add the id of the page in the table page_uncache 
                 $dbw = wfGetDB( DB_MASTER );
                        $ns  = $wgArticle->mTitle->getNamespace();
                        $t   = $wgArticle->mTitle->getDBkey();
                        $id  = $wgArticle->mTitle->getArticleID();
 
                        if ( $t == '' || $id == 0 || $ns == NS_SPECIAL ){
                                $wgOut->addHTML( wfMsgHtml('uncache_impossible') );
                                return false;
                        }
 
                        if($dbw->insert('page_uncache', array('page_id' => $id)))
                                $wgOut->addHTML( wfMsgHtml('uncaching_done') );
                        else
                                $wgOut->addHTML( wfMsgHtml('uncaching_impossible') );
 
                        return false;
                }
 
                return true;
        }
 
        function ManageCache($wgArticle)
        {
                $dbw = wfGetDB( DB_MASTER );
                $id  = $wgArticle->mTitle->getArticleID();
                $res = $dbw->select( 'page_uncache', 'page_id', 'page_id='.$id );
 
                return $dbw->fetchObject( $res ) === false;
        }
}
<?php
/**
 * CacheManager.i18n.php
 * Internationalisation file
 * Version 1.0 BETA from Ludovic MOUTON (2008)
 * GPL
 */
 
$messages = array();
 
/** English */
$messages['en'] = array(
        'tab_label_cache' => 'Cache',
        'tab_label_uncache' => 'Don\'t cache',
        'ask_cache' => 'Do you want to cache this page?',
        'ask_uncache' => 'Do you want to uncache this page?',
        'yes' => 'Yes',
        'uncaching_impossible' => 'Uncaching is impossible.',
        'uncaching_done' => 'Uncaching done.',
        'caching_impossible' => 'Caching is impossible.',
        'caching_done' => 'Caching done.'
);
 
/** French */
$messages['fr'] = array(
        'tab_label_cache' => 'Cacher',
        'tab_label_uncache' => 'Ne pas cacher',
        'ask_cache' => 'Souhaitez-vous mettre en cache cette page?',
        'ask_uncache' => 'Souhaitez-vous arrêter la mise en cache de cette page?',
        'yes' => 'Oui',
        'uncaching_impossible' => 'Impossible d\'arrêter la mise en cache de la page.',
        'uncaching_done' => 'La page n\'est plus en cache.',
        'caching_impossible' => 'Impossible de mettre la page en cache.',
        'caching_done' => 'La page a été mise en cache.'
);

[edit] See also

Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox