Jump to content

Wikifunctions:MediaWiki parser functions

From Wikifunctions

Functions which have similar semantics to Wikitext parser functions (a.k.a. magic words) and templates used across wikis.

MediaWiki builtins

{{urlencode:x:y/z á é|QUERY}}

(produces x%3Ay%2Fz+%C3%A1+%C3%A9)

{{urlencode:x:y/z á é|WIKI}}

(produces x:y/z_%C3%A1_%C3%A9)

URI percent encode (Z10761): encodes certain characters using URI percent encoding syntax

(produces x:y/z%20%C3%A1%20%C3%A9)

{{urlencode:x:y/z á é|PATH}}

(produces x%3Ay%2Fz%20%C3%A1%20%C3%A9)

encode URI component (Z10608): Description missing

(matches)

{{anchorencode:x y z á é}}

(produces x_y_z_á_é)

{{formatnum:987654321.654321}}display natural number (Z14280): Uses the language to determine which formatting function to use.
{{formatnum:987654321.654321|NOSEP}}
{{formatnum:৯৮,৭৬,৫৪,৩২১.৬৫৪৩২১|R}}
{{#formatdate:25 dec 2009|ymd}}

(produces 2009 December 25)

{{#formatdate:dec 25,2009|dmy}}

(produces 25 December 2009)

date (enwiki) (Z20773): A date in the dmy format

(produces 25 December 2009, not localised)

{{#formatdate:2009-12-25|mdy}}

(produces December 25, 2009)

date (enwiki, mdy) (Z20782): Displays a date in the mdy format in english

(produces December 25, 2009, not localised)

{{#formatdate:2009 dec 25|ISO 8601}}

(produces 2009-12-25)

format date (YMD, hyphen-delimited, unpadded) (Z21360): Description missing

(produces 2009-12-25 but also 2009-1-1)

{{lc:DATA CENTER}}to lowercase (Z10047): Convert a string to lowercase letters
{{lcfirst:DATA center}}
{{uc:text transform}}to uppercase (Z10018): Convert a string to uppercase letters
{{ucfirst:text tRAnSFORM}}
{{padleft:xyz|5|abc}}pad string with leading characters to specified length (Z14770): add specified characters at the start until the string is of the required length
{{padright:xyz|5|abc}}string end padding (Z10108): Add string end padding
{{#language:ar|en}}

(converts code for a language to that language's name in some other language)

composition of language tag to language (Z860): Converts BCP 47 language tag to Z60/Natural language object with linguonym for language in target language (Z21329): Description missing

(convert language codes to objects, then use those to look up the name)

{{#bcp47:sr-ec}}
{{#dir:en}}
{{PLURAL:0|is|are}}

(chooses from given list of declensions based on variable)

use the relevant function English count plurality (Z11218): takes a natural number and the plural and singular cases of a count noun and returns the correct plurality, French regular plural (Z11548): returns the regular plural of a noun in French, etc.

(chooses from given list of declensions based on variable)

{{GRAMMAR:elative|wiki}}

(looks up the relevant declension for the given case)

first text of lexeme matching grammatical features (Z19530): Description missing

(declines any lexeme into the relevant form, as a string, based on the given feature)

{{GENDER:Username|male text|female text|text for unspecified}}

(chooses from given list of declensions based on some editor's gender setting)

first text of lexeme matching grammatical features (Z19530): Description missing if it's encoded that way on Wikidata, else something like N-ifs (Z19601): An if statement any amount of times, with a matching list of options, and a final default. See triple ifs (Z19565) for a clearer way to input 2 conditions, and built-in Z802 for 1 condition.

(chooses from Wikidata forms or given list of declensions based on variable)

Extension:ParserFunctions

MediaWikiFunction
{{#expr:}}N/A; rewrite expression as a composition of logical and arithmetic functions, such as less than (natural numbers) (Z13689): returns true if the first is strictly less than the second and divide rational numbers (Z19708): Description missing
{{#if:}}is string blank (Z10083): Checks if a string just contains whitespaces
{{#ifeq:}}compose If (Z802): depending on whether the condition is true will return one or the other value with string equality (Z866): True if the first string and the second string are the same
{{#ifexpr:}}N/A per {{#expr:}}
{{#switch:}}
{{#time:}}
{{#len:}}string length (Z11040): Return the length of this string
{{#pos:}}
{{#rpos:}}
{{#sub:}}
{{#count:}}count substrings (Z14450): returns the number of times a substring occurs in a string
{{#replace:}}
{{#explode:}}
{{#urldecode:}}URI percent decode (Z10774): decodes a percent-encoded input string (imperfect match)

Module:String

These are the alternate string manipulation set used on WMF wikis, where $wgPFEnableStringFunctions is disabled. They mostly cover the same functionality as the ones from ParserExtensions but with different names and/or parameter semantics.

MediaWikiFunction
{{#invoke:String|len|target_string}}string length (Z11040): Return the length of this string
{{#invoke:String|sub|target_string|start_index|end_index}}
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
{{#invoke:String|pos|target_string|index_value}}
{{#invoke:String|find|source_string|target_string|start_index|plain_flag}}
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
{{#invoke:String|rep|source|count}}Replicate string n-times (Z12624): Replicates a string n times: (e.g. f("a",5) -> "aaaaa")
{{#invoke:String|escapePattern|pattern_string}}
{{#invoke:String|count|source_str|pattern_string|plain_flag}}count substrings (Z14450): returns the number of times a substring occurs in a string
{{#invoke:String|join|separator|string1|string2|...}}join list of strings with delimiter (Z12899): returns string composed of list elements separated by a given delimiter

Other English Wikipedia templates

MediaWikiFunction
{{ordinal|321}}Natural number with English ordinal suffix (Z14531): Description missing
{{P1|a}}Echo (Z801): returns the argument unchanged
{{P1|a|b}}Boolean left (Z10265): Returns the first parameter, Get first item of a Typed pair (Z821): Description missing, first element (Z811): returns the first element of the input list (labelled “Item 1”)
{{P2|a|b}}Boolean right (Z10298): Always returns the second parameter, Get second element of a typed pair (Z822): Description missing, second element (Z16360): returns the second element from a list