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): no description

(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): no description

(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 code to language (Z860) with linguonym for language in target language (Z21329)

(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), French regular plural (Z11548), etc.

(chooses from given list of declensions based on variable)

{{GRAMMAR:elative|wiki}}

(looks up the relevant declension for the given case)

Return monolingual text from grammatical features (Z19530): renvoie le texte unilingue à partir des caractéristiques grammaticales

(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)

Return monolingual text from grammatical features (Z19530) if it's encoded that way on Wikidata, else something like N-ifs (Z19601)

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

Extension:ParserFunctions

MediaWiki Function
{{#expr:}} N/A; rewrite expression as a composition of logical and arithmetic functions, such as less than (natural numbers) (Z13689) and divide rational numbers (Z19708)
{{#if:}} is string blank (Z10083): Checks if a string just contains whitespaces
{{#ifeq:}} compose If (Z802) with string equality (Z866)
{{#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) (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.

MediaWiki Function
{{#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 (Z12899): returns string composed of list elements separated by a given delimiter

Other English Wikipedia templates

MediaWiki Function
{{ordinal|321}} Natural number with English ordinal suffix (Z14531): no description
{{P1|a}} Echo (Z801): returns the argument unchanged
{{P1|a|b}} Boolean Left (Z10265), Get first item of a Typed pair (Z821), first element (Z811)
{{P2|a|b}} Boolean right (Z10298), Get second element of a Typed pair (Z822), second element (Z16360)