Help:Extension:ParserFunctions
ParserFunctions
{{#
関数 名 :引数 1 |引数 2 |引数 3 ... }}
#expr
グループ |
( )
|
1234.5 e (2.718) pi (3.142)
| |
e + ,-
| |
not ceil trunc floor abs exp ln sin cos tan acos asin atan
| |
^
| |
* / div mod
| |
+ -
| |
round
| |
= != <> > < >= <=
| |
and
| |
or
|
このmw.ext.ParserFunctions.expr
{{#expr:
数式 }}
Basic example
{{#expr: 1 + 1 }}
→ 2
ブールfalse
、true
と
{{#expr: 1 and -1 }}
→ 1{{#expr: 1 and 0 }}
→ 0{{#expr: 1 or -1 }}
→ 1{{#expr: -1 or 0 }}
→ 1{{#expr: 0 or 0 }}
→ 0
#iferror
{{#expr: }}
→{{#expr: 1+ }}
→ Expression error: Missing operand for +.{{#expr: 1 = }}
→ Expression error: Missing operand for =.{{#expr: 1 foo 2 }}
→ Expression error: Unrecognized word "foo".
{{#expr: +1 }}
→ 1{{#expr: -1 }}
→ -1{{#expr: + 1 }}
→ 1{{#expr: - 1 }}
→ -1
メモ: マジックワードの{{formatnum :{{NUMBEROFUSERS}}|R}}
を
{{#expr:{{NUMBEROFUSERS}}+100}}
→ Expression error: Unrecognized punctuation character ",".{{#expr:{{formatnum:{{NUMBEROFUSERS}}|R}}+100}}
→ 17922969
四捨五入 機能
ceil
とfloor
を
テスト ケース | ||
---|---|---|
{{#expr: 1/3 round 5 }} |
0.33333 | |
{{#expr: 1/6 round 5 }} |
0.16667 | |
{{#expr: 8.99999/9 round 5 }} |
1 | この |
{{#expr: 1234.5678 round -2 }} |
1200 | |
{{#expr: 1234.5678 round 2 }} |
1234.57 | |
{{#expr: 1234.5678 round 2.3 }} |
1234.57 | round の |
{{#expr: trunc 1234.5678 }} |
1234 | |
{{#expr: 1/3 round 0 }} |
0 | |
{{#expr: 1/2 round 0 }} |
1 | |
{{#expr: 3/4 round 0 }} |
1 | |
{{#expr: -1/3 round 0 }} |
-0 | |
{{#expr: -1/2 round 0 }} |
-1 | |
{{#expr: -3/4 round 0 }} |
-1 | |
ceill や floor による | ||
{{#expr: ceil(1/3) }} |
1 | |
{{#expr: floor(1/3) }} |
0 | |
{{#expr: ceil(-1/3) }} |
-0 | |
{{#expr: floor(-1/3) }} |
-1 | |
{{#expr: ceil 1/3 }} |
0.33333333333333 | 1が |
文字 列
{{#expr: "a" = "a" }}
→ Expression error: Unrecognized punctuation character """.{{#expr: a = a }}
→ Expression error: Unrecognized word "a".{{#ifeq: a | a | 1 | 0 }}
→ 1
#if
この
{{#if: テスト
文字 列 | テスト文字 列 が空 ではない場合 の値 | テスト文字 列 が空 (または空白 のみ) の場合 の値 }}
{{#if: パラメーター 1 | パラメーター 2 | パラメーター 3 }}
この
{{#if: | yes | no}}
→ no{{#if: string | yes | no}}
→ yes{{#if: | yes | no}}
→ no{{#if: | yes | no}}
→ no
テスト
{{#if: 1==2 | yes | no }}
→ yes{{#if: 0 | yes | no }}
→ yes
{{#if: foo | yes }}
→ yes{{#if: | yes }}
→{{#if: foo | | no}}
→
#if
#if
{{#if:テスト文字 列
|テスト文字 列 が空 ではない場合 の値
|{{#if:テスト文字 列
|テスト文字 列 が空 ではない場合 の値
|テスト文字 列 が空 (または空白 のみ) の場合 の値
}}
}}
テスト
#if
|
(パイプ{{{1}}}
"ではなく
{{#if:{{{1|}}}|
変数 1にテキストを入力 しました|変数 1にテキストがありません}}
このパーサー
#ifeq
このパーサー#switch
の
{{#ifeq: string 1 | string 2 | value if identical | value if different }}
{{#ifeq: 01 | 1 | equal | not equal}}
→ equal{{#ifeq: 0 | -0 | equal | not equal}}
→ equal{{#ifeq: 1e3 | 1000 | equal | not equal}}
→ equal{{#ifeq: {{#expr:10^3}} | 1000 | equal | not equal}}
→ equal
それ
{{#ifeq: foo | bar | equal | not equal}}
→ not equal{{#ifeq: foo | Foo | equal | not equal}}
→ not equal{{#ifeq: "01" | "1" | equal | not equal}}
→ not equal (引用 符 なしで、上記 の同様 の例 と比較 してください){{#ifeq: 10^3 | 1000 | equal | not equal}}
→ not equal (前述 した同様 の例 と対比 してみるには、#expr
は有効 な特定 の数値 を先 に返 します)
Template:Timer
をパーサーを
{{#ifeq: {{{1|}}} | short | 20 | 40 }}
{{timer|short}}
→ 20{{timer|20}}
→ 40{{timer}}
→ 40
#iferror
この#expr
や #time
や #rel2abs
のようなclass="error"
をtrue
と
{{#iferror: test string | value if error | value if correct }}
correct
のtest string
がerror
の
{{#iferror: {{#expr: 1 + 2 }} | error | correct }}
→ correct{{#iferror: {{#expr: 1 + X }} | error | correct }}
→ error{{#iferror: {{#expr: 1 + 2 }} | error }}
→ 3{{#iferror: {{#expr: 1 + X }} | error }}
→ error{{#iferror: {{#expr: 1 + 2 }} }}
→ 3{{#iferror: {{#expr: 1 + X }} }}
→ {{#iferror: {{#expr: . }} | error | correct }}
→ correct{{#iferror: <strong class="error">a</strong> | error | correct }}
→ error
エラーによっては{{#iferror:}}
を
#ifexpr
この
{{#ifexpr: expression | value if true | value if false }}
expression
#expr
とまったく
false
と
{{#ifexpr: | yes | no}}
→ no
false
とtrue
と#ifeq
と#expr
のみを
{{#ifeq: {{#expr: expression }} | 0 | value if false | value if true }}
value if true
を
{{#ifexpr: = | yes | no }}
→ Expression error: Unexpected = operator.
{{#ifeq: {{#expr: = }} | 0 | no | yes }}
→ yes
{{#ifexpr: 1 > 0 | yes }}
→ yes{{#ifexpr: 1 < 0 | yes }}
→{{#ifexpr: 0 = 0 | yes }}
→ yes{{#ifexpr: 1 > 0 | | no}}
→{{#ifexpr: 1 < 0 | | no}}
→ no{{#ifexpr: 1 > 0 }}
→
{{#ifexpr: 0 = 0 or 1 = 0 | yes}}
→ yes{{#ifexpr: 0 = 0 and 1 = 0 | | no}}
→ no{{#ifexpr: 2 > 0 or 1 < 0 | yes}}
→ yes{{#ifexpr: 2 > 0 and 1 > 0 | yes | no}}
→ yes
#ifexist
この
{{#ifexist: page title | value if exists | value if doesn't exist }}
ページがtrue
(false
(
{{#ifexist: Help:Extension:ParserFunctions/ja | exists | doesn't exist }}
→ exists{{#ifexist: XXHelp:Extension:ParserFunctions/jaXX | exists | doesn't exist }}
→ doesn't exist
{{#ifexist: Special:Watchlist | exists | doesn't exist }}
→ exists{{#ifexist: Special:CheckUser | exists | doesn't exist }}
→ exists (Checkuser拡張 機能 がこのウィキにインストールされているため){{#ifexist: MediaWiki:Copyright | exists | doesn't exist }}
→ exists (MediaWiki:Copyrightがカスタマイズされているため)
ページが#ifexist:
を{{#ifexist:Foo }}
がこのページ(Help:Extension:ParserFunctions/ja)にライブで
#ifexist:
を
{{#ifexist: File:Example.png | exists | doesn't exist }}
→ doesn't exist{{#ifexist: Image:Example.png | exists | doesn't exist }}
→ doesn't exist{{#ifexist: Media:Example.png | exists | doesn't exist }}
→ exists
ファイルのローカル
#ifexist:
はインターウィキリンクでは
ifexistの制限
#ifexist:
は、「#ifexist:
a.new
(a:not(.new)
($wgExpensiveParserFunctionLimit
で
存在 し、必要 なページ
#rel2abs
この
{{#rel2abs: path }}
{{#rel2abs: path | base path }}
path
.
→現在 の階層 ..
→ 1 つ上 の階層 に移動 /foo
→下位 ディレクトリ /foo に 1階層 移動
base path
を
{{#rel2abs: /quok | Help:Foo/bar/baz }}
→ Help:Foo/bar/baz/quok{{#rel2abs: ./quok | Help:Foo/bar/baz }}
→ Help:Foo/bar/baz/quok{{#rel2abs: ../quok | Help:Foo/bar/baz }}
→ Help:Foo/bar/quok{{#rel2abs: ../. | Help:Foo/bar/baz }}
→ Help:Foo/bar
/.
や /./
のような
{{#rel2abs: ../quok/. | Help:Foo/bar/baz }}
→ Help:Foo/bar/quok{{#rel2abs: ../../quok | Help:Foo/bar/baz }}
→ Help:Foo/quok{{#rel2abs: ../../../quok | Help:Foo/bar/baz }}
→ quok{{#rel2abs: ../../../../quok | Help:Foo/bar/baz }}
→ Error: Invalid depth in path: "Help:Foo/bar/baz/../../../../quok" (tried to access a node above the root node).
For a similar group of functions see also Help:Magic words#URL data. Built-in parser functions include: 'localurl:', 'fullurl:', 'anchorencode:' etc.
#switch
この
{{#switch: comparison string | case = result | case = result | ... | case = result | default result }}
{{#switch: baz | foo = Foo | baz = Baz | Bar }}
→ Baz{{#switch: foo | foo = Foo | baz = Baz | Bar }}
→ Foo{{#switch: zzz | foo = Foo | baz = Baz | Bar }}
→ Bar
#switch with partial transclusion tags can affect a configuration file that enables an editor unfamiliar with template coding to view and edit configurable elements.
既定
case
comparison string
がヒットしないdefault result
を
{{#switch: test | foo = Foo | baz = Baz | Bar }}
→ Bar
この{{}}
のない
{{#switch: test | Bar | foo = Foo | baz = Baz }}
→{{#switch: test | foo = Foo | baz = Baz | B=ar }}
→{{#switch: test | test = Foo | baz = Baz | B=ar }}
→ Foo
#default
" のcase
{{#switch: comparison string | case = result | case = result | ... | case = result | #default = default result }}
この
{{#switch: test | foo = Foo | #default = Bar | baz = Baz }}
→ Bar
default
パラメータがresult
(
{{#switch: test | foo = Foo | baz = Baz }}
→
グループ化 の結果
いくつかの case
result
{{#switch: comparison string | case1 = result1 | case2 | case3 | case4 = result234 | case5 = result5 | case6 | case7 = result67 | #default = default result }}
ここではケース2と3、4ともresult234
です。ケース6と7のresult67
です
#default =
」は
パラメータとともに使用
この{{{parameter name}}}
」に
{{#switch: {{{1}}} | foo = Foo | baz = Baz | Bar }}
{{{1}}}
がfoo
にFoo
をbaz
にBaz
をBar
を
{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | Bar }}
ここで、{{{1}}}
がfoo
、zoo
、またはroo
にFoo
をbaz
にBaz
をBar
を
さらに、テストパラメータ
{{#switch: {{{1}}} | foo = Foo | bar = Bar }}
この{{{1}}}
がfoo
またはbar
にFoo
またはBar
を
これは、デフォルトの
{{#switch: {{{1}}} | foo | zoo | roo = Foo | baz = Baz | }}
{{{parameter name}}}
」に{{{parameter name}}}
」
- (
{{{1}}}
が存在 しないか、空 の場合 ):{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}
→ Foo
- (
{{{1}}}
の値 が「test
」の場合 ):{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}
→ Bar
- (
{{{1}}}
の値 が「{{{1}}}
」の場合 ):{{#switch: {{{1}}} | {{{1}}} = Foo | baz = Baz | Bar }}
→ Foo
この{{{1|}}}
)に
比較 の挙動
#ifeq
{{#switch: 0 + 1 | 1 = one | 2 = two | three}}
→ three{{#switch: {{#expr: 0 + 1}} | 1 = one | 2 = two | three}}
→ one{{#switch: 02 | +1 = one | +2 = two | three}}
→ two{{#switch: 100 | 1e1 = ten | 1e2 = hundred | other}}
→ hundred
{{#switch: a | a = A | b = B | C}}
→ A{{#switch: A | a = A | b = B | C}}
→ C
case
{{#switch: | = Nothing | foo = Foo | Something }}
→ Nothing
cases
は
{{#switch: b | f = Foo | b = Bar | b = Baz | }}
→ Bar
生 の等号
"Case" =
に
{{#switch: 1=2
| 1=2 = raw
| 1<nowiki>=</nowiki>2 = nowiki
| 1{{=}}2 = template
| default
}}
|
template |
{{#switch: 1=2
| 1=2 = html
| default
}}
|
html |
#ifeqの置換
#switch
を
{{#switch:{{{1}}} |condition1=branch1 |condition2=branch2 |condition3=branch3 |branch4}}
は、
{{#ifeq:{{{1}}}|condition1 |branch1 |{{#ifeq:{{{1}}}|condition2 |branch2 |{{#ifeq:{{{1}}}|condition3 |branch3 |branch4}}}}}}
つまり、
{{#ifeq:{{{1}}}|condition1
|<!--then-->branch1
|<!--else-->{{#ifeq:{{{1}}}|condition2
|<!--then-->branch2
|<!--else-->{{#ifeq:{{{1}}}|condition3
|<!--then-->branch3
|<!--else-->branch4}}}}}}
{{#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
}}
}}
}}
#time
コード | ( | |
---|---|---|
Y
|
4 |
2024 |
y
|
2 |
24 |
L
|
1 | |
o [note 1]
|
2024[note 3] | |
n
|
4 | |
m
|
04 | |
M
|
サイトの |
4 |
F
|
サイトの |
4 |
xg
|
ポーランド{{#time:F Y|June 2010|pl}} → czerwiec 2010( {{#time:d xg Y|20 June 2010|pl}} → 20 czerwca 2010( | |
j
|
17 | |
d
|
17 | |
z
|
107 | |
W
|
ISO 8601 |
16 |
N
|
ISO 8601 |
3 |
w
|
3 | |
D
|
||
l
|
||
a
|
pm | |
A
|
a の |
PM |
g
|
10 | |
h
|
10 | |
G
|
22 | |
H
|
22 | |
i
|
26 | |
s
|
55 | |
U
|
Unix |
1713392815 |
タイムゾーン (1.22wmf2 | ||
e
|
タイムゾーン |
UTC |
I
|
0 | |
O
|
グリニッジ |
+0000 |
P
|
グリニッジ |
+00:00 |
T
|
タイムゾーンの |
UTC |
Z
|
タイムゾーンのオフセット ( |
0 |
その | ||
t
|
30 | |
c
|
ISO 8601 Y-m-d"T"H:i:s+00:00 と |
2024-04-17T22:26:55+00:00 |
r
|
RFC 5322 D, j M Y H:i:s +0000 と |
Wed, 17 Apr 2024 22:26:55 +0000 |
グレゴリオ | ||
イスラム | ||
xmj
|
8 | |
xmF
|
シャウワール | |
xmn
|
10 | |
xmY
|
1445 | |
イラン | ||
xit
|
31 | |
xiz
|
28 | |
xij
|
29 | |
xiF
|
ファルヴァルディーン | |
xin
|
1 | |
xiY
|
1403 | |
xiy
|
2 |
03 |
ユダヤ | ||
xjj
|
9 | |
xjF
|
ニサン | |
xjt
|
30 | |
xjx
|
ニサン | |
xjn
|
7 | |
xjY
|
5784 | |
タイ | ||
xkY
|
タイ |
2567 |
xoY
|
113 | |
xtY
|
||
フラグ | ||
xn
|
ヒンディー{{#time:H, xnH}} は ०६, 06 を | |
xN
|
xn とxN が | |
xr
|
(MediaWiki 1.20 |
{{#time:xrY}} → MMXXIV
|
xh
|
{{#time:xhY}} → ב'כ"ד
|
このパーサ{{CURRENTTIMESTAMP}}
– つまりページが
{{#time: format string }}
{{#time: format string | date/time object }}
{{#time: format string | date/time object | language code }}
{{#time: format string | date/time object | language code | local }}
- バックスラッシュに
続 けて書式 指定 文字 列 を記述 すると、ひとまとまりのリテラル文字 として解釈 文字 列 を二 重 引用 符 で囲 むと、リテラル文字 として扱 い引用 符 を除去 。
またさらに、xx
の
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.
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). 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).
{{#time: Y-m-d }}
→ 2024-04-17{{#time: [[Y]] m d }}
→ 2024 04 17{{#time: [[Y (year)]] }}
→ 2024 (24UTCpmWed, 17 Apr 2024 22:26:55 +0000){{#time: [[Y "(year)"]] }}
→ 2024 (year){{#time: i's" }}
→ 26'55"
PHPはdate/time object
を20 December 2000
), relative (e.g. +20 hours
), and combined times (e.g. 30 July +1 year
) are accepted.
{{#time: r|now}}
→ Wed, 17 Apr 2024 22:26:56 +0000{{#time: r|+2 hours}}
→ Thu, 18 Apr 2024 00:26:56 +0000{{#time: r|now + 2 hours}}
→ Thu, 18 Apr 2024 00:26:56 +0000{{#time: r|20 December 2000}}
→ Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|December 20, 2000}}
→ Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|2000-12-20}}
→ Wed, 20 Dec 2000 00:00:00 +0000{{#time: r|2000 December 20}}
→ Error: Invalid time.
ISO 639-3 (?) の
により、
{{#time:d F Y|1988-02-28|nl}}
→ 28 februari 1988{{#time:l|now|uk}}
→ середа{{#time:d xg Y|20 June 2010|pl}}
→ 20 czerwca 2010
local
パラメーターは、「
これは
$wgLocaltimezone
がUTC
にlocal
がtrue
またはfalse
に{{#time: Y F d H:i:s|now|it|0}}
→ 2024 aprile 17 22:26:56{{#time: Y F d H:i:s|now|it|1}}
→ 2024 aprile 17 22:26:56{{#time: Y F d H:i:s|+2 hours||0}}
→ 2024 4月 18 00:26:56{{#time: Y F d H:i:s|+2 hours||1}}
→ 2024 4月 18 00:26:56
{{#time:c|2019-05-16T17:05:43+02:00|it}}
→ 2019-05-16T15:05:43+00:00{{#time:c|2019-05-16T17:05:43+02:00|it|0}}
→ 2019-05-16T15:05:43+00:00{{#time:c|2019-05-16T17:05:43+02:00|it|true}}
→ 2019-05-16T15:05:43+00:00
Unix タイムスタンプを@
{{#time: U | now }}
→ 1713392816{{#time: r | @1713392815 }}
→ Wed, 17 Apr 2024 22:26:55 +0000
{{#time: Y | January 1 }}
→ 2024
4
{{#time: Y m d H:i:s | 1959 }}
→ 1959 04 17 00:00:00
6
{{#time: Y m d H:i:s | 195909 }}
→ 2024 04 17 19:59:09記述 したコードは年 とゼロ補完 の月 と日 を要求 したのに、出力 は時間 として解釈 。{{#time: Y m d H:i:s | 196009 }}
→ Error: Invalid time. 19:60:09 は有効 な時刻 ではないにもかかわらず、196009 は1960年 9月 とは解釈 されません。
{{#time: d F Y | January 0 2008 }}
→ 31 12月 2007{{#time: d F | January 32 }}
→ Error: Invalid time.{{#time: d F | February 29 2008 }}
→ 29 2月 {{#time: d F | February 29 2007 }}
→ 01 3月 {{#time:Y-F|now -1 months}}
→ 2024-3月
#time
に
タイムゾーンの問題 点
この#timeパーサ
{{#time:g:i A | -4 hours }}
→ 6:26 PM
ところがインドは UTC (
{{#time:g:i A | +5.5 hours }}
→ 10:26 PM
そこで
{{#time:g:i A | +330 minutes }}
→ 3:56 AM{{#time:g:i A | +19800 seconds }}
→ 3:56 AM
(
#timel
このlocal
パラメータに true
を{{#time: ... }}
と
{{#timel: format string }}
{{#timel: format string | date/time object }}
{{#timel: format string | date/time object | language code }}
$wgLocaltimezone
がUTC
にlocal
がtrue
またはfalse
に{{#time:c|now|it}}
→ 2024-04-17T22:26:56+00:00{{#time:c|now|it|0}}
→ 2024-04-17T22:26:56+00:00{{#time:c|now|it|1}}
→ 2024-04-17T22:26:56+00:00{{#timel:c|now|it}}
→ 2024-04-17T22:26:56+00:00
#titleparts
この
{{#titleparts: ページ
名 |返 すセグメント数 |最初 に返 すセグメント }}
「
{{#titleparts: Talk:Foo/bar/baz/quok }}
→ Talk:Foo/bar/baz/quok{{#titleparts: Talk:Foo/bar/baz/quok | 1 }}
→ Talk:Foo {{ROOTPAGENAME }} も参照 してください。{{#titleparts: Talk:Foo/bar/baz/quok | 2 }}
→ Talk:Foo/bar{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 2 }}
→ bar/baz{{#titleparts: Talk:Foo/bar/baz/quok | 2 | 3 }}
→ baz/quok{{#titleparts: Talk:Foo/bar/baz/quok | 3 | 2 }}
→ bar/baz/quok{{#titleparts: Talk:Foo/bar/baz/quok | | 2 }}
→ bar/baz/quok{{#titleparts: Talk:Foo/bar/baz/quok | | 5 }}
→
{{#titleparts: Talk:Foo/bar/baz/quok | -1 }}
→ Talk:Foo/bar/baz文字 列 の最後 から数 えてはじめのセグメントを削除 します。 {{BASEPAGENAME}} も参照 してください。{{#titleparts: Talk:Foo/bar/baz/quok | -4 }}
→文字 列 の末尾 から4つのセグメントすべてを削除 します{{#titleparts: Talk:Foo/bar/baz/quok | -5 }}
→文字 列 の末尾 から5つのセグメントを削除 します(存在 する以上 ){{#titleparts: Talk:Foo/bar/baz/quok | | -1 }}
→ quok最後 のセグメントを返 します。 {{SUBPAGENAME}} も参照 してください。{{#titleparts: Talk:Foo/bar/baz/quok | -1 | 2 }}
→ bar/baz文字 列 の末尾 から1つのセグメントを削除 してから、2番目 以降 のセグメントを返 します{{#titleparts: Talk:Foo/bar/baz/quok | -1 | -2 }}
→ baz最後 から2番目 の要素 からコピーを開始 します。文字 列 の末尾 から1つのセグメントを削除 します
- たとえば、pagenameに
"
、"
、または"
が含 まれている場合 は、"
に置 き換 えられます。 - HTMLからプレーンテキストへの
他 の変換 は実行 されないため、HTMLタグは、ページタイトルで無効 であっても、この最初 のステップではそのまま残 ります。
Some magic keywords or parser functions of MediaWiki (such as {{PAGENAME }}
and similar) are known to return strings that are needlessly HTML-encoded, even if their own input parameter was not HTML-encoded:
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 {{PAGESINCAT: }}
but which are still not working properly with HTML-encoded input strings.
For example, if the current page is Category:Côte-d'Or, then:
{{#ifeq: {{FULLPAGENAME}} | Category:Côte-d'Or | 1 | 0 }}
, and{{#ifeq: {{FULLPAGENAME}} | Category:Côte-d'Or | 1 | 0 }}
are both returning1
; (the #ifeq parser function does perform the HTML-decoding of its input parameters).{{#switch: {{FULLPAGENAME}} | Category:Côte-d'Or = 1 | #default = 0 }}
, and{{#switch: {{FULLPAGENAME}} | Category:Côte-d'Or = 1 | #default = 0 }}
are both returning1
; (the #switch parser function does perform the HTML-decoding of its input parameters).{{#ifexist: {{FULLPAGENAME}} | 1 | 0 }}
,{{#ifexist: Category:Côte-d'Or | 1 | 0 }}
, or even{{#ifexist: Category:Côte-d'Or | 1 | 0 }}
will all return1
if that category page exists (the #ifexist parser function does perform the HTML-decoding of its input parameters);{{PAGESINCAT: Côte-d'Or }}
will return a non-zero number, if that category contains pages or subcategories, but:{{PAGESINCAT: {{CURRENTPAGENAME}} }}
, may still unconditionally return 0, just like:{{PAGESINCAT: {{PAGENAME:Category:Côte-d'Or}} }}
{{PAGESINCAT: {{PAGENAME:Category:Côte-d'Or}} }}
The reason of this unexpected behavior is that, with the current versions of MediaWiki, there are two caveats:
{{FULLPAGENAME}}
, or even{{FULLPAGENAME:Côte-d'Or}}
may return the actually HTML-encoded stringCategory:Côte-d'Or
and not the expectedCategory:Côte-d'Or
, and that:{{PAGESINCAT: Côte-d'Or }}
unconditionally returns 0 (the PAGESINCAT magic keyword does not perform any HTML-decoding of its input parameter).
The simple workaround using titleparts (which will continue to work if the two caveats are fixed in a later version of MediaWiki) is:
{{PAGESINCAT: {{#titleparts: {{CURRENTPAGENAME}} }} }}
{{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d'Or}} }} }}
{{PAGESINCAT: {{#titleparts: {{PAGENAME:Category:Côte-d'Or}} }} }}
, that all return the actual number of pages in the same category.
Then the decoded pagename is canonicalized into a standard page title supported by MediaWiki, as much as possible:
- All underscores are automatically replaced with spaces:
{{#titleparts: Talk:Foo/bah_boo|1|2}}
→ bah boo Not bah_boo, despite the underscore in the original.
- The string is split a maximum of 25 times; further slashes are ignored and the 25th element will contain the rest of the string. The string is also limited to 255 characters, as it is treated as a page title:
{{#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 }}
→ y/z/aa/bb/cc/dd/ee- 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:
{{#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}}
→ z
- 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).
{{#titleparts: talk:a/b/c }}
→ Talk:A/b/c
You can use #titleparts as a small "string parser and converter", but consider that it returns the first substring capitalized:
{{#titleparts: one/two/three/four|1|1 }}
→ One{{#titleparts: one/two/three/four|1|2 }}
→ two
{{lc: {{#titleparts: one/two/three/four|1|1 }} }}
→ one
You can prepend a 'dummy' slash at the beginning of the string to get the correct first substring capitalization (uppercase or lowercase). Use 2
instead of 1
for first segment to return:
{{#titleparts: /one/two/three/four|1|2 }}
→ one{{#titleparts: /One/two/three/four|1|2 }}
→ One
String functions
The ParserFunctions extension optionally defines various string functions (#len
, #pos
, #rpos
, #sub
, #count
, #replace
, #explode
, #urldecode
) if $wgPFEnableStringFunctions
is set to true
.
See here for examples.
Here is a short overview of Module:String functions:
- #len (length of string):
{{#invoke:String|len|target_string}}
- #sub (substring):
{{#invoke:String|sub|target_string|start_index|end_index}}
- #match:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
- #pos (position of target):
{{#invoke:String|pos|target_string|index_value}}
- #find:
{{#invoke:String|find|source_string|target_string|start_index|plain_flag}}
- #replace:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
- #rep (repeat):
{{#invoke:String|rep|source|count}}
- #escapePattern:
{{#invoke:String|escapePattern|pattern_string}}
- #count:
{{#invoke:String|count|source_str|pattern_string|plain_flag}}
- #join:
{{#invoke:String|join|separator|string1|string2|...}}
全般 的 な情報
subst展開
パーサーsubst:
を
{{subst:#ifexist: Help:Extension:ParserFunctions/ja | [[Help:Extension:ParserFunctions/ja]] | Help:Extension:ParserFunctions/ja }}
→[[Help:Extension:ParserFunctions/ja]]
というページが存在 するため、コード Help:Extension:ParserFunctions/ja がウィキテキストに挿入 されます。
Subst<ref>
…</ref>
; では{{subst:#tag:ref|
…}}
を
リダイレクト
#time:
…|now-
…}} は
表 内 でのパイプ記号 のエスケープ
パーサー|
) に|
) {{!}}
マジックワード が
パイプ|
を
パイプ |
{{!}} |
| |
パイプ |
| |
| |
空白 類 の除去
{{#ifeq: foo | foo | equal | not equal }}
→ equal{{#ifeq: "foo " | " foo" | equal | not equal }}
→ not equal
then
foo{{#if:|| bar }}foo
→ foobarfoofoo{{#if:||<nowiki /> bar <nowiki />}}foo
→ foo bar foo
ただし
<span style="white-space: pre;">foo{{#if:||<nowiki/> bar <nowiki/>}}foo</span>
→ foo bar foo
このサンプルでは white-space: pre
この 
(
(
<span style="white-space: pre;">foo{{#if:||   bar   }}foo</span>
→ foo bar foofoo{{#if:|| bar }}foo
→ foo bar foo
パラメータ
foo{{1x|content= bar}}foo
→ foobarfoofoo{{1x|1= bar}}foo
→ foobarfoofoo{{1x| bar }}foo
→ foo bar foo
関連 項目
- Help:テンプレート
内 でのパーサー関数 - パーサー
拡張 機能 のタグ - Parser function hooks - コアと
拡張 機能 が追加 したパーサ関数 の一覧 (未 完成 )。 - m:Help:
計算 - m:Help:
改行 と空白 アキ - m:Help:ParserFunctions
構文 と TeX構文 の比較 対照 - Help:マジックワード
- Module:String は Extension:StringFunctions を
廃止 - Extension:PhpTags
- Wikibase
対応 のパーサ関数 (拡張子 により Wikidata収載 を有効 化 ): d:Wikidata:How to use data on Wikimedia projects#Parser function
脚注
- ↑ 2011
年 のr86805以前 とは扱 いが変 わりました。 - ↑ phabricator.wikimedia.orgのExtParserFunctions.php を
参照 してください。