模組:WikidataDescription
外观
-- Simple wrapper for mw.wikibase.description
local p = {}
function p._fromQID(qid, prefix)
if qid == nil then
return ''
end
local prefix = prefix or ''
local result, lang = mw.wikibase.getDescriptionWithLang(qid)
-- don't get english fallback results
if result and string.sub(lang,0,2) == 'zh' then
return prefix .. result
else
return ''
end
end
function p.fromQID(frame)
return p._fromQID(frame.args[1], frame.args[2])
end
-- 这个方法不计expensive function和PEIS,但会算作transclusion
local function testTitle(name)
local title = mw.title.new(name)
return title:getContent() ~= nil
end
-- {{#invoke:WikidataDescription|fromTitle|络丝蛋白}} => "人类大脑糖蛋白"
-- {{#invoke:WikidataDescription|fromTitle|络丝蛋白|,}} => ",人类大脑糖蛋白"
function p.fromTitle(frame)
local pageTitle = frame.args[1]
-- 尝试尽量优化fast path
local qid = mw.wikibase.getEntityIdForTitle(pageTitle)
-- 假设繁简和重定向的情况是罕见的。避免这个slow path影响fast path的expensive function count
if not qid then
-- 先尝试寻找仅简繁不同的页面标题。需注意另一变体的标题也可能是重定向
local zhConvert = require('Module:ZhConversion')
local zh_converted = false
-- 此处可能遗漏繁简混杂的情况。更完善的做法见
-- [[Module:Va]]的redirect_target和[[Module:WikidataLink]]
local newt = zhConvert.zh_convert(pageTitle)
if newt ~= pageTitle then
-- 目前mw.wikibase.getEntityIdForTitle不计expensive或者其他限制,所以就大方用了
-- 尽管这样会令本函数的结构变得复杂、存在多个exit point。若有需要可以退回到版本88906727
qid = mw.wikibase.getEntityIdForTitle(newt)
if qid then
return p._fromQID(qid, frame.args[2])
end
if testTitle(newt) then
-- 若另一变体确实存在则改用新标题,以继续检查其是否为重定向
pageTitle = newt
zh_converted = true
end
end
local redirect = require('Module:Redirect')
if redirect.luaIsRedirect(pageTitle) then
newt = redirect.getTarget(pageTitle)
elseif not zh_converted then
return '' -- 没有另一变体、也非重定向,则终止
end
qid = mw.wikibase.getEntityIdForTitle(newt)
end
return p._fromQID(qid, frame.args[2])
end
return p