Module:Interwiki
模块文档[创建]
您可能想要创建本Scribunto模块的文档。 编者可以在本模块的沙盒 (创建 | 镜像)和测试样例 (创建)页面进行实验。 请在/doc子页面中添加分类。 本模块的子页面。 |
-- Originally from https://sv.wikipedia.org/wiki/Modul:Interwiki by [[user:Innocent_bystander]]
-- Modified by [[user:Jarekt]]
-- Use as follows:
-- {{#invoke:Interwiki|interwiki}}
-- or, specifying an item to draw interwikis from:
-- {{#invoke:Interwiki|interwiki|qid=Q5}}
local p = {}
-- Excludes some wikiprojects that are not Wikipedia, even if their code ends with 'wiki'
-- If using this code on other wiki please add your wiki to this list
local excludedProjects = {
wikidatawiki = 1,
-- commonswiki = 1,
specieswiki = 1,
metawiki = 1,
mediawikiwiki = 1,
sourceswiki = 1 -- https://wikisource.org
}
local redirectProperties = {
P301 = 1,
P460 = 1
}
function redirect(entity) -- access the first valid value of P460
if entity and entity.claims then
for P in pairs(redirectProperties) do
for i, j in pairs(entity:getBestStatements( P )) do
if j.mainsnak.snaktype == 'value' then
return j.mainsnak.datavalue.value.id
end
end
end
end
return nil
end
function p.interwiki(frame)
local s = {}
local entity = mw.wikibase.getEntity()
local qid = frame.args.qid or frame:getParent().args.qid -- uses parameter qid of the module if it exists, otherwise follow P460
if not qid or qid == '' then
qid = redirect(entity)
end
if qid then
local entity2 = mw.wikibase.getEntity(qid)
if entity2 and entity2.sitelinks then
for i, j in pairs(entity2.sitelinks) do
local lang = mw.ustring.sub( j.site, 1, -5) -- split j.site into language and project parts
local proj = mw.ustring.sub( j.site, -4)
if not excludedProjects[j.site] and proj == 'wikisource' then -- excludes sites on the list as well as Wikisource, Wikiquote, Wikivoyage etc
if mw.title.getCurrentTitle().namespace == 14 or ((entity and not entity.sitelinks[j.site]) or not entity) then -- excludes interwiki to projects that already have sitelinks in the present page, but just overwrite it on categories
lang = mw.ustring.gsub(lang, '_','-')
table.insert(s, '[[' .. lang .. ':' .. j.title .. ']]' ) -- put together a interwiki-link to other projects
end
end
end
end
end
return table.concat(s, '\n')
end
return p