Extension:Variables/de-formal
Variables Release status: stable |
|
---|---|
Implementation | Parser function |
Description | Introduces parser functions for dealing with page-scoped variables. |
Author(s) |
|
Maintainer(s) | MGChecker |
Latest version | 2.5.1 (2019-07-11) |
MediaWiki | 1.29+ |
PHP | 5.5+ |
Database changes | No |
License | No license specified |
Download | README RELEASE-NOTES |
Quarterly downloads | Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin'). |
Public wikis using | Lua error in Module:Extension at line 172: bad argument #1 to 'inNamespace' (unrecognized namespace name 'skin'). |
Translate the Variables extension if it is available at translatewiki.net | |
Vagrant role | variables |
Issues | Open tasks · Report a bug |
InternalParseBeforeSanitize
hook for MediaWiki 1.35 - MediaWiki 1.38 , or missing the #var_final
parser function in MediaWiki 1.39 +, please update to the latest version (master
branch for now). See <translate> task <tvar name=1>T276627</tvar></translate> and <translate> task <tvar name=1>T250963</tvar></translate> for further information.The Variables extension allows you to define a variable on a page, use it later in that same page or included templates, change its value, possibly to a value given by an expression in terms of the old value, etc.
It is much like a template, only very lightweight and scoped to only a single page, so you can use many variables on a page without slowing down the wiki with huge numbers of templates. Combine this extension with the Erweiterung:ParserFunctions extension for best results.
Assigning a value to a variable
#vardefine
{{#vardefine:variablename | specifiedvalue }}
Assigns the value specifiedvalue to the (already existing or hereby introduced) variable variablename.
- Example:
{{#vardefine:iconwidth|25}}
makingiconwidth = 25
#vardefineecho
{{#vardefineecho:variablename | specifiedvalue }}
Works exactly as #vardefine
, but the affected value is printed.
- Example: making
iconwidth = {{#vardefineecho:iconwidth|25}}
Retrieving the value of a variable
#var
The value of the variable variablename is produced by
{{#var:variablename}}
If undefined, this produces an empty string; it does not give an error message.
It is possible to get a default 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 is much shorter and better arranged. Before version 2.0 though, the default value always was expanded. From version 2.0 on the default only gets expanded in case it is actually used.
The value can be used in parser functions, etc.
#varexists
{{#varexists:variablename }}
returns 1 if the variable is already defined. If the variable is not defined the return value is void.
It supports a second and a third parameter to replace these values.
{{#varexists:variablename | if-value | else-value }}
This is equivalent to:
{{#if: {{#varexists: variablename }} | if-value | else-value }}
but it is much shorter and better arranged. Before version 2.5 though, both were always expanded. From version 2.5 on the if and else values only get expanded when their cases are actually entered.
#var_final
Experimental function introduced in Variables 2.0. This function will output the final, last value a variable has at the end of the page rendering. Naturally, the value will be inserted after the parser went over the entire wiki markup, so this function can't be used in other functions, expecting the right value is being used. Example:
{{#var_final:variablename | defaultvalue }}
The default value will be used if the variable doesn't exist at the final page rendering stage or if its value is an empty string. The default will be expanded right where the function is used, so the parameter will be expanded, even if it won't be needed.
Examples
The Erweiterung:ParserFunctions extension must also be installed to use #expr
.
Compute x = 2*a + b
:
{{#vardefine:x|{{#expr:2*{{#var:a}}+{{#var:b}}}}}}
Add one to n:
{{#vardefine:n|{{#expr:{{#var:n}}+1}}}}
Installation
- <translate> [[<tvar name=2>Special:ExtensionDistributor/Variables/de-formal</tvar>|Download]] and move the extracted <tvar name=name>
Variables/de-formal
</tvar> folder to your <tvar name=ext>extensions/
</tvar> directory.</translate>
<translate> Developers and code contributors should install the extension [[<tvar name=git>Special:MyLanguage/Download from Git</tvar>|from Git]] instead, using:</translate>cd extensions/
git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/Variables%2Fde-formal - <translate> Add the following code at the bottom of your <tvar name=1>LocalSettings.php </tvar> file:</translate>
wfLoadExtension( 'Variables/de-formal' );
- Configure as required
- File:OOjs UI icon check-constructive.svg <translate> Done</translate> – <translate> Navigate to <tvar name=special>Special:Version</tvar> on your wiki to verify that the extension is successfully installed.</translate>
Konfiguration
This extension provides two configuration parameters:
$egVariablesAreVolatile
- Allows to disable the parser frame being marked as volatile by the extension, i.e. disable template caching.
- Default:
true;
$egVariablesDisabledFunctions
- Allows to disable specified parser functions provided by this extension.
- Default:
[];
- Example:
[ 'var_final', 'vardefineecho' ];
Compatibility
The recommended version of the Variables extension for recent MediaWiki releases is listed below. Older versions of the extensions might work as well, but aren't tested for new MediaWiki releases.
MediaWiki version | Variables version |
---|---|
1.19–1.22 | 2.1.x |
1.23–1.28 | 2.2.x |
1.29–1.34 | 2.5.x |
1.35–1.39 | 2.5.x (deprecated) |
1.40+ | unsupported (WIP) |
Alternatives
As this extension will not be enabled for wikis run by the Wikimedia Foundation (WMF), here are some alternatives:[1][2][3][4]
- If you use Variables as cache for expensive operations, you can transform the section where you need them into a template and pass the required information as template parameters instead. This will work until you reach the expansion depth limit, which can be increased if necessary.
- If you use Variables to do more complex template operations than possible with simple Erweiterung:ParserFunctions , and if you have sufficient permissions on the server, you can use Scribunto's Lua functionality instead. You may not be able to install Scribunto on shared hosting. Note that this does not add support for global Variables. However, Extension:VariablesLua adds a Scribunto interface for the Variables store.
- Don't use variables, instead duplicate the information you need as a variable. If that information is acquired by an expensive template call, performance may suffer. This will work until you reach the node count limit.
- If you require variables just for autonumbering, you could look into Extension:NumerAlpha .
Siehe auch
- Extension:PhpTags
- Extension:MyVariables - Creates new variables in the MediaWiki sense
- Extension:Loops — Provides parser functions for performing loops
- Extension:Arrays — Create an array and provide array functions (such as search, split, and sort) and set operations (such as intersect, union and diff)
- Extension:HashTables — New parser functions for handling hash tables in MediaWiki
References
- ↑ 1.0 1.1 phab:T9865
- ↑ 2.0 2.1 phab:T65324
- ↑ 3.0 3.1 phab:T113859
- ↑ 4.0 4.1 phab:T151192
File:OOjs UI icon information-progressive.svg | <translate> This extension is included in the following wiki farms/hosts and/or packages:</translate>
<translate> This is not an authoritative list.</translate> <translate> Some wiki farms/hosts and/or packages may contain this extension even if they are not listed here.</translate> <translate> Always check with your wiki farms/hosts or bundle to confirm.</translate> |
- Pages with script errors
- Pages with broken file links
- Stable extensions/de-formal
- Parser function extensions/de-formal
- Extensions with manual MediaWiki version
- Extensions with no license specified/de-formal
- Extensions in Wikimedia version control/de-formal
- All extensions/de-formal
- Extensions not in ExtensionJson
- Modifiable variables extensions/de-formal