Modul:WikidataIB: Forskjell mellom sideversjoner

Fra Wikisida.no
Hopp til navigering Hopp til søk
m (debugging)
m (regex-justering)
Linje 184: Linje 184:
-- find Q-value
-- find Q-value
local qval = ""
local qval = ""
out[#out].output = out[#out].output .. " snak=" .. v.mainsnak.datavalue.value.unit
--out[#out].output = out[#out].output .. " snak=" .. v.mainsnak.datavalue.value.unit
_, qval = string.find(v.mainsnak.datavalue.value.unit, "^https*://www.wikidata.org/entity/(Q%s+)$")
_, qval = string.find(v.mainsnak.datavalue.value.unit, "^https*://www.wikidata.org/entity/(Q%d+)$")
--out[#out].output = out[#out].output .. " qval=" .. qval
--out[#out].output = out[#out].output .. " qval=" .. qval
if qval and qval ~= "" then
if qval and qval ~= "" then

Sideversjonen fra 10. feb. 2016 kl. 22:00

Dokumentasjon for denne modulen kan opprettes på Modul:WikidataIB/dok

local p = {}

local rankOrder = {
	['truth'] = 1,
	['preferred'] = 2,
	['normal'] = 3,
	['deprecated'] = 4
}

local i18n = {
	['wikidata-linkback-edit'] = 'Redigere på Wikidata',
	['wiki-article-not-available'] = 'Artikkelen er ikke tilgjengelig på denne wikien enda',
	['wikidata-more-results-exists'] = 'Flere verdier er tilgjengelige på wikidata',
	['standard-timeformat'] = 'j. F Y',
	['argument-property'] = 'property',
	['argument-list'] = 'liste',
	['argument-date'] = 'dato',
	['argument-value'] = 'verdi',
	['argument-label'] = 'pnavn',
	['argument-noedit'] = 'noedit',
	['argument-fallback'] = 'fallback',
	['argument-suffix'] = 'suffix',
	['category-rowproperty-no-arguments'] = '[[Kategori:Artikler med invoke WikidataIB rowProperty uten argumenter]]',
	['category-getpropertyvalue-no-arguments'] = '[[Kategori:Artikler med invoke WikidataIB getPropertyValue uten argumenter]]',
	['category-getpropertylabel-no-arguments'] = '[[Kategori:Artikler med invoke WikidataIB getPropertyLabel uten argumenter]]'
}

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, order_descending)
	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 ((order_descending and a.order < b.order) or (not order_descending and a.order > b.order))) then
			return true
		else
			return false
		end
	end
	table.sort(t, compare)
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 getQValueLink(qvalue, fallback, fallbacklang)
	local sitelink = mw.wikibase.sitelink(qvalue)
	local label = mw.wikibase.label(qvalue)
	out = nil
	if qvalue then
		if label == nil then
			-- let's try the fallback-languages
			local checkQ = mw.wikibase.getEntityObject(qvalue)
			if checkQ then
				local i = 0
				while i < #fallbacklang do
					i = i + 1
					label = checkQ:getLabel( fallbacklang[i] )
					if label ~= nil then
						break
					end
				end
					
			end
			if label == nil then
				-- no such luck, fall back to Q-values
				label = qvalue
			end
		end
				
		if sitelink and sitelink ~= label then
			out = "[[" .. sitelink .. "|" .. label .. "]]"
		elseif sitelink  then
			out = "[[" .. sitelink .. "]]"
		else
			if fallback then
				out = "[[:d:Q" .. qvalue .. "|" .. label .. "]]<abbr title='" .. i18n['wiki-article-not-available'] .. "'>[*]</abbr>"
			else
				out = label
			end
		end
	end
	return out
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, fallback, usesuffix, value_from_wiki)
	if value_from_wiki and #value_from_wiki and value_from_wiki ~= "" then
		return value_from_wiki
	else
		local lang = mw.language.getContentLanguage()
		local fallbacklang = mw.language.getFallbacksFor( lang.code )
		local entity = mw.wikibase.getEntityObject()
		local claims
		if entity and entity.claims then
			claims = entity.claims[propertyID]
		end
		local out = {}
		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
					if v.rank ~= mw.wikibase.entity.claimRanks.RANK_DEPRECATED then
						out[#out + 1] = { output = getQValueLink("Q" .. v.mainsnak.datavalue.value["numeric-id"], fallback, fallbacklang) }
						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)
						highestrank = compareRank(highestrank, v.rank)
					end
				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.rank ~= mw.wikibase.entity.claimRanks.RANK_DEPRECATED then
						if v.mainsnak.datavalue.value['amount'] then
							out[#out + 1] = { output = lang:formatNum(tonumber(v.mainsnak.datavalue.value['amount'])) }
						end
						-- now let's check for a suffix
						if usesuffix and v.mainsnak.datavalue.value.unit then
							-- find Q-value
							local qval = ""
							--out[#out].output = out[#out].output .. " snak=" .. v.mainsnak.datavalue.value.unit
							_, qval = string.find(v.mainsnak.datavalue.value.unit, "^https*://www.wikidata.org/entity/(Q%d+)$")
							--out[#out].output = out[#out].output .. " qval=" .. qval
							if qval and qval ~= "" then
								local suffix = getQValueLink(qval, fallback, fallbacklang)
								if suffix then
									out[#out].output = out[#out].output .. " " .. suffix
								end
							end
						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)
						highestrank = compareRank(highestrank, v.rank)
					end
				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.rank ~= mw.wikibase.entity.claimRanks.RANK_DEPRECATED then
						if v.mainsnak.datavalue.value['time'] then
							local timeformat = dateformat
							if not timeformat or timeformat == "" then
								timeformat = i18n['standard-timeformat']
							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)
						highestrank = compareRank(highestrank, v.rank)
					end
				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)
		-- Now we'll make the output
		local i = 0
		local ret = {}
		while i < #out do
			i = i + 1
			-- check if the rank is within the highestrank
			if out[i].rank == highestrank then
				if list then
					list = list - 1
				end
	
				ret[#ret + 1] = out[i].output
				if not list or list < 0 then
					if i < #out then
						-- We still have entries, show it
						ret[#ret + 1] = '<abbr title="' .. i18n['wikidata-more-results-exists'] .. '>…</abbr>'
						break
					end
				end
			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

-- This is used to get the type of the property
function getUnitType(propertyID, value_from_wiki)
	if value_from_wiki and #value_from_wiki and value_from_wiki ~= "" then
		return value_from_wiki
	else
		local lang = mw.language.getContentLanguage()
		local fallbacklang = mw.language.getFallbacksFor( lang.code )
		local entity = mw.wikibase.getEntityObject(propertyID)
		local claims
		if entity and entity.claims then
			claims = entity.claims['P2237']
		end
		local out = {}
		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
					if v.rank ~= mw.wikibase.entity.claimRanks.RANK_DEPRECATED then
						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
							-- let's try the fallback-languages
							local checkQ = mw.wikibase.getEntityObject("Q" .. v.mainsnak.datavalue.value["numeric-id"])
							if checkQ then
								local i = 0
								while i < #fallbacklang do
									i = i + 1
									label = checkQ:getLabel( fallbacklang[i] )
									if label ~= nil then
										break
									end
								end
									
							end
							if label == nil then
								-- no such luck, fall back to Q-values
								label = "Q" .. v.mainsnak.datavalue.value["numeric-id"]
							end
						end
								
						if sitelink and sitelink ~= label then
							out[#out + 1] = { output = "[[" .. sitelink .. "|" .. label .. "]]" }
						elseif sitelink  then
							out[#out + 1] = { output = "[[" .. sitelink .. "]]" }
						else
							if fallback then
								out[#out + 1] = { output = "[[:d:Q" .. v.mainsnak.datavalue.value["numeric-id"] .. "|" .. label .. "]]<abbr title='" .. i18n['wiki-article-not-available'] .. "'>[*]</abbr>" }
							else
								out[#out + 1] = { output = label }
							end
						end
						out[#out].rank = v.rank
						out[#out].order = getOrder(v.qualifiers)
						highestrank = compareRank(highestrank, v.rank)
					end
				end
			end
		end
		-- Now we'll have to sort the table according to rank and order
		sortTableRankAndOrder(out)
		-- Now we'll make the output
		local i = 0
		local ret = {}
		local list = 0
		while i < #out do
			i = i + 1
			-- check if the rank is within the highestrank
			if out[i].rank == highestrank then
				if list then
					list = list - 1
				end
	
				ret[#ret + 1] = out[i].output
				if not list or list < 0 then
					if i < #out then
						break
					end
				end
			end
		end
		local ent = mw.wikibase.getEntityObject()
		ret[#ret + 1] = pTable(ent.claims)
		return table.concat(ret, ", ")
	end
end

function p.getPropertyUnit(frame)
	-- Get argument "property"
	local property = getArgument(frame, i18n['argument-property'])
	if not property then
		-- No such argument, try getting it as the first argument
		property = getArgument(frame, '1')
	end
	-- Get argument "verdi"
	local wiki_value = getArgument(frame, i18n['argument-value'])
	return getUnitType(property, wiki_value)
end	
	

function addLinkback(str, id, property)
	if not id then
		id = mw.wikibase.getEntityObject()
	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 = i18n['wikidata-linkback-edit']
	local icon = '[%s [[File:Blue pencil.svg|%s|10px|baseline|link=]] ]'
	local url = mw.uri.fullUrl('d:' .. id, 'uselang=nb')
	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}}
--  noedit   - angir om en "editeringslink" som peker mot Wikidata IKKE skal legges på raden. Kun tilgjengelig hvis alle data hentes fra Wikidata.
--             angis slik: {{#invoke:WikidataIB|rowProperty|P26|noedit=1}}
--  fallback - angir om lenker skal opprettes mot wikidata hvis lokal artikkel ikke finnes
--             angis slik: {{#invoke:WikidataIB|rowProperty|P26|fallback=1}}
--
function p.rowProperty(frame)
	-- Get argument "property"
	local property = getArgument(frame, i18n['argument-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 i18n['category-rowproperty-no-arguments']
	end
	-- Get argument "liste"
	local list = getArgument(frame, i18n['argument-list'])
	if list and list ~= "" and tonumber(list) ~= 0 then
		list = tonumber(list)
	else
		list = nil
	end
	-- Get argument "dato"
	local date = getArgument(frame, i18n['argument-date'])
	-- Get argument "pnavn"
	local label = getArgument(frame, i18n['argument-label'])
	-- Get argument "verdi"
	local wiki_value = getArgument(frame, i18n['argument-value'])
	-- Get argument "noedit"
	-- 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, i18n['argument-noedit'])
	if edit and edit == "1" and not wiki_value then
		edit = false
	else
		edit = true
	end
	-- Get argument "fallback"
	local fallback = getArgument(frame, i18n['argument-fallback'])
	if fallback and fallback ~= "" then
		fallback = true
	else
		fallback = false
	end
	-- Get argument "suffix"
	local usesuffix = getArgument(frame, i18n['argument-suffix'])
	local value = getValue(property, list, date, edit, fallback, usesuffix, wiki_value)
	local retval = ""
	if value and value ~= "" then
		retval = retval .. '<tr class="rad" valign="top"><th colspan="2">' .. getProperty(property, label) .. '</th><td colspan="2"'
		if list then
			retval = retval .. ' class="reducible"'
		end
		retval = retval .. '>'
		if edit and (not wiki_value or wiki_value == "") then
			retval = retval .. addLinkback(value)
		else
			retval = retval .. value
		end
		retval = retval .. '</td></tr>'
	end
	return retval
end

function p.getPropertyValue(frame)
	-- Get argument "property"
	local property = getArgument(frame, i18n['argument-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 i18n['category-getpropertyvalue-no-arguments']
	end
	-- Get argument "liste"
	local list = getArgument(frame, i18n['argument-list'])
	if list and list ~= "" and list == "1" then
		list = true
	else
		list = false
	end
	-- Get argument "dato"
	local date = getArgument(frame, i18n['argument-date'])
	-- Get argument "verdi"
	local wiki_value = getArgument(frame, i18n['argument-value'])
	-- Get argument "noedit"
	-- 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, i18n['argument-noedit'])
	if edit and edit == "1" and not wiki_value then
		edit = false
	else
		edit = true
	end
	-- Get argument "fallback"
	local fallback = getArgument(frame, i18n['argument-fallback'])
	if fallback and fallback ~= "" then
		fallback = true
	else
		fallback = false
	end
	-- Get argument "suffix"
	local usesuffix = getArgument(frame, i18n['argument-suffix'])
	local value = getValue(property, list, date, edit, fallback, usesuffix, wiki_value)
	local retval = ""
	if value and value ~= "" then
		if edit and (not wiki_value or wiki_value == "") then
			retval = addLinkback(value)
		else
			retval = value
		end
	end
	return retval
end

function p.getPropertyLabel(frame)
	-- Get argument "property"
	local property = getArgument(frame, i18n['argument-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 i18n['category-getpropertylabel-no-arguments']
	end
	-- Get argument "pnavn"
	local label = getArgument(frame, i18n['argument-label'])
	return getProperty(property, label)
end

return p