Help:Selection Functions

A "selection function" is a function that does a logical test and based on the answer outputs the desired code.

As with templates, selection functions may not contain that are part of a function or syntax that are not nested within the function. For example, if the function is being used for a link with the square brackets outside the function but the selection function wants to be able to handle the pipe that distinguishes between link and text. This is particularly important with selection functions as they are often used with tables. The way to overcome this is to use ! which outputs the function of the pipe, but resolves the selection command first. Alternatively, if operating on a table a user can opt to use HTML tables, however this should only be resorted to if using wikitext is being overly problematic, as members of the wiki do not otherwise have reason to learn HTML, meaning the number of people who can operate on the template would be limited.

Involvement of selection commands in the mainspace should be limited to usage in embedded templates. Selection functions main advantage comes with the different values that can be input into their parameters.

Selection functions can have their values imprinted with " " except where stated otherwise.

choose/option
Choose/option is a random selection function. It operates with two sets of tags,  and. Due to being tags they cannot be substituted.

Theoretically the way choose/option works is it sets up an array with all the potential options, then finds the length of the array (the amount of members) and finds a random number between 0 and the length-1. The option in the array at that number is then printed onto the page.

A fault of choose/option is that it renders everything inside it separate from the rest of the article. This is especially notable in how it carries parameters. This can be avoided however, through use of the  function.



With the above syntax the article is rendered like any other function, and parameters stay intact.

Although unrelated to selection, choose/option's fault can be used as an advantage. Some things do not render as expected under certain circumstances. For example, an image link featuring an external image or image URL will not work, and instead of the URL being rendered as an image, it will be rendered as text when used in the linktext portion of a link.


 * → #|http://images1.wikia.nocookie.net/__cb14/finalfantasy/images/8/89/Wiki-wordmark.png

Alternatively, if we use choose/option and only one option, the function will render what is seen in the option (the image URL) and display it, then the link will kick-in and use whatever is inside as link text.


 * #| http://images1.wikia.nocookie.net/__cb14/finalfantasy/images/8/89/Wiki-wordmark.png
 * #| http://images1.wikia.nocookie.net/__cb14/finalfantasy/images/8/89/Wiki-wordmark.png

parameter pipe
A parameter pipe is a pipe insert after a parameter name in a template parameter to give an "else" use.

For example, if we wanted to not display any value then we could do:

And if the parameter did not exist then nothing will be displayed in its place. Alternatively, if wanted to have something display if not used, such as if no class is used for it to say none, we could do:

And either the value of " " would display, or if the parameter is not present, then "None".

If using the parameter pipe, a blank value (such as " " followed by a pipe or before content) will override it and have nothing display instead. For this to be caught must be used. Also if content wanted to be changed based on the presence of a parameter then  would have to be used also. If content wanted to be changed based on the presence of a parameter but still treat a present parameter with a blank value then would have to be used.

#if
The  function is an if none/if null test. The test is done in a parameter, and if that parameter contains something then content in the second parameter is performed, and if it contains nothing then content in the third parameter is performed if it exists, and nothing otherwise.


 * &#123;
 * &#123;

Since the function tests whether the parameter is blank, for it to be of any use it must contain a function that could be nothing. This includes all other select functions, although since other select functions have their own tests it would be redundant.

What is usually put into the test parameter is a template parameter with a parameter pipe followed by nothing:. The pipe causes everything after it to display if not found, but since there is nothing it is simply blank. Therefore using this will test whether the parameter is used in a template, or if it is used then if it is blank. For example, we could test whether an other information parameter exists



If " " does not exist in the template with the parameter then it will not display, or if does exist but does not contain content follow the " " then it will also not display. If a parameter existed but presented no content, and we wanted nothing to display instead of the value of the else, we could use.

In addition to the parameter pipe, the function can be used. If a delimiter exists and contains content after it then it would return true.



If the first parameter contains a " " followed by text it will display text following the colon (and before the following colon if applicable), while if a colon doesn't appear it will display the full text.

#ifeq
The  function tests whether two given values are the same. The values are input into the first and second parameter, and if they match the third parameter is output, or if they don't a fourth parameter if used is output, otherwise nothing.





Strings are case sensitive, so wrapping both strings in or  removes case sensitivity.



This function is usually used to see if a template parameter input equals a pre-defined option. For example, the  parameter in the Gallery template only collapses if the "hide" value is input, otherwise it defaults to "show".

One potential use for the  function is testing whether a parameter exists and doing one thing if it does, and another if it does not. does this, however if a parameter is used in a template but contains a blank value then the blank value is treated as if the parameter was not in the template at all. can handle this by having an unlikely string default value.



In the above example, " " will return the second string, " " will also return the second string, while  not being included will return the first string. If " " is used then the first parameter will be returned, however it is an unlikely string to be input into the parameter so it would never normally occur.

#switch
is essentially the same as but it tests the input against multiple cases instead of just one.



The " " case is optional, and if it is left out then nothing will display if the input text does not match any of the cases.

Multiple inputs can be matched with an output by giving a parameter no " " before moving onto the next case. For example:

A "test 1" input will output "output 1", while "test 2" and "test 3" inputs will output "output 2".

As with, the function is case-sensitive. and can be used to rectify this. Also the function looks for exact matches, however using allows the function to only require matching the amount of specific characters. This prevents different suffixes or use of plural effecting the outcome, assuming that most inputs are longer than what  shortens them to. cannot test for containing a string, and instead a series if nested will have to be used.

ifstring
compares a search term to a given string and determines whether the search term is found within the string.



The third and fourth parameters display the value if found and if not found respectively.

The comparison is not case sensitive.

The comparison can be made case sensitive by adding a " " parameter and setting it to "sensitive"

Empty value parameters default to the string if found, and nothing if not found.

Entering no string outputs value if not found.

Entering no search term outputs value if not found.

ifnum
evaluates a given string and determines whether it is a valid number or mathematical expression.



The function will accept strings that are valid expressions.



Empty value parameters default to the string if valid, and nothing if invalid.

Entering no string outputs value if valid.

plural
The  function is used for deciding whether to output a string in singular form or plural form. A number is input and if that number is 1 then it displays the string in singular, and in all other circumstances it displays the string in plural.

For example:
 * → 0 Singulars
 * → 1 Singular
 * → 2 Singulars

Due to only testing whether the value is equal to 1, the function displays plural for non-numeric strings.
 * → NaN Singulars

The function does not automatically use expressions, but the  function can be used.
 * → NaN Singulars
 * → NaN Singulars

#ifexpr
tests whether a logical expression test is true or false. It can do many individual things.

The main functions for this are comparing two expressions for being less than, greater than, equal to, or not equal to each other.



Each side of the equation can still use normal expression operators such as addition and modulus.

If  has no comparison being made, but a non-0 valid expression, then the value if true will be returned.

However, if an invalid expression is used, instead of returning the value if false an error occurs.

This error can be caught with the function, however the  function allows the user to test whether an input expression is valid or invalid in simpler terms without the requirement of two functions.

does not support AND or OR logic, however they can effectively still be used using advanced logic. Each true statement is equal to 1. So basic OR logic will be like this:

An endless amount of test cases can be used, and so long as any one of them is true the output will be true.

For basic AND logic, the same method can be used, instead replacing addition with multiplication.



An endless amount of test cases can be used, and if they are all true then the output will be true.

As an applied example, a year is a leap year if it is divisible by 4, but not if it is also divisible by 100 unless it is also divisible by 400. Therefore it must be divisible by 4 AND [it must not be divisible by 100 OR it must be divisible by 400].



ifregistered
The  function checks whether a viewing user is registered and displays a string based on the outcome. There is no "test" parameter as it automatically takes the value held by the wiki in the current session.



For example, if you are logged-in:

Or if you are not:

Empty parameters default to no value. Logged-in:

Logged-out:

This can be used in conjunction with to output different results: Logged-in:

Logged-out:

Unlike most other selection functions, the value returned by  cannot be printed to the page with. This would both defeat the purpose of the function in appearing differently for each user, and is also not possible since this function is processed post-page load. can print based on the inserted username or IP address, however the username or IP address will have to be inserted manually.

ifuser
The  function detects whether the user or user talk page it is placed on is a registered username, or an anonymous editor's IP. If it is placed on a page that does not belong to a user then it will cause an error.



In this function the user can be specified with a user parameter.

For example:

This function mainly has substitution in mind, and the main purpose of this function is with the Welcome template for knowing whether to substitute the welcome for a user or the welcome for an IP.

ifgroup
The  function detects the group rights of the viewing user. The function compares these rights to a specified group or list of groups, and displays text accordingly.



The group names are dictated by the stored names. Even unregistered users carry a group. The names of the groups can be found here. "wikistaff" can be used to group rollback, sysop, and bureaucrat.



The template only detects groups, not individual rights. So if we wanted to detect each user with rollback, then we would have to input both rollback and sysop. Multiple rights can be input, and they must be comma or semi-colon separated.

Entering no value if not member returns nothing:

Entering no member values returns group display name and link if true, and nothing if false. Entering multiple just return the group "code" name (the input).

Like, this cannot be substituted. This would defeat the purpose of the template being different to the user, and it is impossible as the value is discovered post-page load.

gender
The  function allows a user to input a user's username and it then outputs the user's gender according to their user account.



Theoretical examples:
 * → male
 * → female
 * → unspecified

If the value if unspecified parameter is not set then the value if male is used for users who have an unspecified gender.
 * → male

The function cannot be used in conjunction with as the value displayed by   is processed after the output of.

#iferror
The  function checks whether it contains an error, and if it does then it displays a specified message, otherwise it displays the correct string.



The only likely source of error messages used in code will be from the  and  functions.



Another parameter can be added if a different output is wanted if the output is not an error.

For example:

Alternatively, a user could just input only the test to have a non-error test be displayed and an error to not be displayed.



For example:

#ifexist
The  function checks whether a pagename exists and then outputs values in response to this. This is an "expensive" function as it has to do a database lookup, which means it makes the page take longer to load, and the MediaWiki software logs each use and may return errors if too many are used. Due to this it should not be used in templates designed for the mainspace or repeated use in other namespaces.



For example:

Not including the second parameter causes the value if does not exist to be nothing.

The function also accepts redirects as valid pages: