Difference between revisions of "Extension:ExtraMagic"

From Organic Design
Jump to: navigation, search
(add CURRENTUSERREALNAME update to version 2.0.3)
m
Line 2: Line 2:
 
/**
 
/**
 
  * ExtraMagic extension - Adds useful variables and parser functions
 
  * ExtraMagic extension - Adds useful variables and parser functions
  *
+
  * {{Category:Extensions|ExtraMagic}}{{php}}
 
  * See http://www.organicdesign.co.nz/Extension:ExtraMagic.php
 
  * See http://www.organicdesign.co.nz/Extension:ExtraMagic.php
 
  *
 
  *

Revision as of 00:22, 20 July 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.php
*
* @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.3, 2009-07-20');

$wgExtensionCredits['parserhook'][] = array( 'name' => 'ExtraMagic', 'author' => 'User:Nad', 'description' => 'Adds useful variables and parser functions', 'url' => 'http://www.organicdesign.co.nz/Extension:ExtraMagic.php', 'version' => EXTRAMAGIC_VERSION );


$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTUSERREALNAME', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'DOMAIN', 'NUMBERINGOFF', 'GUID' );

$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; $parser->disableCache(); 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_CURRENTUSERREALNAME: global $wgUser; $parser->disableCache(); $ret = $wgUser->getRealName(); 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_DOMAIN: $parser->disableCache(); $ret = $_SERVER['SERVER_NAME']; break;

case MAG_NUMBERINGOFF: global $wgUser; $wgUser->setOption( 'numberheadings', false ); $ret = ; break;

case MAG_GUID: $parser->disableCache(); $ret = strftime( '%Y%m%d', time() ) . '-' . substr( strtoupper( uniqid(, true) ), -5 ); break;

} return true; }