Modul:Official links
Utseende
Moduldokumentasjon
![]() | Denne modulen er IKKE i bruk lenger. Vennligst se Module:External links/conf/Offisielle lenker for definisjonene som benyttes |
require('Module:No globals')
local contLang = mw.language.getContentLanguage()
local conf = require 'Module:Official website/conf'(contLang:getCode())
local labels = {}
local function buildList( pid, t )
if #t == 0 then
return ''
elseif #t == 1 then
return t[1]
end
local last = table.remove(t, 1)
local str = table.concat(t, mw.message.newRawMessage(conf:g('initial-' .. pid .. '-combiner', 'initial-default-combiner')):plain())
return mw.message.newRawMessage(conf:g('final-' .. pid .. '-combiner', 'final-default-combiner'), str, last):plain()
end
local function getEntityLabel( id )
local entity = mw.wikibase.getEntity( id )
if entity then
return entity:getLabel()
end
end
local function findEntityLabel( id )
if not labels[id] then
local label = getEntityLabel( id )
if label then
labels[id] = label -- labels is an outer structure
end
end
return labels[id]
end
local mainFormatter = {}
mainFormatter['string'] = function( pid, datavalue )
if datavalue['type'] ~= 'string' then
return nil
end
return datavalue.value
end
local qualFormatter = {}
qualFormatter['wikibase-entityid'] = function( pid, datavalue )
if datavalue['type'] ~= 'wikibase-entityid' then
return nil
end
if datavalue.value['entity-type'] ~= 'item' then
return nil
end
return findEntityLabel( 'Q'..datavalue.value["numeric-id"] )
end
local main = {}
main.P856 = {
types = {
snaktype = 'value',
datatype = 'url',
},
formatter = mainFormatter['string']
}
main.P1019 = {
types = {
snaktype = 'value',
datatype = 'url',
},
formatter = mainFormatter['string']
}
main.P1581 = {
types = {
snaktype = 'value',
datatype = 'url',
},
formatter = mainFormatter['string']
}
local qual = {}
qual.P407 = {
types = {
snaktype = 'value',
datatype = 'wikibase-item',
},
formatter = qualFormatter['wikibase-entityid']
}
local qorder = {'P407'}
local p = {}
function p.findMainLinks(pid, qid)
local fragments = {}
local entity = mw.wikibase.getEntityObject( qid )
-- to avoid deep tests
if not entity then
return fragments
end
local statements = entity.claims[pid]
-- to avoid deep tests
if not statements then
return fragments
end
for _, claim in ipairs( statements ) do
-- to avoid deep tests
if not claim then
claim = {}
end
local valid = true
if claim['type'] ~= 'statement' then
valid = valid and false
end
if claim['rank'] == 'deprecated' then
valid = valid and false
end
local mainsnak = claim.mainsnak or {}
if not mainsnak or not main[pid] then
valid = valid and false
end
if ((main[pid] and mainsnak.snaktype ~= main[pid].types.snaktype)
or (main[pid] and mainsnak.datatype ~= main[pid].types.datatype))
then
valid = valid and false
end
if valid then
local mainStr = main[pid].formatter(pid, mainsnak.datavalue)
local optionals = {}
local qualifiers = claim.qualifiers or {}
for _, qualid in ipairs( qorder ) do
if qualifiers[qualid] then
local items = {}
for _, qualsnak in ipairs( qualifiers[qualid] ) do
if qualsnak and qual[qualid] then
if not (qualsnak.snaktype ~= qual[qualid].types.snaktype
or qualsnak.datatype ~= qual[qualid].types.datatype)
then
items[1+#items] = qual[qualid].formatter(qualsnak.property, qualsnak.datavalue)
end
end
end
local text = buildList(qualid, items)
if qualid == 'P407' then
if not text or text == '' then
-- this should never happen, unless it will be possible to add empty qualifiers
text = mw.message.newRawMessage(conf:g('qualifier-'.. qualid..'-empty', 'qualifier-default-empty')):plain()
end
text = mw.message.newRawMessage(conf:g('ext-link-' .. claim['rank']), mainStr, text):plain()
end
optionals[1+#optionals] = text
else
local text = mw.message.newRawMessage(conf:g('qualifier-'.. pid..'-missing', 'qualifier-default-missing')):plain()
optionals[1+#optionals] = mw.message.newRawMessage(conf:g('ext-link-' .. claim['rank']), mainStr, text):plain()
end
end
if #optionals > 0 then
mainStr = buildList(pid, optionals)
end
fragments[1+#fragments] = mainStr
end
end
return fragments
end
function p.links( frame )
local items = {}
for _,v in ipairs(frame.args) do
local _, _, ch, num= v:find("([pP])(%d+)")
if ch then
local pid = ch:upper()..num
local label = findEntityLabel( pid )
if not label then
break
end
local links = p.findMainLinks(pid)
if not links or #links == 0 then
break
end
items[1+#items] = { contLang:ucfirst( label ), buildList('', links) }
end
end
for i,v in ipairs(items) do
items[i] = mw.message.newRawMessage(conf:g(i==1 and 'first-list-item' or 'rest-list-item'), unpack(v)):plain()
end
if #items > 0 then
return table.concat(items, "\n") .. '[[Kategori:Artikler med offisielt nettsted fra Wikidata]]'
end
return "''Ikke noe offisielt nettsted tilgjengelig''" .. '[[Kategori:Artikler uten offisielt nettsted]]'
end
return p