Extension:MagicNoCache

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

Release status: beta

Implementation Page action
Description Disables caching for a page
Author(s) Kimon Andreou (KimonTalk)
Last version 1.1.0 (2009-10-17)
MediaWiki Tested on 1.10-1.15
License GPL
Download See below
Hooks used
MagicWordMagicWords

MagicWordwgVariableIDs
LanguageGetMagic
ParserBeforeTidy

Check usage (experimental)

This extension creates a new magic word __NOCACHE__ and disables the cache for any page in which it finds it.

The extension will attempt to disable the caching of the page by calling the disableCache() function of the Parser object and also the enableClientCache() function of the OutputPage object ($wgOut)

I wrote the extension because I was looking for a way to make sure that specific pages never get cached as they get updated extremely frequently.

[edit] Installation

  1. Copy the code below and paste it into a new file called "MagicNoCache.php"
  2. Place this file in $IP/extensions/MagicNoCache/MagicNoCache.php
  3. Append the following line to the end of your LocalSettings.php file:
    require_once( "$IP/extensions/MagicNoCache/MagicNoCache.php" );
    
Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

[edit] Source code

<?php
#Extension to allow editors to disable caching on select pages
#by using the magic word __NOCACHE__

# @addtogroup Extensions
# @author Kimon Andreou
# @copyright 2007 Kimon Andreou
# @license GPL General Public License 2.0 or later

 
#Check to see if we're being called as an extension or directly
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
#register ourselves with Special:Version
$wgExtensionCredits['parserhook'][] = array(
  'name' => 'MagicNoCache',
  'url' => 'http://www.mediawiki.org/wiki/Extension:MagicNoCache',
  'author' => 'Kimon Andreou',
  'description' => 'Adds a NOCACHE magic word to disable caching of certain pages.',
  'version' => '1.1'
);
 
#declare var to be used for hooks
$NoCache = new NoCache();
 
#register hooks
$wgHooks['MagicWordMagicWords'][] = array($NoCache, 'addMagicWord');
$wgHooks['MagicWordwgVariableIDs'][] = array($NoCache, 'addMagicWordId');
$wgHooks['LanguageGetMagic'][] = array($NoCache, 'addMagicWordLanguage');
$wgHooks['ParserBeforeTidy'][] = array($NoCache, 'checkForMagicWord');
 
#extension class
class NoCache
{
  #constructor - empty
  function NoCache() {}
 
  #register the new magic word with the system
  function addMagicWord(&$magicWords) {
    $magicWords[] = 'MAG_NOCACHE';
    return true;
  }
 
  #add the id
  function addMagicWordId(&$magicWords) {
    $magicWords[] = 'MAG_NOCACHE';
    return true;
  }
 
  #set the magic word for the various languages - English is default
  #todo: add more languages
  function addMagicWordLanguage(&$magicWords, $langCode) {
    switch($langCode) {
    default:
      $magicWords['MAG_NOCACHE'] = array(0, '__NOCACHE__');
    }
    return true;
  }
 
  #ok, check to see if we have the magic word in the article
  function checkForMagicWord(&$parser, &$text) {
    global $wgOut, $wgAction;
    $mw = MagicWord::get('MAG_NOCACHE');
 
    #If it's there, remove it and disable caching
    if (!in_array($wgAction, array('submit','edit')) && $mw->matchAndRemove($text)) {
      $parser->disableCache();
      $wgOut->enableClientCache(false);
    }
 
    return true;
  }
}

[edit] See also

Language: English  • Ελληνικά
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox