Help:Extension:ParserFunctions/ja

拡張機能は「」を補完する MediaWiki に既存の11のパーサ関数を提供します. (文字列処理のために追加のパーサー関数を提供するように設定できるかもしれません. ここでいう文字列関数の説明文書はを参照してください. ) この拡張機能が提供するすべてのパーサー関数は以下の形式です:

#expr
この関数は、数式を計算してその計算結果を返します. この関数は 関数を介して  でも使えます.



表の右欄は使用できる演算子で、上ほど優先順位が高いものです. 各演算子の機能の詳細は計算のヘルプ ヘルプ:Calculation を参照してください. 出力結果の精度と形式には幅があり、ウィキを実行するサーバーのOSバージョン、サイト表示言語の数値形式の影響を受けます.

ブール代数を使用して評価する際、値がゼロなら 、非ゼロなら正の数でも負の数でも と評価されます:



空の入力式は空の文字列を返します. 無効な式はいくつかのエラー メッセージのうちのいずれかを返します. このエラーは  関数を使用して捕捉できます:



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:



Note, if using the output of magic words, you must raw-format them in order to remove commas and translate the numerals. For example, results in, where we want 0, which can be obtained using. This is especially important in some languages, where numerals are translated. For example, in Bengali, produces ৩০,০৬১.



丸め
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.

切り上げや切り捨てを行なうには、それぞれ単項の  と   を使用してください.

文字列
Expressions only work with number-like values, they cannot compare strings or characters. can be used instead.



#if
この関数はテスト文字列を評価し、それが空であるかどうかを判定します. 空白のみを含むテスト文字列は空であると見做されます.





この関数は、初めに最初の引数が空でないか確かめます. 最初の引数が空でない場合、2 番目の引数を表示します. 最初の引数が空または空白文字（空白、改行等）のみを含む場合、3 番目の引数を表示します.



テスト文字列は常に純粋なテキストとして評価されます. そのため数式は評価されません:



最後の引数（偽）は省略可能です:



The function may be nested. To do so, nest the inner #if function in its full form in place of a parameter of the enclosing #if function. Up to seven levels of nesting is possible, although that may depend on the wiki or a memory limit.

(So that if the parameter does not have a value, it evaluates to an empty string instead of the string " ".)
 * 1) if 文において引数をテスト文字列として用いることも可能です. 変数名の後には （パイプ記号）を追加する必要があります.



このパーサー関数の他の例については を参照してください.

#ifeq
This parser function compares two input strings, determines whether they are identical, and returns one of two strings based on the result. If more comparisons and output strings are required, consider using.



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



それ以外は文字列として比較します. その場合は大文字小文字を識別します.


 * →  (compare to similar example above, without the quotes)
 * →  (compare to similar example above, with   returning a valid number first)
 * →  (compare to similar example above, without the quotes)
 * →  (compare to similar example above, with   returning a valid number first)

As a practical example, consider an existing   using the parser to choose between two standard times, short and long. 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. The template code is defined as:



the following ensue:


 * → 20
 * → 40
 * → 40

#iferror
この関数は入力文字列を取り、2つの結果から1つを返します. この関数は、  や   や   のような他のパーサー関数、ループや再帰のようなテンプレートエラー、その他の「フェースソフト」なパーサーエラーによって作られた   を含む HTML オブジェクトが入力文字列に含まれていた場合に   と評価します.



返値の文字列の一方または両方が省略できます. もし  の文字列が省略されていた場合、エラーがないのであれば   が返されます. の文字列も省略されていた場合は、エラー時にから文字列が返されます.


 * → &zwnj;
 * → &zwnj;
 * → &zwnj;
 * → &zwnj;
 * → &zwnj;
 * → &zwnj;

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



The  input is evaluated exactly as for   above, with the same operators being available. The output is then evaluated as a boolean expression.

An empty input expression evaluates to :



As mentioned above, zero evaluates to  and any nonzero value evaluates to , so this function is equivalent to one using   and   only:



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

comparing

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



#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.



The function evaluates to  if the page exists, whether it contains content, is visibly blank (contains meta-data such as category links or, but no visible content), is blank, or is a. Only pages that are redlinked evaluate to, including if the page used to exist but has been deleted.



The function evaluates to  for  that have been customized, and for  that are defined by the software.



If a page checks a target using, then that page will appear in the  list for the target page. So if the code  were included live on this page, /Foo will list.

On wikis using a shared media repository,  can be used to check if a file has been uploaded to the repository but not to the wiki itself:



If a local description page has been created for the file, the result is exists for all of the above.

does not work with interwiki links.

ifexistの制限
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, any further  functions automatically return false, whether the target page exists or not, and the page is categorized into Category:. The name of the may vary depending on the content language of your wiki.

使用事例によっては、 (存在しないページへのリンクを選択) または  (存在するページへのリンクを選択) を使用して ifexist 効果を css で代用できます. さらに、単一ページで使用するメモリ消耗の激しいパーサ関数の件数の上限は  で制御され、必要な場合は上限値を LocalSettings.php で変更できます.

ifexist and wanted pages
A page that does not exist and is tested for using #ifexist will end up on the Wanted Pages.

#rel2abs
この関数は相対ファイル パスを絶対ファイル パスに変換します.



部分では、以下の構文を使用できます:


 * → 現在の階層
 * → 1 つ上の階層に移動
 * → 下位ディレクトリ /foo に 1 階層移動

を指定していない場合は、ページの完全なページ名が代わりに使用されます:



や  のような無効な構文は無視されます. Since no more than two consecutive full stops are permitted, sequences such as these can be used to separate successive statements:



#switch
この関数は入力値1件を複数のテストケースと比較し、ヒットした場合は当該の文字列を返します.

例:




 * 1) switch with partial transclusion tags can effect a configuration file that enables an editor unfamiliar with template coding to view and edit configurable elements.

既定
文字列に対応する がヒットしない場合、 を返します.



In this syntax, the default result must be the last parameter and must not contain a raw equals sign (an equals sign without ). If it does, it will be treated as a case comparison, and no text will display if no cases match. This is because the default value has not been defined (is empty). If a case matches however, its associated string will be returned.



代替方法として、既定の結果を " " の 文字列で明示的に宣言することもできます.

Default results declared in this way may be placed anywhere within the function:



If the  parameter is omitted and no match is made, no   is returned:



Grouping results
いくつかの  文字列に同じ   文字列を返す「フォールスルー」値を持たせることができます. これにより重複を最小限に抑えることができます.

ここではケース2と3、4とも出力は です. ケース6と7の出力は両方とも です The " " in the last parameter may be omitted in the above case.

Use with parameters
The function may be used with parameters as the test string. 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 " ". (This is the value the parameter will default to if the pipe is absent and the parameter doesn't exist or have a value. を参照してください. )



In the above case, if  equals , the function will return. If it equals, the function will return. If the parameter is empty or does not exist, the function will return.

As in the section above, cases can be combined to give a single result.



Here, if  equals ,   or  , the function will return. If it equals, the function will return. If the parameter is empty or does not exist, the function will return.

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.



In this case, the function returns an empty string unless  exists and equals   or , in which case it returns   or  , respectively.

This has the same effect as declaring the default result as empty.



If for some reason you decide to set a case as " ", the function will return that case's result when the parameter doesn't exist or doesn't have a value. The parameter would have to exist and have a value other than the string " " to return the function's default result.


 * (when  doesn't exist or is empty):
 * →  Foo 
 * (when  has the value " "):
 * →  Bar 
 * (when  has the value " "):
 * →  Foo 

In this hypothetical case, you would need to add the pipe to the parameter.

比較の挙動
同様、比較は比較する文字列と対象の格文字列が数値である場合、数値として処理されます. あるいは大文字小文字を識別する文字列として処理されます.





文字列は空白の場合があります.



一致が見つかると、それ以降の は無視されます.



生の等号
"Case" 文字列には生の等号を含めることができません. これを回避するには、単一の等号記号  を含むテンプレート = を作成するか、または等号記号をHTMLコード   に置き換えます.

例:





#ifeqの置換
拡張深度を減らすには を使用.

例えば:



は、以下と同等です



i.e. deep nesting, linear:

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:

#time
このパーサ関数は日付あるいは時間 (グレゴリア暦) を取って指定の文法どおりにフォーマットします. 日・時間のオブジェクトを特定する方法; 既定値は マジックワード  – つまりページが最近 HTML 形式で表示された日時.



右の図には利用できる書式コードをまとめてあります. もし書式文字列内にこれら以外の文字がふくまれると、未処理で出力します. 空白スペースも扱いは同様です (コードの解析に不要なため). また書式文字列内の文字をエスケープする方法が2つあります. またさらに、有向グラフ の場合、ひとまとまりのリテラル文字"x"と解釈します.
 * 1) バックスラッシュに続けて書式指定文字列を記述すると、ひとまとまりのリテラル文字として解釈.
 * 2) 文字列を二重引用符で囲むと、リテラル文字として扱い引用符を除去.



PHPは を受け取ると、どの書式でも strtotime 関数で処理します (訳注：英文形式の日付を Unix タイムスタンプに変換). 絶対値 (例： ) でも相対値 (例： ) でも受け取ります.


 * &rarr; 
 * &rarr; 
 * &rarr; 
 * &rarr; 
 * &rarr; 
 * &rarr; 
 * &rarr; 

ISO 639-3 (?) の により、文字列を選択した言語で表示します.



The  parameter specifies if the date/time object refers to the local timezone or to UTC.

This is a boolean parameters: its value is determined by casting the value of the argument (see the official PHP documentation for details on how string are cast to boolean values).

See the following examples for details:





Unix タイムスタンプを計算する場合は、文字列の先頭に 記号を付けると日付計算に使えます.



完全または部分的な絶対日付を指定できます. この関数は current 値を使用して、指定されていない日付の部分を「補完」します.



4桁の数字は常に年として解釈され、時間や分としては解釈されません.



6桁の数字は時間と解釈、できるかぎり分と秒まで出力しようとしますが、それ以外の場合はエラーになります (たとえば年月日とは解釈しません).


 * →  記述したコードは年とゼロ補完の月と日を要求したのに、出力は時間として解釈.
 * →  19:60:09 は有効な時刻ではないにもかかわらず、196009 は1960年9月とは解釈されません.

関数は日付計算をこなそうとします. （訳注：Fは「サイトの言語での完全な月名」、dはゼロ補完の日を求め、入力値の不備で出力結果が不定形. ）



に対する書式文字列の文字数上限は半角で6000文字です.

タイムゾーンの問題点
この#timeパーサ関数(より具体的にはPHP DateTime )には相対的なタイムゾーンオフセットとして整数以外 は渡せないというバグがあります. EDT (東部夏時間) など時間通りのタイムゾーンを使用する場合、この問題は発生しません. 例をあげます. ：


 * &rarr;

ところがインドは UTC (協定世界時) との時差が +5.5 時間のため、タイムゾーンを適用すると通常なら正しいオフセットの計算に失敗するはずです. 実例はこちら:


 * &rarr;

そこで回避策として、単純に時間を分か秒 (minutes か seconds) に換算して、次の例のように与えます.


 * &rarr;
 * &rarr;

(関数の開発者 Tim Starling がこれを解決する構文を書いてくれました. )

#timel
この関数は  と同一ですが、  パラメーターに   を設定した場合に、常にウィキのローカルの時間を使用する点が異なります (タイムゾーンの設定は  に従う).

Syntax of the function is:





For instance, see the following examples:





#titleparts
This function separates a page title into segments based on slashes, then returns some of those segments as output.



If the number of segments to return parameter is not specified, it defaults to "0", which returns all the segments from the first segment to return (included). If the first segment to return parameter is not specified or is "0", it defaults to "1":


 * →  See also.
 * →  See also.

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":


 * →  Strips one segment from the end of the string. も参照してください.
 * →   Strips all 4 segments from the end of the string
 * →   Strips 5 segments from the end of the string (more than exist)
 * →   Returns last segment. も参照してください.
 * →   Strips one segment from the end of the string, then returns the second segment and beyond
 * →   Start copying at the second last element; strip one segment from the end of the string

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


 * For example, any occurrence of,  , or   in pagename will be replaced by.
 * 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.

Then the decoded pagename is canonicalized into a standard page title supported by MediaWiki, as much as possible:


 * 1) All underscores are automatically replaced with spaces:
 * →  Not bah_boo, despite the underscore in the original.
 * 1) 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:
 * 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:
 * 1) 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).
 * 1) 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).
 * 1) 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).

{{Warning|1= Certain characters that are illegal in a page title will cause #titleparts to not parse the string:


 * → {{#titleparts: {one/two} | 1 | 1 }}. 以下の値にはなりません: {one
 * → {{#titleparts: page/123 | 1 | 2 }}. 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.
 * → "{{#titleparts: red/#00FF00/blue| 1 | 3 }}". Does not work because "#" is also illegal in page titles.

}}

StringFunctions
All of these functions are integrated from the StringFunctions extension, but are only available if an administrator sets   in.

All of these functions operate in O(n) time complexity, making them safe against DoS attacks.

#len:
The #len function returns the length of the given string. 構文は以下の通りです:

The return value is always a number of characters in the source string (after expansions of template invocations, but before conversion to HTML). If no string is specified, the return value is zero.

#pos:
The #pos function returns the position of a given search term within the string. 構文は以下の通りです:

The offset parameter, if specified, tells a starting position where this function should begin searching.

If the search term is found, the return value is a zero-based integer of the first position within the string.

If the search term is not found, the function returns an empty string.

#rpos:
The #rpos function returns the last position of a given search term within the string. 構文は以下の通りです:

If the search term is found, the return value is a zero-based integer of its last position within the string.

If the search term is not found, the function returns -1.

#sub:
The #sub function returns a substring from the given string. 構文は以下の通りです:

The start parameter, if positive (or zero), specifies a zero-based index of the first character to be returned.

例: は  を返します.

は  を返します.

If the start parameter is negative, it specifies how many characters from the end should be returned.

例: は  を返します.

The length parameter, if present and positive, specifies the maximum length of the returned string.

例: は  を返します.

If the length parameter is negative, it specifies how many characters will be omitted from the end of the string.

例: は  を返します.

If the start parameter is negative, it specifies how many characters from the end should be returned. The length parameter, if present and positive, specifies the maximum length of the returned string from the starting point.

例: は  を返します.

#replace:
The #replace function returns the given string with all occurrences of a search term replaced with a replacement term.

If the search term is unspecified or empty, a single space will be searched for.

If the replacement term is unspecified or empty, all occurrences of the search term will be removed from the string.

Currently the syntax doesn't provide a switch to toggle case-sensitivity setting. But you may make use of magic words of formatting (e.g. your_string_here ) as a workaround. For example, if you want to remove the word "Category:" from the string regardless of its case, you may type:
 * Case-insensitive replace

But the disadvantage is that the output will become all lower-case. If you want to keep the casing after replacement, you have to use multiple nesting levels (i.e. multiple replace calls) to achieve the same thing.

#explode:
The #explode function splits the given string into pieces and then returns one of the pieces. The syntax is:

The delimiter parameter specifies a string to be used to divide the string into pieces. This delimiter string is then not part of any piece, and when two delimiter strings are next to each other, they create an empty piece between them. If this parameter is not specified, a single space is used. The limit parameter is available in ParserFunctions only, not the standalone StringFunctions version, and allows you to limit the number of parts returned, with all remaining text included in the final part.

The position parameter specifies which piece is to be returned. Pieces are counted from 0. If this parameter is not specified, the first piece is used (piece with number 0). When a negative value is used as position, the pieces are counted from the end. In this case, piece number -1 means the last piece. Examples:


 * returns.
 * returns.
 * returns.
 * returns.

The return value is the position-th piece. If there are fewer pieces than the position specifies, an empty string is returned.

Limits
This module defines three global settings:


 * 
 * 
 * 

These are used to limit some parameters of some functions to ensure the functions operate in O(n) time complexity, and are therefore safe against DoS attacks.

$wgStringFunctionsLimitSearch
This setting is used by #pos, #rpos, #replace, and #explode. All these functions search for a substring in a larger string while they operate, which can run in O(n*m) and therefore make the software more vulnerable to DoS attacks. By setting this value to a specific small number, the time complexity is decreased to O(n).

This setting limits the maximum allowed length of the string being searched for.

The default value is 30 multibyte characters.

$wgStringFunctionsLimitReplace
This setting is used by #replace. This function replaces all occurrences of one string for another, which can be used to quickly generate very large amounts of data, and therefore makes the software more vulnerable to DoS attacks. This setting limits the maximum allowed length of the replacing string.

The default value is 30 multibyte characters.

subst展開
Parser functions can be substituted by prefixing the hash character with :


 * → the code   will be inserted in the wikitext since the page exists.

Substitution does not work within, you can use &hellip;  for this purpose.

リダイレクト
Especially   could be handy in  to pages including dates, but this does not work.

表内でのパイプ記号のエスケープ
Parser functions will mangle syntax and pipe characters, treating all the raw pipe characters as parameter dividers. To avoid this, most wikis used a template    :! with its contents only a raw pipe character, since MW 1.24 a  replaced this kludge. 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. It will then be interpreted as a table row or column separator. Alternatively, raw HTML table syntax can be used, although this is less intuitive and more error-prone.

パイプ記号は、プレーンな (解釈されない) 文字としてエスケープすることもできます. これには HTML エンティティ  を使用します.

空白類の除去
空白 (改行、タブ、空白アキ) はパーサ関数の全パラメータの開始から終了まで除去します. これを回避するには、引用符で囲んでから文字列を比較します.



then 部分および else 部分での空白の除去を回避する方法は m:Template:Ifを参照してください. 空白の代わりに &lt; &gt;  &lt; /nowiki &gt; を使用することもできます.


 * → foofoo
 * → foofoo

ただし連続する複数の空白文字はパーサにより1文字に圧縮されるため、この方法により処理できるのは1文字の空白文字に限定されます.




 * || → || foofoo 
 * }

このサンプルでは  書式を採用してブラウザ上で非表示でも、空白スペースが保持されるように強制します. ソフトウェアにより、ブラウザに渡す前に空白文字が除去されるため発生します.

この挙動を回避するには、ソフトウェアによって置換されないように空白スペースを (breakable space) もしくは  (non-breakable space) に置換します.


 * →  foofoo 
 * → foofoo

関連項目

 * m:Help:計算
 * m:Help:Newlines and spaces
 * m:Help:Comparison between ParserFunctions syntax and TeX syntax
 * Module:String は を廃止
 * Module:String は を廃止
 * Module:String は を廃止