Extension:VariablesExtension
From MediaWiki.org
|
Release status: stable |
|
|---|---|
| Implementation | Parser function |
| Description | Define page-scoped variables. |
| Author(s) | Rob Adams and others |
| Last Version | 1.2.2 (March 28, 2009) |
| License | Public domain |
| Download | See below |
|
check usage (experimental) |
|
Variables is a very simple MediaWiki extension that allows you to define a variable on a page, use it later in that same page, change its value, possibly to a value given by an expression in terms of the old value, etc.
It's much like a template, only very lightweight and scoped to only a single page, so you can use many variables on a page without polluting the wiki with huge numbers of templates. Combine with the ParserFunctions extension for best results.
This dead simple extension was written by Rob Adams, and he has released it into the public domain. It was ported to MediaWiki version 1.8 by Hempel. It was ported to MediaWiki 1.10 by Rob Adams. #vardefineecho was added by Xiloynaha, #varexists and #var with an optional default value by Danwe.
Contents |
[edit] Assigning a value to a variable
[edit] #vardefine
- {{#vardefine:variablename|specifiedvalue}}
assigns the value specifiedvalue to the (already existing or hereby introduced) variable variablename.
[edit] #vardefineecho
- {{#vardefineecho:variablename|specifiedvalue}}
works exactly as #vardefine, but the affected value is printed.
[edit] Retrieving the value of a variable (#var)
The value of the variable variablename is produced by
- {{#var:variablename}}
If undefined, this produces the empty string; it does not give an error message.
It's possible to define a value for the case that the variable is undefined or void:
- {{#var:variablename |defaultvalue}}
This is equivalent to:
- {{#if: {{#var:variablename}} |{{#var:variablename}} |defaultvalue}}
but it's much shorter and better arranged.
The value can be used in parser functions, etc.
[edit] #varexists
- {{#varexists:variablename}} returns 1 if the variable is already defined (also when the value is a void string). If the variable is not defined the return value is void.
[edit] Examples
- Note that the ParserFunctions extension must also be installed to use #expr
Compute 2*a + b:
- {{#expr:2*{{#var:a}}+{{#var:b}}}}
Add one to n:
- {{#vardefine:n|{{#expr:{{#var:n}}+1}}}}
[edit] Variables and conditional parser functions
This section only applies to MediaWiki versions below 1.12.
[edit] ParserFunctions
It must be noted that everything in conditional parser functions such as #ifexpr gets executed (though only one result gets displayed), regardless of the condition, see m:ParserFunctions#Code_execution. This applies also to #vardefine. Thus:
{{#ifexpr:..|
{{#vardefine:a|b}}|
{{#vardefine:a|c}}
}}
first assigns b and then c (hence effectively just c) regardless of the condition, while
{{#vardefine:a | {{#ifexpr:..|b|c}} }}
assigns only the applicable value.
Similarly
{{#ifexpr:..| {{#vardefine:a|b}}|}}
assigns b regardless of the condition, while
{{#vardefine:a | {{#ifexpr:..|b|{{#var:a}} }} }}
conditionally assigns value b to variable a (a dummy assignment of the value of a to a is done if the condition is not fulfilled).
[edit] Control Structure Functions
Another way around the aforementioned limitation is to use the Control Structure Functions extension, which allows one to delay the parsing of wiki markup through the use of character escape sequences. So, the first above example becomes this:
{{#ifexpr:..|
\o#vardefine:a\pb\c|
\o#vardefine:a\pc\c
}}
Alternatively, the Character Escapes extension can be used to automate the escape sequences:
{{#ifexpr:..|
<esc>{{#vardefine:a|b}}</esc>|
<esc>{{#vardefine:a|c}}</esc>
}}
The Control Structure Functions extension also has loop functions that support character escape sequences. The wiki markup:
{{ #vardefine: i | 0 }}{{
#while: expr
| <esc>{{ #var: i }} < 3</esc>
| <esc>
* {{ #var: i }}{{ #vardefine: i | {{ #expr: {{ #var: i }} + 1 }} }}</esc>
}}
produces the following:
- 0
- 1
- 2
[edit] Installation
[edit] Variables.php
Copy this into a file called $mediwikipath/extensions/Variables/Variables.php:
<?php if ( !defined( 'MEDIAWIKI' ) ) { die( 'This file is a MediaWiki extension, it is not a valid entry point' ); } $wgExtensionFunctions[] = 'wfSetupVariables'; $wgExtensionCredits['parserhook'][] = array( 'name' => 'Variables', 'version' => '1.2.2', 'url' => 'http://www.mediawiki.org/wiki/Extension:VariablesExtension', 'author' => 'Rob Adams, Tom Hempel and Daniel Werner', 'description' => 'Define page-scoped variables', ); $wgHooks['LanguageGetMagic'][] = 'wfVariablesLanguageGetMagic'; class ExtVariables { var $mVariables = array(); function vardefine( &$parser, $expr = '', $value = '' ) { $this->mVariables[$expr] = $value; return ''; } function vardefineecho( &$parser, $expr = '', $value = '' ) { $this->mVariables[$expr] = $value; return $value; } function varf( &$parser, $expr = '', $defaultVal = '' ) { if ( $this->mVariables[$expr] != '' ) { return $this->mVariables[$expr]; } else { return $defaultVal; } } function varexists( &$parser, $expr = '' ) { return array_key_exists($expr, $this->mVariables); } } function wfSetupVariables() { global $wgParser, $wgExtVariables, $wgHooks; $wgExtVariables = new ExtVariables; $wgParser->setFunctionHook( 'vardefine', array( &$wgExtVariables, 'vardefine' ) ); $wgParser->setFunctionHook( 'vardefineecho', array( &$wgExtVariables, 'vardefineecho' ) ); $wgParser->setFunctionHook( 'var', array( &$wgExtVariables, 'varf' ) ); $wgParser->setFunctionHook( 'varexists', array( &$wgExtVariables, 'varexists' ) ); } function wfVariablesLanguageGetMagic( &$magicWords, $langCode = 0 ) { require_once( dirname( __FILE__ ) . '/Variables.i18n.php' ); foreach( efVariablesWords( $langCode ) as $word => $trans ) $magicWords[$word] = $trans; return true; }
[edit] Variables.i18n.php
Copy this into a file called $mediwikipath/extensions/Variables/Variables.i18n.php:
<?php /** * Get translated magic words, if available * * @param string $lang Language code * @return array */ function efVariablesWords( $lang ) { $words = array(); /** * English */ $words['en'] = array( 'var' => array( 0, 'var' ), 'vardefine' => array( 0, 'vardefine' ), 'vardefineecho' => array( 0, 'vardefineecho' ), 'varexists' => array( 0, 'varexists' ), ); # English is used as a fallback, and the English synonyms are # used if a translation has not been provided for a given word return ( $lang == 'en' || !isset( $words[$lang] ) ) ? $words['en'] : array_merge( $words['en'], $words[$lang] ); }
[edit] LocalSettings.php
Then add
require_once( "$IP/extensions/Variables/Variables.php" );
to the end of LocalSettings.php.
[edit] See also
- Extension:DynamicFunctions##arg:
- bugzilla:7865 - Enable VariablesExtension on Wikimedia wikis (status: wontfix)
- Extension:Variables - creates new variables in the MediaWiki sense.
- Extension:Control Structure Functions - ParserFunctions extension rehashed with loop functionality added.
- Extension:Loops
- Extension:ArrayExtension - create an array and provide array functions (such as search, split, and sort) and set operations (such as intersect, union and diff).