Difference between revisions of "Extension:ExtraMagic"

From Organic Design wiki
(NUMBERINGOFF)
(2.0.0 - add REQUEST parser function)
Line 1: Line 1:
 
<?php
 
<?php
# Add some useful magic words{{php}}{{Category:Extensions|ExtraMagic}}
+
/**
 +
* 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');
 
  
$wgHooks['MagicWordMagicWords'][]          = 'wfAddCustomVariable';
+
$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'NUMBERINGOFF' );
$wgHooks['MagicWordwgVariableIDs'][]      = 'wfAddCustomVariableID';
 
$wgHooks['LanguageGetMagic'][]            = 'wfAddCustomVariableLang';
 
$wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomVariable';
 
  
function wfAddCustomVariable(&$magicWords) {
+
$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 wfAddCustomVariableID(&$variables) {
+
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 wfAddCustomVariableLang(&$langMagic, $langCode = 0) {
+
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(0,$var);
+
$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 wfGetCustomVariable(&$parser,&$cache,&$index,&$ret) {
+
/**
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;
+
break;
  
 
case MAG_CURRENTLANG:
 
case MAG_CURRENTLANG:
 
global $wgUser;
 
global $wgUser;
 
$parser->disableCache();
 
$parser->disableCache();
$ret = $wgUser->getOption('language');
+
$ret = $wgUser->getOption( 'language' );
break;
+
break;
  
 
case MAG_CURRENTSKIN:
 
case MAG_CURRENTSKIN:
 
global $wgUser;
 
global $wgUser;
 
$parser->disableCache();
 
$parser->disableCache();
$ret = $wgUser->getOption('skin');
+
$ret = $wgUser->getOption( 'skin' );
break;
+
break;
  
 
case MAG_IPADDRESS:
 
case MAG_IPADDRESS:
 
$parser->disableCache();
 
$parser->disableCache();
 
$ret = $_SERVER['REMOTE_ADDR'];
 
$ret = $_SERVER['REMOTE_ADDR'];
break;
+
break;
  
 
case MAG_NUMBERINGOFF:
 
case MAG_NUMBERINGOFF:
 
global $wgUser;
 
global $wgUser;
$wgUser->setOption('numberheadings', false);
+
$wgUser->setOption( 'numberheadings', false );
 
$ret = '';
 
$ret = '';
break;
+
break;
}
+
}
 
return true;
 
return true;
}
+
}

Revision as of 23:59, 16 March 2009

<?php /**

* ExtraMagic extension - Adds useful variables and parser functions
*
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:Php

* 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; }