Modul:WikidataIB: Forskjell mellom sideversjoner

Fra Wikisida.no
Hopp til navigering Hopp til søk
m (timeformat)
(+linkback)
Linje 180: Linje 180:
return lang:ucfirst(out)
return lang:ucfirst(out)
end
end
end

function addLinkback(str, id, property)
if not id then
id = tools.getEntity()
end
if not id then
return str
end
if type(id) == 'table' then
id = id.id
end
local class = ''
if property then
class = 'wd_' .. string.lower(property)
end
local title = "Redigere på Wikidata"
local icon = '[%s [[File:Blue pencil.svg|%s|10px|baseline|link=]] ]'
local url = mw.uri.fullUrl('d:' .. id, 'uselang=no')
url.fragment = property
url = tostring(url)
local v = mw.html.create('span')
:addClass(class)
:wikitext(str)
:tag('span')
:addClass('noprint plainlinks wikidata-linkback')
:css('padding-left', '.5em')
:wikitext(icon:format(url, title))
:allDone()
return tostring(v)
end
end


Linje 228: Linje 259:
-- 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>
-- 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')
local edit = getArgument(frame, 'edit')
if edit and edit ~= "" and edit == "1" and property_text ~= "" and claim_value ~= "" then
if edit and edit == "1" then
edit = true
edit = true
else
else
edit = false
edit = false
end
end
return '<tr class="rad" valign="top"><th colspan="2">' .. getProperty(property, property_text) .. '</th><td colspan="2">' .. getValue(property, list, date, edit, claim_value) .. '</td></tr>'
local retval ='<tr class="rad" valign="top"><th colspan="2">' .. getProperty(property, property_text) .. '</th><td colspan="2">'
if edit then
retval = retval .. addLinkback(getValue(property, list, date, edit, claim_value))
else
retval = retval .. getValue(property, list, date, edit, claim_value)
end
retval = retval .. '</td></tr>'
return retval
end
end



Sideversjonen fra 6. feb. 2016 kl. 17:35

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 = nil -- pTable(qualifiers)
	if qualifiers then
		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
	end
	return out
end

function getOrder(qualifiers)
	local out = nil -- pTable(qualifiers)
	if qualifiers then
		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
	end
	return out
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, value_from_wiki)
	if value_from_wiki and #value_from_wiki  then
		return value_from_wiki
	else
		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 = {}
		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
					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
						local date = getDate(v.qualifiers, dateformat, lang.code)
						if date then
							out[#out].output = out[#out].output .. " " .. date
						end
					end
					out[#out].rank = v.rank
					out[#out].order = getOrder(v.qualifiers)
				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
					if v.mainsnak.datavalue.value['amount'] then
						out[#out + 1] = { output = lang:formatNum(tonumber(v.mainsnak.datavalue.value['amount'])) }
					end
					if dateformat then
						local date = getDate(v.qualifiers, dateformat, lang.code)
						if date then
							out[#out].output = out[#out].output .. " " .. date
						end
					end
					out[#out].rank = v.rank
					out[#out].order = getOrder(v.qualifiers)
				end
			elseif (claims[1] and claims[1].mainsnak.snaktype == "value" and claims[1].mainsnak.datavalue.type == 'time') then
				-- this is a quantity, output as value
				for k, v in pairs(claims) do
					if v.mainsnak.datavalue.value['time'] then
						local timeformat = dateformat
						if not timeformat or timeformat == "" then
							timeformat = "j. F Y"
						end
						out[#out + 1] = { output = mw.language.new(lang.code):formatDate(timeformat, v.mainsnak.datavalue.value.time) }
					end
					if dateformat then
						local date = getDate(v.qualifiers, dateformat, lang.code)
						if date then
							out[#out].output = out[#out].output .. " " .. date
						end
					end
					out[#out].rank = v.rank
					out[#out].order = getOrder(v.qualifiers)
				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)
		-- Now we'll make the output
		local i = 0
		local ret = {}
		while i < #out do
			i = i + 1
			ret[#ret + 1] = out[i].output
			if not list then
				break
			end
		end
		return table.concat(ret, ", ")
	end
end

-- This is used to get the name of the property with upper first case
function getProperty(propertyID, propertyname_from_wiki)
	if propertyname_from_wiki and #propertyname_from_wiki then
		return propertyname_from_wiki
	else
		local lang = mw.language.getContentLanguage()
		local out =  mw.wikibase.label(propertyID)
		return lang:ucfirst(out)
	end
end

function addLinkback(str, id, property)
	if not id then
		id = tools.getEntity()
	end
	if not id then
		return str
	end
	if type(id) == 'table' then
		id = id.id
	end
	
	local class = ''
	if property then
		class = 'wd_' .. string.lower(property)
	end
	local title = "Redigere på Wikidata"
	local icon = '[%s [[File:Blue pencil.svg|%s|10px|baseline|link=]] ]'
	local url = mw.uri.fullUrl('d:' .. id, 'uselang=no')
	url.fragment = property
	url = tostring(url)
	local v = mw.html.create('span')
		:addClass(class)
		:wikitext(str)
		:tag('span')
			:addClass('noprint plainlinks wikidata-linkback')
			:css('padding-left', '.5em')
			:wikitext(icon:format(url, title))
		:allDone()
	return tostring(v)
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"
--  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}}
-- følgende vil implementeres etterhvert
--  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 argument "dato"
	local date = getArgument(frame, 'dato')
	-- Get argument "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 == "1" then
		edit = true
	else
		edit = false
	end
	local retval ='<tr class="rad" valign="top"><th colspan="2">' .. getProperty(property, property_text) .. '</th><td colspan="2">' 
	if edit then
		retval = retval .. addLinkback(getValue(property, list, date, edit, claim_value))
	else
		retval = retval .. getValue(property, list, date, edit, claim_value)
	end
	retval = retval .. '</td></tr>'
	return retval
end

return p