Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Get MediaWiki
Get extensions
Tech blog
Contribute
Support
User help
FAQ
Technical manual
Support desk
Communication
Development
Developer portal
Code statistics
mediawiki.org
Community portal
Recent changes
Translate content
Random page
Village pump
Sandbox
Search
English
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Export translations
Translate
English
Language statistics
Message group statistics
Export
Tools
Tools
move to sidebar
hide
Actions
Language statistics
Message group statistics
Export
General
Upload file
Special pages
Printable version
Get shortened URL
Download QR code
<noinclude> <languages/> </noinclude><span lang="en" dir="ltr" class="mw-content-ltr">The {{ll|Extension:ParserFunctions|nsp=0}} extension provides eleven additional [[Special:MyLanguage/Parser functions|parser functions]] to supplement the "{{ll|Help:Magic words#Parser functions|magic words}}", which are already present in MediaWiki.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(It ''may'' be configured to provide additional parser functions for string handling; these string functions are documented {{ll|Extension:ParserFunctions/String functions|elsewhere}}.)</span> <span lang="en" dir="ltr" class="mw-content-ltr">All the parser functions provided by this extension take the form:</span> :<code><nowiki>{{</nowiki><span lang="en" dir="ltr" class="mw-content-ltr">'''#functionname''': ''argument 1'' | ''argument 2'' | ''argument 3'' ...</span> <nowiki>}}</nowiki></code> {{PD Help Page}} == #expr == {{for|a more in-depth manual on the finer points of how the expression evaluator works, including some additional operators not covered here|Manual:Expr parser function syntax}} {| class="wikitable" style="float:{{dir|{{pagelang}}|left|right}}; width:30%; margin-{{dir|{{pagelang}}|right|left}}:1em" |- ! {{hl1}} | ধরন ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Operators</span> |- | {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Grouping (parentheses)</span> | style="word-spacing: 0.5em; font-weight: bold;" | <code>( )</code> |- | {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Numbers</span> | <code>'''1234.5'''</code> <code>'''e'''</code> (2.718) <code>'''pi'''</code> (3.142) |- | {{hl2}} | | <span lang="en" dir="ltr" class="mw-content-ltr">binary operator</span> <code>'''e'''</code> <span lang="en" dir="ltr" class="mw-content-ltr">unary</span> <code>'''+'''</code>,<code>'''-'''</code> |- | {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Unary</span> | style="word-spacing: 0.5em; font-weight: bold;" | <code>not ceil trunc floor abs exp ln sin cos tan acos asin atan sqrt</code> |- | {{hl2}} rowspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Binary</span> | style="word-spacing: 0.5em; font-weight: bold;" | <code>^</code> |- | style="word-spacing: 0.5em; font-weight: bold;" | <code>* / div mod fmod</code> |- | style="word-spacing: 0.5em; font-weight: bold;" | <code>+ -</code> |- | {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Round</span> | style="word-spacing: 0.5em; font-weight: bold;" | <code>round</code> |- | {{hl2}} rowspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Logic</span> | style="word-spacing: 0.5em; font-weight: bold;" | <code>= != <> > < >= <=</code> |- | style="word-spacing: 0.5em; font-weight: bold;" | <code>and</code> |- | style="word-spacing: 0.5em; font-weight: bold;" | <code>or</code> |} <span lang="en" dir="ltr" class="mw-content-ltr">This function evaluates a mathematical expression and returns the calculated value.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This function is also available in {{ll|Extension:Scribunto|nsp=0}} via the <code>mw.ext.ParserFunctions.expr</code> function.</span> :<code><nowiki>{{#expr:</nowiki> ''<span lang="en" dir="ltr" class="mw-content-ltr">expression</span>'' <nowiki>}}</nowiki></code> Basic example :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 + 1 }}</syntaxhighlight> → '''{{#expr: 1 + 1 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The available operators are listed to the right, in order of precedence. See [[m:Special:MyLanguage/Help:Calculation|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. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> When evaluating using [[w:boolean algebra|boolean algebra]], zero evaluates to {{phpi|false}}, and any nonzero value, positive or negative, evaluates to {{phpi|true}}: </div> :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 and -1 }}</syntaxhighlight> → '''{{#expr: 1 and -1 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 and 0 }}</syntaxhighlight> → '''{{#expr: 1 and 0 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 or -1 }}</syntaxhighlight> → '''{{#expr: 1 or -1 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: -1 or 0 }}</syntaxhighlight> → '''{{#expr: -1 or 0 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 0 or 0 }}</syntaxhighlight> → '''{{#expr: 0 or 0 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> An empty input expression returns an empty string. Invalid expressions return one of several error messages, which can be caught using the <code>[[##iferror|#iferror]]</code> function: </div> :<syntaxhighlight lang="wikitext" inline>{{#expr: }}</syntaxhighlight> → ''' {{#expr: }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 1+ }}</syntaxhighlight> → '''{{#expr: 1+ }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 = }}</syntaxhighlight> → '''{{#expr: 1 = }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: 1 foo 2 }}</syntaxhighlight> → '''{{#expr: 1 foo 2 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The order of addition and subtraction operands before or after a number is meaningful and may be treated as a positive or negative value instead of as an operand with an erroneous input: </div> :<syntaxhighlight lang="wikitext" inline>{{#expr: +1 }}</syntaxhighlight> → '''{{#expr: +1 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: -1 }}</syntaxhighlight> → '''{{#expr: -1 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: + 1 }}</syntaxhighlight> → '''{{#expr: + 1 }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: - 1 }}</syntaxhighlight> → '''{{#expr: - 1 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Note, if using the output of magic words, you must raw-format them in order to remove commas and translate the numerals.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, <nowiki>{{NUMBEROFUSERS}}</nowiki> results in {{NUMBEROFUSERS}}, where we want {{formatnum:{{NUMBEROFUSERS}}|R}}, which can be obtained using <code><nowiki>{{</nowiki>{{ll|Help:Magic words#Formatting|formatnum}}<nowiki>:{{NUMBEROFUSERS}}|R}}</nowiki></code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is especially important in some languages, where numerals are translated.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, in Bengali, <nowiki>{{NUMBEROFUSERS}}</nowiki> produces ৩০,০৬১.</span> :<syntaxhighlight lang="wikitext" inline>{{#expr:{{NUMBEROFUSERS}}+100}}</syntaxhighlight> → ''' {{#expr:{{NUMBEROFUSERS}}+100}}''' :<syntaxhighlight lang="wikitext" inline>{{#expr:{{formatnum:{{NUMBEROFUSERS}}|R}}+100}}</syntaxhighlight> → '''{{#expr:{{formatnum:{{NUMBEROFUSERS}}|R}}+100}}''' {{Warning|1=<span lang="en" dir="ltr" class="mw-content-ltr">The operator <code>mod</code> gives wrong results for some values of the second argument:</span> <!-- the following line now produces the correct value: :<syntaxhighlight lang="wikitext" inline>{{#expr: 123 mod (2^32-1)}}</syntaxhighlight> → '''{{#expr: 123 mod (2^32-1)}}''' (should be 123)<br /> but see this one: --> :<syntaxhighlight lang="wikitext" inline>{{#expr: 123 mod (2^64-1)}}</syntaxhighlight> → {{#expr: 123 mod (2^64-1)}} <span lang="en" dir="ltr" class="mw-content-ltr">(produces an empty string; should be 123)</span><br /> <!-- and the following bug has now been solved, as shown on the linked bug report: Depending on the specification and configuration of the server running the wiki, there may also be other errors: :<syntaxhighlight lang="wikitext" inline>{{#expr: 20060618093259 mod 10000}}</syntaxhighlight> → '''{{#expr: 20060618093259 mod 10000}}''' in most cases, but may occasionally give '''-6357'''. See {{bugzilla|6356}}. --> }} {{tip|1=<span lang="en" dir="ltr" class="mw-content-ltr">If you want to do calculations based on dates (ex. test whether current date and time is after some other date and time), first convert the time to number of seconds after January 1, 1970 (UTC) using <nowiki>{{#time: xNU }}</nowiki>, then you can simply add and subtract dates as numbers.</span>}} <div lang="en" dir="ltr" class="mw-content-ltr"> === Rounding === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> [https://php.net/function.round Rounds] off the number on the left to a multiple of 1/10 raised to a power, with the exponent equal to the truncated value of the number given on the right. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> To round up or down use unary <code>ceil</code> or <code>floor</code> respectively. </div> {| class="wikitable" ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Test case</span> ! {{hl1}} | ফলাফল ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Method of rounding</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1/3 round 5 }}</code> || {{#expr: 1/3 round 5 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Final digit is < 5, so no apparent rounding occurs</span> (0.333333… → 0.33333) |- | <code style="white-space:nowrap;"><nowiki>{{#expr: 1/6 round 5 }}</nowiki></code> || {{#expr: 1/6 round 5 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Final digit is ≥ 5, so it is rounded up</span> (0.166666… → 0.16667) |- | <code style="white-space:nowrap;"><nowiki>{{#expr: 8.99999/9 round 5 }}</nowiki></code> || {{#expr: 8.99999/9 round 5 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Again, the result is rounded up on the last digit, which results in additional rounding</span> (0.999998… → 1.00000 → 1) |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1234.5678 '''round -2''' }}</code> || {{#expr: 1234.567 round -2 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Rounded to nearest 100 because negative values round to the left of the decimal point</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1234.5678 '''round 2''' }}</code> || {{#expr: 1234.567 round 2 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Rounded to nearest 100th because positive values round to the right of the decimal point</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1234.5678 round 2'''.3''' }}</code> || {{#expr: 1234.567 round 2.3 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Decimals in the rounding index make no difference in the rounded result</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''trunc''' 1234.5678 }}</code> || {{#expr: trunc 1234.567 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Decimal portion truncated (chopped off)</span> |- ! colspan=3 {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Rounding to the nearest integer</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1/3 '''round 0''' }}</code> || {{#expr: 1/3 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Down to the ''nearest'' integer, which is zero</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1/2 '''round 0''' }}</code> || {{#expr: 1/2 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Up to the nearest integer, which is one</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 3/4 '''round 0''' }}</code> || {{#expr: 3/4 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Up to the nearest integer, which is one</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> -1/3 '''round 0''' }}</code> || {{#expr: -1/3 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Up to the nearest integer, which is zero</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> -1/2 '''round 0''' }}</code> || {{#expr: -1/2 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Down to the nearest integer, which is negative one</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> -3/4 '''round 0''' }}</code> || {{#expr: -3/4 round 0 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Down to the nearest integer, which is negative one</span> |- ! colspan=3 {{hl2}} | <span lang="en" dir="ltr" class="mw-content-ltr">Rounding up or down with ''ceil'' and ''floor''</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''ceil('''1/3''')''' }}</code> || {{#expr: ceil(1/3) }} || <span lang="en" dir="ltr" class="mw-content-ltr">Up to the next ''larger'' integer, which is one</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''floor('''1/3''')''' }}</code> || {{#expr: floor(1/3) }} || <span lang="en" dir="ltr" class="mw-content-ltr">Down to the next ''smaller'' integer, which is zero</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''ceil('''-1/3''')''' }}</code> || {{#expr: ceil(-1/3) }} || <span lang="en" dir="ltr" class="mw-content-ltr">Up to the next larger integer, which is zero</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''floor('''-1/3''')''' }}</code> || {{#expr: floor(-1/3) }} || <span lang="en" dir="ltr" class="mw-content-ltr">Down to the next smaller integer, which is negative one</span> |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> '''ceil''' 1/3 }}</code> || {{#expr: ceil 1/3 }} || <span lang="en" dir="ltr" class="mw-content-ltr">Not rounded, since 1 already is an integer</span><br />{{Warning|1=<span lang="en" dir="ltr" class="mw-content-ltr">Interpreted as '''(ceil''' 1''')'''/3, not '''ceil('''1/3''')''', as you might expect</span>}} |- ! colspan=3 {{hl2}} | Rounding large numbers |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1e-92 round 400 }}</code> || {{#expr:1e-92 round 400}} || Rounding to a very large number leads to infinity. Hence, the original value without the infinity is given as the answer. |- | <code style="white-space:nowrap;"><nowiki>{{#expr:</nowiki> 1e108 round 200 }}</code> || {{#expr:1e108 round 200}} || Same as above. |} <div lang="en" dir="ltr" class="mw-content-ltr"> === Strings === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Expressions only work with number-like values, they cannot compare strings or characters. [[#ifeq]] can be used instead. </div> :<syntaxhighlight lang="wikitext" inline>{{#expr: "a" = "a" }}</syntaxhighlight> → '''{{#expr: "a" = "a" }}''' :<syntaxhighlight lang="wikitext" inline>{{#expr: a = a }}</syntaxhighlight> → '''{{#expr: a = a }}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: a | a | 1 | 0 }}</syntaxhighlight> → '''{{#ifeq: a | a | 1 | 0 }}''' == #if == <div lang="en" dir="ltr" class="mw-content-ltr"> This function evaluates a test string and determines whether or not it is empty. A test string containing only white space is considered to be empty. </div> :<syntaxhighlight lang="wikitext" inline>{{#if: test string | value if test string is not empty | value if test string is empty (or only white space)}}</syntaxhighlight> :<syntaxhighlight lang="wikitext" inline>{{#if: first parameter | second parameter | third parameter }}</syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> This function first tests whether the first parameter is not empty. If the first parameter is not empty, the function displays the second argument. If the first parameter is empty or contains only whitespace characters (spaces, newlines, etc.) it displays the third argument. </div> :<syntaxhighlight lang="wikitext" inline>{{#if: | yes | no}}</syntaxhighlight> → '''{{#if: | yes | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#if: string | yes | no}}</syntaxhighlight> → '''{{#if: string | yes | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#if: | yes | no}}</syntaxhighlight> → '''{{#if: | yes | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#if: | yes | no}}</syntaxhighlight> → '''{{#if: | yes | no}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The test string is always interpreted as pure text, so mathematical expressions are not evaluated (see [[##ifexpr|#ifexpr]] for that): </div> :<syntaxhighlight lang="wikitext" inline>{{#if: 1==2 | yes | no }}</syntaxhighlight> → '''{{#if: 1==2 | yes | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#if: 0 | yes | no }}</syntaxhighlight> → '''{{#if: 0 | yes | no }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The last parameter (false) may be omitted: </div> :<syntaxhighlight lang="wikitext" inline>{{#if: foo | yes }}</syntaxhighlight> → ''' {{#if: foo | yes }}''' :<syntaxhighlight lang="wikitext" inline>{{#if: | yes }}</syntaxhighlight> → ''' {{#if: | yes }}''' :<syntaxhighlight lang="wikitext" inline>{{#if: foo | | no}}</syntaxhighlight> → ''' {{#if: foo | | no}}''' <span lang="en" dir="ltr" class="mw-content-ltr">The function may be nested. To do so, nest the inner <code>#if</code> function in its full form in place of a parameter of the enclosing <code>#if</code> function. Up to seven levels of nesting is possible, although that may depend on the wiki or a memory limit.</span> <!-- Here's a nested #if that returns "wowzers" if <nowiki>{{{1}}}</nowiki> is supplied, and "zowzers" if <nowiki>{{{2}}}</nowiki> is supplied (hence "wowzers zowzers" if both are supplied). In this example, the code is indented; unindented markup for nested parserfunctions quickly becomes unreadable. Note that a HTML comment is used here, which allows a line break in the source code without including it in the output. --> <syntaxhighlight lang="wikitext"> {{#if:test string |value if test string is not empty |{{#if:test string |value if test string is not empty |value if test string is empty (or only white space) }} }} </syntaxhighlight> {{#if:test string |value if test string is not empty |{{#if:test string |value if test string is not empty |value if test string is empty (or only white space) }} }} <span lang="en" dir="ltr" class="mw-content-ltr">You can also use a parameter as the test string in your <code>#if</code> statement. You need to ensure you add the <code>|</code> (pipe symbol) after the name of the variable.</span> <span lang="en" dir="ltr" class="mw-content-ltr">(So that if the parameter does not have a value, it evaluates to an empty string instead of the string "<code><nowiki>{{{1}}}</nowiki></code>".)</span> :{{tmpl|0=<code class="mw-highlight"><!-- -->{{((}}#if:{{#tag:span|{{(((}}|class=nb}}{{#tag:span|1|class=cp}}{{red|{{!}}}}{{#tag:span|{{)))}}|class=nb}}{{!}}$1{{!}}$2{{))}}<!-- --></code> |1=<span lang="en" dir="ltr" class="mw-content-ltr">You entered text in variable 1</span> |2=<span lang="en" dir="ltr" class="mw-content-ltr">There is no text in variable 1</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> See {{ll|Help:Parser functions in templates}} for more examples of this parser function. </div> {{Anchor|ifeq|IFEQ|Ifeq|IfEq|If equal}} == #ifeq == <span lang="en" dir="ltr" class="mw-content-ltr">This parser function compares two input strings, determines whether they are identical, and returns one of two strings based on the result.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If more comparisons and output strings are required, consider using <code>#switch</code>.</span> :<code><nowiki>{{#ifeq:</nowiki> ''string 1'' <nowiki>|</nowiki> ''string 2'' <nowiki>|</nowiki> ''value if identical'' <nowiki>|</nowiki> ''value if different'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> If both strings are valid numerical values, the strings are compared numerically: </div> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: 01 | 1 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: 01 | 1 | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: 0 | -0 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: 0 | -0 | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: 1e3 | 1000 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: 1e3 | 1000 | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: {{#expr:10^3}} | 1000 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: {{#expr:10^3}} | 1000 | equal | not equal}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> Otherwise, the comparison is made as text; this comparison is case-sensitive: </div> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: foo | bar | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: foo | bar | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: foo | Foo | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: foo | Foo | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: "01" | "1" | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: "01" | "1" | equal | not equal}}''' ''<span lang="en" dir="ltr" class="mw-content-ltr">(compare to similar example above, without the quotes)</span>'' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: 10^3 | 1000 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: 10^3 | 1000 | equal | not equal}}''' ''<span lang="en" dir="ltr" class="mw-content-ltr">(compare to similar example above, with <code>[[##expr|#expr]]</code> returning a valid number first)</span>'' <span lang="en" dir="ltr" class="mw-content-ltr">As a practical example, consider an existing {{ll|Help:Templates|template}} <code>Template:Timer</code> using the parser to choose between two standard times, short and long.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It takes the parameter as the first input to compare against the string "short" – there is no convention for the order, but it is simpler to read if the parameter goes first.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The template code is defined as:</span> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: {{{1|}}} | short | 20 | 40 }}</syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> the following ensue: </div> :<syntaxhighlight lang="wikitext" inline>{{timer|short}}</syntaxhighlight> → '''20''' :<syntaxhighlight lang="wikitext" inline>{{timer|20}}</syntaxhighlight> → '''40''' :<syntaxhighlight lang="wikitext" inline>{{timer}}</syntaxhighlight> → '''40''' {{Warning|1=<span lang="en" dir="ltr" class="mw-content-ltr">When used inside a parser function, any parser tags and other parser functions must be temporarily replaced with {{ll|Strip marker|a unique code}}. This affects comparisons:</span> :<code><nowiki>{{#ifeq: <nowiki>foo</nowiki> | <nowiki>foo</nowiki> | equal | not equal}}</nowiki></code> → '''{{#ifeq: <nowiki>foo</nowiki> | <nowiki>foo</nowiki> | equal | not equal}} ''' :<code><nowiki>{{#ifeq: <math>foo</math> | <math>foo</math> | equal | not equal}}</nowiki></code> → '''{{#ifeq: <math>foo</math> | <math>foo</math> | equal | not equal}} ''' :<code><nowiki>{{#ifeq: {{</nowiki>[[Help:Magic words#Miscellaneous|#tag:]]<nowiki>math|foo}} | {{#tag:math|foo}} | equal | not equal}}</nowiki></code> → '''{{#ifeq: {{#tag:math|foo}} | {{#tag:math|foo}} | equal | not equal}} ''' :<syntaxhighlight lang="wikitext" inline>{{#ifeq: [[foo]] | [[foo]] | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: [[foo]] | [[foo]] | equal | not equal}} ''' <div lang="en" dir="ltr" class="mw-content-ltr"> If the strings to be compared are given as equal calls to the same {{ll|Help:Templates|template}} containing such tags, then the condition is true, but in the case of two templates with identical content containing such tags it is false. </div> }} {{Warning|1=<span lang="en" dir="ltr" class="mw-content-ltr">Literal comparisons to {{ll|Help:Magic words#Page names|page-name magic words}} may fail depending on site configuration.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, <nowiki>{{FULLPAGENAME}}</nowiki>, depending on wiki, may capitalize the first letter, and will replace all underscores with spaces.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> To work around this, apply the magic word to both parameters: </div> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: {{FULLPAGENAME: L'Aquila}} | {{FULLPAGENAME}} | equal | not equal}}</syntaxhighlight> → '''equal''' }} == #iferror == <div lang="en" dir="ltr" class="mw-content-ltr"> This function takes an input string and returns one of two results; the function evaluates to <code>true</code> if the input string contains an HTML object with <code>class="error"</code>, as generated by other parser functions such as <code>[[##expr|#expr]]</code>, <code>[[##time|#time]]</code> and <code>[[##rel2abs|#rel2abs]]</code>, [[Special:MyLanguage/Help:Templates|template]] errors such as loops and recursions, and other "failsoft" parser errors. </div> :<code><nowiki>{{#iferror:</nowiki> ''test string'' <nowiki>|</nowiki> ''value if error'' <nowiki>|</nowiki> ''value if correct'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> One or both of the return strings can be omitted. If the <code>correct</code> string is omitted, the <code>test string</code> is returned if it is not erroneous. If the <code>error</code> string is also omitted, an empty string is returned on an error: </div> :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + 2 }} | error | correct }}</syntaxhighlight> → '''{{#iferror: {{#expr: 1 + 2 }} | error | correct }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + X }} | error | correct }}</syntaxhighlight> → '''{{#iferror: {{#expr: 1 + X }} | error | correct }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + 2 }} | error }}</syntaxhighlight> → '''{{#iferror: {{#expr: 1 + 2 }} | error }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + X }} | error }}</syntaxhighlight> → '''{{#iferror: {{#expr: 1 + X }} | error }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + 2 }} }}</syntaxhighlight> → '''{{#iferror: {{#expr: 1 + 2 }} }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: 1 + X }} }}</syntaxhighlight> → '''‌{{#iferror: {{#expr: 1 + X }} }}'''<!-- maybe the zwnj protects the trailing space before no output --> :<syntaxhighlight lang="wikitext" inline>{{#iferror: {{#expr: . }} | error | correct }}</syntaxhighlight> → '''{{#iferror: {{#expr: . }} | error | correct }}''' :<syntaxhighlight lang="wikitext" inline>{{#iferror: <strong class="error">a</strong> | error | correct }}</syntaxhighlight> → '''{{#iferror: <strong class="error">a</strong> | error | correct }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Some errors may cause a tracking category to be added, using <code><nowiki>{{#iferror:}}</nowiki></code> will not suppress the addition of the category.</span> == #ifexpr == <div lang="en" dir="ltr" class="mw-content-ltr"> This function evaluates a mathematical expression and returns one of two strings depending on the boolean value of the result: </div> :<code><nowiki>{{#ifexpr:</nowiki> ''expression'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> The <code>expression</code> input is evaluated exactly as for <code>[[##expr|#expr]]</code> above, with the same operators being available. The output is then evaluated as a boolean expression. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> An empty input expression evaluates to {{phpi|false}}: </div> :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: | yes | no}}</syntaxhighlight> → '''{{#ifexpr: | yes | no}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> As mentioned above, zero evaluates to {{phpi|false}} and any nonzero value evaluates to {{phpi|true}}, so this function is equivalent to one using <code>[[##ifeq|#ifeq]]</code> and <code>[[##expr|#expr]]</code> only: </div> :<code><nowiki>{{#ifeq: {{#expr: </nowiki>''expression''<nowiki> }} | 0 | </nowiki> ''value if false'' <nowiki>|</nowiki> ''value if true'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> except for an empty or wrong input expression (an error message is treated as an empty string; it is not equal to zero, so we get <code>value if true</code>). </div> :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: = | yes | no }}</syntaxhighlight> → ''' {{#ifexpr: = | yes | no }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> comparing </div> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: {{#expr: = }} | 0 | no | yes }}</syntaxhighlight> → ''' {{#ifeq: {{#expr: = }} | 0 | no | yes }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> Either or both of the return values may be omitted; no output is given when the appropriate branch is left empty: </div> :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1 > 0 | yes }}</syntaxhighlight> → '''{{#ifexpr: 1 > 0 | yes }}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1 < 0 | yes }}</syntaxhighlight> → ''' {{#ifexpr: 1 < 0 | yes }}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 0 = 0 | yes }}</syntaxhighlight> → ''' {{#ifexpr: 0 = 0 | yes }}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1 > 0 | | no}}</syntaxhighlight> → ''' {{#ifexpr: 1 > 0 | | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1 < 0 | | no}}</syntaxhighlight> → ''' {{#ifexpr: 1 < 0 | | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 1 > 0 }}</syntaxhighlight> → ''' {{#ifexpr: 1 > 0 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Boolean operators of equality or inequality operators are supported.</span> :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 0 = 0 or 1 = 0 | yes}}</syntaxhighlight> → '''{{#ifexpr: 0 = 0 or 1 = 0 | yes}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 0 = 0 and 1 = 0 | | no}}</syntaxhighlight> → '''{{#ifexpr: 0 = 0 and 1 = 0 | | no}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 2 > 0 or 1 < 0 | yes}}</syntaxhighlight> → '''{{#ifexpr: 2 > 0 or 1 < 0 | yes}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 2 > 0 and 1 > 0 | yes | no}}</syntaxhighlight> → '''{{#ifexpr: 2 > 0 and 1 > 0 | yes | no}}''' {{Warning|1=<span lang="en" dir="ltr" class="mw-content-ltr">The results of numerical comparisons with <code>[[##ifexpr|#ifexpr]]</code> do not always match those of <code>[[##ifeq|#ifeq]]</code> and <code>[[##switch|#switch]]</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">These latter two are more accurate than <code>[[##ifexpr|#ifexpr]]</code>, and so may not return equivalent results.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Consider these comparisons with the final digit changed:</span> :<syntaxhighlight lang="wikitext" inline>{{#ifeq: 12345678901234567 | 12345678901234568 | equal | not equal}}</syntaxhighlight> → '''{{#ifeq: 12345678901234567 | 12345678901234568 | equal | not equal}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: 12345678901234567 | 12345678901234568 = equal | not equal}}</syntaxhighlight> → '''{{#switch: 12345678901234567 | 12345678901234568 = equal | not equal}}''' <span lang="en" dir="ltr" class="mw-content-ltr">Because PHP used in <code>[[##ifeq|#ifeq]]</code> and <code>[[##switch|#switch]]</code> compares two numbers of type integer, it returns the expected result correctly.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Whereas with <code>[[##ifexpr|#ifexpr]]</code> and the same numbers:</span> :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 12345678901234567 = 12345678901234568 | equal | not equal}}</syntaxhighlight> → '''{{#ifexpr: 12345678901234567 = 12345678901234568 | equal | not equal}}''' <span lang="en" dir="ltr" class="mw-content-ltr">With the different digit, the result of equal is actually incorrect.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This behaviour in <code>[[##ifexpr|#ifexpr]]</code> is caused because MediaWiki converts literal numbers in expressions to type float, which, for large integers like these, involves rounding.</span><br /><br /> }} {{Anchor|ifexist|Ifexist|ifExist|IfExist|IFexist|IFEXIST}} == #ifexist == {{hatnote|See [[Manual:Checking for page existence]] for other methods of checking if a page exists with different limitations}} <div lang="en" dir="ltr" class="mw-content-ltr"> 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. </div> :<code><nowiki>{{#ifexist:</nowiki> ''page title'' | ''value if exists'' | ''value if doesn't exist'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> The function evaluates to {{phpi|true}} if the page exists, whether it contains content, is visibly blank (contains meta-data such as category links or {{ll|Help:Magic words|magic words}}, but no visible content), is blank, or is a {{ll|Help:Redirects|redirect}}. Only pages that are redlinked evaluate to {{phpi|false}}, including if the page used to exist but has been deleted. </div> :<code><nowiki>{{#ifexist:</nowiki> {{FULLPAGENAME}} | exists | doesn't exist <nowiki>}}</nowiki></code> → '''{{#ifexist: {{FULLPAGENAME}} | exists | doesn't exist }}''' :<code><nowiki>{{#ifexist:</nowiki> XX{{FULLPAGENAME}}XX | exists | doesn't exist <nowiki>}}</nowiki></code> → '''{{#ifexist: XX{{FULLPAGENAME}}XX | exists | doesn't exist }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The function evaluates to {{phpi|true}} for {{ll|Help:System message|system messages}} that have been customized, and for {{ll|Help:Special pages|special pages}} that are defined by the software. </div> :<code><nowiki>{{#ifexist:</nowiki> {{#special:Watchlist}} | exists | doesn't exist <nowiki>}}</nowiki></code> → '''{{#ifexist: Special:Watchlist | exists | doesn't exist }}''' :<code><nowiki>{{#ifexist:</nowiki> {{#special:CheckUser}} | exists | doesn't exist <nowiki>}}</nowiki></code> → {{#ifexist: Special:Checkuser | '''exists''' <span lang="en" dir="ltr" class="mw-content-ltr">(because the {{ll|Extension:Checkuser|nsp=0}} extension is installed on this wiki)</span> | '''doesn't exist''' <span lang="en" dir="ltr" class="mw-content-ltr">(because the {{ll|Extension:Checkuser|nsp=0}} extension is not installed on this wiki)</span> }} :<syntaxhighlight lang="wikitext" inline>{{#ifexist: MediaWiki:Copyright | exists | doesn't exist }}</syntaxhighlight> → {{#ifexist: MediaWiki:Copyright | '''exists''' <span lang="en" dir="ltr" class="mw-content-ltr">(because [[{{ns:8}}:Copyright]] has been customized)</span> | '''doesn't exist''' <span lang="en" dir="ltr" class="mw-content-ltr">(because [[{{ns:8}}:Copyright]] has not been customized)</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> If a page checks a target using <code>#ifexist:</code>, then that page will appear in the [[{{#special:WhatLinksHere}}]] list for the target page. So if the code <code><nowiki>{{#ifexist:Foo }}</nowiki></code> were included live on this page ({{FULLPAGENAME}}), [[{{#special:WhatLinksHere}}/Foo]] will list {{FULLPAGENAME}}. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> On wikis using a shared media repository, <code>#ifexist:</code> can be used to check if a file has been uploaded to the repository but not to the wiki itself: </div> :<code><nowiki>{{#ifexist:</nowiki> {{ns:File}}:Example.png <nowiki>| exists | doesn't exist }}</nowiki></code> → '''{{#ifexist: {{ns:File}}:Example.png | exists | doesn't exist }}''' :<code><nowiki>{{#ifexist:</nowiki> Image:Example.png <nowiki>| exists | doesn't exist }}</nowiki></code> → '''{{#ifexist: Image:Example.png | exists | doesn't exist }}''' :<code><nowiki>{{#ifexist:</nowiki> {{ns:-2}}:Example.png <nowiki>| exists | doesn't exist }}</nowiki></code> → '''{{#ifexist: {{ns:-2}}:Example.png | exists | doesn't exist }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> If a local description page has been created for the file, the result is '''exists''' for all of the above. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <code>#ifexist:</code> does not work with interwiki links. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === ifexist limits === </div> <span lang="en" dir="ltr" class="mw-content-ltr"><code>#ifexist:</code> is considered an "expensive parser function"; only a limited number of which can be included on any one page (including functions inside transcluded templates).</span> <span lang="en" dir="ltr" class="mw-content-ltr">When this limit is exceeded, any further <code>#ifexist:</code> functions automatically return false, whether the target page exists or not, and the page is categorized into [[:Category:{{MediaWiki:expensive-parserfunction-category}}]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">The name of the {{ll|Help:Tracking categories|tracking category}} may vary depending on the content language of your wiki.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For some use cases it is possible to emulate the ifexist effect with css, by using the selectors <code>a.new</code> (to select links to unexisting pages) or <code>a:not(.new)</code> (to select links to existing pages).</span> <span lang="en" dir="ltr" class="mw-content-ltr">Furthermore, since the number of expensive parser functions that can be used on a single page is controlled by <code>{{ll|Manual:$wgExpensiveParserFunctionLimit|$wgExpensiveParserFunctionLimit}}</code>, one can also increase the limit in LocalSettings.php if needed.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> === ifexist and wanted pages === </div> <span lang="en" dir="ltr" class="mw-content-ltr">A page that does not exist and is tested for using #ifexist will end up on the [[Special:MyLanguage/Manual:WantedPages|Wanted Pages]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">See {{phab|T14019}} for the reason, and [[w:Template:Linkless exists]] for a workaround.</span> == #rel2abs == <div lang="en" dir="ltr" class="mw-content-ltr"> This function converts a relative file path into an absolute filepath. </div> :<code><nowiki>{{#rel2abs:</nowiki> ''path'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#rel2abs:</nowiki> ''path'' <nowiki>|</nowiki> ''base path'' <nowiki>}}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> Within the <code>path</code> input, the following syntax is valid: </div> *<code>.</code> → <span lang="en" dir="ltr" class="mw-content-ltr">the current level</span> *<code>..</code> → <span lang="en" dir="ltr" class="mw-content-ltr">go up one level</span> *<code>/foo</code> → <span lang="en" dir="ltr" class="mw-content-ltr">go down one level into the subdirectory /foo</span> <div lang="en" dir="ltr" class="mw-content-ltr"> If the <code>base path</code> is not specified, the full page name of the page will be used instead: </div> :<code><nowiki>{{#rel2abs:</nowiki> /quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: /quok | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ./quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ./quok | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../quok | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ../. | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../. | {{ns:12}}:Foo/bar/baz }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Invalid syntax, such as <code>/.</code> or <code>/./</code>, is ignored.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Since no more than two consecutive full stops are permitted, sequences such as these can be used to separate successive statements:</span> :<code><nowiki>{{#rel2abs:</nowiki> ../quok/. | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../quok/. | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../../quok | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ../../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../../../quok | {{ns:12}}:Foo/bar/baz }}''' :<code><nowiki>{{#rel2abs:</nowiki> ../../../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></code> → '''{{#rel2abs: ../../../../quok | {{ns:12}}:Foo/bar/baz }}''' <span lang="en" dir="ltr" class="mw-content-ltr">For a similar group of functions see also [[Special:MyLanguage/Help:Magic words#URL data|Help:Magic words#URL data]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">Built-in parser functions include: 'localurl:', 'fullurl:', 'anchorencode:' etc.</span> == #switch == ''<span lang="en" dir="ltr" class="mw-content-ltr">See also:</span> [[w:Help:Switch parser function]]'' <div lang="en" dir="ltr" class="mw-content-ltr"> This function compares one input value against several test cases, returning an associated string if a match is found. </div> {{pre|1= <nowiki>{{#switch:</nowiki> ''comparison string'' {{!}} ''case'' = ''result'' {{!}} ''case'' = ''result'' {{!}} ''...'' {{!}} ''case'' = ''result'' {{!}} ''default result'' <nowiki>}}</nowiki> }} <div lang="en" dir="ltr" class="mw-content-ltr"> Examples: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: baz | foo = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' {{#switch: baz | foo = Foo | baz = Baz | Bar }} ''' :<syntaxhighlight lang="wikitext" inline>{{#switch: foo | foo = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' {{#switch: foo | foo = Foo | baz = Baz | Bar }} ''' :<syntaxhighlight lang="wikitext" inline>{{#switch: zzz | foo = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' {{#switch: zzz | foo = Foo | baz = Baz | Bar }} ''' <div lang="en" dir="ltr" class="mw-content-ltr"> <nowiki>#</nowiki>switch with partial transclusion tags can affect a configuration file that enables an editor unfamiliar with template coding to view and edit configurable elements. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Default === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The <code>''default result''</code> is returned if no <code>''case''</code> string matches the <code>''comparison string''</code>: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | foo = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' {{#switch: test | foo = Foo | baz = Baz | Bar }} ''' <span lang="en" dir="ltr" class="mw-content-ltr">In this syntax, the default result must be the last parameter and must not contain a raw equals sign (an equals sign without <code><nowiki>{{}}</nowiki></code>).</span> <span lang="en" dir="ltr" class="mw-content-ltr">If it does, it will be treated as a case comparison, and no text will display if no cases match.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This is because the default value has not been defined (is empty).</span> <span lang="en" dir="ltr" class="mw-content-ltr">If a case matches however, its associated string will be returned.</span> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | Bar | foo = Foo | baz = Baz }}</syntaxhighlight> → ''' {{#switch: test | Bar | foo =Foo | baz =Baz }} '''<!-- do not remove the spaces between «''' {{» and «}} '''» --> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | foo = Foo | baz = Baz | B=ar }}</syntaxhighlight> → ''' {{#switch: test | foo = Foo | baz = Baz | B=ar }} '''<!-- do not remove the spaces between «''' {{» and «}} '''» --> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | test = Foo | baz = Baz | B=ar }}</syntaxhighlight> → ''' {{#switch: test | test = Foo | baz = Baz | B=ar }} '''<!-- do not remove the spaces between «''' {{» and «}} '''» --> <div lang="en" dir="ltr" class="mw-content-ltr"> Alternatively, the default result may be explicitly declared with a <code>''case''</code> string of "<code>#default</code>". </div> {{pre|1= <nowiki>{{#switch:</nowiki> ''comparison string'' {{!}} ''case'' = ''result'' {{!}} ''case'' = ''result'' {{!}} ''...'' {{!}} ''case'' = ''result'' {{!}} #default = ''default result'' <nowiki>}}</nowiki> }} <div lang="en" dir="ltr" class="mw-content-ltr"> Default results declared in this way may be placed anywhere within the function: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | foo = Foo | #default = Bar | baz = Baz }}</syntaxhighlight> → ''' {{#switch: test | foo = Foo | #default = Bar | baz = Baz }} ''' <div lang="en" dir="ltr" class="mw-content-ltr"> If the <code>''default''</code> parameter is omitted and no match is made, no <code>''result''</code> is returned: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: test | foo = Foo | baz = Baz }}</syntaxhighlight> → ''' {{#switch: test | foo = Foo | baz = Baz }} '''<!-- do not remove the spaces between «''' {{» and «}} '''» --> <div lang="en" dir="ltr" class="mw-content-ltr"> === Grouping results === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> It is possible to have 'fall through' values, where several <code>''case''</code> strings return the same <code>''result''</code> string. This minimizes duplication. </div> {{pre|1= <nowiki>{{#switch:</nowiki> ''comparison string'' {{!}} ''case1'' = ''result1'' {{!}} ''case2'' {{!}} ''case3'' {{!}} ''case4'' = ''result234'' {{!}} ''case5'' = ''result5'' {{!}} ''case6'' {{!}} ''case7'' = ''result67'' {{!}} #default = ''default result'' <nowiki>}}</nowiki> }} <span lang="en" dir="ltr" class="mw-content-ltr">Here cases 2, 3 and 4 all return <code>''result234''</code>; cases 6 and 7 both return <code>''result67''</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The "<code>#default = </code>" in the last parameter may be omitted in the above case.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> === Use with parameters === </div> <span lang="en" dir="ltr" class="mw-content-ltr">The function may be used with parameters as the test string.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In this case, it is not necessary to place the pipe after the parameter name, because it is very unlikely that you will choose to set a case to be the string "<code><nowiki>{{{</nowiki>''parameter name''<nowiki>}}}</nowiki></code>".</span> (<span lang="en" dir="ltr" class="mw-content-ltr">This is the value the parameter will default to if the pipe is absent and the parameter doesn't exist or have a value.</span> <span lang="en" dir="ltr" class="mw-content-ltr">See {{ll|Help:Parser functions in templates}}.</span>) :<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | foo = Foo | baz = Baz | Bar }}</syntaxhighlight> <span lang="en" dir="ltr" class="mw-content-ltr">In the above case, if <code><nowiki>{{{1}}}</nowiki></code> equals <code>foo</code>, the function will return <code>Foo</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If it equals <code>baz</code>, the function will return <code>Baz</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If the parameter is empty or does not exist, the function will return <code>Bar</code>.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> As in the section above, cases can be combined to give a single result. </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | Bar }}</syntaxhighlight> <span lang="en" dir="ltr" class="mw-content-ltr">Here, if <code><nowiki>{{{1}}}</nowiki></code> equals <code>foo</code>, <code>zoo</code> or <code>roo</code>, the function will return <code>Foo</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If it equals <code>baz</code>, the function will return <code>Baz</code>.</span> <span lang="en" dir="ltr" class="mw-content-ltr">If the parameter is empty or does not exist, the function will return <code>Bar</code>.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Additionally, the default result can be omitted if you do not wish to return anything if the test parameter value does not match any of the cases. </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | foo = Foo | bar = Bar }}</syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> In this case, the function returns an empty string unless <code><nowiki>{{{1}}}</nowiki></code> exists and equals <code>foo</code> or <code>bar</code>, in which case it returns <code>Foo</code> or <code>Bar</code>, respectively. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> This has the same effect as declaring the default result as empty. </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | }}</syntaxhighlight> <span lang="en" dir="ltr" class="mw-content-ltr">If for some reason you decide to set a case as "<code><nowiki>{{{</nowiki>''parameter name''<nowiki>}}}</nowiki></code>", the function will return that case's result when the parameter doesn't exist or doesn't have a value.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The parameter would have to exist and have a value other than the string "<code><nowiki>{{{</nowiki>''parameter name''<nowiki>}}}</nowiki></code>" to return the function's default result.</span> :<span lang="en" dir="ltr" class="mw-content-ltr">(when <code><nowiki>{{{1}}}</nowiki></code> doesn't exist or is empty):</span> ::<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' Foo ''' :<span lang="en" dir="ltr" class="mw-content-ltr">(when <code><nowiki>{{{1}}}</nowiki></code> has the value "<code>test</code>"):</span> ::<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' Bar ''' :<span lang="en" dir="ltr" class="mw-content-ltr">(when <code><nowiki>{{{1}}}</nowiki></code> has the value "<code><nowiki>{{{1}}}</nowiki></code>"):</span> ::<syntaxhighlight lang="wikitext" inline>{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}</syntaxhighlight> → ''' Foo ''' <div lang="en" dir="ltr" class="mw-content-ltr"> In this hypothetical case, you would need to add the pipe to the parameter (<code><nowiki>{{{1</nowiki><span style="color:red; font-weight:bold">|</span><nowiki>}}}</nowiki></code>). </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Comparison behavior === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> As with <code>[[##ifeq|#ifeq]]</code>, the comparison is made numerically if both the comparison string and the case string being tested are numeric; or as a case-sensitive string otherwise: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: 0 + 1 | 1 = one | 2 = two | three}}</syntaxhighlight> → '''{{#switch: 0 + 1 | 1 = one | 2 = two | three}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: {{#expr: 0 + 1}} | 1 = one | 2 = two | three}}</syntaxhighlight> → '''{{#switch: {{#expr: 0 + 1}} | 1 = one | 2 = two | three}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: 02 | +1 = one | +2 = two | three}}</syntaxhighlight> → '''{{#switch: 02 | +1 = one | +2 = two | three}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: 100 | 1e1 = ten | 1e2 = hundred | other}}</syntaxhighlight> → '''{{#switch: 100 | 1e1 = ten | 1e2 = hundred | other}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: a | a = A | b = B | C}}</syntaxhighlight> → '''{{#switch: a | a = A | b = B | C}}''' :<syntaxhighlight lang="wikitext" inline>{{#switch: A | a = A | b = B | C}}</syntaxhighlight> → '''{{#switch: A | a = A | b = B | C}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> A <code>''case''</code> string may be empty: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: | = Nothing | foo = Foo | Something }}</syntaxhighlight> → '''{{#switch: | = Nothing | foo = Foo | Something }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> Once a match is found, subsequent <code>''cases''</code> are ignored: </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: b | f = Foo | b = Bar | b = Baz | }}</syntaxhighlight> → '''{{#switch: b | f = Foo | b = Bar | b = Baz | }}''' {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> Numerical comparisons with <code>[[##switch|#switch]]</code> and <code>[[##ifeq|#ifeq]]</code> are not equivalent to comparisons in expressions (see also above): </div> :<syntaxhighlight lang="wikitext" inline>{{#switch: 12345678901234567 | 12345678901234568 = A | B}}</syntaxhighlight> → '''{{#switch: 12345678901234567 | 12345678901234568 = A | B}}''' :<syntaxhighlight lang="wikitext" inline>{{#ifexpr: 12345678901234567 = 12345678901234568 | A | B}}</syntaxhighlight> → '''{{#ifexpr: 12345678901234567 = 12345678901234568 | A | B}}''' }} <div lang="en" dir="ltr" class="mw-content-ltr"> === Raw equal signs === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> "Case" strings cannot contain raw equals signs. To work around this, use the [[Special:MyLanguage/Help:Magic words#Other|{{((}}={{))}}]] magic word, or replace equals sign with HTML code <code><nowiki>&#61;</nowiki></code>. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Example: </div> {| class="wikitable" ! <span lang="en" dir="ltr" class="mw-content-ltr">You type</span> ! <span lang="en" dir="ltr" class="mw-content-ltr">You get</span> |- | <syntaxhighlight lang=wikitext> {{#switch: 1=2 | 1=2 = raw | 1<nowiki>=</nowiki>2 = nowiki | 1{{=}}2 = template | default }} </syntaxhighlight> | '''{{#switch: 1=2 | 1=2 = raw | 1<nowiki>=</nowiki>2 = nowiki | 1{{=}}2 = template | default }}''' |- | <syntaxhighlight lang=wikitext> {{#switch: 1=2 | 1=2 = html | default }} </syntaxhighlight> | '''{{#switch: 1=2 | 1=2 = html | default }}''' |} {{note|1=<span lang="en" dir="ltr" class="mw-content-ltr">For a simple real life example of the use of this function, check [https://en.wikipedia.org/w/index.php?title=Template:NBA_color&action=edit&oldid=600531646 Template:NBA color]. Two complex examples can be found at {{ll|Template:Extension}} and [[w:Template:BOTREQ]].</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> === Replacing #ifeq === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> <code>#switch</code> can be used to reduce [[Special:MyLanguage/Help:Expansion depth|expansion depth]]. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For example: </div> * <syntaxhighlight lang="wikitext" inline>{{#switch:{{{1}}} |condition1=branch1 |condition2=branch2 |condition3=branch3 |branch4}}</syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> is equivalent to </div> * <syntaxhighlight lang="wikitext" inline>{{#ifeq:{{{1}}}|condition1 |branch1 |{{#ifeq:{{{1}}}|condition2 |branch2 |{{#ifeq:{{{1}}}|condition3 |branch3 |branch4}}}}}}</syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> i.e. deep nesting, linear: </div> <syntaxhighlight lang="wikitext"> {{#ifeq:{{{1}}}|condition1 |<!--then-->branch1 |<!--else-->{{#ifeq:{{{1}}}|condition2 |<!--then-->branch2 |<!--else-->{{#ifeq:{{{1}}}|condition3 |<!--then-->branch3 |<!--else-->branch4}}}}}} </syntaxhighlight> <div lang="en" dir="ltr" class="mw-content-ltr"> On the other hand, the switch replacement could be complicated/impractical for IFs nested in both branches (shown with alternatives of indentation, indented on both sides), making full symmetrical tree: </div> <syntaxhighlight lang="wikitext"> {{#ifeq:{{{1}}}|condition1 |<!--then-->branch1t{{ #ifeq:{{{1}}}|condition2 |<!--then-->branch1t2t{{#ifeq:{{{1}}}|condition4|<!--then-->branch1t2t4t|<!--else-->branch1t2t4e}} |<!--else-->branch1t2e{{#ifeq:{{{1}}}|condition5|<!--then-->branch1t2e5t|<!--else-->branch1t2e5e}} }} |<!--else-->branch1e{{#ifeq:{{{1}}}|condition3 |<!--then-->branch1e3t{{#ifeq:{{{1}}}|condition6|branch1e3t6t|branch1e3t6e}} |<!--else-->branch1e3e{{ #ifeq:{{{1}}}|condition7 |branch1e3e7t |branch1e3e7t }} }} }} </syntaxhighlight> {{Anchor|time|TIME}} == #time == {| class="wikitable plainlinks" style="float: {{dir|{{pagelang}}|left|right}}; font-size: 90%; line-height: normal; margin-{{dir|{{pagelang}}|right|left}}: 1em; width: 40%;" |- ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Code</span> ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Description</span> ! {{hl1}} | <span lang="en" dir="ltr" class="mw-content-ltr">Current output <br /><small class="noprint">([{{fullurl:Help:Extension:ParserFunctions{{#translation:}}|action=purge}} Purge this page's cache] to update)</small></span> |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Year</span> |- | style="text-align: center;" | <code>Y</code> | <span lang="en" dir="ltr" class="mw-content-ltr">4-digit year.</span> | style="text-align: center;" | {{#time:Y}} |- | style="text-align: center;" | <code>y</code> | <span lang="en" dir="ltr" class="mw-content-ltr">2-digit year.</span> | style="text-align: center;" | {{#time:y}} |- | style="text-align: center;" | <code>L</code> | <span lang="en" dir="ltr" class="mw-content-ltr">1 if it's a leap year, 0 if not.</span> | style="text-align: center;" | {{#time:L}} |- | style="text-align: center;" | <code>o</code>{{#tag:ref|<span lang="en" dir="ltr" class="mw-content-ltr">Requires PHP 5.1.0 and newer and [[rev:45208]].</span>|group=note}} | <span lang="en" dir="ltr" class="mw-content-ltr">ISO-8601 year of the specified week.</span>{{#tag:ref|<span lang="en" dir="ltr" class="mw-content-ltr">This has the same value as Y, except that if the ISO week number (W) belongs to the previous or next year, that year is used instead.</span>|group=note}} | style="text-align: center;" | {{#time:o}}{{#tag:ref|<span lang="en" dir="ltr" class="mw-content-ltr">Will output literal ''o'' if note 1 not fulfilled.</span>|group=note}} |- | colspan="3" style="font-size: 88%;" |<references group="note"/> |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Month</span> |- | style="text-align: center;" | <code>n</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Month index, not zero-padded.</span> | style="text-align: center;" | {{#time:n}} |- | style="text-align: center;" | <code>m</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Month index, zero-padded.</span> | style="text-align: center;" | {{#time:m}} |- | style="text-align: center;" | <code>M</code> | <span lang="en" dir="ltr" class="mw-content-ltr">An abbreviation of the month name, in the site language.</span> | style="text-align: center;" | {{#time:M}} |- | style="text-align: center;" | <code>F</code> | <span lang="en" dir="ltr" class="mw-content-ltr">The full month name in the site language.</span> | style="text-align: center;" | {{#time:F}} |- | style="text-align: center;" | <code>xg</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Output the full month name in the [[w:Genitive case|genitive]] form for site languages that distinguish between genitive and [[w:Nominative case|nominative]] forms. This option is useful for many [[w:Slavic languages|Slavic languages]] like Polish, Russian, Belarusian, Czech, Slovak, Slovene, Ukrainian, etc.</span> | style="line-height: 1.6;" | <span lang="en" dir="ltr" class="mw-content-ltr">For Polish:</span><br /><code style="font-size: 85%;"><nowiki>{{#time:F Y|June 2010|pl}}</nowiki></code> → {{#time:F Y|June 2010|pl}}<br /><span lang="en" dir="ltr" class="mw-content-ltr">(nominative)</span><br /><code style="font-size: 85%;"><nowiki>{{#time:d xg Y|20 June 2010|pl}}</nowiki></code> → {{#time:d xg Y|20 June 2010|pl}}<br /><span lang="en" dir="ltr" class="mw-content-ltr">(genitive)</span> |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month or the year</span> |- | style="text-align: center;" | <code>j</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month, not zero-padded.</span> | style="text-align: center;" | {{#time:j}} |- | style="text-align: center;" | <code>d</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month, zero-padded.</span> | style="text-align: center;" | {{#time:d}} |- | style="text-align: center;" | <code>z</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the year (January 1 = 0).<br />{{note}} To get the ISO day of the year add 1.</span> | style="text-align: center;" | {{#time:z}} |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Week and day of the week</span> |- | style="text-align: center;" | <code>W</code> | <span lang="en" dir="ltr" class="mw-content-ltr">ISO 8601 week number, zero-padded.</span> | style="text-align: center;" | {{#time:W}} |- | style="text-align: center;" | <code>N</code> | <span lang="en" dir="ltr" class="mw-content-ltr">ISO 8601 day of the week (Monday = 1, Sunday = 7).</span> | style="text-align: center;" | {{#time:N}} |- | style="text-align: center;" | <code>w</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Number of the day of the week (Sunday = 0, Saturday = 6).</span> | style="text-align: center;" | {{#time:w}} |- | style="text-align: center;" | <code>D</code> | <span lang="en" dir="ltr" class="mw-content-ltr">An abbreviation for the day of the week. Rarely internationalized.</span> | style="text-align: center;" | {{#time:D}} |- | style="text-align: center;" | <code>l</code> | <span lang="en" dir="ltr" class="mw-content-ltr">The full weekday name. Rarely internationalized.</span> | style="text-align: center;" | {{#time:l}} |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Hour</span> |- | style="text-align: center;" | <code>a</code> | <span lang="en" dir="ltr" class="mw-content-ltr">"am" during the morning (00:00:00 → 11:59:59), "pm" otherwise (12:00:00 → 23:59:59).</span> | style="text-align: center;" | {{#time:a}} |- | style="text-align: center;" | <code>A</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Uppercase version of <code>a</code> above.</span> | style="text-align: center;" | {{#time:A}} |- | style="text-align: center;" | <code>g</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Hour in 12-hour format, not zero-padded.</span> | style="text-align: center;" | {{#time:g}} |- | style="text-align: center;" | <code>h</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Hour in 12-hour format, zero-padded.</span> | style="text-align: center;" | {{#time:h}} |- | style="text-align: center;" | <code>G</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Hour in 24-hour format, not zero-padded.</span> | style="text-align: center;" | {{#time:G}} |- | style="text-align: center;" | <code>H</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Hour in 24-hour format, zero-padded.</span> | style="text-align: center;" | {{#time:H}} |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Minutes and seconds</span> |- | style="text-align: center;" | <code>i</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Minutes past the hour, zero-padded.</span> | style="text-align: center;" | {{#time:i}} |- | style="text-align: center;" | <code>s</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Seconds past the minute, zero-padded.</span> | style="text-align: center;" | {{#time:s}} |- | style="text-align: center;" | <code>U</code> | <span lang="en" dir="ltr" class="mw-content-ltr">[[w:Unix time|Unix time]].</span> <span lang="en" dir="ltr" class="mw-content-ltr">Seconds since January 1 1970 00:00:00 GMT.</span> | style="text-align: center;" | {{#time:U}} |- ! id="TimeZone" {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Timezone (as of [[MediaWiki_1.22/Roadmap|1.22wmf2]])</span> |- | style="text-align: center;" | <code>e</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Timezone identifier.</span> | style="text-align: center;" | {{#time:e}} |- | style="text-align: center;" | <code>I</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Whether or not the date is in daylight savings time.</span> | style="text-align: center;" | {{#time:I}} |- | style="text-align: center;" | <code>O</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Difference to Greenwich time (GMT)</span> | style="text-align: center;" | {{#time:O}} |- | style="text-align: center;" | <code>P</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Difference to Greenwich time (GMT), with colon</span> | style="text-align: center;" | {{#time:P}} |- | style="text-align: center;" | <code>T</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Timezone abbreviation.</span> | style="text-align: center;" | {{#time:T}} |- | style="text-align: center;" | <code>Z</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Timezone offset in seconds.</span> | style="text-align: center;" | {{#time:Z}} |- ! {{hl2}} colspan="3"| <span lang="en" dir="ltr" class="mw-content-ltr">Miscellaneous</span> |- | style="text-align: center;" | <code>t</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Number of days in the current month.</span> | style="text-align: center;" | {{#time:t}} |- | style="text-align: center;" | <code>c</code> | <span lang="en" dir="ltr" class="mw-content-ltr">ISO 8601 formatted date, equivalent to <code>Y-m-d"T"H:i:s+00:00</code>.</span> | style="font-size: 75%;" | {{#time:c}} |- | style="text-align: center;" | <code>r</code> | <span lang="en" dir="ltr" class="mw-content-ltr">[[rfc:5322|RFC 5322]] formatted date, equivalent to <code>D, j M Y H:i:s +0000</code>, with weekday name and month name not internationalized.</span> | style="font-size: 75%;" | {{#time:r}} |- ! {{hl2}} colspan="3" | অগ্রেগরীয় পঞ্জিকাসমূহ |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Islamic</span> |- | style="text-align: center;" | <code>xmj</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month.</span> | {{#time:xmj}} |- | style="text-align: center;" | <code>xmF</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full month name.</span> | {{#time:xmF}} |- | style="text-align: center;" | <code>xmn</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Month index.</span> | {{#time:xmn}} |- | style="text-align: center;" | <code>xmY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year.</span> | {{#time:xmY}} |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Iranian (Jalaly)</span> |- | style="text-align: center;" | <code>xit</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Number of days in the month.</span> | {{#time:xit}} |- | style="text-align: center;" | <code>xiz</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the year.</span> | {{#time:xiz}} |- | style="text-align: center;" | <code>xij</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month.</span> | {{#time:xij}} |- | style="text-align: center;" | <code>xiF</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full month name.</span> | {{#time:xiF}} |- | style="text-align: center;" | <code>xin</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Month index.</span> | {{#time:xin}} |- | style="text-align: center;" | <code>xiY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year.</span> | {{#time:xiY}} |- | style="text-align: center;" | <code>xiy</code> | <span lang="en" dir="ltr" class="mw-content-ltr">2-digit year.</span> | {{#time:xiy}} |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Hebrew</span> |- | style="text-align: center;" | <code>xjj</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Day of the month.</span> | {{#time:xjj}} |- | style="text-align: center;" | <code>xjF</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full month name.</span> | {{#time:xjF}} |- | style="text-align: center;" | <code>xjt</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Number of days in month.</span> | {{#time:xjt}} |- | style="text-align: center;" | <code>xjx</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Genitive form of the month name.</span> | {{#time:xjx}} |- | style="text-align: center;" | <code>xjn</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Month number.</span> | {{#time:xjn}} |- | style="text-align: center;" | <code>xjY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year.</span> | {{#time:xjY}} |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Thai solar</span> |- | style="text-align: center;" | <code>xkY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year in [[w:Thai solar calendar|Thai solar calendar]]. <br />{{note}} For years before 1941 the dates in Jan-Mar range are not [[w:Thai_solar_calendar#New_year|calculated]] properly.</span> | {{#time:xkY}} |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Minguo/Juche year</span> |- | style="text-align: center;" | <code>xoY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year.</span> | {{#time:xoY}} |- ! {{hl3}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Japanese nengo</span> |- | style="text-align: center;" | <code>xtY</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Full year.</span> | {{#time:xtY}} |- ! {{hl2}} colspan="3" | <span lang="en" dir="ltr" class="mw-content-ltr">Flags</span> |- | style="text-align: center;" | <code>xn</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Format the next numeric code as a raw ASCII number.</span> | <span lang="en" dir="ltr" class="mw-content-ltr">In the Hindi language, <syntaxhighlight lang="wikitext" inline>{{#time:H, xnH}}</syntaxhighlight> produces ०६, 06.</span> |- | style="text-align: center;" | <code>xN</code> | colspan="2" | <span lang="en" dir="ltr" class="mw-content-ltr">Like <code>xn</code>, but as a toggled flag, which endures until the end of the string or until the next appearance of <code>xN</code> in the string.</span> |- | style="text-align: center;" | <code>xr</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Format the next number as a roman numeral. Only works for numbers up to 10,000<br /><small>(up to 3,000 in pre MediaWiki 1.20)</small>.</span> | <syntaxhighlight lang="wikitext" inline>{{#time:xrY}}</syntaxhighlight> → {{#time:xrY}} |- | style="text-align: center;" | <code>xh</code> | <span lang="en" dir="ltr" class="mw-content-ltr">Format the next number as a Hebrew numeral.</span> | <syntaxhighlight lang="wikitext" inline>{{#time:xhY}}</syntaxhighlight> → {{#time:xhY}} |} <div lang="en" dir="ltr" class="mw-content-ltr"> This parser function takes a date and/or time (in the Gregorian calendar) and formats it according to the syntax given. A date/time object can be specified; the default is the value of the [[Special:MyLanguage/Help:Magic words#Date and time|magic word]] <syntaxhighlight lang="wikitext" inline>{{CURRENTTIMESTAMP}}</syntaxhighlight> – that is, the time the page was last rendered into HTML. </div> :<code><nowiki>{{#time:</nowiki> ''format string'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#time:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#time:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>|</nowiki> ''language code'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#time:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>|</nowiki> ''language code'' <nowiki>|</nowiki> ''local'' <nowiki>}}</nowiki></code> <span lang="en" dir="ltr" class="mw-content-ltr">The list of accepted formatting codes is given in the table to the right.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Any character in the formatting string that is not recognized is passed through unaltered; this applies also to blank spaces (the system does not need them for interpreting the codes).</span> <span lang="en" dir="ltr" class="mw-content-ltr">If no character is recognized in the formatting string, and the date/time object is without error, then the formatting string is returned as output.</span> <span lang="en" dir="ltr" class="mw-content-ltr">There are also two ways to escape characters within the formatting string:</span> <div lang="en" dir="ltr" class="mw-content-ltr"> #A backslash followed by a formatting character is interpreted as a single literal character </div> <div lang="en" dir="ltr" class="mw-content-ltr"> #Characters enclosed in double quotes are considered literal characters, and the quotes are removed. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> In addition, the digraph <code>xx</code> is interpreted as a single literal "x". </div> <div lang="en" dir="ltr" class="mw-content-ltr"> As the list of formatting codes continues to evolve (with the support of new calendars, or of new date fields computed and formatted differently), you should escape all literal characters (not just ASCII letters currently used by formatting codes) that need to be passed through unaltered. </div> <span lang="en" dir="ltr" class="mw-content-ltr">Unfortunately, for now, the ASCII single quote is still not recognized as a simple alternative for marking literal text to the currently supported ASCII double quotes (for example, double quotes are mandatory for in other uses like the delimitation of string values in JSON, C, C++...) and backslashes (which have to be escaped as well in string constants used by many languages, including JSON, C, C++, PHP, JavaScript, Lua).</span> <span lang="en" dir="ltr" class="mw-content-ltr">So you still cannot embed any literal double quote without escaping it with a backslash (or you can use other curly, angular or square quotation marks instead).</span> :<syntaxhighlight lang="wikitext" inline>{{#time: Y-m-d }}</syntaxhighlight> → '''{{#time: Y-m-d }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: [[Y]] m d }}</syntaxhighlight> → '''{{#time: [[Y]] m d }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: [[Y (year)]] }}</syntaxhighlight> → '''{{#time: [[Y (year)]] }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: [[Y "(year)"]] }}</syntaxhighlight> → '''{{#time: [[Y "(year)"]] }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: i's" }}</syntaxhighlight> → '''{{#time: i's" }}''' <span lang="en" dir="ltr" class="mw-content-ltr">The <code>''date/time object''</code> can be in any format accepted by PHP's [https://php.net/function.strtotime strtotime()] function.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Absolute (e.g. <code>20 December 2000</code>), relative (e.g. <code>+20 hours</code>), and combined times (e.g. <code>30 July +1 year</code>) are accepted.</span> :<syntaxhighlight lang="wikitext" inline>{{#time: r|now}}</syntaxhighlight> → '''{{#time: r|now}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|+2 hours}}</syntaxhighlight> → '''{{#time: r|+2 hours}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|now + 2 hours}}</syntaxhighlight> → '''{{#time: r|now + 2 hours}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|20 December 2000}}</syntaxhighlight> → '''{{#time: r|20 December 2000}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|December 20, 2000}}</syntaxhighlight> → '''{{#time: r|December 20, 2000}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|2000-12-20}}</syntaxhighlight> → '''{{#time: r|2000-12-20}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r|2000 December 20}}</syntaxhighlight> → '''{{#time: r|2000 December 20}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The <code>''language code''</code> in [[w:ISO 639-3|ISO 639-3]] (?) allows the string to be displayed in the chosen language </div> :<syntaxhighlight lang="wikitext" inline>{{#time:d F Y|1988-02-28|nl}}</syntaxhighlight> → '''{{#time:d F Y|1988-02-28|nl}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:l|now|uk}}</syntaxhighlight> → '''{{#time:l|now|uk}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:d xg Y|20 June 2010|pl}}</syntaxhighlight> → '''{{#time:d xg Y|20 June 2010|pl}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The <code>''local''</code> parameter specifies if the ''date/time object'' refers to the local timezone or to UTC. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> This is a boolean parameters: its value is determined by casting the value of the argument (see the [https://php.net/language.types.boolean#language.types.boolean.casting official PHP documentation] for details on how string are cast to boolean values). </div> {{note|2=reminder|1=<span lang="en" dir="ltr" class="mw-content-ltr">Please note that, if the variable {{phpi|$wgLocaltimezone}} is set to <code>UTC</code>, there is no difference in the output when <code>local</code> is set to <code>true</code> or <code>false</code>.</span>}} <div lang="en" dir="ltr" class="mw-content-ltr"> See the following examples for details: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y F d H:i:s|now|it|0}}</syntaxhighlight> → '''{{#time: Y F d H:i:s|now|it|0}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: Y F d H:i:s|now|it|1}}</syntaxhighlight> → '''{{#time: Y F d H:i:s|now|it|1}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: Y F d H:i:s|+2 hours||0}}</syntaxhighlight> → '''{{#time: Y F d H:i:s|+2 hours||0}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: Y F d H:i:s|+2 hours||1}}</syntaxhighlight> → '''{{#time: Y F d H:i:s|+2 hours||1}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:c|2019-05-16T17:05:43+02:00|it}}</syntaxhighlight> → '''{{#time:c|2019-05-16T17:05:43+02:00|it}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:c|2019-05-16T17:05:43+02:00|it|0}}</syntaxhighlight> → '''{{#time:c|2019-05-16T17:05:43+02:00|it|0}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:c|2019-05-16T17:05:43+02:00|it|true}}</syntaxhighlight> → '''{{#time:c|2019-05-16T17:05:43+02:00|it|true}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> If you've calculated a Unix timestamp, you may use it in date calculations by pre-pending an <code>@</code> symbol. </div> :<syntaxhighlight lang="wikitext" inline>{{#time: U | now }}</syntaxhighlight> → '''{{#time: U | now}}''' :<code><nowiki>{{#time: r | @</nowiki>{{#time: U}} <nowiki>}}</nowiki></code> → '''{{#time: r | @{{#time:U}} }}''' {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> Without the <code>@</code> prefix before numeric timestamp values, the result is an error most of the time, or is an unexpected value: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: r | 1970-01-01 00:16:39 }}</syntaxhighlight> → '''{{#time: r | 1970-01-01 00:16:39 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: U | 1970-01-01 00:16:39 }}</syntaxhighlight> → '''{{#time: U | 1970-01-01 00:16:39 }}''' :<code><nowiki>{{#time: r | @</nowiki>{{#time: U | 1970-01-01 00:16:39 }}<nowiki> }}</nowiki></code> → '''{{#time: r | @{{#time: U | 1970-01-01 00:16:39 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(correct)</span> :<code><nowiki>{{#time: r | </nowiki>{{#time: U | 1970-01-01 00:16:39 }}<nowiki> }}</nowiki></code> → '''{{#time: r | {{#time: U | 1970-01-01 00:16:39 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(unsupported year format)</span> :<syntaxhighlight lang="wikitext" inline>{{#time: r | 1970-01-01 00:16:40 }}</syntaxhighlight> → '''{{#time: r | 1970-01-01 00:16:40 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: U | 1970-01-01 00:16:40 }}</syntaxhighlight> → '''{{#time: U | 1970-01-01 00:16:40 }}''' :<code><nowiki>{{#time: r | @</nowiki>{{#time: U | 1970-01-01 00:16:40 }}<nowiki> }}</nowiki></code> → '''{{#time: r | @{{#time: U | 1970-01-01 00:16:40 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(correct)</span> :<code><nowiki>{{#time: r | </nowiki>{{#time: U | 1970-01-01 00:16:40 }}<nowiki> }}</nowiki></code> → '''{{#time: r | {{#time: U | 1970-01-01 00:16:40 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(interpreted as a year with current month and day of the month)</span> :<syntaxhighlight lang="wikitext" inline>{{#time: r | 1970-01-01 02:46:39 }}</syntaxhighlight> → '''{{#time: r | 1970-01-01 02:46:39 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: U | 1970-01-01 02:46:39 }}</syntaxhighlight> → '''{{#time: U | 1970-01-01 02:46:39 }}''' :<code><nowiki>{{#time: r | @</nowiki>{{#time: U | 1970-01-01 02:46:39 }}<nowiki> }}</nowiki></code> → '''{{#time: r | @{{#time: U | 1970-01-01 02:46:39 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(correct)</span> :<code><nowiki>{{#time: r | </nowiki>{{#time: U | 1970-01-01 02:46:39 }}<nowiki> }}</nowiki></code> → '''{{#time: r | {{#time: U | 1970-01-01 02:46:39 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(interpreted as a year with current month and day of the month)</span> :<syntaxhighlight lang="wikitext" inline>{{#time: r | 1970-01-01 02:46:40 }}</syntaxhighlight> → '''{{#time: r | 1970-01-01 02:46:40 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: U | 1970-01-01 02:46:40 }}</syntaxhighlight> → '''{{#time: U | 1970-01-01 02:46:40 }}''' :<code><nowiki>{{#time: r | @</nowiki>{{#time: U | 1970-01-01 02:46:40 }}<nowiki> }}</nowiki></code> → '''{{#time: r | @{{#time: U | 1970-01-01 02:46:40 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(correct)</span> :<code><nowiki>{{#time: r | </nowiki>{{#time: U | 1970-01-01 02:46:40 }}<nowiki> }}</nowiki></code> → '''{{#time: r | {{#time: U | 1970-01-01 02:46:40 }} }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(unsupported year format)</span> }} {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> The range of acceptable input is 1 January 0111 → 31 December 9999. For the years 100 through 110 the output is inconsistent, Y and leap years are like the years 100-110, r, D, l and U are like interpreting these years as 2000-2010. </div> :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 29 Feb 0100 }}</syntaxhighlight> → '''{{#time: d F Y | 29 Feb 0100 }}'''<br /> <span lang="en" dir="ltr" class="mw-content-ltr">(correct, no leap year), but</span> :<syntaxhighlight lang="wikitext" inline>{{#time: r | 29 Feb 0100 }}</syntaxhighlight> → '''{{#time: r | 29 Feb 0100 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">(wrong, even if 100 is interpreted as 2000, because that is a leap year)</span> :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 15 April 10000 }}</syntaxhighlight> → '''{{#time: d F Y | 15 April 10000 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: r | 10000-4-15 }}</syntaxhighlight> → '''{{#time: r | 10000-4-15 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> Year numbers 0-99 are interpreted as 2000-2069 and 1970-1999, except when written in 4-digit format with leading zeros: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 1 Jan 6 }}</syntaxhighlight> → '''{{#time: d F Y | 1 Jan 6 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 1 Jan 06 }}</syntaxhighlight> → '''{{#time: d F Y | 1 Jan 06 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 1 Jan 006 }}</syntaxhighlight> → '''{{#time: d F Y | 1 Jan 006 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | 1 Jan 0006 }}</syntaxhighlight> → '''{{#time: d F Y | 1 Jan 0006 }}''' (4-digit format) <div lang="en" dir="ltr" class="mw-content-ltr"> The weekday is supplied for the years 100-110 and from 1753, for the years 111-1752 the r-output shows "Unknown" and the l-output "<>". As a consequence, the r-output is not accepted as input for these years. </div> }} <div lang="en" dir="ltr" class="mw-content-ltr"> Full or partial absolute dates can be specified; the function will "fill in" parts of the date that are not specified using the ''current'' values: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y | January 1 }}</syntaxhighlight> → '''{{#time: Y | January 1 }}''' {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> The fill-in feature is not consistent; some parts are filled in using the current values, others are not: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | June }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | June }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Gives the start of the day, but the current day of the month and the current year.</span> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | 2003 }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | 2003 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Gives the start of the day, but the current day of the year.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> There's exception case of the filled day: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | June 2003 }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | June 2003 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Gives the start of the day and the start of the month.</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> A four-digit number is always interpreted as a year, never as hours and minutes:<ref>Prior to {{rev|86805}} in 2011 this was not the case.</ref> </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | 1959 }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | 1959 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> A six-digit number is interpreted as hours, minutes and seconds if possible, but otherwise as an error (not, for instance, a year and month): </div> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | 195909 }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | 195909 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Input is treated as a time rather than a year+month code.</span> :<syntaxhighlight lang="wikitext" inline>{{#time: Y m d H:i:s | 196009 }}</syntaxhighlight> → '''{{#time: Y m d H:i:s | 196009 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Although 19:60:09 is not a valid time, 196009 is not interpreted as September 1960.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> The function performs a certain amount of date mathematics: </div> :<syntaxhighlight lang="wikitext" inline>{{#time: d F Y | January 0 2008 }}</syntaxhighlight> → '''{{#time: d F Y | January 0 2008 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F | January 32 }}</syntaxhighlight> → '''{{#time: d F | January 32 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F | February 29 2008 }}</syntaxhighlight> → '''{{#time: d F | February 29 2008 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time: d F | February 29 2007 }}</syntaxhighlight> → '''{{#time: d F | February 29 2007 }}''' :<syntaxhighlight lang="wikitext" inline>{{#time:Y-F|now -1 months}}</syntaxhighlight> → '''{{#time:Y-F|now -1 months}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> The total length of the format strings of the calls of <code>#time</code> is limited to 6000 characters<ref>[https://phabricator.wikimedia.org/diffusion/EPFN/browse/master/includes/ParserFunctions.php$31 ParserFunctions.php] at phabricator.wikimedia.org</ref>. </div> <span id="Time_Zone_issue"></span> ==== সময় অঞ্চল সমস্যা ==== <div lang="en" dir="ltr" class="mw-content-ltr"> There is a bug in this #time parser function (more specifically in ''PHP DateTime'') that does not allow the passing-in of ''non-integers'' as relative time zone offsets. This issue does not apply when using an on-the-hour time zone, such as EDT. For example: </div> * <syntaxhighlight lang="wikitext" inline>{{#time:g:i A | -4 hours }}</syntaxhighlight> → {{#time:g:i A | -4 hours}} <div lang="en" dir="ltr" class="mw-content-ltr"> However, India is on a +5.5 hours time offset from UTC, and thus using its time zone will not normally allow the correct calculation of a relative time zone offset. Here's what happens: </div> * <syntaxhighlight lang="wikitext" inline>{{#time:g:i A | +5.5 hours }}</syntaxhighlight> → {{#time:g:i A | +5.5 hours }} <div lang="en" dir="ltr" class="mw-content-ltr"> To workaround this issue, simply convert the time into minutes or seconds, like this: </div> * <syntaxhighlight lang="wikitext" inline>{{#time:g:i A | +330 minutes }}</syntaxhighlight> → {{#time:g:i A | +330 minutes }} * <syntaxhighlight lang="wikitext" inline>{{#time:g:i A | +19800 seconds }}</syntaxhighlight> → {{#time:g:i A | +19800 seconds }} <div lang="en" dir="ltr" class="mw-content-ltr"> (Tim Starling, the developer of this function, provided the exact syntax for this solution.) </div> ==== #time format like in signatures ==== Sometimes it is useful to construct a timestamp, which looks like the automatic timestamp generated by [[Help:Signatures|signatures]] in discussions on talk pages. On an English-language wiki, it can be created with: * <syntaxhighlight lang="wikitext" inline>{{#timel:H:i, j xg Y (e)|+330 minutes}}</syntaxhighlight> → {{#timel:H:i, j xg Y (e)|+330 minutes}} == #timel == <div lang="en" dir="ltr" class="mw-content-ltr"> This function is a syntactic shortcut that operates identically to <code><nowiki>{{#time: ... }}</nowiki></code> with the <code>local</code> parameter set to {{phpi|true}}, so it always uses the preferred time zone of the user or the configured time zone of the wiki (as set in {{ll|Manual:$wgLocaltimezone|$wgLocaltimezone}}) </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Syntax of the function is: </div> :<code><nowiki>{{#timel:</nowiki> ''format string'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#timel:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>}}</nowiki></code> :<code><nowiki>{{#timel:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>|</nowiki> ''language code'' <nowiki>}}</nowiki></code> {{note|2=reminder|1=<span lang="en" dir="ltr" class="mw-content-ltr">Please note that, if the variable {{phpi|$wgLocaltimezone}} is set to <code>UTC</code>, there is no difference in the output when <code>local</code> is set to {{phpi|true}} or {{phpi|false}}</span>}} [[File:Time-Timel.png|thumb|<span lang="en" dir="ltr" class="mw-content-ltr">Example of the use of #time and #timel parser functions from a server where the timezone is not UTC</span>]] <div lang="en" dir="ltr" class="mw-content-ltr"> For instance, see the following examples: </div> :<syntaxhighlight lang="wikitext" inline>{{#time:c|now|it}}</syntaxhighlight> → '''{{#time:c|now|it}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:c|now|it|0}}</syntaxhighlight> → '''{{#time:c|now|it|0}}''' :<syntaxhighlight lang="wikitext" inline>{{#time:c|now|it|1}}</syntaxhighlight> → '''{{#time:c|now|it|1}}''' :<syntaxhighlight lang="wikitext" inline>{{#timel:c|now|it}}</syntaxhighlight> → '''{{#timel:c|now|it}}''' [[File:Maldiskusjon Sommertid–Wikipedia.jpg|thumb|<span lang="en" dir="ltr" class="mw-content-ltr">Warning Example from https://no.wikipedia.org/wiki/Maldiskusjon:Sommertid</span>]] {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> Be aware that U for both time and timel will return the same number of seconds since 1970-01-01 00:00:00 UTC on Wikipedias with different timezones than UTC (formerly known as GMT) </div> :<code>U</code> <span lang="en" dir="ltr" class="mw-content-ltr">Unix time. Seconds since January 1 1970 00:00:00 GMT.</span> :<code>Z</code> <span lang="en" dir="ltr" class="mw-content-ltr">Timezone offset in seconds.</span> :<syntaxhighlight lang="wikitext" inline>{{#time: U}}</syntaxhighlight> → '''{{#time: U}}''' :<syntaxhighlight lang="wikitext" inline>{{#timel: U}}</syntaxhighlight> → '''{{#timel: U}}''' :<syntaxhighlight lang="wikitext" inline>{{#time: Z}}</syntaxhighlight> → '''{{#time: Z}}''' :<syntaxhighlight lang="wikitext" inline>{{#timel: Z}}</syntaxhighlight> → '''{{#timel: Z}}''' }} {{-}} {{Anchor|titleparts}} == #titleparts == <div lang="en" dir="ltr" class="mw-content-ltr"> This function separates a page title into segments based on slashes, then returns some of those segments as output. </div> : {{tmpl|0=<code class="mw-highlight">{{((}}#titleparts: ''$1'' {{!}} ''$2'' {{!}} ''$3'' {{))}}</code> |1=<span lang="en" dir="ltr" class="mw-content-ltr">pagename</span> |2=<span lang="en" dir="ltr" class="mw-content-ltr">number of segments to return</span> |3=<span lang="en" dir="ltr" class="mw-content-ltr">segment to start at</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> If the ''number of segments to return'' parameter is not specified, it defaults to "0", which returns all the segments from the ''segment to start at'' to the end (included). If the ''segment to start at'' parameter is not specified or is "0", it defaults to "1": </div> : <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">{{ns:1}}:Foo/bar/baz/quok</span> <nowiki>}}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok }}''' : <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">{{ns:1}}:Foo</span>/bar/baz/quok <nowiki>| 1 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 1 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">See also <nowiki>{{</nowiki>{{ll|Help:Magic_words#Page_names|ROOTPAGENAME}}<nowiki>}}</nowiki>.</span> : <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">{{ns:1}}:Foo/bar</span>/baz/quok <nowiki>| 2 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 2 }}''' : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/<span style="color: blue;">bar/baz</span>/quok <nowiki>| 2 | 2 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 2 | 2 }}''' : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/<span style="color: blue;">baz/quok</span> <nowiki>| 2 | 3 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 2 | 3 }}''' : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/<span style="color: blue;">bar/baz/quok</span> <nowiki>| 3 | 2 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 3 | 2 }}''' : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/<span style="color: blue;">bar/baz/quok</span> <nowiki>| | 2 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | | 2 }}''' : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| | 5 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | | 5 }} ''' <div lang="en" dir="ltr" class="mw-content-ltr"> Negative values are accepted for both values. Negative values for the ''number of segments to return'' parameter effectively 'strips' segments from the end of the string. Negative values for the ''first segment to return'' translates to "start with this segment counting from the right": </div> : <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">{{ns:1}}:Foo/bar/baz</span>/quok <nowiki>| -1 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -1 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Strips one segment from the end of the string.</span> <span lang="en" dir="ltr" class="mw-content-ltr">See also {{[[Special:MyLanguage/Help:Magic_words#Page_names|BASEPAGENAME]]}}.</span> : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| -4 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -4 }} ''' <span lang="en" dir="ltr" class="mw-content-ltr">Strips all 4 segments from the end of the string</span> : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| -5 }}</nowiki></code> → '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -5 }} ''' <span lang="en" dir="ltr" class="mw-content-ltr">Strips 5 segments from the end of the string (more than exist)</span> : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/<span style="color: blue;">quok</span> <nowiki>| | -1 }}</nowiki></code> → ''' {{#titleparts: {{ns:1}}:Foo/bar/baz/quok | | -1 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Returns last segment.</span> <span lang="en" dir="ltr" class="mw-content-ltr">See also {{[[Special:MyLanguage/Help:Magic_words#Page_names|SUBPAGENAME]]}}.</span> : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/<span style="color: blue;">bar/baz</span>/quok <nowiki>| -1 | 2 }}</nowiki></code> → ''' {{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -1 | 2 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Strips one segment from the end of the string, then returns the second segment and beyond</span> : <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/<span style="color: blue;">baz</span>/quok <nowiki>| -1 | -2 }}</nowiki></code> → ''' {{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -1 | -2 }}''' <span lang="en" dir="ltr" class="mw-content-ltr">Start copying at the second last element; strip one segment from the end of the string</span> <div lang="en" dir="ltr" class="mw-content-ltr"> Before processing, the ''pagename'' parameter is HTML-decoded: if it contains some standard HTML character entities, they will be converted to plain characters (internally encoded with UTF-8, i.e. the same encoding as in the MediaWiki source page using this parser function). </div> : <span lang="en" dir="ltr" class="mw-content-ltr">For example, any occurrence of <code>&quot;</code>, <code>&#34;</code>, or <code>&#x22;</code> in ''pagename'' will be replaced by <code>"</code>.</span> : <span lang="en" dir="ltr" class="mw-content-ltr">No other conversion from HTML to plain text is performed, so HTML tags are left intact at this initial step even if they are invalid in page titles.</span> {{tip| 1= <div lang="en" dir="ltr" class="mw-content-ltr"> Some magic keywords or parser functions of MediaWiki (such as <code><nowiki>{{</nowiki>{{ll|Help:Magic words#Page names|PAGENAME}}<nowiki>}}</nowiki></code> and similar) are known to return strings that are needlessly HTML-encoded, even if their own input parameter was not HTML-encoded: </div> <div lang="en" dir="ltr" class="mw-content-ltr"> The titleparts parser function can then be used as a workaround, to convert these returned strings so that they can be processed correctly by some other parser functions also taking a page name in parameter (such as <code><nowiki>{{</nowiki>{{ll|Help:Magic words#PAGESINCAT|PAGESINCAT:}}<nowiki>}}</nowiki></code>) but which are still not working properly with HTML-encoded input strings. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> For example, if the current page is [[commons:Category:Côte-d'Or|Category:Côte-d'Or]], then: </div> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{#ifeq: {{FULLPAGENAME}} | Category:Côte-d'Or | 1 | 0 }}</nowiki></code>, and <code><nowiki>{{#ifeq: {{FULLPAGENAME}} | Category:Côte-d&apos;Or | 1 | 0 }}</nowiki></code> are both returning <code>1</code>; (the #ifeq parser function does perform the HTML-decoding of its input parameters).</span> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{#switch: {{FULLPAGENAME}} | Category:Côte-d'Or = 1 | #default = 0 }}</nowiki></code>, and <code><nowiki>{{#switch: {{FULLPAGENAME}} | Category:Côte-d&apos;Or = 1 | #default = 0 }}</nowiki></code> are both returning <code>1</code>; (the #switch parser function does perform the HTML-decoding of its input parameters).</span> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{#ifexist: {{FULLPAGENAME}} | 1 | 0 }}</nowiki></code>, <code><nowiki>{{#ifexist: Category:Côte-d'Or | 1 | 0 }}</nowiki></code>, or even <code><nowiki>{{#ifexist: Category:Côte-d&apos;Or | 1 | 0 }}</nowiki></code> will all return <code>1</code> if that category page exists (the #ifexist parser function does perform the HTML-decoding of its input parameters);</span> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{PAGESINCAT: Côte-d'Or }}</nowiki></code> will return a non-zero number, if that category contains pages or subcategories, '''but''':</span> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{PAGESINCAT: {{CURRENTPAGENAME}} }}</nowiki></code>, may still '''unconditionally''' return 0, just like:</span> * <code><nowiki>{{PAGESINCAT: {{PAGENAME:Category:Côte-d'Or}} }}</nowiki></code> * <code><nowiki>{{PAGESINCAT: {{PAGENAME:Category:Côte-d&apos;Or}} }}</nowiki></code> <div lang="en" dir="ltr" class="mw-content-ltr"> The reason of this unexpected behavior is that, with the current versions of MediaWiki, there are two caveats: </div> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{FULLPAGENAME}}</nowiki></code>, or even <code><nowiki>{{FULLPAGENAME:Côte-d'Or}}</nowiki></code> ''may'' return the actually HTML-encoded string <code>Category:Côte-d&apos;Or</code> and not the expected <code>Category:Côte-d'Or</code>, and that:</span> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{PAGESINCAT: Côte-d&apos;Or }}</nowiki></code> '''unconditionally''' returns 0 (the PAGESINCAT magic keyword does not perform any HTML-decoding of its input parameter).</span> <div lang="en" dir="ltr" class="mw-content-ltr"> The simple workaround using titleparts (which will continue to work if the two caveats are fixed in a later version of MediaWiki) is: </div> * <code><nowiki>{{PAGESINCAT: {{#titleparts: {{CURRENTPAGENAME}} }} }}</nowiki></code> * <code><nowiki>{{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d'Or}} }} }}</nowiki></code> * <span lang="en" dir="ltr" class="mw-content-ltr"><code><nowiki>{{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d&apos;Or}} }} }}</nowiki></code>, that all return the actual number of pages in the same category.</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> Then the decoded ''pagename'' is canonicalized into a standard page title supported by MediaWiki, as much as possible: </div> # <span lang="en" dir="ltr" class="mw-content-ltr">All underscores are automatically replaced with spaces:</span> #: <code><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bah_boo|1|2}}</code> → '''{{#titleparts: {{ns:1}}:Foo/bah_boo|1|2}}''' <span lang="en" dir="ltr" class="mw-content-ltr">Not bah_boo, despite the underscore in the original.</span> # <span lang="en" dir="ltr" class="mw-content-ltr">The string is split a maximum of 25 times; further slashes are ignored and the 25th element will contain the rest of the string.</span> <span lang="en" dir="ltr" class="mw-content-ltr">The string is also limited to 255 characters, as it is treated as a [[Special:MyLanguage/Manual:Page table#Schema summary|page title]]:</span> #: <code><nowiki>{{#titleparts:</nowiki> a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/<span style="color: blue;">y/z/aa/bb/cc/dd/ee</span> <nowiki>| 1 | 25 }}</nowiki></code> → '''{{#titleparts: a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee | 1 | 25 }}''' #: <span lang="en" dir="ltr" class="mw-content-ltr">If for whatever reason you needed to push this function to its limit, although very unlikely, it is possible to bypass the 25 split limit by nesting function calls:</span> #: <code><nowiki>{{#titleparts: {{#titleparts:</nowiki> a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/<span style="color: blue;">z</span>/aa/bb/cc/dd/ee<nowiki>| 1 | 25 }} | 1 | 2}}</nowiki></code> → '''{{#titleparts: {{#titleparts: a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z/aa/bb/cc/dd/ee | 1 | 25 }} | 1 | 2}}''' # <span lang="en" dir="ltr" class="mw-content-ltr">Finally the first substring is capitalized according to the capitalization settings of the local wiki (if that substring also starts by a local namespace name, that namespace name is also normalized).</span> #: <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">talk:a</span>/b/c <nowiki>}}</nowiki></code> → '''{{#titleparts: talk:a/b/c }}''' {{tip| 1= <div lang="en" dir="ltr" class="mw-content-ltr"> You can use #titleparts as a small "string parser and converter", but consider that it returns the first substring capitalized: </div> : <code><nowiki>{{#titleparts:</nowiki> <span style="color: blue;">one</span>/two/three/four<nowiki>|1|1 }}</nowiki></code> → '''{{#titleparts: one/two/three/four|1|1 }}''' : <code><nowiki>{{#titleparts:</nowiki> one/<span style="color: blue;">two</span>/three/four<nowiki>|1|2 }}</nowiki></code> → '''{{#titleparts: one/two/three/four|1|2 }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> If lower case is needed, use lc: function to control output: </div> : <code><nowiki>{{lc: {{#titleparts:</nowiki> <span style="color: blue;">one</span>/two/three/four<nowiki>|1|1 }} }}</nowiki></code> → '''{{lc: {{#titleparts:one/two/three/four|1|1 }}}}''' <div lang="en" dir="ltr" class="mw-content-ltr"> You can prepend a 'dummy' slash at the beginning of the string to get the correct first substring capitalization (uppercase or lowercase). Use <code>2</code> instead of <code>1</code> for ''first segment to return'': </div> : <code><nowiki>{{#titleparts:</nowiki> '''/'''<span style="color: blue;">one</span>/two/three/four<nowiki>|1|</nowiki>'''<u>2</u>'''<nowiki> }}</nowiki></code> → '''{{#titleparts: /one/two/three/four|1|2 }}''' : <code><nowiki>{{#titleparts:</nowiki> '''/'''<span style="color: blue;">One</span>/two/three/four<nowiki>|1|</nowiki>'''<u>2</u>'''<nowiki> }}</nowiki></code> → '''{{#titleparts: /One/two/three/four|1|2 }}''' }} {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> Certain characters that are illegal in a page title will cause #titleparts to not parse the string: </div> : <code><nowiki>{{#titleparts: {one/two} | 1 | 1 }}</nowiki></code> → '''{{#titleparts: {one/two} | 1 | 1 }}'''. <span lang="en" dir="ltr" class="mw-content-ltr">Does not produce the expected:</span> '''{one''' : <code><nowiki>{{#titleparts: [[page]]/123 | 1 | 2 }}</nowiki></code> → '''{{#titleparts: [[page]]/123 | 1 | 2 }}'''. <span lang="en" dir="ltr" class="mw-content-ltr">Does not work because brackets are illegal in page titles and this parser function does not process links embedded in its input ''pagename'' parameter, even when they use the MediaWiki syntax, or any other HTML or MediaWiki tags.</span> : <code><nowiki>{{#titleparts: red/#00FF00/blue | 1 | 3 }}</nowiki></code> → "{{#titleparts: red/#00FF00/blue| 1 | 3 }}". <span lang="en" dir="ltr" class="mw-content-ltr">Does not work because "#" is also illegal in page titles.</span> }} {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> If any part of the title is just "<code>.</code>" or "<code>..</code>", #titleparts will not parse the string: </div> : <code><nowiki>{{#titleparts: one/./three | 1 | 1 }}</nowiki></code> → '''{{#titleparts: one/./three | 1 | 1 }}'''. <span lang="en" dir="ltr" class="mw-content-ltr">The whole string is returned. It does not produce the expected:</span> '''one''' }} {{Warning|1= <span lang="en" dir="ltr" class="mw-content-ltr">This function does not degrade gracefully if the input exceeds 255 bytes in UTF-8. If the input string is 256 bytes or more, the whole string is returned.</span> }} <div lang="en" dir="ltr" class="mw-content-ltr"> == String functions == </div> {{Main|Extension:ParserFunctions/String functions}} <span lang="en" dir="ltr" class="mw-content-ltr">The ParserFunctions extension optionally defines various string functions (<code>#len</code>, <code>#pos</code>, <code>#rpos</code>, <code>#sub</code>, <code> #count</code>, <code>#replace</code>, <code>#explode</code>, <code>#urldecode</code>) if <code>$wgPFEnableStringFunctions </code> is set to {{phpi|true}}.</span> {{Warning |1=<span lang="en" dir="ltr" class="mw-content-ltr">In 2013, it was decided that '''these functions will ''never'' be enabled on any Wikimedia wiki''', because they are inefficient when used on a large scale (see [[phab:T8455]] for some history).</span> '''<span lang="en" dir="ltr" class="mw-content-ltr">These functions do NOT work on Wikimedia wikis!</span>'''<br/><br/><span lang="en" dir="ltr" class="mw-content-ltr">If you are here to write something on a Wikimedia project, you are looking for something else: if your home wiki has string functions, it probably uses {{ll|Extension:Scribunto|Lua}}.</span> <span lang="en" dir="ltr" class="mw-content-ltr">For example, the English Wikipedia uses [[w:Module:String|Module:String]], which does some of the same things with wildly different syntax.</span> <span lang="en" dir="ltr" class="mw-content-ltr">There are also individual [[w:Template:String-handling templates|String-handling templates]].</span> }} <span lang="en" dir="ltr" class="mw-content-ltr">See [[w:Help:Manipulating strings#StringFunctions (from ParserFunctions)|here]] for examples.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Here is a short overview of [[Module:String]] functions:</span> * [[w:Module:String#len|#len]] <span lang="en" dir="ltr" class="mw-content-ltr">(length of string)</span>: <code><nowiki>{{</nowiki>[[:w:Wikipedia:Lua|#invoke]]<nowiki>:String|len|target_string}}</nowiki></code> * [[w:Module:String#sub|#sub]] <span lang="en" dir="ltr" class="mw-content-ltr">(substring)</span>: <code><nowiki>{{#invoke:String|sub|target_string|start_index|end_index}}</nowiki></code> * [[w:Module:String#match|#match]]: <code><nowiki>{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}</nowiki></code> * [[w:Module:String#pos|#pos]] <span lang="en" dir="ltr" class="mw-content-ltr">(position of target)</span>: <code><nowiki>{{#invoke:String|pos|target_string|index_value}}</nowiki></code> * [[w:Module:String#find|#find]]: <code><nowiki>{{#invoke:String|find|source_string|target_string|start_index|plain_flag}}</nowiki></code> * [[w:Module:String#replace (gsub)|#replace]]: <code><nowiki>{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}</nowiki></code> * [[w:Module:String#rep|#rep]] <span lang="en" dir="ltr" class="mw-content-ltr">(repeat)</span>: <code><nowiki>{{#invoke:String|rep|source|count}}</nowiki></code> * [[w:Module:String#escapePattern|#escapePattern]]: <code><nowiki>{{#invoke:String|escapePattern|pattern_string}}</nowiki></code> * [[w:Module:String#count|#count]]: <code><nowiki>{{#invoke:String|count|source_str|pattern_string|plain_flag}}</nowiki></code> * [[w:Module:String#join|#join]]: <code><nowiki>{{#invoke:String|join|separator|string1|string2|...}}</nowiki></code> See also [[Manual:Performing string operations with parser functions]] for a different set of hacks used to perform string functions when these are disabled, which were used on Wikimedia wikis before the Scribunto was developed. <div lang="en" dir="ltr" class="mw-content-ltr"> == General points == </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Substitution === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Parser functions can be [[Special:MyLanguage/Help:Substitution|substituted]] by prefixing the hash character with <code>'''subst:'''</code>: </div> :<code><nowiki>{{subst:#ifexist:</nowiki> {{FULLPAGENAME}} <nowiki>| [[</nowiki>{{FULLPAGENAME}}<nowiki>]] | </nowiki>{{FULLPAGENAME}}<nowiki> }}</nowiki></code> → <span lang="en" dir="ltr" class="mw-content-ltr">the code '''<code><nowiki>[[</nowiki>{{FULLPAGENAME}}<nowiki>]]</nowiki></code>''' will be inserted in the wikitext since the page {{FULLPAGENAME}} exists.</span> {{Warning|1= <div lang="en" dir="ltr" class="mw-content-ltr"> The results of substituted parser functions are undefined if the expressions contain ''un''substituted volatile code such as {{ll|Help:Magic words#Variables|variables}} or other parser functions. For consistent results, all the volatile code in the expression to be evaluated must be substituted. See [[w:Help:Substitution|Help:Substitution]]. </div>}} <div lang="en" dir="ltr" class="mw-content-ltr"> Substitution does not work within {{ll|Extension:Cite#Substitution and embedded parser functions|{{tag|ref|open}}…{{tag|ref|close}} }}; you can use <code>{{((}}subst:#tag:ref{{!}}</code>…<code>{{))}}</code> for this purpose. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Redirects === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Especially [[#.23time|<nowiki>{{</nowiki><code>#time:</code>…|<code>now-</code>…<nowiki>}}</nowiki>]] could be handy in {{ll|Help:Redirects|redirects}} to pages including dates, but this does not work. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> === Escaping pipe characters in tables === </div> <span lang="en" dir="ltr" class="mw-content-ltr">Parser functions will mangle {{ll|Help:Tables|wikitable}} syntax and pipe characters (<code><nowiki>|</nowiki></code>), treating all the raw pipe characters as parameter dividers.</span> <span lang="en" dir="ltr" class="mw-content-ltr">To avoid this, most wikis used a template [[{{ns:10}}:!]] with its contents only a raw pipe character (<code><nowiki>|</nowiki></code>), since MW 1.24 a {{ll|Help:Magic words#Other|<code><nowiki>{{!}}</nowiki></code> magic word}} replaced this kludge.</span> <span lang="en" dir="ltr" class="mw-content-ltr">This 'hides' the pipe from the MediaWiki parser, ensuring that it is not considered until after all the templates and variables on a page have been expanded.</span> <span lang="en" dir="ltr" class="mw-content-ltr">It will then be interpreted as a table row or column separator.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Alternatively, raw HTML table syntax can be used, although this is less intuitive and more error-prone.</span> <div lang="en" dir="ltr" class="mw-content-ltr"> You can also escape the pipe character for display as a plain, uninterpreted character using an HTML entity: <code>&#124;</code> . </div> {| class="wikitable" ! বিবরণ ! আপনি লিখবেন ! আপনি পাবেন |- | <span lang="en" dir="ltr" class="mw-content-ltr">Escaping pipe character as table row/column separator</span> | <pre> {{!}} </pre> | style="text-align:center;" | | |- | <span lang="en" dir="ltr" class="mw-content-ltr">Escaping pipe character as a plain character</span> | <pre> &#124; </pre> | style="text-align:center;" | | |} <div lang="en" dir="ltr" class="mw-content-ltr"> === Stripping whitespace === </div> <div lang="en" dir="ltr" class="mw-content-ltr"> Whitespace, including newlines, tabs, and spaces, is stripped from the beginning and end of all the parameters of these parser functions. If this is not desirable, comparison of strings can be done after putting them in quotation marks. </div> :<code><nowiki>{{#ifeq: foo | foo | equal | not equal }}</nowiki></code> → '''{{#ifeq: foo | foo | equal | not equal }}''' :<code><nowiki>{{#ifeq: "foo " | " foo" | equal | not equal }}</nowiki></code> → '''{{#ifeq: "foo " | " foo" | equal | not equal }}''' <div lang="en" dir="ltr" class="mw-content-ltr"> To prevent the trimming of then and else parts, see [[m:Template:If]]. Some people achieve this by using <nowiki><</nowiki>{{ll|Wiki markup#Nowiki|nowiki}}<nowiki>></nowiki> <nowiki><</nowiki>/nowiki<nowiki>></nowiki> instead of spaces. </div> :<syntaxhighlight lang="wikitext" inline>foo{{#if:|| bar }}foo</syntaxhighlight> → '''foo{{#if:|| bar }}foo''' :<syntaxhighlight lang="xml" inline>foo{{#if:||<nowiki /> bar <nowiki />}}foo</syntaxhighlight> → '''foo{{#if:||<nowiki /> bar <nowiki />}}foo''' <div lang="en" dir="ltr" class="mw-content-ltr"> However, this method can be used to render a '''single''' whitespace character only, since the parser squeezes multiple whitespace characters in a row into one. </div> :{| |<syntaxhighlight lang="html"><span style="white-space: pre;">foo{{#if:||<nowiki/> bar <nowiki/>}}foo</span></syntaxhighlight>|| → ||'''<span style="white-space: pre;">foo{{#if:||<nowiki/> bar <nowiki/>}}foo</span>''' |} <div lang="en" dir="ltr" class="mw-content-ltr"> In this example, the <code>white-space: pre</code> style is used to force the whitespace to be preserved by the browser, but even with it the spaces are not shown. This happens because the spaces are stripped by the software, before being sent to the browser. </div> <div lang="en" dir="ltr" class="mw-content-ltr"> It is possible to workaround this behavior replacing whitespaces with <code>&#32;</code> (''breakable space'') or <code>&nbsp;</code> (''non-breakable space''), since they are not modified by the software: </div> :<syntaxhighlight lang="html" inline><span style="white-space: pre;">foo{{#if:||   bar   }}foo</span></syntaxhighlight> → '''<span style="white-space: pre;">foo{{#if:||   bar   }}foo</span>''' :<syntaxhighlight lang="html" inline>foo{{#if:|| bar }}foo</syntaxhighlight> → '''foo{{#if:|| bar }}foo''' <span lang="en" dir="ltr" class="mw-content-ltr">Beware that not all parameters are created equal.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In ParserFunctions, whitespace at the beginning and end is always stripped.</span> <span lang="en" dir="ltr" class="mw-content-ltr">In {{ll|Help:Templates|templates}}, whitespace at the beginning and end is stripped for named parameters and named unnamed parameters but ''not'' from unnamed parameters:</span> :<code>foo{{tlx|1x|content{{=}} bar }}foo</code> → '''foo{{1x|content= bar }}foo''' :<code>foo{{tlx|1x|1{{=}} bar }}foo</code> → '''foo{{1x|1= bar }}foo''' :<code>foo{{tlx|1x| bar }}foo</code> → '''foo{{1x| bar }}foo''' <span id="See_also"></span> == আরও দেখুন == * {{ll|Help:Parser functions in templates}} * {{ll|Parser extension tags}} * {{ll|Parser function hooks}} - <span lang="en" dir="ltr" class="mw-content-ltr">an (incomplete) list of parser functions added by core and extensions.</span> * [[m:Special:MyLanguage/Help:Calculation|<span lang="en" dir="ltr" class="mw-content-ltr">m:Help:Calculation</span>]] * [[m:Special:MyLanguage/Help:Newlines and spaces|<span lang="en" dir="ltr" class="mw-content-ltr">m:Help:Newlines and spaces</span>]] * [[m:Special:MyLanguage/Help:Comparison between ParserFunctions syntax and TeX syntax|<span lang="en" dir="ltr" class="mw-content-ltr">m:Help:Comparison between ParserFunctions syntax and TeX syntax</span>]] * {{ll|Help:Magic words}} <div lang="en" dir="ltr" class="mw-content-ltr"> * [[Module:String]] obsoleting {{ll|Extension:StringFunctions}} </div> * {{ll|Extension:PhpTags}} * <span lang="en" dir="ltr" class="mw-content-ltr">Parser functions for Wikibase (the extensions that enables Wikidata):</span> [[d:Special:MyLanguage/Wikidata:How to use data on Wikimedia projects#Parser function|d:Wikidata:How to use data on Wikimedia projects#Parser function]] <span id="References"></span> == তথ্যসূত্র == {{reflist}} {{Parser functions nav}} [[Category:Extension help{{#translation:}}|ParserFunctions]] [[Category:Magic words{{#translation:}}]]
Toggle limited content width