如何在内置的mediawiki支持lua脚本的情况下解析wikitext?

Wiktionary上faint的词条位于https://en.wiktionary.org/wiki/faint

词源部分的wikitext为:

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")。

我从wiktionary的免费可用dump中提取了约10000个条目here

为此, 我的想法是提取模板及其扩展(以某种形式)。为了探索可能性,我一直在尝试在mediawiki的调试控制台中对模块的编辑页面内尝试各种查询,比如这里:

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")没有返回任何内容。

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

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

点赞
用户9383219
用户9383219

为了扩展维基文本中的模板(以及其他内容),需使用frame.preprocess,该方法是在frame对象上调用的。使用mw.getCurrentFrame获取frame对象。例如在控制台中输入= mw.getCurrentFrame():preprocess('{{l|en|word}}'),以获取从{{l|en|word}}得出的维基文本。目前为<span class="Latn" lang="en">[[word#English|word]]</span>

您还可以使用MediaWiki API中的扩展模板操作 (https://en.wiktionary.org/w/api.php?action=expandtemplates&text={{l|en|word}}),或者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. });

`` `

这些都是扩展模板的一些方法。

2018-10-15 20:08:19