Manual:Parser functions/ja

Parser functions, added in MediaWiki 1.7, are a type of extension that integrate closely with the parser. The phrase "parser function" should not be confused with Extension:ParserFunctions, which is a collection of simple parser functions. (See Help:Extension:ParserFunctions for those.)

説明
Whereas a Tag extension is expected to take unprocessed text and return HTML to the browser, a parser function can 'interact' with other wiki elements in the page. For example, the output of a parser function could be used as a template parameter or in the construction of a link.

パーサー関数の典型的な構文は以下の通りです:

For more information, see the documentation for. This documentation states:
 * The callback function should have the form:
 * Or with :  "
 * Or with :  "

パーサー関数の作成は、タグフックを作成する場合よりも多少込み入っています. これは、関数名がマジックワードでなければならないためです; マジックワードは、別名および多言語対応をサポートする一種のキーワードです.

単純な例
パーサー関数を生成する拡張機能の例を以下に示します.

This file should be called ExampleExtension.php if the name of your extension is ExampleExtension:

Another file, ExampleExtension.i18n.php, should contain:

With this extension enabled,



以下の出力を生成します:


 * param1 is hello and param2 is hi and param3 is hey

This magicWords array is not optional. If it is omitted, the parser function simply will not work; the will be rendered as though the extension were not installed.

より長い関数
より複雑な関数を記述する場合には、フック関数を _body.php または .hooks.php ファイルに分離し、これらを適当なクラスの静的関数としてもよいでしょう. この場合、クラスは でロードして、フックから静的関数を呼び出せます. 例えば:

以下のコードを  ファイルに追加します: Then put this is in your  file:
 * See: writing an event handler for other styles.

Caching
As with tag extensions, $parser->disableCache may be used to disable the cache for dynamic extensions.

Controlling the parsing of output
To have the wikitext returned by your parser function be fully parsed (including expansion of templates), set the noparse option to false when returning: It seems the default value for noparse changed from false to true, at least in some situations, sometime around version 1.12.

Conversely, to have your parser function return HTML that remains unparsed, rather than returning wikitext, use this: However, This is a test. will produce something like this:

This is

param1 is hello and param2 is hi and param3 is hey a test.

This happens due to a hardcoded "\n\n" that is prepended to the HTML output of parser functions. To avoid that and make sure the HTML code is rendered inline to the surrounding text, you can use this:

Naming
By default, MW adds a hash character (number sign, "#") to the name of each parser function. To suppress that addition (and obtain a parser function with no "#" prefix), include the SFH_NO_HASH constant in the optional flags argument to setFunctionHook, as described below.

When choosing a name without a hash prefix, note that transclusion of a page with a name starting with that function name followed by a colon is no longer possible. In particular, avoid function names equal to a namespace name. In the case that interwiki transclusion is enabled, also avoid function names equal to an interwiki prefix.

The setFunctionHook hook
For more details of the interface into the parser, see the documentation for setFunctionHook in includes/Parser.php. Here's a (possibly dated) copy of those comments:

function setFunctionHook( $id, $callback, $flags = 0 ) Parameters:


 * string $id - The magic word ID
 * mixed $callback - The callback function (and object) to use
 * integer $flags - Optional, set it to the SFH_NO_HASH constant to call the function without "#".

Return value: The old callback function for this name, if any

Create a function, e.g.,. The callback function should have the form:

The callback may either return the text result of the function, or an array with the text in element 0, and a number of flags in the other elements. The names of the flags are specified in the keys. Valid flags are:


 * found : The text returned is valid, stop processing the template. This is on by default.
 * nowiki : Wiki markup in the return value should be escaped
 * noparse : Unsafe HTML tags should not be stripped, etc.
 * noargs : Don't replace triple-brace arguments in the return value
 * isHTML : The returned text is HTML, armour it against wikitext transformation

Named parameters
Parser functions do not support named parameters the way templates and tag extensions do, but it is occasionally useful to fake it. Users are often accustomed to using vertical bars ( | ) to separate arguments, so it's nice to be able to do that in the parser function context, too. Here's a simple example of how to accomplish this.

Your callback function typically looks like this: To fake named parameters, simply omit all the arguments after, and pair it with. Here is an example: