Module:Extract:修订间差异
武外梗百科 爱国好学自强图新的百科全书
更多操作
无编辑摘要 |
无编辑摘要 |
||
| 第2行: | 第2行: | ||
function p.extractLead(frame) | function p.extractLead(frame) | ||
local page = frame.args[1] | local page = frame.args[1] | ||
local sentences = tonumber(frame.args[2]) or 2 | local sentences = tonumber(frame.args[2]) or 2 | ||
| 第15行: | 第14行: | ||
end | end | ||
local api = mw. | -- Scribunto 正确 API 用法 | ||
local api = mw.api.new() | |||
local result = api:get({ | local result = api:get({ | ||
action = "query", | action = "query", | ||
| 第24行: | 第25行: | ||
}) | }) | ||
if not result or not result.query or not result.query.pages then | if not result | ||
or not result.query | |||
or not result.query.pages | |||
then | |||
return "" | return "" | ||
end | end | ||
for _, v in pairs(result.query.pages) do | for _, v in pairs(result.query.pages) do | ||
if v.extract and v.extract ~= "" then | |||
return v.extract | |||
end | |||
end | end | ||
2025年12月13日 (六) 22:15的最新版本
此模块的文档可以在Module:Extract/doc创建
local p = {}
function p.extractLead(frame)
local page = frame.args[1]
local sentences = tonumber(frame.args[2]) or 2
if not page or page == "" then
return ""
end
local title = mw.title.new(page)
if not title or not title.exists then
return ""
end
-- Scribunto 正确 API 用法
local api = mw.api.new()
local result = api:get({
action = "query",
prop = "extracts",
exsentences = sentences,
explaintext = true,
titles = page
})
if not result
or not result.query
or not result.query.pages
then
return ""
end
for _, v in pairs(result.query.pages) do
if v.extract and v.extract ~= "" then
return v.extract
end
end
return ""
end
return p