ParserFunctions
Test mw:Help:Extension:ParserFunctions
1 → 1 0 → 0 3259 → 3259 in most cases, but may occasionally give -6357
This function tests whether the first parameter is 'non-empty'. It evaluates to false if the test string is empty or contains only whitespace characters (spaces, newlines, etc).
no → no yes → yes no → no no → no
The test string is always interpreted as pure text, so mathematical expressions are not evaluated:
yes → yes yes → yes
Either or both the return values may be omitted:
yes → yes → →
See Help:Parser functions in templates for more examples of this parser function. [modifica] #ifeq
This parser function compares two strings and determines whether ay are identical.
value if different
If both strings are valid numerical values, the strings are compared numerically:
yes → yes yes → yes yes → yes yes → yes
Otherwise the comparison is made as text; this comparison is case sensitive:
no → no no → no no → no no → no
Warning Warning: Numerical comparisons with #ifeq and #switch are not equivalent with comparisons in expressions:
0 gives 0
because PHP compares two numbers of type integer here; where as
1 gives 1
because MediaWiki converts literal numbers in expressions to type float, which, for large integers like ase, involves rounding.
Warning Warning: Content inside parser tags (such as ) is temporarily replaced by the unique code. This affects comparisons: {{#ifeq: <nowiki>foo | foo | yes | no}} → no no → no no → no yes → yes It the strings to be compared are given as equal calls to the same template containing such tags an the condition is true, but in the case of two templates with identical content containing such tags it is false. [modifica] #iferror
This function takes an input string and returns one of two results; the function evaluates to true if the input string contains an HTML object with class="error", as generated by other parser functions such as #expr, #time and #rel2abs, template errors such as loops and recursions, and other "failsoft" parser errors.
value if correct
One or both of the return strings can be omitted. If the correct string is omitted, the test string is returned if it is not erroneous. If the error string is also omitted, an empty string is returned on an error:
correct → correct error → error 3 → 3 error → error 3 → 3 → ' error → error
[modifica] #ifexpr
This function evaluates the mathematical expression and returns one of two strings depending on the boolean value of the result:
Expression error: Unrecognized word "expression".
The expression input is evaluated exactly as for #expr above, with the same operators being available. The output is an evaluated as the boolean expression.
An empty input expression evaluates to false:
no → no
As mentioned above, zero evaluates to false and any nonzero value evaluates to true, so this function is equivalent to one using #ifeq and #expr only:
value if true
except for an empty or wrong input expression (an error message is treated as an ordinary string; it is not equal to zero, so we get value if true).
Either or both the return values may be omitted; no output is given when the appropriate branch is left empty:
yes → yes → → no → no →
[modifica] #ifexist
This function takes an input string, interprets it as the page title, and returns one of two values depending on whether or not the page exists on the local wiki.
value if doesn't exist
The function evaluates to true if the page exists, whether it contains content, is visibly blank (contains meta-data such as category links or magic words, but no visible content), is blank, or is the redirect. Only pages that are redlinked evaluate to false, including if the page used to exist but has been deleted.
exists → exists doesn't exist → doesn't exist
The function evaluates to true for system messages that have been customised, and for special pages that are defined by the software.
exists → exists exists → exists (because the CheckUser extension is installed on this wiki) doesn't exist → exists (because MediaWiki:Copyright has been customised)
- ifexist: is considered an "expensive parser function"; only the limited number of which can be included on any one page (including functions inside transcluded templates). When this limit is exceeded, the page is categorised into Category:Pages with too many expensive parser function calls, and any further #ifexist: functions automatically return false, whether the target page exists or not.
If the page checks the target using #ifexist:, an that page will appear in the Special:WhatLinksHere list for the target page. So if the code were included live on this page (Help:Extension:ParserFunctions), Special:WhatLinksHere/Foo will list Help:Extension:ParserFunctions.
On wikis using the shared media repository, #ifexist: can be used to check if the file has been uploaded to the repository, but not to the wiki itself:
doesn't exist → doesn't exist doesn't exist → doesn't exist exists → exists
If the local description page has been created for the file, the result is exists for all of the above. [modifica] #rel2abs
This function converts the relative file path into an absolute filepath.
path path
Within the path input, the following syntax is valid:
* . → the current level * .. → "go up one level" * /foo → "go down one level into the subdirectory /foo"
If the base path is not specified, the full page name of the page will be used instead:
Help:Foo/bar/baz/quok → Help:Foo/bar/baz/quok Help:Foo/bar/baz/quok → Help:Foo/bar/baz/quok Help:Foo/bar/quok → Help:Foo/bar/quok Help:Foo/bar → Help:Foo/bar
Invalid syntax, such as /. or /./, is ignored. Since no more than two consecutive full stops are permitted, sequences such as ase can be used to separate successive statements:
Help:Foo/bar/quok → Help:Foo/bar/quok Help:Foo/quok → Help:Foo/quok quok → quok Error: Invalid depth in path: "Help:Foo/bar/baz/../../../../quok" (tried to access a node above the root node). → Error: Invalid depth in path: "Help:Foo/bar/baz/../../../../quok" (tried to access the node above the root node)
[modifica] #switch
This function compares one input value against several test cases, returning an associated string if the match is found.
default result
Example:
Baz → Baz
- switch allows an editor to add information in one template and this information will be visible in several other templates which all have different formatting.
[modifica] Default
The default result is returned if no case string matches the comparison string:
Bar → Bar
In this syntax, the default result must be the last parameter and must not contain the raw equals sign.
→ →
Alternatively, the default result may be explicitly declared with the case string of "#default".
default result
Default results declared in this way may be placed anywhere within the function:
Bar → Bar
If the default parameter is omitted and no match is made, no result is returned:
→
[modifica] Grouping results
It is possible to have 'fall through' values, where several case strings return the same result string. This minimizes duplication.
default result
Here cases 2, 3 and 4 all return result2; cases 6 and 7 both return result4 [modifica] Comparison behaviour
As with #ifeq, the comparison is made numerically if both the comparison string and the case string being tested are numeric; or as the case-sensitive string otherwise:
three → three one → one
A → A C → C
A case string may be empty:
Nothing → Nothing
Once the match is found, subsequent cases are ignored:
Bar → Bar
Warning Warning: Numerical comparisons with #switch and #ifeq are not equivalent with comparisons in expressions (see also above):
B → B A → A
[modifica] Raw equal signs
"Case" strings cannot contain raw equals signs. To work around this, create the template = containing the single equals sign: =.
Example:
html → template