Изменения

Перейти к: навигация, поиск

Справка:Функции парсера

33 972 байта добавлено, 20:51, 17 января 2010
м
#ifexpr: removing ":" after function (not used in text), and linking both functions for better readability
The {{mediawiki|Extension:ParserFunctions|ParserFunctions}} extension provides ten additional parser functions to supplement the "[[Help:Magic words#Parser functions|magic words]]", which are already present in MediaWiki. All the parser functions provided by this extension take the form:
:<tt><nowiki>{{</nowiki> '''#functionname''': ''argument 1'' | ''argument 2'' | ''argument 3'' ... <nowiki>}}</nowiki></tt>

==#expr==

{| {{prettytable|style=float:right; width:30%; margin-left:1em;}}
|-
!{{hl1}} | Type
!{{hl1}} | Operators
|-
|{{hl2}} | Grouping (parentheses)
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | ( )
|-
|{{hl2}} | Numbers
| style="word-spacing:0.5em;" | <tt>'''1234.5'''</tt> <tt>'''2.4E5'''</tt> <tt>'''e'''</tt>&nbsp;(2.718) <tt>'''pi'''</tt>&nbsp;(3.142)
|-
|{{hl2}} rowspan="3" | Logic
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | or
|-
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | and
|-
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | = != <> > < <= >=
|-
|{{hl2}} | Round
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | round
|-
|{{hl2}} rowspan="3" | Binary
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | + - mod
|-
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | * / div
|-
| <tt>'''^'''</tt>
|-
|{{hl2}} rowspan="2" | Unary
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | not ceil trunc floor abs ln sin cos tan acos asin atan
|-
| style="word-spacing:0.5em; font-family:monospace; font-weight:bold;" | e + -
|}

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

:<tt><nowiki>{{#expr:</nowiki> ''expression'' <nowiki>}}</nowiki></tt>

The available operators are listed to the right, in order of precedence. See {{mediawiki|m: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.

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

:<tt><nowiki>{{#expr: 1 and -1 }}</nowiki></tt> &rarr; '''{{#expr: 1 and -1 }}'''
:<tt><nowiki>{{#expr: 1 and 0 }}</nowiki></tt> &rarr; '''{{#expr: 1 and 0 }}'''

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

:<tt><nowiki>{{#expr: }}</nowiki></tt> &rarr; ''' {{#expr: }}'''
:<tt><nowiki>{{#expr: 1+ }}</nowiki></tt> &rarr; '''{{#expr: 1+ }}'''
:<tt><nowiki>{{#expr: 1 foo 2 }}</nowiki></tt> &rarr; '''{{#expr: 1 foo 2 }}'''

{{warning|Some expressions may invoke floating-point errors when used with very large or very small numbers:<br />
<tt><nowiki>{{#expr: 20060618093259 mod 10000}}</nowiki></tt> &rarr; '''{{#expr: 20060618093259 mod 10000}}''' in most cases, but may occasionally give '''-6357'''. This varies with the specification and configuration of the server running the wiki. See {{bugzilla|6356}}.
}}

==#if==

:<tt><nowiki>{{#if:</nowiki> ''test string'' | ''value if true'' | ''value if false'' <nowiki>}}</nowiki></tt>

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

:<tt><nowiki>{{#if: | yes | no}}</nowiki></tt> &rarr; '''{{#if: | yes | no}}'''
:<tt><nowiki>{{#if: string | yes | no}}</nowiki></tt> &rarr; '''{{#if: string | yes | no}}'''
:<tt><nowiki>{{#if: &nbsp;&nbsp;&nbsp;&nbsp; | yes | no}}</nowiki></tt> &rarr; '''{{#if: | yes | no}}'''
:<tt><nowiki>{{#if: </nowiki><br /><br /><br /><nowiki> | yes | no}}</nowiki></tt> &rarr; '''{{#if:


| yes | no}}'''

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

:<tt><nowiki>{{#if: 1==2 | yes | no}}</nowiki></tt> &rarr; '''{{#if: 1==2 | yes | no}}'''

Either or both the return values may be omitted:

:<tt><nowiki>{{#if: foo | yes }}</nowiki></tt> &rarr; ''' {{#if: foo | yes }}'''
:<tt><nowiki>{{#if: | yes }}</nowiki></tt> &rarr; ''' {{#if: | yes }}'''
:<tt><nowiki>{{#if: foo | | no}}</nowiki></tt> &rarr; ''' {{#if: foo | | no}}'''

See [[Help:Parser functions in templates]] for more examples of this parser function.

==#ifeq==

This parser function compares two strings and determines whether they are identical.

:<tt><nowiki>{{#ifeq:</nowiki> ''string 1'' <nowiki>|</nowiki> ''string 2'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></tt>

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

:<tt><nowiki>{{#ifeq: 01 | 1 | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: 01 | 1 | yes | no}}'''
:<tt><nowiki>{{#ifeq: 0 | -0 | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: 0 | -0 | yes | no}}'''

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

:<tt><nowiki>{{#ifeq: foo | bar | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: foo | bar | yes | no}}'''
:<tt><nowiki>{{#ifeq: foo | Foo | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: foo | Foo | yes | no}}'''
:<tt><nowiki>{{#ifeq: "01" | "1" | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: "01" | "1" | yes | no}}'''

{{warning|Content inside parser tags (such as <tt><nowiki><nowiki></nowiki></tt>) is hashed before the parser functions are evaluated, resulting in errors:
<tt><nowiki>{{#ifeq: &lt;nowiki>foo&lt;/nowiki> | &lt;nowiki>foo&lt;/nowiki> | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: <nowiki>foo</nowiki> | <nowiki>foo</nowiki> | yes | no}} '''<br/>
<tt><nowiki>{{#ifeq: &lt;math>foo&lt;/math> | &lt;math>foo&lt;/math> | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: <math>foo</math> | <math>foo</math> | yes | no}} '''<br/>
<tt><nowiki>{{#ifeq: [[foo]] | [[foo]] | yes | no}}</nowiki></tt> &rarr; '''{{#ifeq: [[foo]] | [[foo]] | yes | no}} '''<br/>
}}

==#iferror==

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

:<tt><nowiki>{{#iferror:</nowiki> ''test string'' <nowiki>|</nowiki> ''value if error'' <nowiki>|</nowiki> ''value if correct'' <nowiki>}}</nowiki></tt>

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

:<tt><nowiki>{{#iferror: {{#expr: 1 + 2 }} | error | correct }}</nowiki></tt> &rarr; '''{{#iferror: {{#expr: 1 + 2 }} | error | correct }}'''
:<tt><nowiki>{{#iferror: {{#expr: 1 + X }} | error | correct }}</nowiki></tt> &rarr; '''{{#iferror: {{#expr: 1 + X }} | error | correct }}'''
:<tt><nowiki>{{#iferror: {{#expr: 1 + 2 }} | error }}</nowiki></tt> &rarr; '''{{#iferror: {{#expr: 1 + 2 }} | error }}'''
:<tt><nowiki>{{#iferror: {{#expr: 1 + X }} | error }}</nowiki></tt> &rarr; '''{{#iferror: {{#expr: 1 + X }} | error }}'''
:<tt><nowiki>{{#iferror: {{#expr: 1 + 2 }} }}</nowiki></tt> &rarr; ''' {{#iferror: {{#expr: 1 + 2 }} }}'''
:<tt><nowiki>{{#iferror: {{#expr: 1 + X }} }}</nowiki></tt> &rarr; ''' {{#iferror: {{#expr: 1 + X }} }}'''

==#ifexpr==

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

:<tt><nowiki>{{#ifexpr:</nowiki> ''expression'' <nowiki>|</nowiki> ''value if true'' <nowiki>|</nowiki> ''value if false'' <nowiki>}}</nowiki></tt>

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

:<tt><nowiki>{{#ifeq: {{#expr: </nowiki>''expression''<nowiki> }} | 0 | </nowiki> ''value if false'' <nowiki>|</nowiki> ''value if true'' <nowiki>}}</nowiki></tt>

An empty input expression evaluates to false:

:<tt><nowiki>{{#ifexpr: | yes | no}}</nowiki></tt> &rarr; '''{{#ifexpr: | yes | no}}'''

Either or both the return values may be omitted; no output is given when the appropriate branch is left empty:

:<tt><nowiki>{{#ifexpr: 1 > 0 | yes }}</nowiki></tt> &rarr; '''{{#ifexpr: 1 > 0 | yes }}'''
:<tt><nowiki>{{#ifexpr: 1 < 0 | yes }}</nowiki></tt> &rarr; ''' {{#ifexpr: 1 < 0 | yes }}'''
:<tt><nowiki>{{#ifexpr: 1 > 0 | | no}}</nowiki></tt> &rarr; ''' {{#ifexpr: 1 > 0 | | no}}'''
:<tt><nowiki>{{#ifexpr: 1 < 0 | | no}}</nowiki></tt> &rarr; ''' {{#ifexpr: 1 < 0 | | no}}'''
:<tt><nowiki>{{#ifexpr: 1 > 0 }}</nowiki></tt> &rarr; ''' {{#ifexpr: 1 > 0 }}'''

==#ifexist==

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

:<tt><nowiki>{{#ifexist:</nowiki> ''page title'' | ''value if exists'' | ''value if doesn't exist'' <nowiki>}}</nowiki></tt>

The function evaluates to <tt>true</tt> if the page exists, whether it contains content, is visibly blank (contains meta-data such as category links or [[Help:Magic words|magic words]], but no visible content), is blank, or is a [[Help:Redirect|redirect]]. Only pages that are redlinked evaluate to false, including if the page used to exist but has been deleted.

:<tt><nowiki>{{#ifexist:</nowiki> {{FULLPAGENAME}} | exists | doesn't exist <nowiki>}}</nowiki></tt> &rarr; '''{{#ifexist: {{FULLPAGENAME}} | exists | doesn't exist }}'''
:<tt><nowiki>{{#ifexist:</nowiki> XXX{{FULLPAGENAME}}XXX | exists | doesn't exist <nowiki>}}</nowiki></tt> &rarr; '''{{#ifexist: XXX{{FULLPAGENAME}}XXX | exists | doesn't exist }}'''

The function evaluates to true for [[Help:System messages|system messages]] that have been customised, and for [[Help:Special pages|special pages]] that are defined by the software.

:<tt><nowiki>{{#ifexist:</nowiki> {{#special:Watchlist}} | exists | doesn't exist <nowiki>}}</nowiki></tt> &rarr; '''{{#ifexist: Special:Watchlist | exists | doesn't exist }}'''
:<tt><nowiki>{{#ifexist:</nowiki> {{#special:CheckUser}} | exists | doesn't exist <nowiki>}}</nowiki></tt> &rarr; {{#ifexist: Special:Checkuser | '''exists''' (because the {{mediawiki|Extension:Checkuser|CheckUser extension}} is installed on this wiki) | '''doesn't exist''' (because the {{mediawiki|Extension:Checkuser|CheckUser extension}} is not installed on this wiki) }}
:<tt><nowiki>{{#ifexist: MediaWiki:Copyright | exists | doesn't exist }}</nowiki></tt> &rarr; {{#ifexist: MediaWiki:Copyright | '''exists''' (because [[{{ns:8}}:Copyright]] has been customised) | '''doesn't exist''' (because [[{{ns:8}}:Copyright]] has not been customised) }}

<tt>#ifexist:</tt> is considered an "expensive parser function", only a limited number of which can be included on any one page (including functions inside transcluded templates). When this limit is exceeded, the page is categorised into [[:Category:{{MediaWiki:expensive-parserfunction-category}}]], and any further <tt>#ifexist:</tt> functions automatically return false, whether the target page exists or not.
{{admin tip|Configure the maximum number of allowed expensive parser functions using the {{mediawiki|Manual:$wgExpensiveParserFunctionLimit|$wgExpensiveParserFunctionLimit}} variable.}}

If a page checks a target using <tt>#ifexist:</tt>, then that page will appear in the [[{{#special:WhatLinksHere}}]] list for the target page. So if the code <tt><nowiki>{{#ifexist:Foo}}</nowiki></tt> were included live on this page ({{FULLPAGENAME}}), [[{{#special:WhatLinksHere}}/Foo]] will list {{FULLPAGENAME}}.

On wikis using a shared media repository, <tt>#ifexist:</tt> can be used to check if a file has been uploaded to the repository, but not to the wiki itself:
:<tt><nowiki>{{#ifexist:</nowiki> {{ns:File}}:Example.png <nowiki>| exists | doesn't exist }}</nowiki></tt> &rarr; '''{{#ifexist: {{ns:File}}:Example.png | exists | doesn't exist }}'''
:<tt><nowiki>{{#ifexist:</nowiki> Image:Example.png <nowiki>| exists | doesn't exist }}</nowiki></tt> &rarr; '''{{#ifexist: Image:Example.png | exists | doesn't exist }}'''
:<tt><nowiki>{{#ifexist:</nowiki> {{ns:-2}}:Example.png <nowiki>| exists | doesn't exist }}</nowiki></tt> &rarr; '''{{#ifexist: {{ns:-2}}:Example.png | exists | doesn't exist }}'''
If a local description page has been created for the file, the result is '''exists''' for all of the above.

==#rel2abs==

This function converts a relative file path into an absolute filepath.

:<tt><nowiki>{{#rel2abs:</nowiki> ''path'' <nowiki>}}</nowiki></tt>
:<tt><nowiki>{{#rel2abs:</nowiki> ''path'' <nowiki>|</nowiki> ''base path'' <nowiki>}}</nowiki></tt>

Within the <tt>path</tt> input, the following syntax is valid:
*<code>.</code> &rarr; the current level
*<code>..</code> &rarr; "go up one level"
*<code>/foo</code> &rarr; "go down one level into the subdirectory /foo"

If the <tt>base path</tt> is not specified, the full page name of the page will be used instead:

:<tt><nowiki>{{#rel2abs:</nowiki> /quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: /quok | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ./quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ./quok | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../quok | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ../. | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../. | {{ns:12}}:Foo/bar/baz }}

Invalid syntax, such as <tt>/.</tt> or <tt>/./</tt>, is ignored. Since no more than two consecutive full stops are permitted, sequences such as these can be used to separate successive statements:

:<tt><nowiki>{{#rel2abs:</nowiki> ../quok/. | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../quok/. | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../../quok | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ../../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../../../quok | {{ns:12}}:Foo/bar/baz }}
:<tt><nowiki>{{#rel2abs:</nowiki> ../../../../quok | {{ns:12}}:Foo/bar/baz <nowiki>}}</nowiki></tt> &rarr; '''{{#rel2abs: ../../../../quok | {{ns:12}}:Foo/bar/baz }}

==#switch==

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

<nowiki>{{#switch:</nowiki> ''comparison string''
| ''case'' = ''result''
| ''case'' = ''result''
| ''...''
| ''case'' = ''result''
| ''default result''
<nowiki>}}</nowiki>

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

:<tt><nowiki>{{#switch: test | foo = Foo | #default = Bar | baz = Baz }}</nowiki></tt> &rarr; '''{{#switch: test | foo = Foo | #default = Bar | baz = Baz }}'''

If the <tt>''default''</tt> parameter is omitted and no match is made, no content is added. It is possible to have 'fall through' values, where several <tt>''case''</tt> strings return the same <tt>''result''</tt> string. This minimises duplication.

<nowiki>{{#switch:</nowiki> ''comparison string''
| ''case1'' = ''result1''
| ''case2''
| ''case3''
| ''case4'' = ''result2''
| ''case5'' = ''result3''
| ''case6''
| ''case7'' = ''result4''
| ''default result''
<nowiki>}}</nowiki>

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

As with <tt>[[##ifeq|#ifeq]]</tt>, the comparison is made numerically if both the comparison string and the case string being tested are numeric; or as case-sensitive string otherwise. A <tt>''case''</tt> string may be empty:
:<tt><nowiki>{{#switch: | = Nothing | foo = Foo | Something }}</nowiki></tt> &rarr; '''{{#switch: | = Nothing | foo = Foo | Something }}'''
Once a match is found, subsequent <tt>''cases''</tt> are ignored:
:<tt><nowiki>{{#switch: b | f = Foo | b = Bar | b = Baz | }}</nowiki></tt> &rarr; '''{{#switch: b | f = Foo | b = Bar | b = Baz | }}'''
{{warning}}
"Case" strings cannot contain raw equals signs. To work around this, create a template at {{tl|{{=}}}} containing a single equals sign: <tt>=</tt> Then, use this template as follows:

<tt><nowiki>{{#switch: 1=2 | 1=2 = raw | 1&lt;nowiki>=&lt;/nowiki>2 = nowiki | 1&amp;#61;2 = html | 1{{=}}2 = template | foo }}</nowiki></tt> &rarr; '''{{#switch: 1=2 | 1=2 = raw | 1<nowiki>=</nowiki>2 = nowiki | 1&#61;2 = html | 1{{=}}2 = template | foo}}'''

==#time==

{| {{prettytable|style=float:right; width:40%; font-size:90%; line-height:1.25em; margin-left:1em;}}
! {{hl1}} | Code
! {{hl1}} | Description
! {{hl1}} | Current output
|-
! {{hl2}} colspan="3"| Year
|-
| style="text-align:center;" | Y
| 4-digit year.
| style="text-align:center;" | {{#time:Y}}
|-
| style="text-align:center;" | y
| 2-digit year.
| style="text-align:center;" | {{#time:y}}
|-
| style="text-align:center;" | L
| 1 or 0 whether it's a leap year or not
| style="text-align:center;" | {{#time:L}}
|-
| style="text-align:center;" | o ¹
| ISO-8601 year number. ²
| style="text-align:center;" | {{#time:o}} ³
|-
| colspan="3" style="font-size:88%" |
¹ Requires PHP 5.1.0 and newer and [[rev:45208]]<br />
² 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.<br />
³ Will output literal ''o'' if ¹ not fulfilled
|-
! {{hl2}} colspan="3"| Month
|-
| style="text-align:center;" | n
| Month index, not zero-padded.
| style="text-align:center;" | {{#time:n}}
|-
| style="text-align:center;" | m
| Month index, zero-padded.
| style="text-align:center;" | {{#time:m}}
|-
| style="text-align:center;" | M
| An abbreviation of the month name, in the site language.
| style="text-align:center;" | {{#time:M}}
|-
| style="text-align:center;" | F
| The full month name in the site language.
| style="text-align:center;" | {{#time:F}}
|-
! {{hl2}} colspan="3"| Week
|-
| style="text-align:center;" | W
| ISO 8601 week number, zero-padded.
| style="text-align:center;" | {{#time:W}}
|-
! {{hl2}} colspan="3"| Day
|-
| style="text-align:center;" | j
| Day of the month, not zero-padded.
| style="text-align:center;" | {{#time:j}}
|-
| style="text-align:center;" | d
| Day of the month, zero-padded.
| style="text-align:center;" | {{#time:d}}
|-
| style="text-align:center;" | z
| Day of the year (January&nbsp;1 = 0)
| style="text-align:center;" | {{#time:z}}
|-
| style="text-align:center;" | D
| An abbreviation for the day of the week. Rarely internationalised.
| style="text-align:center;" | {{#time:D}}
|-
| style="text-align:center;" | l
| The full weekday name. Rarely internationalised.
| style="text-align:center;" | {{#time:l}}
|-
| style="text-align:center;" | N
| ISO 8601 day of the week.
| style="text-align:center;" | {{#time:N}}
|-
| style="text-align:center;" | w
| number of the day of the week (Monday = 1).
| style="text-align:center;" | {{#time:w}}
|-
! {{hl2}} colspan="3"| Hour
|-
| style="text-align:center;" | a
| "am" during the morning (00:00:00 &rarr; 11:59:59), "pm" otherwise (12:00:00 &rarr; 23:59:59)
| style="text-align:center;" | {{#time:a}}
|-
| style="text-align:center;" | A
| Uppercase version of <tt>a</tt> above.
| style="text-align:center;" | {{#time:A}}
|-
| style="text-align:center;" | g
| Hour in 12-hour format, not zero-padded.
| style="text-align:center;" | {{#time:g}}
|-
| style="text-align:center;" | h
| Hour in 12-hour format, zero-padded.
| style="text-align:center;" | {{#time:h}}
|-
| style="text-align:center;" | G
| Hour in 24-hour format, not zero-padded.
| style="text-align:center;" | {{#time:G}}
|-
| style="text-align:center;" | H
| Hour in 24-hour format, zero-padded.
| style="text-align:center;" | {{#time:H}}
|-
! {{hl2}} colspan="3"| Minutes and seconds
|-
| style="text-align:center;" | i
| Minutes past the hour, zero-padded.
| style="text-align:center;" | {{#time:i}}
|-
| style="text-align:center;" | s
| Seconds past the minute, zero-padded.
| style="text-align:center;" | {{#time:s}}
|-
| style="text-align:center;" | U
| Seconds since January 1 1970 00:00:00 GMT.
| style="text-align:center;" | {{#time:U}}
|-
! {{hl2}} colspan="3"| Miscellaneous
|-
| style="text-align:center;" | L
| 1 if this year is a leap year in the Gregorian calendar, 0 otherwise
| style="text-align:center;" | {{#time:L}}
|-
| style="text-align:center;" | t
| Number of days in the current month.
| style="text-align:center;" | {{#time:t}}
|-
| style="text-align:center;" | c
| ISO 8601 formatted date, equivalent to <tt><nowiki>Y-m-dTH:i:s+00:00</nowiki></tt>.
| style="font-size:75%;" | {{#time:c}}
|-
| style="text-align:center;" | r
| RFC 2822 formatted date, equivalent to <tt><nowiki>D, j M Y H:i:s +0000</nowiki></tt>, with weekday name and month name not internationalised.
| style="font-size:75%;" | {{#time:r}}
|-
! {{hl2}} colspan="3" | Non-Gregorian calendars
|-
! {{hl3}} colspan="3" | Iranian
|-
| xij
| Day of the month
| {{#time:xij}}
|-
| xiF
| Full month name
| {{#time:xiF}}
|-
| xin
| Month index
| {{#time:xin}}
|-
| xiY
| Full year
| {{#time:xiY}}
|-
! {{hl3}} colspan="3" | Hebrew
|-
| xjj
| Day of the month
| {{#time:xjj}}
|-
| xjF
| Full month name
| {{#time:xjF}}
|-
| xjx
| Genitive form of the month name
| {{#time:xjx}}
|-
| xjn
| Month number
| {{#time:xjn}}
|-
| xjY
| Full year
| {{#time:xjY}}
|-
! {{hl3}} colspan="3" | Thai solar
|-
| xkY
| Full year
| {{#time:xkY}}
|-
! {{hl2}} colspan="3" | Flags
|-
| xn
| Format the next numeric code as a raw ASCII number.
|In the Hindi language, <tt><nowiki>{{#time:H, xnH}}</nowiki></tt> produces ०६, 06
|-
| xN
| colspan="2" | Like <tt>xn</tt>, but as a toggled flag, which endures until the end of the string or until the next appearance of <tt>xN</tt> in the string.
|-
| xr
| Format the next number as a roman numeral. Only works for numbers up to 3000.
| <tt><nowiki>{{#time:xrY}}</nowiki></tt> &rarr; {{#time:xrY}}
|-
| xg
| colspan="2" | Before a month flag (<tt>n</tt>, <tt>m</tt>, <tt>M</tt>, <tt>F</tt>), output the genitive form if the site language distinguishes between genitive and nominative forms.
|}

This parser function takes a date and/or time construct and formats it according to the syntax given. A date/time object can be specified; the default is the value of the [[Help:Magic words#Date & time|magic word]] <tt><nowiki>{{CURRENTTIMESTAMP}}</nowiki></tt> &ndash; that is, the time the page was last rendered into HTML.

:<tt><nowiki>{{#time:</nowiki> ''format string'' <nowiki>}}</nowiki></tt>
:<tt><nowiki>{{#time:</nowiki> ''format string'' <nowiki>|</nowiki> ''date/time object'' <nowiki>}}</nowiki></tt>

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

:<tt><nowiki>{{#time: Y-m-d }}</nowiki></tt> &rarr; '''{{#time: Y-m-d }}'''
:<tt><nowiki>{{#time: [[Y]] m d }}</nowiki></tt> &rarr; '''{{#time: [[Y]] m d }}'''
:<tt><nowiki>{{#time: [[Y (year)]] }}</nowiki></tt> &rarr; '''{{#time: [[Y (year)]] }}'''
:<tt><nowiki>{{#time: [[Y "(year)"]] }}</nowiki></tt> &rarr; '''{{#time: [[Y "(year)"]] }}'''
:<tt><nowiki>{{#time: i's" }}</nowiki></tt> &rarr; '''{{#time: i's" }}'''

The <tt>''date/time object''</tt> can be in any format accepted by PHP's [http://uk3.php.net/manual/en/function.strtotime.php strtotime()] function. Both absolute (eg <tt>20 December 2000</tt>) and relative (eg <tt>+20 hours</tt>) times are accepted.

{{warning|The range of acceptable input is January 1 0100 &rarr; December 31 9999. Values outside this range will be misinterpreted:<br />
<tt><nowiki>{{#time: d F Y | 15 April 0099 }}</nowiki></tt> &rarr; '''{{#time: d F Y | 15 April 0099 }}<br />
<tt><nowiki>{{#time: d F Y | 15 April 10000 }}</nowiki></tt> &rarr; '''{{#time: d F Y | 15 April 10000 }}
}}

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:

:<tt><nowiki>{{#time: Y | January 1 }}</nowiki></tt> &rarr; '''{{#time: Y | January 1 }}

A four-digit number is interpreted as hours and minutes if possible, and otherwise as year:<br />
<tt><nowiki>{{#time: Y m d H:i:s | 1959 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | 1959 }} <small>Input is treated as a time rather than a year.</small><br />
<tt><nowiki>{{#time: Y m d H:i:s | 1960 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | 1960 }} <small>Since 19:60 is not a valid time, 1960 is treated as a year.</small><br />

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):<br />
<tt><nowiki>{{#time: Y m d H:i:s | 195909 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | 195909 }} <small>Input is treated as a time rather than a year+month code.</small><br />
<tt><nowiki>{{#time: Y m d H:i:s | 196009 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | 196009 }} <small>Although 19:60:09 is not a valid time, 196009 is not interpreted as September 1960.</small><br />

{{warning|The fill-in feature is not consistent; some parts are filled in using the current values, others are not:<br />
<tt><nowiki>{{#time: Y m d H:i:s | January 1 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | January 1 }}<br />
<tt><nowiki>{{#time: Y m d H:i:s | February 2007 }}</nowiki></tt> &rarr; '''{{#time: Y m d H:i:s | February 2007 }} <small>Goes to the start of the month, not the current day.</small>
}}

The function performs a certain amount of date mathematics:

:<tt><nowiki>{{#time: d F Y | January 0 2008 }}</nowiki></tt> &rarr; '''{{#time: d F Y | January 0 2008 }}
:<tt><nowiki>{{#time: d F | January 32 }}</nowiki></tt> &rarr; '''{{#time: d F | January 32 }}
:<tt><nowiki>{{#time: d F | February 29 2008 }}</nowiki></tt> &rarr; '''{{#time: d F | February 29 2008 }}
:<tt><nowiki>{{#time: d F | February 29 2007 }}</nowiki></tt> &rarr; '''{{#time: d F | February 29 2007 }}

<br clear="all" />

==#timel==
This function is identical to <tt><nowiki>{{#time: ... }}</nowiki></tt>, except that it uses the local time of the wiki (as set in {{mediawiki|manual:$wgLocaltimezone|$wgLocaltimezone}}) when no date is given.

:<tt><nowiki>{{#time: Y-m-d }}</nowiki></tt> &rarr; '''{{#time: Y-m-d }}'''
:<tt><nowiki>{{#timel: Y-m-d }}</nowiki></tt> &rarr; '''{{#time: Y-m-d }}'''
:<tt><nowiki>{{#time: Y F d h:i:s}}</nowiki></tt> &rarr; '''{{#time: Y F d h:i:s}}'''
:<tt><nowiki>{{#timel: Y F d h:i:s}}</nowiki></tt> &rarr; '''{{#time: Y F d h:i:s}}'''

==#titleparts==

This function separates a pagetitle into segments based on slashes, then returns some of those segments as output.

:<tt><nowiki>{{#titleparts:</nowiki> ''pagename'' <nowiki>|</nowiki> ''number of segments to return'' <nowiki>|</nowiki> ''first segment to return'' <nowiki>}}</nowiki></tt>

If the <tt>number of segments</tt> parameter is not specified, it defaults to "0", which returns ''all'' the segments. If the <tt>first segment</tt> parameter is not specified or is "0", it defaults to "1":

:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>}}</nowiki></tt> &rarr; '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok }}'''
:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| 1 }}</nowiki></tt> &rarr; '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| 2 }}</nowiki></tt> &rarr; '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 2 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| 2 | 2 }}</nowiki></tt> &rarr; '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | 2 | 2 }}'''

Negative values are accepted for both values. Negative values for <tt>number of segments</tt> effectively 'strips' segments from the end of the string. Negative values for <tt>first segment</tt> translates to "add this value to the total number of segments", loosely equivalent to "count from the right":

:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| -1 }}</nowiki></tt> &rarr; '''{{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| | -1 }}</nowiki></tt> &rarr; ''' {{#titleparts: {{ns:1}}:Foo/bar/baz/quok | | -1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> {{ns:1}}:Foo/bar/baz/quok <nowiki>| -1 | 2 }}</nowiki></tt> &rarr; ''' {{#titleparts: {{ns:1}}:Foo/bar/baz/quok | -1 | 2 }}''' <small>Strips one segment from the end of the string, then returns the second segment and beyond</small>

The string is split a maximum of 25 times; further slashes are ignored. The string is also limited to 255 characters, as it is treated as a page title:

:<tt><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/y/z/aa/bb/cc/dd/ee <nowiki>| 1 | 25 }}</nowiki></tt> &rarr; '''{{#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 }}'''

{{warning}}
You can use #titleparts as a small "string parser & converter", but consider that it returns the first substring capitalized. If lower case is needed, use lc: function to control output.

:<tt><nowiki>{{#titleparts:</nowiki> one/two/three/four<nowiki>|1|1 }}</nowiki></tt> &rarr; '''{{#titleparts:one/two/three/four|1|1 }}'''
:<tt><nowiki>{{#titleparts:</nowiki> one/two/three/four<nowiki>|1|2 }}</nowiki></tt> &rarr; '''{{#titleparts:one/two/three/four|1|2 }}'''
:<tt><nowiki>{{lc:{{#titleparts:</nowiki> one/two/three/four<nowiki>|1|1 }} }}</nowiki></tt> &rarr; '''{{lc:{{#titleparts:one/two/three/four|1|1 }}}}'''

==General points==
===Substitution===
Parser functions can be [[w:Help:Substitution|substituted]] by prefixing the hash character with <tt>'''subst:'''</tt>:

:<tt><nowiki>{{subst:#ifexist:</nowiki> {{FULLPAGENAME}} <nowiki>| [[</nowiki>{{FULLPAGENAME}}<nowiki>]] | </nowiki>{{FULLPAGENAME}}<nowiki> }}</nowiki></tt> &rarr; the code '''<tt><nowiki>[[</nowiki>{{FULLPAGENAME}}<nowiki>]]</nowiki></tt>''' will be inserted in the wikitext since the page {{FULLPAGENAME}} exists.

{{warning|The results of substituted parser functions are undefined if the expressions contain ''un''substituted volatile code such as [[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]].}}

===Tables ===
Parser functions will mangle [[Help:Tables|wikitable]] syntax, treating all the raw pipe characters as parameter divisors. To avoid this, most wikis create the template [[{{ns:10}}:!]] with its contents only a raw pipe character. 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. Alternatively, raw HTML table syntax can be used, although this is less intuitive and more error-prone.
{{admin tip|You can improve the reliability of HTML table code (and other raw HTML input) by using HTML tidy. See {{mediawiki|Manual:Build Tidy from source|Installing Tidy}} and {{mediawiki|Manual:$wgUseTidy|$wgUseTidy}}.}}
{{admin tip|You can also use the {{mediawiki|Extension:Pipe Escape|Pipe Escape}} extension to improve the handling of pipe characters.}}

===Stripping whitespace===

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, adding any non-whitespace characters (including the HTML ''encoding'' for a whitespace character, invisible Unicode characters such as the zero-width space or direction marks, or sequences recognised and stripped by the MediaWiki parser such as <tt><nowiki><nowiki/></nowiki></tt>) will prevent further stripping:

<pre>{{#ifeq: foo | foo | equal | not equal }}</pre>
:&rarr; '''{{#ifeq: foo | foo | equal | not equal }}'''
<pre>{{#ifeq: &amp;#32;foo &amp;#32; | &amp;#32; foo&amp;#32; | equal | not equal }}</pre>
:&rarr; '''{{#ifeq: &#32; foo &#32; | &#32; foo &#32; | equal | not equal }}'''
<pre>{{#ifeq: <nowiki/>foo <nowiki/> | <nowiki/> foo<nowiki/> | equal | not equal }}</pre>
:&rarr; '''{{#ifeq: <nowiki/> foo <nowiki/> | <nowiki/> foo <nowiki/> | equal | not equal }}'''

==See also==
*[[Help:Parser functions in templates]]
*{{Mediawiki|meta:Help:Newlines and spaces|Help:Newlines and spaces}}
*{{Mediawiki|Help:Magic words}}
* [[Extension:StringFunctions]]

{{Languages|Help:Extension:ParserFunctions}}

[[Category:Extension help|ParserFunctions]]
Анонимный участник

Навигация