Difference between revisions of "Extension:ExtraMagic"
(NUMBERINGOFF) |
(2.0.0 - add REQUEST parser function) |
||
Line 1: | Line 1: | ||
<?php | <?php | ||
− | + | /** | |
+ | * ExtraMagic extension - Adds useful variables and parser functions | ||
+ | *{{Category:Extensions|ExtraMagic}}{{php}} | ||
+ | * See http://www.organicdesign.co.nz/Extension:ExtraMagic | ||
+ | * | ||
+ | * @package MediaWiki | ||
+ | * @subpackage Extensions | ||
+ | * @author [http://www.organicdesign.co.nz/User:Nad User:Nad] | ||
+ | * @copyright © 2007 [http://www.organicdesign.co.nz/User:Nad User:Nad] | ||
+ | * @licence GNU General Public Licence 2.0 or later | ||
+ | */ | ||
+ | if (!defined('MEDIAWIKI')) die('Not an entry point.'); | ||
+ | |||
+ | define('EXTRAMAGIC_VERSION', '2.0.0, 2009-03-17'); | ||
+ | |||
+ | $wgExtensionCredits['parserhook'][] = array( | ||
+ | 'name' => 'ExtraMagic', | ||
+ | 'author' => '[http://www.organicdesign.co.nz/User:Nad User:Nad]', | ||
+ | 'description' => 'Adds useful variables and parser functions', | ||
+ | 'url' => 'http://www.mediawiki.org/wiki/Extension:RecentActivity', | ||
+ | 'version' => EXTRAMAGIC_VERSION | ||
+ | ); | ||
− | |||
− | $ | + | $wgCustomVariables = array( 'CURRENTUSER', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'NUMBERINGOFF' ); |
− | |||
− | |||
− | |||
− | function | + | $wgExtensionFunctions[] = 'efSetupExtraMagic'; |
+ | $wgHooks['MagicWordMagicWords'][] = 'efAddCustomVariable'; | ||
+ | $wgHooks['MagicWordwgVariableIDs'][] = 'efAddCustomVariableID'; | ||
+ | $wgHooks['LanguageGetMagic'][] = 'efAddCustomVariableLang'; | ||
+ | $wgHooks['ParserGetVariableValueSwitch'][] = 'efGetCustomVariable'; | ||
+ | |||
+ | /** | ||
+ | * Called from $wgExtensionFunctions array when initialising extensions | ||
+ | */ | ||
+ | function efSetupExtraMagic() { | ||
+ | global $wgParser; | ||
+ | $wgParser->setFunctionHook( 'REQUEST', 'efExtraMagicExpandRequest', SFH_NO_HASH ); | ||
+ | } | ||
+ | |||
+ | /** | ||
+ | * Register magic words | ||
+ | */ | ||
+ | function efAddCustomVariable( &$magicWords ) { | ||
global $wgCustomVariables; | global $wgCustomVariables; | ||
− | foreach($wgCustomVariables as $var) $magicWords[] = "MAG_$var"; | + | foreach( $wgCustomVariables as $var ) $magicWords[] = "MAG_$var"; |
return true; | return true; | ||
− | + | } | |
− | function | + | function efAddCustomVariableID( &$variables ) { |
global $wgCustomVariables; | global $wgCustomVariables; | ||
− | foreach($wgCustomVariables as $var) $variables[] = constant("MAG_$var"); | + | foreach( $wgCustomVariables as $var ) $variables[] = constant( "MAG_$var" ); |
return true; | return true; | ||
− | + | } | |
− | function | + | function efAddCustomVariableLang( &$langMagic, $langCode = 0 ) { |
global $wgCustomVariables; | global $wgCustomVariables; | ||
− | foreach($wgCustomVariables as $var) { | + | |
+ | # Magic words | ||
+ | foreach( $wgCustomVariables as $var ) { | ||
$magic = "MAG_$var"; | $magic = "MAG_$var"; | ||
− | $langMagic[defined($magic) ? constant($magic) : $magic] = array( | + | $langMagic[defined( $magic ) ? constant( $magic ) : $magic] = array( $langCode, $var ); |
− | + | } | |
+ | |||
+ | # Parser functions | ||
+ | $langMagic['REQUEST'] = array( $langCode, 'REQUEST' ); | ||
+ | |||
return true; | return true; | ||
− | } | + | } |
+ | |||
+ | /** | ||
+ | * Expand parser functions | ||
+ | */ | ||
+ | function efExtraMagicExpandRequest( &$parser, $param ) { | ||
+ | global $wgRequest; | ||
+ | return $wgRequest->getText( $param ); | ||
+ | } | ||
− | function | + | /** |
− | switch ($index) { | + | * Process variable values |
+ | */ | ||
+ | function efGetCustomVariable( &$parser, &$cache, &$index, &$ret ) { | ||
+ | switch ( $index ) { | ||
case MAG_CURRENTUSER: | case MAG_CURRENTUSER: | ||
Line 37: | Line 88: | ||
$parser->disableCache(); | $parser->disableCache(); | ||
$ret = $wgUser->mName; | $ret = $wgUser->mName; | ||
− | + | break; | |
case MAG_CURRENTLANG: | case MAG_CURRENTLANG: | ||
global $wgUser; | global $wgUser; | ||
$parser->disableCache(); | $parser->disableCache(); | ||
− | $ret = $wgUser->getOption('language'); | + | $ret = $wgUser->getOption( 'language' ); |
− | + | break; | |
case MAG_CURRENTSKIN: | case MAG_CURRENTSKIN: | ||
global $wgUser; | global $wgUser; | ||
$parser->disableCache(); | $parser->disableCache(); | ||
− | $ret = $wgUser->getOption('skin'); | + | $ret = $wgUser->getOption( 'skin' ); |
− | + | break; | |
case MAG_IPADDRESS: | case MAG_IPADDRESS: | ||
$parser->disableCache(); | $parser->disableCache(); | ||
$ret = $_SERVER['REMOTE_ADDR']; | $ret = $_SERVER['REMOTE_ADDR']; | ||
− | + | break; | |
case MAG_NUMBERINGOFF: | case MAG_NUMBERINGOFF: | ||
global $wgUser; | global $wgUser; | ||
− | $wgUser->setOption('numberheadings', false); | + | $wgUser->setOption( 'numberheadings', false ); |
$ret = ''; | $ret = ''; | ||
− | + | break; | |
− | + | } | |
return true; | return true; | ||
− | + | } |
Revision as of 23:59, 16 March 2009
<?php /**
* ExtraMagic extension - Adds useful variables and parser functions *
* See http://www.organicdesign.co.nz/Extension:ExtraMagic * * @package MediaWiki * @subpackage Extensions * @author User:Nad * @copyright © 2007 User:Nad * @licence GNU General Public Licence 2.0 or later */
if (!defined('MEDIAWIKI')) die('Not an entry point.');
define('EXTRAMAGIC_VERSION', '2.0.0, 2009-03-17');
$wgExtensionCredits['parserhook'][] = array( 'name' => 'ExtraMagic', 'author' => 'User:Nad', 'description' => 'Adds useful variables and parser functions', 'url' => 'http://www.mediawiki.org/wiki/Extension:RecentActivity', 'version' => EXTRAMAGIC_VERSION );
$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'NUMBERINGOFF' );
$wgExtensionFunctions[] = 'efSetupExtraMagic'; $wgHooks['MagicWordMagicWords'][] = 'efAddCustomVariable'; $wgHooks['MagicWordwgVariableIDs'][] = 'efAddCustomVariableID'; $wgHooks['LanguageGetMagic'][] = 'efAddCustomVariableLang'; $wgHooks['ParserGetVariableValueSwitch'][] = 'efGetCustomVariable';
/**
* Called from $wgExtensionFunctions array when initialising extensions */
function efSetupExtraMagic() { global $wgParser; $wgParser->setFunctionHook( 'REQUEST', 'efExtraMagicExpandRequest', SFH_NO_HASH ); }
/**
* Register magic words */
function efAddCustomVariable( &$magicWords ) { global $wgCustomVariables; foreach( $wgCustomVariables as $var ) $magicWords[] = "MAG_$var"; return true; }
function efAddCustomVariableID( &$variables ) { global $wgCustomVariables; foreach( $wgCustomVariables as $var ) $variables[] = constant( "MAG_$var" ); return true; }
function efAddCustomVariableLang( &$langMagic, $langCode = 0 ) { global $wgCustomVariables;
# Magic words foreach( $wgCustomVariables as $var ) { $magic = "MAG_$var"; $langMagic[defined( $magic ) ? constant( $magic ) : $magic] = array( $langCode, $var ); }
# Parser functions $langMagic['REQUEST'] = array( $langCode, 'REQUEST' );
return true; }
/**
* Expand parser functions */
function efExtraMagicExpandRequest( &$parser, $param ) { global $wgRequest; return $wgRequest->getText( $param ); }
/**
* Process variable values */
function efGetCustomVariable( &$parser, &$cache, &$index, &$ret ) { switch ( $index ) {
case MAG_CURRENTUSER: global $wgUser; $parser->disableCache(); $ret = $wgUser->mName; break;
case MAG_CURRENTLANG: global $wgUser; $parser->disableCache(); $ret = $wgUser->getOption( 'language' ); break;
case MAG_CURRENTSKIN: global $wgUser; $parser->disableCache(); $ret = $wgUser->getOption( 'skin' ); break;
case MAG_IPADDRESS: $parser->disableCache(); $ret = $_SERVER['REMOTE_ADDR']; break;
case MAG_NUMBERINGOFF: global $wgUser; $wgUser->setOption( 'numberheadings', false ); $ret = ; break; } return true; }