利用者:わたあめひみつ/条件文2

Template:MoveToMediaWiki Other languages: Template:ParserFunctions

This MediaWiki extension is a collection of parser functions. These parser functions have a hash character in front of the function name, so they typically have the syntax:

{{#functionname: argument 1 | argument 2 | argument 3...}}

Functions[編集]

This module defines seven functions at present: expr, if, ifeq, ifexist, ifexpr, switch, and time.

#expr:[編集]

The expr function computes mathematical expressions based on permutations of numbers (or variables/parameters that translate to numbers) and operators. It does not work with strings; use ifeq below instead. The syntax is:

{{ #expr: expression }}

A list of supported operators follows. For more details about the operator precedence see Help:Calculation, it's roughly (1) grouping (parentheses), (2) unary (+/- signs and NOT), (3) multiplicative (*, /, div, mod), (4) additive (+ and -), (5) round, (6) comparative (=, !=, <, >, etc.), (7) logical AND, (8) logical OR. Within the same precedence class operators are evaluated left to right. As always some redundant parentheses are better than erroneous terse code.

Operator Operation Example
none {{#expr: 123456789012345}} = 1.2345678901234E+14
{{#expr: 0.000001}} = 1.0E-6
( ) Grouping operators {{#expr: (30 + 7) * 7 }} = 259
+ Unary + sign {{#expr: +30 * +7}} = 210
- Unary - sign (negation) {{#expr: -30 * -7}} = 210
not Unary NOT, logical NOT {{#expr: not 0 * 7}} = 7
{{#expr: not 30+7}} = 7
* Multiplication {{#expr: 30 * 7}} = 210
/ Division, same as div {{#expr: 30 / 7}} = 4.2857142857143
div Division, same as /,
no integer division
{{#expr: 30 div 7}} = 4.2857142857143
{{#expr: 5 div 2 * 2 + 5 mod 2}} = 6
mod "Modulo", remainder of division after truncating both operands to an integer.
Caveat, div and mod are different from all programming languages.
{{#expr: 30 mod 7}} = 2
{{#expr: -8 mod -3}} = -2
{{#expr: -8 mod +3}} = -2
{{#expr: 8 mod 2.7}} = 0
{{#expr: 8 mod 3.2}} = 2
{{#expr: 8.9 mod 3}} = 2
+ Addition {{#expr: 30 + 7}} = 37
- Subtraction {{#expr: 30 - 7}} = 23
round Rounds off the number on the left to the power of 1/10 given on the right {{#expr: 30 / 7 round 3}} = 4.286
{{#expr: 30 / 7 round 0}} = 4
{{#expr: 3456 round -2}} = 3500
= Equality (numerical incl. logical) {{#expr: 30 = 7}} = 0
<> Inequality, same as != {{#expr: 30 <> 7}} = 1
!= Inequality, same as <>, logical xor {{#expr: 1 != 0}} = 1
< Less than {{#expr: 30 < 7}} = 0
> Greater than {{#expr: 30 > 7}} = 1
<= Less than or equal to {{#expr: 30 <= 7}} = 0
>= Greater than or equal to {{#expr: 30 >= 7}} = 1
and Logical AND {{#expr: 4<5 and 4 mod 2}} = 0
or Logical OR {{#expr: 4<5 or 4 mod 2}} = 1

The boolean operators consider 0 to be "false" and any other number to be "true", on output "true" is shown as 1.

Numbers are given in decimal with "." for the decimal point. The formatnum: function can be used to change the decimal point to a comma for the appropriate locales. Scientific notation with E plus exponent is not yet supported on input for expressions, but used on output, for details see Help:Calculation.


#if:[編集]

The if function is an if-then-else construct. The syntax is:

{{ #if: <condition> | <then text> | <else text> }}

If the condition is an empty string or consists only of whitespace, then it is considered false, and the else text is returned. Otherwise, the then text is returned. The else text may be omitted, in which case the result will be blank if the condition is false.

An example:

                      {{Template|parameter=something}}  {{Template}} {{Template|parameter=}}
                                     |                        |                |
                                     |                        |                |
                                     |                        |                |
{{ #if: {{{parameter|}}} | Parameter is defined. | Parameter is undefined, or empty }}

Note that the if function does not support "=" signs or mathematical expressions. {{#if: 1 = 2|yes|no}} will return "yes", because the string "1 = 2" is not blank. It is intended as an "if not empty" structure.


#ifeq:[編集]

ifeq compares two strings or numbers, and returns another string depending on the result of that comparison. The syntax is:

{{ #ifeq: <text 1> | <text 2> | <equal text> | <not equal text> }}

If both strings can be interpreted as numbers the comparison is numerical. To force a string comparison add tokens that can't be interpreted as numbers:

{{ #ifeq: +07 | 007 | 1 | 0 }} gives 1
{{ #ifeq:"+07"|"007"| 1 | 0 }} gives 0

Comparison of strings is case-sensitive:

{{ #ifeq: A | a | 1 | 0 }} gives 0
For compatibility with older templates #if: cannot directly distinguish defined and undefined parameter values, it's a shorthand for a comparison with the empty string. With #ifeq: it's directly possible to identify undefined parameters:
{{ #if: {{{x| }}}|not blank|blank}} = blank,
{{ #ifeq: {{{x| }}}| |blank|not blank}} = blank,
{{ #ifeq: {{{x| }}}|{{{x|u}}}|defined|undefined}} = undefined.
An undefined parameter without default counts in the comparison as a string consisting of the tag:
{{ #ifeq: {{{x}}}|{{ concat|{|{|{x}|}|} }}|1|0}} = 1.


#ifexist:[編集]

ifexist returns one of two results based on whether or not a named title exists. The usual case-sensitivity applies: if a page exists then also a non-canonical name for that page gives a positive result. E.g. on Meta:

{{#ifexist:Bugs|Foo|RFC 3092}} gives RFC 3092, because Bugs exists
{{#ifexist:bugs|Foo|RFC 3092}} gives RFC 3092, because bugs is in canonical form the existing Bugs
{{#ifexist:BUGS|Foo|RFC 3092}} gives Foo because BUGS does not exist
{{#ifexist:m:Help:Calculation|Yes|Oops}} gives Oops although m:Help:Calculation exists, because of the interwiki prefix.

The first parameter is the title to check for, the second is the positive result, and the third, the negative result. If the parameter passed does not produce a valid title object, then the result is negative.

m:Template:exists (リンク元編集) gives the same result, except that the result is positive for an interwiki link. m:Template:if interwiki link (リンク元編集) exploits this difference.


#ifexpr:[編集]

ifexpr evaluates a mathematical expression and returns one of two strings depending on the result.

{{#ifexpr: <expression> | <then text> | <else text> }}

If the expression evaluates to zero, then the else text is returned, otherwise the then text is returned. Expression syntax is the same as for expr.

At the moment the else text is also returned for an empty expression:
{{#ifexpr: {{ns:0}}|Toast|'''or else'''}} gives or else
Omitting both then text and else text gives no output except possibly an error message; this can be used to check the correctness of an expression, or to check the wording of the error message (emulated assertions, forced errors):
{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|1|0}}}} 0 で除算しました。
{{#ifexpr: 1/{{#ifeq: {{ns:4}}|Meta|0|1}}}}
{{#if:{{#ifexpr: 1=2}}|wrong|correct}} correct
{{#if:{{#ifexpr: 1E2}}|wrong|correct}} correct
{{#if:{{#ifexpr: 1/0}}|wrong|correct}} wrong
{{#if:{{#ifexpr: a=b}}|wrong|correct}} wrong

For an application, see also m:Template:evalns (リンク元編集).


#switch:[編集]

switch compares a single value against multiple others, returning a string if a match is found. The syntax is basically:

{{ #switch: <comparison value>
| <value1> = <result1>
| <value2> = <result2>
| ...
| <valuen> = <resultn>
| <default result>
}}

switch will search through each value passed until a match is found with the comparison value. When found, the result for that value is returned (the text string after the equal sign). If no match is found, but the last item has no equal sign in it, it will be returned as the default result. If your default result must have an equal sign, you may use #default:

{{ #switch: <comparison value>
| <value> = <result>
| #default = <default result>
}}

Note that it's also possible to have "fall through" for values (reducing the need to duplicate results). For example:

{{ #switch: <comparison value>
| <value1>
| <value2>
| <value3> = <result3>
| ...
| <valuen> = <resultn>
| <default result>
}}

Note how value1 and value2 contain no equal sign. If they're matched, they are given the result for value3 (that is, whatever is in result3).

As for #ifeq: the comparison is numerical where possible:
Template:Fl gives Yes
Template:Fl gives No
The matched value can be empty, therefore the following constructs are equivalent:
Template:Fl gives empty
Template:Fl gives empty

Comparison of strings is case-sensitive:

Template:Fl gives UPPER
Template:Fl gives UPPER
Template:Fl gives lower

This is not to be confused with the fact that parser function names are case-insensitive:

Template:Fl gives UPPER

To have the #switch statement be case-insensitive, use the construct {{lc:}} or {{uc:}}

Template:Fl gives lower
Template:Fl gives lower
Template:Fl gives lower

This is usually used in templates, when you want to have case-insensitivity on in param values:

Template:Fl gives abc or ABC


#switch may be used instead of #ifeq:

Template:Fl gives true
Template:Fl gives true

#time:[編集]

#time is a time and date formatting function. The syntax is either

{{ #time: format }}

or

{{ #time: format | time }}

If the time is not specified, the time at which the article is converted to HTML is used. Note that due to caching, this may be up to a week different to the time at which the article is viewed. Manual updates may be required, this can be achieved by saving the page without making any changes (a "null edit") or viewed with action=purge in search string of URL or viewed by a user whose option is "Disable page caching".

The format parameter is a format string similar to the one used by PHP's date.

The following format codes have the same meaning as in PHP. Significant differences from PHP's behaviour, apart from internationalisation (i.e. language and locale differences), should be considered an error of ParserFunctions and should be reported. All numeric format codes return numbers formatted according to the local language, use the xn code described below to override this.

Code Description Example output Current output
Y The 4-digit year. e.g. 2006 2024
y The 2-digit year. 00 to 99, e.g. 06 for year 2006. 24
L Whether it's a leap year. 1 if it is a leap year, 0 otherwise. 1
n The month number, not zero-padded. 1 to 12 5
m The month number, zero-padded. 01 to 12 05
M An abbreviation of the month name. Often internationalised. Jan to Dec 5月
F The full month name. Often internationalised. January 5月
t Number of days in the month. 28 to 31 31
j The day of the month, not zero-padded. 1 to 31 23
d The day of the month, zero-padded. 01 to 31 23
z The day of the year (starting from 0) 0 to 364, or 365 on a leap year 143
D An abbreviation for the day of the week. Rarely internationalised. Mon to Sun
l The full weekday name. Rarely internationalised. Monday to Sunday 木曜日
w number of the day of the week (according to the American week). 0 (for Sunday) through 6 (for Saturday) 4
N ISO 8601 day of the week (according to the ISO 8601 week). 1 (for Monday) through 7 (for Sunday) 4
W ISO 8601 week number (ISO years have full weeks from monday to sunday,

and ISO week number 1 always contains January 4 or the first thursday of the civil year).

1 to 52 or 53 (depends on year) 21
a am (between 01:00:00 and 12:59:59 on the same day) or pm, with lowercase (used with the 12-hour format). am or pm pm
A Same as with code a above, but with uppercase. AM or PM PM
g 12-hour format of the hour without leading zeros (one or two digits, used with am/pm or AM/PM). 1 to 12 6
h 12-hour format of the hour, with leading padding zero (two digits, used with am/pm or AM/PM). 01 to 12 06
G 24-hour format of the hour, without leading padding zero (one or two digits). 0 to 23 18
H 24-hour format of the hour, with leading pading zero (two digits). 00 to 23 18
i The minute, with leading padding zero (two digits). 00 to 59 59
s The second, with leading padding zero (two digits). 00 to 59 34
U Seconds since January 1 1970 00:00:00 GMT. 0 to infinite 1716490774
c ISO 8601 formatted date, same as {{#time:Y-m-dTH:m:s{{#time:+H:m|+0 hours}}}}. fixed length string 2024-05-23T18:59:34+00:00
r RFC 2822 formatted date, same as {{#time:D, j M Y H:m:s {{#time:+H:m|+0 hours}}}}. variable length string Thu, 23 May 2024 18:59:34 +0000

The following format codes are extensions to the PHP syntax:

Code Description
xn Format the next numeric code as a raw ASCII number. For example, in the Hindi language, {{ #time:H, xnH}} produces ०६, 06.
xN Toggle a permanent raw number formatting flag. Like xn, except it lasts until the end of the string or until the same code appears again.
xr Format the next numeric code as a roman numeral. Only works for numbers up to 3000.
xg Output the genitive form of the month name, for languages which make such a distinction between genitive and nominative.
xx A literal "x"

Any unrecognised character will be passed through to the output unmodified. There are also two quoting conventions which can be used to output literal characters.

  • Characters enclosed in double quotes will be considered literal (with the quotes themselves removed). Unmatched quotes will be considered literal quotes. Example:
    • {{ #time: "The month is" F}} → The month is January
    • {{ #time:i's"}} → 20'11"
  • Backslash escaping as in PHP's date() is supported. \H produces a literal H, \" produces a literal ".

More format codes may be added in the future, as demanded by the users of this extension. This may come in the form of either a more complete implementation of PHP's format codes, or additional "x" codes.

The format of the time parameter is identical to the one used by PHP's strtotime(). It supports both absolute and relative dates, such as "December 11" and/or "+10 hours", which can be used for timezone translation. Please see the GNU tar manual for more information.

Examples[編集]

  • {{#time:j F Y|-14 days}} gives 9 5月 2024 (14 days ago)
  • {{#time:H:i|+6 hours}} gives 00:59 (6 hours later than UTC)
  • {{#time:H:i|8:15 +6 hours}} gives 14:15
  • {{#time:m/Y|-1 months}}gives 04/2024 (1 month ago)

Range[編集]

The range of proper functioning is 1970-1-1 00:00:01 through 2038-1-19 03:14:07 (1 through 1 seconds after the start of 1970). See also w:en:Year 2038 problem.

February 29[編集]

Caution should be taken with February 29, as {{#time:j|February 29}} will vary with the year. For example

  • {{#time:j|February 29 2006}} gives 1
  • {{#time:j|February 29 2008}} gives 29


Caveats[編集]

Most observed problems turned out to be general issues not limited to parser functions.

Substitution[編集]

Applying "subst:" to a parser function works, provided that there is no space between "subst:" and "#". For details see Help:Substitution. Note that unless a technique like optional substitution is used, substituting a template which uses a parser function does not replace that parser function with its result. This is often undesirable.

Like other parser functions the parser functions in this extension are affected by 5678 in a predictable way. Summary: undefined parameters can be overwritten by corresponding parameters, for details see /5678 and Substitution. Substitution is the only case where this is critical with respect to parameter defaults. It doesn't affect defined parameters.

Tables[編集]

Currently wiki pipe table syntax doesn't work inside conditionals, there are two main workarounds.

  • Hide the pipe from parser functions by putting it in a template, e.g. m:Template:! (リンク元編集) as on w:en:.
  • Use html style table syntax instead.
  • See also Help:Table, completely empty rows or columns are not displayed. Empty cells could be also transformed into dummy &nbsp; cells on pages not affected by 5569.

Note that "|" and "=" were always tricky within templates, this is no new issue.

If all else fails, try setting $wgUseTidy=true; in your LocalSettings.php.

Expressions[編集]

  • div is no integer division and (as is) redundant, use / (slash) for real divisions.
  • mod uses PHP's % operator, which is different from modulo-operators in all other programming languages, see also m:Template:mod (リンク元編集) and 6068.
  • mod sometimes returns wrong results for the same input values, see 6356 and /MOD10000. Update: values less than 1E+12 are apparently not affected.
  • Valid #expr: results like 1.0E-7 are not yet supported as #expr: input:
    {{#expr:1.0E-7}} yields 1.0E-7.
  • Under certain conditions round 0 results in -0 instead of 0. For an expression x using 0+(x) fixes this oddity.

Conditional whitespace[編集]

Because conditionals trim the leading and trailing whitespaces around pipe characters (unlike template parameters), inserting a conditional whitespace character is not always simple. If you only want to insert spaces, you can use the HTML entity &#32;, which inserts " ".

if you want to insert new lines or other whitespace, you can insert non-printing characters between the pipe and the whitespace:

first paragraph. {{#if:{{{paragraph}}}|<nowiki /> 

second paragraph.}}

first paragraph.

second paragraph.


Installation[編集]

Download both of these files and put them in a new directory called ParserFunctions in your extensions directory.

Then put the following at the end of your LocalSettings.php:

require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );

You can also browse the code tree here:

1.8 and 1.9alpha[編集]

All the ParserFunctions work under 1.8 and 1.9alpha, also in the localised forms.

1.7[編集]

All the ParserFunctions work under 1.7, but only in English. However, the extension may cause problems when used together with the Cite extension; cf. [1].

1.6[編集]

When including the ParserFunctions in 1.6, some notices may be shown. Removing the following line (line 10) in ParserFunctions.php fixes the problem:

$wgHooks['LanguageGetMagic'][]       = 'wfParserFunctionsLanguageGetMagic';

Most ParserFunctions (except #if, which does not work at all) work just as well on MediaWiki 1.6, but the syntax of ParserFunctions is without the '#' character. If you want to use the '#' character, find this section of ParserFunctions.php:

 $wgParser->setFunctionHook( 'expr', array( &$wgExtParserFunctions, 'expr' ) );
 $wgParser->setFunctionHook( 'if', array( &$wgExtParserFunctions, 'ifHook' ) );
 $wgParser->setFunctionHook( 'ifeq', array( &$wgExtParserFunctions, 'ifeq' ) );
 $wgParser->setFunctionHook( 'ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) );
 $wgParser->setFunctionHook( 'switch', array( &$wgExtParserFunctions, 'switchHook' ) );
 $wgParser->setFunctionHook( 'ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );

Then, replace it with this:

 $wgParser->setFunctionHook( '#expr', array( &$wgExtParserFunctions, 'expr' ) );
 $wgParser->setFunctionHook( '#if', array( &$wgExtParserFunctions, 'ifHook' ) );
 $wgParser->setFunctionHook( '#ifeq', array( &$wgExtParserFunctions, 'ifeq' ) );
 $wgParser->setFunctionHook( '#ifexpr', array( &$wgExtParserFunctions, 'ifexpr' ) );
 $wgParser->setFunctionHook( '#switch', array( &$wgExtParserFunctions, 'switchHook' ) );
 $wgParser->setFunctionHook( '#ifexist', array( &$wgExtParserFunctions, 'ifexist' ) );

A simple fix for #if in this version -

Replace:

 function ifHook( &$parser, $test = '', $then = '', $else = '' ) {
  if ($test !== '') {

on line 57 with:

 function ifHook( &$parser, $test = '', $then = '', $else = '' ) {
  if ( (string)$test !== '' ){

See also[編集]


External links[編集]

<!--Categories--> [[Category:MediaWiki extensions]]

現行の日本文[編集]

この文書は、テンプレート等における条件文についてのMediaWiki拡張機能を説明する文書です。条件文はすべて次のような文法を持っています。

{{#機能名: 引数 1 | 引数 2 | 引数 3...}}

各機能[編集]

このモジュールには現在、exprififeqifexistifexprswitchTIMEの7つの機能が定義されています。

if[編集]

if機能は、もし〜ならば〜、さもなくば〜の構文です。文法は、

{{ #if: <条件> | <ならば文> | <さもなくば文> }}

もし 条件空文字列もしくはホワイトスペースのみであったならば、条件はと判断され、さもなくば文 が返されます。そうでなければ ならば文 が返されます。 ならば文 は省略できます。その場合には、条件が偽であるときに空文字列が返されます。

例:

                      {{テンプレート|引数=なにがし}}  {{テンプレート}} {{テンプレート|引数=}}
                                     |                        |                |
                                     |                        |                |
                                     |                        |                |
{{ #if: {{{引数|}}} | 引数が定義されている場合 | 引数が定義されていない場合、空の場合 }}

if機能では、等号その他の数学的な演算は一切サポートされていないことに注意してください。たとえば、{{#if: 1 = 2|はい|いいえ}}は、「はい」を返します。なぜなら、"1 = 2"は空文字列でないからです。これは「もし定義されていたら」という構文が意図されています。もし、文字列を比較するのならば、ifeqを使ってください。また、数字を比較するのなら、ifexprを使ってください。

ifeq[編集]

ifeqは2つの文字列を比較し、比較の結果に応じて異なる文字列を返します。文法は、

{{ #ifeq: <比較文字列 1> | <比較文字列 2> | <等しいときに返す文> | <等しくないときに返す文> }}

数字と見なされる文字列は、数字として判断されます。

  • {{ #ifeq: 007 | 7 | 同じ | 違う }} → 同じ

ifexist[編集]

ifexistは、指定されたタイトルのページが存在するかどうかによって、2つのうちの1つの結果を返します。

{{ #ifexist: <検索ページ名> | <ページのあるときに返す文> | <ページのないときに返す文> }}

例えば、

Fooは存在するので、{{#ifexist:Foo|Bar|RFC 3092}} は Bar を返します。
{{#ifexist:Wikipedia:条件文|はい|いいえ}} は はい を返します。
m:Help:Calculationは存在しますが、これは接頭辞がinterwikiなので、{{#ifexist:m:Help:Calculation|はい|おっと}} は おっと を返します。

1つ目のパラメータは、調べたいページのタイトルです。2つ目は存在している場合に返す文、3つ目は存在しない場合に返す文です。入力されるパラメータが有効なタイトルでなければ、存在しない場合の文が返されます。

Template:ExistsMeta) は、Interwikiリンクでも「存在する」文を返すこと以外は、同じ結果を返します。Template:if interwiki linkMeta)は、この違いを利用しています。

switch[編集]

switchは、ひとつの値(基準値)を他の複数の値と比較し、一致するものが見つかったら該当の値を返します。文法は基本的に、

{{ #switch: <基準値>
| <値1> = <返す文1>
| <値2> = <返す文2>
| ...
| <値n> = <返す文n>
| <その他の時に返す文>
}}

switchはそれぞれの値を、基準値と一致するものがみつかるまでそれぞれの値をずっと探そうとします。もし見つかったら、値に相当する文(等号のあとにある文)を返します。もし見つからなかったら、最後の等号のない項を返します。もし、この項にどうしても等号を含めて書く必要がある場合には、#defaultを用います。

{{ #switch: <基準値>
| <値> = <返す文>
| #default = <その他の時に返す文>
}}

同じ返す文を何度も書くのを省略するため、値が「通り抜ける」構造になっていることに気をつけてください。たとえば、

{{ #switch: <基準値>
| <値1>
| <値2>
| <値3> = <返す文3>
| ...
| <値n> = <返す文n>
| <その他の時に返す文>
}}

この例では、値1と値2に等号が付いていません。もしそれらに一致したら、返す文3が返されます。つまり、等号を省略した場合には、次の最初の等号の後の文が返されるのです。

switchifeq 同様、数字と見なされる文字列は数字として判断されます。次の結果に注意してください。

{{#switch: 007
|7 = 7にマッチ
|007 = 007にマッチ
}}

結果:7にマッチ

expr[編集]

expr機能は、数式の演算を行います。文法は、

{{ #expr: 数式 }}

サポートしている演算子(おおむね優先順位の順)は、

演算子 演算
* 乗算 {{#expr: 30 * 7}} = 210
/ または div 除算 {{#expr: 30 / 7}} = 4.2857142857143
+ 加算 {{#expr: 30 + 7}} = 37
- 減算、負号 {{#expr: 30 - 7}} = 23
mod 乗除、除算の「あまり」 {{#expr: 30 mod 7}} = 2
round 四捨五入
roundの右に小数点以下の桁数を入れる
{{#expr: 30 / 7 round 7}} = 4.2857143
= 等号 {{#expr: 30 = 7}} = 0
<> または != 不等号 {{#expr: 30 <> 7}} = 1
< 小なり {{#expr: 30 < 7}} = 0
> 大なり {{#expr: 30 > 7}} = 1
<= 左は右以下 {{#expr: 30 <= 7}} = 0
>= 左は右以上 {{#expr: 30 >= 7}} = 1
and 論理積 {{#expr: 30 and 7}} = 1
or 論理和 {{#expr: 30 or 7}} = 1
not 否定 {{#expr: not 7}} = 0
( ) 優先 {{#expr: (30 + 7) * 7 }} = 259

真偽を求める演算子は、偽の時に0を、真の時に1を返します。十進法で計算され、小数点は、"." です。FORTRANスタイルの科学技術計算はサポートされていません。

たとえば、

{{ #expr: (100 - 32) / 9 * 5 round 0 }}

は、

38

を返します。これは、華氏温度を摂氏温度に変換し、整数に四捨五入します。

ifexpr[編集]

ifexprは、数式を評価し、その結果に従って2つの文字列からひとつを返します。

{{ #ifexpr: <数式> | <ならば文> | <さもなくば文> }}

数式の結果が0であった場合には さもなくば文 が返され、そのほかの場合には ならば文 が返されます。数式の文法はexprと同じです。

time[編集]

timeは、日付や時間のフォーマットをする機能です。文法は

{{ #time: フォーマット }}

または

{{ #time: フォーマット | 日時/時間 }}

のどちらかです。

日時が指定されていない場合には、コードがHTMLに変換された時刻が用いられます。ページのキャッシュに注意してください。記事が閲覧される時間と、1週間までくらいの誤差が出るかもしれません。最新にするには、その都度更新する必要があるでしょう。なにも変更しないで投稿し直す(いわゆるゼロ編集)、action=purge をURLのオプションに付ける、閲覧者のオプションで「ページをキャッシュしない」にする、などで更新できます。

引数 フォーマット は、PHPの date に使われるのと同様です。

以下の フォーマット コードは、PHPにおけるのと同じ意味を持っています。国際化(つまり、言語と地域による相違)を別にすれば、PHPでのふるまいとの重要な相違点は、条件文のエラーと考えられるべきで、報告されるべきです。すべての数値のフォーマットのコードはその言語に従ってフォーマットされます。もし、そうしたくないときには、xn を前に付けてください。

1970年1月1日00時00分01秒 (UTC) から2038年1月19日03時14分07秒までの時刻に限り、計算ができます(2038年問題参照)。

コード 説明 出力例 現在の出力
d 日。ひと桁の時は0が前に付く。 04 23
D 曜日の短縮形。まれに国際化されている。
j 日。ひと桁の時も0が前に付かない。 3 23
l 曜日。まれに国際化されている。 月曜日 木曜日
N ISO 8601形式の曜日。 1(月曜日) から 7(日曜日) 4
w 曜日の数値。 0(日曜日) から 6(土曜日) 4
z 0から数えたその年の通算日数。 0 から 365 143
W ISO 8601形式の週の数。 21
F 月。たいていは国際化されている。 1月 5月
m 月の数字。ひと桁の時は0が前に付く。 01 から 12 05
M 月の短縮形。たいていは国際化されている。 1月 5月
n 月の数字。ひと桁の時も0が前に付かない。 1 から 12 5
t 月の日数。 28 から 31 31
L 閏年かどうかの判定。 閏年なら 1 、そうでなければ 0 1
Y 4桁の西暦年。 2006 2024
y 2桁の西暦年 06 24
a am or pm pm
A AM or PM PM
g 12時間制の時刻。ひと桁の時は0が前に付かない。 1 から 12 6
G 24時間制の時刻。ひと桁の時は0が前に付かない。 0 から 23 18
h 12時間制の時刻。ひと桁の時は0が前に付く。 01 から 12 06
H 24時間制の時刻。ひと桁の時は0が前に付く。 00 から 23 18
i 分。ひと桁の時は0が前に付く。 00 から 59 59
s 秒。ひと桁の時は0が前に付く。 00 から 59 34
c ISO 8601形式の日付。 2024-05-23T18:59:34+00:00
r RFC 2822形式の日付。 Thu, 23 May 2024 18:59:34 +0000
U 1970年1月1日 00:00:00 (GMT) からの秒数 1716490774

以下の フォーマット コードは、PHPの文法の拡張です。

コード 説明
xn 数字を返すコードの前に置いて、それが半角数字となるようにする。例えば、ヒンディー語において、{{ #time:H, xnH | 06:00}} はヒンディー語式の ०६ ではなく 06 を返す。
xN これが書かれてより後のコードが半角数字となるようにする。 xn と似ているが、こちらはもう一度 xN が現れるまでの全てのコードに作用する。
xr 数字を返すコードの前に置いて、それがローマ数字となるようにする。{{ #time:xrH | 06:30}} は VI を返す。
xg 月の名前を返すコードの前に置いて、主格と所有格に違いのある言語においては、それが所有格となるようにする。
xx 文字の「x」そのもの。

上記以外の文字は、そのまま出力されます。また、上記の文字をそのまま文字として出力する2つの方法があります。

  • ダブルクオート ( " ) に囲まれた文字は、そのまま出力されます。このとき、ダブルクオートそのものは出力されません。ダブルクオートが奇数の時、最後のダブルクオートはそのまま出力されます。
    • {{ #time: Fには雪が"1m積もった。" |2006-02-12}} → 2月には雪が1m積もった。
    • {{ #time:i's" |0:20:11}} → 20'11"
  • PHPのdate()同様、バックスラッシュ "\" は次の文字をエスケープします。\H は文字 H を出力し、\" は文字 " を出力します。

この機能の利用者からの希望により、フォーマット コードは今後追加される可能性があります。PHPのフォーマットコードのより完全な実装と、拡張された x コードの両方です。

引数 日時/時間 は、PHPの strtotime() で使われているのと全く同じです。それは December 112006-12-11 のような絶対的な日付、時刻と、時差やタイムゾーンの計算に便利な +10 hours (本当は +1000 という書き方ができるはずですが、2006年9月8日現在、プラスとマイナスが逆になってしまいます)のような相対的な日数、時間の両方をサポートしています。より詳しくはthe GNU tar manualをご覧ください。

  • {{#time:Y年Fj日|-14 days}} → 2024年5月9日(14日前)
  • {{#time:H:i|+9 hours}} → 03:59 (日本時間)
  • {{#time:H:i|8:15 +6 hours}} → 14:15

subst[編集]

substを条件文で用いたい場合には、"subst:"と"#"の間に空白を入れないでください。m:ヘルプ:テンプレート#substも参照してください。

[編集]

現在のところ、wikiのパイプを使った表の文法は、条件文の内部では使えません。次善策としては、

  • 「{{!}}」を使って、条件文からパイプを隠す。
  • htmlタグスタイルの文法を代わりに使う。

インストール[編集]

自分のメディアウィキにこの条件文をインストールするには、次の両方のファイルをダウンロードし、extensionsの中に ParserFunctions というディレクトリを作って、おいてください。

次に、LocalSettings.phpの最後に次の行を足します。

require_once( "$IP/extensions/ParserFunctions/ParserFunctions.php" );

コードツリーを次の場所で見ることができます。

使用例[編集]

関連項目[編集]

外部リンク[編集]

<!--[[Category:MediaWiki extensions]]--> [[Category:ウィキペディアのテンプレート|*しようけんふん]] [[Category:条件文|*]] [[de:Hilfe:Template-Programmierung]] [[en:m:ParserFunctions]] [[fr:Aide:Fonctions parseur]] [[it:Aiuto:ParserFunctions]] [[nl:Help:ParsFuncties]] [[pl:Wikipedia:Funkcje parsera]] [[ru:Википедия:Функции парсера]] [[vi:Trợ giúp:Tính toán]] [[zh:help:模板扩展语法]] <!--{{edi|{{PAGENAME}}}}-->