Module:ItemGroupAsCategory
Jump to navigation
Jump to search
Usage
{{ItemGroupAsCategory}}
or
{{#invoke:ItemGroupAsCategory|main}}
Tests
ItemGroupAsCategory/testcases
All tests passed.
| Name | Expected | Actual | |
|---|---|---|---|
| tests |
ItemGroupAsCategory/sandbox/testcases
1 test failed.
| Name | Expected | Actual | |
|---|---|---|---|
| tests | [[Category:References|3dmr]] | [[Category:references|3dmr]] |
require("strict")
local languages = require("Module:Languages")
local wb = require("Module:Wb")
local p = {}
--[[
Debug:
=p.gen()
=p.gen("Q23326")
=p.gen(mw.wikibase.getEntity("Q23326"))
]]
function p.gen(entity)
if not entity then
entity = wb.get_item()
elseif type(entity) == "string" then
entity = mw.wikibase.getEntity(entity)
elseif type(entity) == "table" then
-- already an entity
else
return "[[Category:ItemGroupAsCategory error]]"
end
if not entity then
return "[[Category:ItemGroupAsCategory data item missing]]"
end
local groups = entity:getAllStatements("group")
if #groups == 0 then
return "[[Category:ItemGroupAsCategory groups missing]]"
end
local title = mw.title.getCurrentTitle()
local lang =languages.languageFromTitle(title)
local ns = languages.pseudoNamespaceFromTitle(title)
if not ns and lang ~= "en" then
ns = string.upper(lang)
end
local tag = entity:getLabel()
local categories = {}
for i, statement in ipairs(groups) do
local qid = statement.mainsnak.datavalue.value.id
local label = mw.wikibase.getLabelByLang(qid, lang)
if ns and label then
label = ns .. ":" .. languages.translationPageName("en", mw.title.new(label))
end
if not label then
label = mw.wikibase.getLabel(qid)
end
if not label then
label = "ItemGroupAsCategory group label missing"
end
categories[i] = "[[Category:" .. label .. "|" .. tag .. "]]"
end
return table.concat(categories, "\n")
end
function p.main(frame)
return p.gen()
end
return p