如何使用对 lua 脚本的内置 mediawiki 支持来解析 wiki 文本?

How do I parse wikitext using built-in mediawiki support for lua scripting?

微弱的维基词典条目位于 https://en.wiktionary.org/wiki/faint

词源部分的 wiki 文本是:

From {{inh|en|enm|faynt}}, {{m|enm|feynt||weak; feeble}}, from {{etyl|fro|en}} {{m|fro|faint}}, {{m|fro|feint||feigned; negligent; sluggish}}, past participle of {{m|fro|feindre}}, {{m|fro|faindre||to feign; sham; work negligently}}, from {{etyl|la|en}} {{m|la|fingere||to touch, handle, usually form, shape, frame, form in thought, imagine, conceive, contrive, devise, feign}}.

它包含 {{xyz|...}}

形式的各种模板

我想解析它们并获得页面上显示的文本输出:

From Middle English faynt, feynt (“weak; feeble”), from Old French faint, feint (“feigned; negligent; sluggish”), past participle of feindre, faindre (“to feign; sham; work negligently”), from Latin fingere (“to touch, handle, usually form, shape, frame, form in thought, imagine, conceive, contrive, devise, feign”).

我从维基词典的免费转储中提取了大约 10000 个条目 here

为此,我的想法是提取模板及其扩展(以某种形式)。为了探索可能性,我一直在摆弄 mediawiki 上的 lua 脚本工具。通过在模块的编辑页面上的调试控制台中尝试各种查询,如下所示:

https://en.wiktionary.org/w/index.php?title=Module:languages/print&action=edit

mw.log(p)
>> table

mw.logObject(p)
>> table#1 {
  ["code_to_name"] = function#1,
  ["name_to_code"] = function#2,
}

p.code_to_name("aaa")
>>

p.code_to_name("ab")
>>

但是,我什至无法正确调用函数。 p.code_to_name("aaa") 没有 return 任何东西。

可能扩展词源部分模板的代码在这里: https://en.wiktionary.org/w/index.php?title=Module:etymology/templates

如何正确调用这段代码? 有没有更简单的方法来实现我解析 wikitext 模板的目标? mediawiki 中是否有一些我可以调用的函数,例如 "parse-wikitext("text")。如果有,我该如何调用它?

要在 wiki 文本中扩展模板(和其他内容),请使用 frame.preprocess, which is called as a method on a frame object. To get a frame object, use mw.getCurrentFrame。例如,在控制台中输入 = mw.getCurrentFrame():preprocess('{{l|en|word}}') 以获取由 {{l|en|word}} 生成的维基文本。目前给出 <span class="Latn" lang="en">[[word#English|word]]</span>.

您也可以使用 Expandtemplates action in the MediaWiki API ( https://en.wiktionary.org/w/api.php?action=expandtemplates&text={{l|en|word}}), or the Special:ExpandTemplates 页面,或 JavaScript(如果您在浏览 Wiktionary 页面时打开浏览器控制台):

new mw.Api().get({
        action: 'parse',
        text: '{{l|en|word}}',
        title: mw.config.values.wgPageName,
    }).done(function (data) {
        const wikitext = data.parse.text['*'];
        if (wikitext)
            console.log(wikitext);
});

如果 mw.api 库尚未加载,您会收到 TypeError ("mw.Api is not a constructor"):

mw.loader.using("mediawiki.api", function() {
    // Use mw.Api here.
});

以上是扩展模板的一些方法。