Modul:WikidataIB
Dokumentasjon for denne modulen kan opprettes på Modul:WikidataIB/dok
local p = {} local rankOrder = { ['truth'] = 1, ['preferred'] = 2, ['normal'] = 3, ['deprecated'] = 4 } function pTable(table) return "<pre>" .. mw.dumpObject(table) .. "</pre>" end function getArgument(frame, argument) local args = frame.args if args[1] == nil then local pFrame = frame:getParent(); args = pFrame.args; for k,v in pairs( frame.args ) do args[k] = v; end end if args[argument] then return args[argument] end return nil end function getDate(qualifiers, dateformat, langcode) local out = "" -- pTable(qualifiers) local qualifierID = "" if qualifiers['P585'] then qualifierID='P585' -- point-in-time elseif qualifiers['P580'] then qualifierID='P580' end -- from -- todo: timespans? P582 for k, v in pairs(qualifiers[qualifierID]) do if v.snaktype == 'value' then out = "(" .. mw.language.new(langcode):formatDate(dateformat, v.datavalue.value.time) .. ")" end end return out end function getOrder(qualifiers) local out = nil -- pTable(qualifiers) local qualifierID = "P1545" -- sorting order / rank / series ordinal if qualifiers[qualifierID] then for k, v in pairs(qualifiers[qualifierID]) do if v.snaktype == 'value' then out = v.datavalue.value end end end return out end function compareRank(oldrank, newrank) --return pTable(rankOrder) .. pTable(oldrank) .. pTable(newrank) if (newrank and not oldrank) or (oldrank and newrank and rankOrder[newrank] < rankOrder[oldrank]) then return newrank else return oldrank end end function sortTableRankAndOrder(t, highestrank) local retval = {} local function compare(a, b) if (a.rank and b.rank and rankOrder[a.rank] < rankOrder[b.rank]) then return true end if (a.rank and b.rank and rankOrder[a.rank] == rankOrder[b.rank]) and (a.order and b.order and a.order < b.order) then return true else return false end end table.sort(t, compare) end -- This is used to get a value -- if the boolean "list" is set, it will concatenate all available values in a comma-separated list -- if "dateformat" is set, it will look for qualifier-dates (P585, P580) and include in parathensis and format accordingly to the dateformat function getValue(propertyID, list, dateformat, edit) local lang = mw.language.getContentLanguage() local entity = mw.wikibase.getEntityObject() local claims if entity and entity.claims then claims = entity.claims[propertyID] end local out = {} local rank = {} local order = {} local highestrank = nil if claims then if (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == "wikibase-entityid") then -- if wiki-linked value output as link if possible for k, v in pairs(claims) do -- todo: get the entries in preferred order local sitelink = mw.wikibase.sitelink("Q" .. v.mainsnak.datavalue.value["numeric-id"]) local label = mw.wikibase.label("Q" .. v.mainsnak.datavalue.value["numeric-id"]) if label == nil then label = "Q" .. v.mainsnak.datavalue.value["numeric-id"] end if sitelink and sitelink ~= label then out[#out + 1] = { output = "[[" .. sitelink .. "|" .. label .. "]]" } elseif sitelink then out[#out + 1] = { output = "[[" .. sitelink .. "]]" } else out[#out + 1] = { output = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='Artikkelen er ikke tilgjengelig på denne wikien enda'>[*]</abbr>" } end if dateformat then out[#out].output = out[#out].output .. " " .. getDate(v.qualifiers, dateformat, lang.code) end out[#out].rank = v.rank out[#out].order = getOrder(v.qualifiers) highestrank = compareRank(highestrank, v.rank) end elseif (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == 'quantity') then -- this is a quantity, output as value for k, v in pairs(claims) do -- todo: get the values in preferred order if v.mainsnak.datavalue.value['amount'] then out[#out + 1] = { output = lang:formatNum(tonumber(v.mainsnak.datavalue.value['amount'])) } end if dateformat then out[#out].output = out[#out].output .. " " .. getDate(v.qualifiers, dateformat, lang.code) end out[#out].rank = v.rank out[#out].order = getOrder(v.qualifiers) highestrank = compareRank(highestrank, v.rank) end else -- Unknown type out[#out + 1] = { output = 'unknown type ' .. claims[1].mainsnak.datavalue.type } end end -- Now we'll have to sort the table according to rank and order sortTableRankAndOrder(out, highestrank) return pTable(out) end -- This is used to get the name of the property with upper first case function getProperty(propertyID) local lang = mw.language.getContentLanguage() local out = mw.wikibase.label(propertyID) return lang:ucfirst(out) end -- -- Denne funksjonen kalles opp slik: {{#invoke:WikidataIB|rowProperty|P26}} -- den vil da returnere en tekst-streng som er en rad i en infoboks slik: <tr class="rad" valign="top"><th colspan="2">[Property-navn]</th><td colspan="2">[claim]</td></tr> -- property-navn og claim hentes fra wikidata -- andre argumenter som kan benyttes: -- property - angir hvilken property som skal benyttes, alternativ måte å spesifisere på: {{#invoke:WikidataIB|rowProperty|property=P26}} -- liste - angir at hvis det er flere verdier under en property skal de listes ut slik: <tr class="rad" valign="top"><th colspan="2">[Property-navn]</th><td colspan="2">[claim1], [claim2], ...</td></tr> -- angis slik: {{#invoke:WikidataIB|rowProperty|P26|liste=1}} -- dato - angir om verdiene skal legges inn med dato i parantes i etterkant slik: <tr class="rad" valign="top"><th colspan="2">[Property-navn]</th><td colspan="2">[claim] (dato)</td></tr> -- angis slik: {{#invoke:WikidataIB|rowProperty|P26|dato=<datoformat>}}, f.eks. {{#invoke:WikidataIB|rowProperty|P26|dato=j. F Y}} vil gi (31. desember 2015) -- denne kan også kombineres med "liste" -- følgende vil implementeres etterhvert -- verdi - standardverdi som benyttes dersom wikidata ikke har noen claims/verdier for den angitte egenskapen (property). Må da også spesifiseres sammen med "propertynavn". -- angis slik: {{#invoke:WikidataIB|rowProperty|P26|verdi=Ola Normann}} -- pnavn - egenskapsnavn som benyttes dersom wikidata ikke har den angitte egenskapen (property). -- angis slik: {{#invoke:WikidataIB|rowProperty|P26|pnavn=Ektefelle}} -- edit - angir om en "editeringslink" som peker mot Wikidata skal legges på raden. Kun tilgjengelig hvis alle data hentes fra Wikidata. -- angis slik: {{#invoke:WikidataIB|rowProperty|P26|edit=1}} -- function p.rowProperty(frame) -- Get argument "property" local property = getArgument(frame, 'property') if not property then -- No such argument, try getting it as the first argument property = getArgument(frame, '1') end if not property then -- No arguments available, no need to proceed return "[[Kategori:Artikler med invoke WikidataIB rowProperty uten argumenter]]" end -- Get argument "liste" local list = getArgument(frame, 'liste') if list and list ~= "" and list == "1" then list = true else list = false end -- Get argumentet "dato" local date = getArgument(frame, 'dato') -- Get argumentet "pnavn" local property_text = getArgument(frame, 'pnavn') -- Get argument "verdi" local claim_value = getArgument(frame, 'verdi') -- Get argument "edit" -- Dette gjør raden får en editeringslink mot wikidata, slik: <tr><td>[Property-navn]</td><td>[claim1]<span class="noprint plainlinks wikidata-linkback" style="padding-left:.5em"><a class="external text" href="//www.wikidata.org/wiki/QXXXXX"><img alt="Redigere på Wikidata" src="//upload.wikimedia.org/wikipedia/commons/thumb/7/73/Blue_pencil.svg/10px-Blue_pencil.svg.png" title="Voir et modifier les données sur Wikidata" width="10" height="10" style="vertical-align: baseline" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/7/73/Blue_pencil.svg/15px-Blue_pencil.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/7/73/Blue_pencil.svg/20px-Blue_pencil.svg.png 2x" data-file-width="600" data-file-height="600" /></a></span></td></tr> local edit = getArgument(frame, 'edit') if edit and edit ~= "" and edit == "1" and property_text ~= "" and claim_value ~= "" then edit = true else edit = false end return '<tr class="rad" valign="top"><th colspan="2">' .. getProperty(property) .. '</th><td colspan="2">' .. getValue(property, list, date, edit) .. '</td></tr>' end return p