Help:Extension:ParserFunctions

The extension provides ten additional parser functions to supplement those already present in MediaWiki. All the parser functions provided by this extension take the form:
 *  

#if:

 *  

This function tests whether the first parameter is 'nonzero'. It evaluates to false if the test string is empty or contains only whitespace characters (space, newline, etc).


 *   &rarr; 
 *   &rarr; 
 *   &rarr; 
 *   &rarr; 

The test string is always interpreted as pure text, so mathematical expressions are not evaluated:


 *   &rarr; 

Either or both the return values may be ommitted:


 *   &rarr;  
 *  </tt> &rarr;  
 *  </tt> &rarr;  

#ifeq:
This parser function compares two strings and determines whether they are identically equal.


 *  </tt>

If both strings are valid numerical values, the strings are compared numerically:


 *  </tt> &rarr; 
 *  </tt> &rarr; 

Otherwise the comparison is made as text; this comparison is case sensitive:


 *  </tt> &rarr; 
 *  </tt> &rarr; 
 *  </tt> &rarr; 

#expr:
This function evaluates a mathematical expression and returns the calculated value.


 *  </tt>

The available operators are listed to the right, in order of precedence. See Help:Calculation for more details of the function of each operator. The accuracy and format of the result returned will vary depending on the operating system of the server running the wiki, and the number format of the site language.

When evaluating using boolean algebra, zero evaluates to false</tt> and any nonzero value, positive or negative, evaluates to true</tt>:


 *  </tt> &rarr; 

An empty input expression returns an empty string. Invalid expressions return one of several error messages, which can be caught using the #iferror:</tt> function:


 *  </tt> &rarr;  
 *  </tt> &rarr; 
 *  </tt> &rarr; 

#ifexpr:
This function evaluates a mathematical expression and returns one of two strings depending on the boolean value of the result:


 * <tt> </tt>

The <tt>expression</tt> input is evaluated exactly as for <tt>#expr:</tt> above, with the same operators being available. The output is then evaluated as a boolean expression. This function is equivalent to one using <tt>#ifeq:</tt> and <tt>#expr:</tt> only:


 * <tt> </tt>

An empty input expression evaluates to false:


 * <tt> </tt> &rarr; 

Either or both the return values may be ommitted:


 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr;  

#switch:
This function compares one input value against several test cases, returning an associated string if a match is found.

The <tt>default result</tt> is returned if no <tt>case</tt> string matches the <tt>comparison string</tt>. In this syntax, the default result must be the last parameter and must not contain a raw equals sign. Alternatively, the default result may be explicitly declared with a case string of "<tt>#default</tt>"; default results declared in this way may be placed anywhere within the function:


 * <tt> </tt> &rarr; 

It is possible to have 'fall through' values, where several <tt>case</tt> strings return the same <tt>result</tt> string. This minimises duplication.

Here cases 2, 3 and 4 all return <tt>result2</tt>; cases 6 and 7 both return <tt>result4</tt>

As with <tt>#ifeq:</tt>, the comparison is made numerically if both the comparison string and the case string being tested are numeric; or as case-sensitive string otherwise.

#ifexist:
This function takes an input string, interprets it as a page title, and returns one of two values depending on whether or not the page exists on the local wiki.


 * <tt> </tt>

The function evaluates to <tt>true</tt> 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 a redirect. Only pages that are redlinked evaluate to false, including if the page used to exist but has been deleted.


 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 

The function evaluates to true for system messages that have been customised, and for special pages that are defined by the software.


 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr;
 * <tt> </tt> &rarr;

<tt>#ifexist:</tt> is considered an "expensive parser function", only a 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:, and any further <tt>#ifexist:</tt> functions automatically return false, whether the target page exists or not.

If a page checks a target using <tt>#ifexist:</tt>, then that page will appear in the list for the target page. So if the code <tt> </tt> were included live on this page, /Foo will list.

#iferror:
This function takes an input string and returns one of two results; the function evaluates to <tt>true</tt> if the input string contains an HTML object with <tt>class="error"</tt>, as generated by other parser functions such as <tt>#expr:</tt>, <tt>#time:</tt> and <tt>#rel2abs:</tt>, template errors such as loops and recursions, and other 'failsoft' parser errors.


 * <tt>

One or both of the return strings can be ommitted. If the <tt>correct</tt> string is ommitted, the <tt>test string</tt> is returned if it is not erroneous. If the <tt>error</tt> string is also ommitted, an empty string is returned on an error:


 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr;  
 * <tt> </tt> &rarr;  

#time:
This parser function takes a date and/or time construct and formats it according to the syntax given. A date/time object can be specified; the default is the value of the magic word <tt> </tt> – that is, the time the page was last rendered into HTML.


 * <tt> </tt>
 * <tt> </tt>

The list of accepted formatting codes is given in the table to the right. Any character in the formatting string that is not recognised is passed through unaltered. There are also two ways to escape characters within the formatting string: The digraph <tt>xx</tt> is interpreted as a single literal "x".
 * 1) A backslash followed by a formatting character is interpreted as a single literal character
 * 2) characters enclosed in double quotes are considered literal characters, and the quotes are removed


 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr; 
 * <tt> </tt> &rarr;