Difference between revisions of "Extension:ExtraMagic"

From Organic Design
m
(add CURRENTUSERREALNAME update to version 2.0.3)
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
 
  *
 
  *
Line 13: Line 13:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
   
 
   
define('EXTRAMAGIC_VERSION', '2.0.2, 2009-04-09');
+
define('EXTRAMAGIC_VERSION', '2.0.3, 2009-07-20');
 
+
 
$wgExtensionCredits['parserhook'][] = array(
 
$wgExtensionCredits['parserhook'][] = array(
 
'name'        => 'ExtraMagic',
 
'name'        => 'ExtraMagic',
Line 22: Line 22:
 
'version'    => EXTRAMAGIC_VERSION
 
'version'    => EXTRAMAGIC_VERSION
 
);
 
);
 
+
 
+
$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'DOMAIN', 'NUMBERINGOFF', 'GUID' );
+
$wgCustomVariables = array( 'CURRENTUSER', 'CURRENTUSERREALNAME', 'CURRENTLANG', 'CURRENTSKIN', 'IPADDRESS', 'DOMAIN', 'NUMBERINGOFF', 'GUID' );
 
+
 
$wgExtensionFunctions[]                    = 'efSetupExtraMagic';
 
$wgExtensionFunctions[]                    = 'efSetupExtraMagic';
 
$wgHooks['MagicWordMagicWords'][]          = 'efAddCustomVariable';
 
$wgHooks['MagicWordMagicWords'][]          = 'efAddCustomVariable';
Line 31: Line 31:
 
$wgHooks['LanguageGetMagic'][]            = 'efAddCustomVariableLang';
 
$wgHooks['LanguageGetMagic'][]            = 'efAddCustomVariableLang';
 
$wgHooks['ParserGetVariableValueSwitch'][] = 'efGetCustomVariable';
 
$wgHooks['ParserGetVariableValueSwitch'][] = 'efGetCustomVariable';
 
+
 
/**
 
/**
 
  * Called from $wgExtensionFunctions array when initialising extensions
 
  * Called from $wgExtensionFunctions array when initialising extensions
Line 39: Line 39:
 
$wgParser->setFunctionHook( 'REQUEST', 'efExtraMagicExpandRequest', SFH_NO_HASH );
 
$wgParser->setFunctionHook( 'REQUEST', 'efExtraMagicExpandRequest', SFH_NO_HASH );
 
}
 
}
 
+
 
/**
 
/**
 
  * Register magic words
 
  * Register magic words
Line 48: Line 48:
 
return true;
 
return true;
 
}
 
}
 
+
 
function efAddCustomVariableID( &$variables ) {
 
function efAddCustomVariableID( &$variables ) {
 
global $wgCustomVariables;
 
global $wgCustomVariables;
Line 54: Line 54:
 
return true;
 
return true;
 
}
 
}
 
+
 
function efAddCustomVariableLang( &$langMagic, $langCode = 0 ) {
 
function efAddCustomVariableLang( &$langMagic, $langCode = 0 ) {
 
global $wgCustomVariables;
 
global $wgCustomVariables;
 
+
 
# Magic words
 
# Magic words
 
foreach( $wgCustomVariables as $var ) {
 
foreach( $wgCustomVariables as $var ) {
Line 63: Line 63:
 
$langMagic[defined( $magic ) ? constant( $magic ) : $magic] = array( $langCode, $var );
 
$langMagic[defined( $magic ) ? constant( $magic ) : $magic] = array( $langCode, $var );
 
}
 
}
+
 
# Parser functions
 
# Parser functions
 
$langMagic['REQUEST'] = array( $langCode, 'REQUEST' );
 
$langMagic['REQUEST'] = array( $langCode, 'REQUEST' );
+
 
return true;
 
return true;
 
}
 
}
 
+
 
/**
 
/**
 
  * Expand parser functions
 
  * Expand parser functions
Line 78: Line 78:
 
return $wgRequest->getText( $param );
 
return $wgRequest->getText( $param );
 
}
 
}
 
+
 
/**
 
/**
 
  * Process variable values
 
  * Process variable values
Line 84: Line 84:
 
function efGetCustomVariable( &$parser, &$cache, &$index, &$ret ) {
 
function efGetCustomVariable( &$parser, &$cache, &$index, &$ret ) {
 
switch ( $index ) {
 
switch ( $index ) {
 
+
 
case MAG_CURRENTUSER:
 
case MAG_CURRENTUSER:
 
global $wgUser;
 
global $wgUser;
 
$parser->disableCache();
 
$parser->disableCache();
 
$ret = $wgUser->mName;
 
$ret = $wgUser->mName;
 +
break;
 +
 +
case MAG_CURRENTUSERREALNAME:
 +
global $wgUser;
 +
$parser->disableCache();
 +
$ret = $wgUser->getRealName();
 
break;
 
break;
  
Line 96: Line 102:
 
$ret = $wgUser->getOption( 'language' );
 
$ret = $wgUser->getOption( 'language' );
 
break;
 
break;
 
+
 
case MAG_CURRENTSKIN:
 
case MAG_CURRENTSKIN:
 
global $wgUser;
 
global $wgUser;
Line 102: Line 108:
 
$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_DOMAIN:
 
case MAG_DOMAIN:
 
$parser->disableCache();
 
$parser->disableCache();
 
$ret = $_SERVER['SERVER_NAME'];
 
$ret = $_SERVER['SERVER_NAME'];
 
break;
 
break;
 
+
 
case MAG_NUMBERINGOFF:
 
case MAG_NUMBERINGOFF:
 
global $wgUser;
 
global $wgUser;
Line 118: Line 124:
 
$ret = '';
 
$ret = '';
 
break;
 
break;
 
+
 
case MAG_GUID:
 
case MAG_GUID:
 
$parser->disableCache();
 
$parser->disableCache();
 
$ret = strftime( '%Y%m%d', time() ) . '-' . substr( strtoupper( uniqid('', true) ), -5 );
 
$ret = strftime( '%Y%m%d', time() ) . '-' . substr( strtoupper( uniqid('', true) ), -5 );
 
break;
 
break;
 
+
 
}
 
}
 
return true;
 
return true;
 
}
 
}

Revision as of 00:21, 20 July 2009

<?php /**

* ExtraMagic extension - Adds useful variables and parser functions
*
* 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; }