Extension:MyVariables

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

Release status: stable

Implementation Example
Description Shows how to add new built in variables
Author(s) User:Nad
Last version 2.1 (2010-11-08)
MediaWiki 1.6.x and above
License GPL
Download see here

Check usage (experimental)

This extension allows the addition of new built in variables. It currently adds three variables, {{CURRENTUSER}}, {{CURRENTUSERREALNAME}} and {{LOGO}}, which serve as examples to follow when adding others for your own use.

This code is based on the method described in Manual:Magic words. To add a new magic word, add its name into the array on the first line, and add a case for it in the last function (wfGetCustomVariable) to calculate and return its value when it's requested.

Contents

[edit] Installation

Download the code and move it to your extensions directory. Then include it in your LocalSettings.php file as in the following example.

require_once( "$IP/extensions/MyVariables/MyVariables.php" );

[edit] Code

[edit] MyVariables.php

<?php
 
/**
* @file
* @ingroup Extensions
* @author Aran Dunkley [http://www.organicdesign.co.nz/nad User:Nad]
* @copyright © 2007 Aran Dunkley
* @licence GNU General Public Licence 2.0 or later
*/
 
if( !defined( 'MEDIAWIKI' ) ) die( "Not an entry point." );
 
define( 'MYVARIABLES_VERSION', "2.1, 2010-11-08" );
 
/** REGISTRATION */
$wgExtensionCredits['parserhook'][] = array(
        'path' => __FILE__,
        'name' => 'MyVariables',
        'url' => 'http://www.mediawiki.org/wiki/Extension:MyVariables',
        'author' => array( 'Aran Dunkley', '...' ),
        'description' => 'Allows the addition of new built in variables',
        'version' => MYVARIABLES_VERSION
);
 
/** EXTENSION */
$wgCustomVariables = array('CURRENTUSER','CURRENTUSERREALNAME','LOGO');
 
$wgHooks['MagicWordMagicWords'][]          = 'wfAddCustomVariable';
$wgHooks['MagicWordwgVariableIDs'][]       = 'wfAddCustomVariableID';
$wgHooks['LanguageGetMagic'][]             = 'wfAddCustomVariableLang';
$wgHooks['ParserGetVariableValueSwitch'][] = 'wfGetCustomVariable';
 
function wfAddCustomVariable(&$magicWords) {
        foreach($GLOBALS['wgCustomVariables'] as $var) $magicWords[] = "MAG_$var";
        return true;
        }
 
function wfAddCustomVariableID(&$variables) {
        foreach($GLOBALS['wgCustomVariables'] as $var) $variables[] = constant("MAG_$var");
        return true;
        }
 
function wfAddCustomVariableLang(&$langMagic, $langCode = 0) {
        foreach($GLOBALS['wgCustomVariables'] as $var) {
                $magic = "MAG_$var";
                $langMagic[defined($magic) ? constant($magic) : $magic] = array(0,$var);
                }
        return true;
        }
 
function wfGetCustomVariable(&$parser,&$cache,&$index,&$ret) {
        switch ($index) {
 
                case MAG_CURRENTUSER:
                        $parser->disableCache(); # Mark this content as uncacheable
                 $ret = $GLOBALS['wgUser']->mName;
                        break;
 
                case MAG_LOGO:
                        $ret = $GLOBALS['wgLogo'];
                        break;
 
                case MAG_CURRENTUSERREALNAME:
                        $parser->disableCache(); # Mark this content as uncacheable
                 $ret = $GLOBALS['wgUser']->mRealName;
                        break;
                }
        return true;
        }

[edit] Notes

  • This extension should not be confused with Variables, which allows the use of variables in the computer programming sense.

[edit] Testing

  • It works on all 1.6.x's
  • Works fine on 1.8.4 --Nad 05:14, 6 April 2007 (UTC)
  • Tested and working on 1.9.3 --Nad 05:08, 6 April 2007 (UTC)
  • Tested and working on 1.11.1 --DavidBiesack 16:18, 30 October 2008 (UTC)
  • Tested and working on 1.13.x --80.81.171.49 10:12, 19 November 2008 (UTC)
  • Tested and working on 1.14.0 --Subfader 20:17, 26 February 2009 (UTC)
  • Tested and working on 1.15.0 --Gregra 17:33, 6 August 2009 (UTC)
  • Tested and working on 1.16alpha --Subfader 23:20, 6 August 2009 (UTC)
  • Tested and working on 1.16.2 Leksey 16:26, 6 March 2011 (UTC)
  • Tested and working on 1.16.4 --Netsu 14:35, 19 April 2011 (UTC)
  • Tested and working on 1.16.5 --198.236.58.30 23:55, 1 June 2011 (UTC)
  • Tested and working on 1.17.0 --[[kgh]] 14:51, 23 June 2011 (UTC)
  • Tested and working on 1.18.0 --Badon (talk) 05:16, 4 March 2012 (UTC)
  • Tested and working on 1.18.1 --Ckong3309 12:24, 26 February 2012 (PST)
  • Tested and working on 1.18.2 --Dg.de (talk) 21:01, 14 April 2012 (UTC)
  • Tested, Not working on 1.19.0 (Remove "constant" at line "foreach($GLOBALS['wgCustomVariables'] as $var) $variables[] = constant("MAG_$var");" and it will work again ) --Albert Ke (talk) 18:13, 8 May 2012 (UTC)

[edit] See also

Personal tools
Namespaces

Variants
Actions
Navigation
Support
Download
Development
Communication
Print/export
Toolbox