Manual:Parser functions/zh

在MediaWiki 1.7中添加的解析器函數是一種與解析器緊密集成的擴展. 短語“解析器函數”不應與Extension:ParserFunctions混淆，後者是簡單解析器函數的集合. （請參閱Help:Extension:ParserFunctions. ）

描述
雖然標籤擴展需要獲取未處理的文本並將超文本標記語言返回給瀏覽器，但解析器函數可以與頁面中的其他wiki元素“交互”. 例如，解析器函數的輸出可以用作模板參數或構造鏈接.

解析器函數的典型語法是：

有關更多信息，請參閱的. 本文檔說明：
 * 回調函數應具有以下形式：
 * 或者和或者 :
 * 或者和或者 :

創建解析器函數比創建新標記稍微複雜一些，因為函數名稱必須是魔術字，這是一個支持別名和本地化的關鍵字.

简单例子
下面是一個創建解析器函數的擴展示例.

註冊進入extension.json，代碼進入ExampleExtension.class.php：

另一個文件ExampleExtension.i18n.php應該包含：

啟用此擴展程序後，



生产：


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

较长的函数
對於更長的函數，您可能希望將鉤子函數拆分為_body.php或.hooks.php文件，並使它們成為類的靜態函數. 然後你可以用$ AutoloadClasses加載類並在鉤子中調用靜態函數; 例如. ：

把它放在你的 文件中： 然後把它放在你的 文件中：
 * 請參閱：編寫事件處理程序以獲取其他樣式.

缓存
與標記擴展名一樣，$parser->disableCache可用於禁用動態擴展的緩存.

控制輸出的解析
要使解析器函數返回的wiki文本得到完全解析（包括擴展模板），請在返回時將 選項設置為false：

至少在某些情況下，有時是1.12版本左右，似乎 的默認值從false變為true.

相反，要使您的解析器函數返回仍未解析的HTML，而不是返回wiki文本，請使用以下命令：

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:

命名
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 ) 参数：


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

参见

 * The ParserFunctions extension is a well-known collection of parser functions.
 * The Parser Hooks PHP library, which provides an object orientated interface for declarative parser hooks
 * The ParserFunctions extension is a well-known collection of parser functions.
 * The Parser Hooks PHP library, which provides an object orientated interface for declarative parser hooks
 * The ParserFunctions extension is a well-known collection of parser functions.
 * The Parser Hooks PHP library, which provides an object orientated interface for declarative parser hooks
 * The ParserFunctions extension is a well-known collection of parser functions.
 * The Parser Hooks PHP library, which provides an object orientated interface for declarative parser hooks
 * The Parser Hooks PHP library, which provides an object orientated interface for declarative parser hooks