モジュール:Wikidata-de

-- module local variables
local wiki =
{
	langcode = mw.language.getContentLanguage().code
}
-- internationalisation
local i18n =
{
	"errors" =
	{
		"property-not-found" = "Eigenschaft nicht gefunden.",
		"entity-not-found" = "Wikidata-Eintrag nicht gefunden.",
		"unknown-claim-type" = "Unbekannter Aussagentyp.",
		"unknown-entity-type" = "Unbekannter Entity-Typ.",
		"qualifier-not-found" = "Qualifikator nicht gefunden.",
		"site-not-found" = "Wikimedia-Projekt nicht gefunden.",
		"invalid-parameters" = "Ungültige Parameter.",
		"module-not-loaded" = "Loading of additional module failed."
	},
	"datetime" =
	{
		-- $1 is a placeholder for the actual number
		0 = "$1 Mrd. Jahren",		-- precision: billion years
		1 = "$100 Mio. Jahren",	-- precision: hundred million years
		2 = "$10 Mio. Jahren",	-- precision: ten million years
		3 = "$1 Mio. Jahren",		-- precision: million years
		4 = "$100.000 Jahren",	-- precision: hundred thousand years
		5 = "$10.000 Jahren",		-- precision: ten thousand years
		6 = "$1. Jahrtausend", 	-- precision: millenium
		7 = "$1. Jahrhundert",	-- precision: century
		8 = "$1er",				-- precision: decade
		-- the following use the format of #time parser function
		9  = "Y",					-- precision: year,
		10 = "F Y",				-- precision: month
		11 = "j. F Y",			-- precision: day
		12 = 'j. F Y, G "Uhr"',	-- precision: hour
		13 = "j. F Y G:i",		-- precision: minute
		14 = "j. F Y G:i:s",		-- precision: second
		"beforenow" = "vor $1",	-- how to format negative numbers for precisions 0 to 5
		"afternow" = "in $1",		-- how to format positive numbers for precisions 0 to 5
		"bc" = '$1 "v.Chr."',		-- how print negative years
		"ad" = "$1"				-- how print positive years
	},
	"monolingualtext" = '%text',
	"FETCH_WIKIDATA" = "ABFRAGE_WIKIDATA"
}
--important properties
local propertyId =
{
	"starttime" = "P580",
	"endtime" = "P582"
}
local formatchar =
{
	10 = {"n","m","M","F","xg"},				--precision: month
	11 = {"W","j","d","z","D","l","N","w"},	--precision: day
	12 = {"a","A","g","h","G","H"},			--precision: hour
	13 = {"i"},								--precision: minute
	14 = {"s","U"}							--precision: second
}
local function printError(code)
	return  .. (i18n.errorscode or code) .. 
end
-- the "qualifiers" and "snaks" field have a respective "qualifiers-order" and "snaks-order" field
-- use these as the second parameter and this function instead of the built-in "pairs" function
-- to iterate over all qualifiers and snaks in the intended order.
local function orderedpairs(array, order)
	if not order then return pairs(array) end
	-- return iterator function
	local i = 0
	return function()
		i = i + 1
		if orderi then
			return orderi, arrayorderi
		end
	end
end
-- Function to check whether a certain item is a parent of a given item.
-- If pExitItem is reached without finding the searched parent item, the search stops.
-- A parent is connected via P31 or P279.
-- Attention: very intensive function, use carefully!
local function isParent(pItem, pParent, pExitItem, pMaxDepth, pDepth)
	if not pDepth then pDepth = 0 end
	if type(pItem) == "number" then pItem = "Q" .. pItem end
	local entity = mw.wikibase.getEntity(pItem)
	if not entity then return false end
	local claims31
	local claims279
	if entity.claims then
		claims31 = entity.claimsmw.wikibase.resolvePropertyId('P31')
		claims279 = entity.claimsmw.wikibase.resolvePropertyId('P279')
	else
		return false
	end
	if not claims31 and not claims279 then return false end
	local parentIds = {}
	if claims31 and #claims31 > 0 then
		for i, v in ipairs(claims31) do parentIds#parentIds+1 = getSnakValue(v.mainsnak, "numeric-id") end
	end
	if claims279 and #claims279 > 0 then
		for i, v in ipairs(claims279) do parentIds#parentIds+1 = getSnakValue(v.mainsnak, "numeric-id") end
	end
	-- check if searched parent or exit item is reached or do recursive call
	if not parentIds1 or #parentIds == 0 then return false end
	local itemString = ""
	local result = nil
	for i, v in ipairs(parentIds) do
		if not v then return false end
		itemString = "Q" .. v
		if itemString == pParent then
			-- successful!
			return true
		elseif itemString == pExitItem or itemString == "Q35120" then
			-- exit if either "exit item" or node item (Q35120) is reached
			return false
		else
			if pDepth+1  0 then era = mw.ustring.gsub(mw.ustring.gsub(i18n.datetime.ad, '"', ""), "$1", era) end
		return era
	end
	-- precision is years or less
	if precision >= 9 then
		-- the following code replaces the UTC suffix with the given negated timezone to convert the global time to the given local time
		timezone = tonumber(timezone)
		if timezone and timezone ~= 0 then
			timezone = -timezone
			timezone = string.format("%.2d%.2d", timezone / 60, timezone % 60)
			if timezone1 ~= '-' then timezone = "+" .. timezone end
			date = mw.text.trim(date, "Z") .. " " .. timezone
		end
		--
		if formatstr then
			for i=(precision+1), 14 do
				for _, ch in pairs(formatchari) do
					if formatstr:find(ch) then
						formatstr = i18n.datetimeprecision
					end
				end
			end
		else
			formatstr = i18n.datetimeprecision
		end
		if year == 0 then formatstr = mw.ustring.gsub(formatstr, i18n.datetime9, "")
		elseif year  0 and i18n.datetime.ad ~= "$1" then
			formatstr = mw.ustring.gsub(formatstr, i18n.datetime9, mw.ustring.gsub(i18n.datetime.ad, "$1", i18n.datetime9))
		end
		return mw.language.new(wiki.langcode):formatDate(formatstr, date)
	end
end
local function printDatavalueTime(data, parameter)
	-- data fields: time ISO 8601 time, timezone int in minutes, before int, after int, precision int, calendarmodel wikidata URI
	--   precision: 0 - billion years, 1 - hundred million years, ..., 6 - millenia, 7 - century, 8 - decade, 9 - year, 10 - month, 11 - day, 12 - hour, 13 - minute, 14 - second
	--   calendarmodel: e.g. http://www.wikidata.org/entity/Q1985727 for the proleptic Gregorian calendar or http://www.wikidata.org/wiki/Q11184 for the Julian calendar
	if parameter then
		para, formatstr = parameter:match("(^:+):(^:+)")
		if parameter == "calendarmodel" then
			data.calendarmodel = string.match(data.calendarmodel, "Q%d+") -- extract entity id from the calendar model URI
		elseif para and para == "time" then
			return formatDate(data.time, data.precision, data.timezone,formatstr)
		elseif parameter == "time" then
			data.time = normalizeDate(data.time)
		end
		return dataparameter
	else
		return formatDate(data.time, data.precision, data.timezone)
	end
end
local function printDatavalueEntity(data, parameter)
	-- data fields: entity-type string, numeric-id int, Wikidata id
	local id
	if data"entity-type" == "item" then id = "Q" .. data"numeric-id"
	elseif data"entity-type" == "property" then id = "P" .. data"numeric-id"
	else return printError("unknown-entity-type")
	end
	if parameter then
		if parameter == "link" then
			local linkTarget = mw.wikibase.sitelink(id)
			local linkName = mw.wikibase.label(id)
			if linkTarget then
				local link = linkTarget
				-- if there is a local Wikipedia article linking to it, use the label or the article title
				if linkName and (linkName ~= linkTarget) then link = link .. "|" .. linkName end
				return "" .. link .. ""
			else
				-- if there is no local Wikipedia article output the label or link to the Wikidata object to input a proper label
				if linkName then return linkName else return "" .. id .. "" end
			end
		else
			return dataparameter
		end
	else
		return mw.wikibase.label(id) or id
	end
end
local function printDatavalueMonolingualText(data, parameter)
	-- data fields: language string, text string
	if parameter then
		return dataparameter
	else
		local result = mw.ustring.gsub(mw.ustring.gsub(i18n.monolingualtext, "%%language", data"language"), "%%text", data"text")
		return result
	end
end
function getSnakValue(snak, parameter)
	-- snaks have three types: "novalue" for null/nil, "somevalue" for not null/not nil, or "value" for actual data
	if snak.snaktype == "value" then
		-- call the respective snak parser
		if snak.datavalue.type == "string" then return snak.datavalue.value
		elseif snak.datavalue.type == "globecoordinate" then return printDatavalueCoordinate(snak.datavalue.value, parameter)
		elseif snak.datavalue.type == "quantity" then return printDatavalueQuantity(snak.datavalue.value, parameter)
		elseif snak.datavalue.type == "time" then return printDatavalueTime(snak.datavalue.value, parameter)
		elseif snak.datavalue.type == "wikibase-entityid" then return printDatavalueEntity(snak.datavalue.value, parameter)
		elseif snak.datavalue.type == "monolingualtext" then return printDatavalueMonolingualText(snak.datavalue.value, parameter)
		end
	end
	return mw.wikibase.renderSnak(snak)
end
function getQualifierSnak(claim, qualifierId)
	-- a "snak" is Wikidata terminology for a typed key/value pair
	-- a claim consists of a main snak holding the main information of this claim,
	-- as well as a list of attribute snaks and a list of references snaks
	if qualifierId then
		-- search the attribute snak with the given qualifier as key
		if claim and claim.qualifiers then
			local qualifier = claim.qualifiersqualifierId
			if qualifier then return qualifier1 end
		end
		return nil, printError("qualifier-not-found")
	else
		-- otherwise return the main snak
		return claim.mainsnak
	end
end
local function datavalueTimeToDateObject(data)
	local sign, year, month, day, hour, minute, second = string.match(data.time, "(.)(%d+)%-(%d+)%-(%d+)T(%d+):(%d+):(%d+)Z")
	local result =
	{
		year = tonumber(year),
		month = tonumber(month),
		day = tonumber(day),
		hour = tonumber(hour),
		min = tonumber(minute),
		sec = tonumber(second),
		timezone = data.timezone,
		julian = data.calendarmodel and string.match(data.calendarmodel, "Q11184$")
	}
	if sign == "-" then result.year = -result.year end
	return result
end
function julianDay(dateObject)
	local year = dateObject.year
	local month = dateObject.month or 0
	local day = dateObject.day or 0
	if month == 0 then month = 1 end
	if day == 0 then day = 1 end
	if month  0 then
				Zitation.fill(wdZmapprop1, wdZmapprop2, value, prop)	
			end
		end
	end
	-- if no title on Wikidata, try to use the URL as title
	if (not ref"P1476") and ref"P854" then
		local URLutil = Zitation.fetch("URLutil")
		Zitation.fill("bas", "Titel", URLutil.getHost(ref"P854"))
	elseif not (ref"P1476") then
		Zitation.fill("bas", "Titel", ref"P854")
	end
	refFormatted, f = Zitation.format()
	return refFormatted
end
function getReferences(frame, claim)
	local result = ""
	-- traverse through all references
	for ref in pairs(claim.references or {}) do
		local refTable = {}
		for snakkey, snakval in orderedpairs(claim.referencesref.snaks or {}, claim.referencesref"snaks-order") do
			if #snakval == 1 then
				refTablesnakkey = getSnakValue(snakval1)
			else
				--
				multival = {}
				for snakidx = 1, #snakval do
						table.insert(multival, getSnakValue(snakvalsnakidx))
				end
				refTablesnakkey = multival
			end
		end
		local formattedRef = formatReference(refTable)
		if formattedRef then result = result .. frame:extensionTag("ref", formattedRef, { name = '_' .. mw.hash.hashValue('fnv164', formattedRef) }) end
	end
	return result
end
local function hasqualifier(claim, qualifierproperty)
	local invert
	if string.sub(qualifierproperty, 1, 1) == "!" then invert = true else invert = false end
	if not claim.qualifiers and not invert then return false end
	if not claim.qualifiers and invert then return true end
	if qualifierproperty ==  then return true end
	if not invert and not claim.qualifiersqualifierproperty then return false end
	if invert and claim.qualifiersstring.sub(qualifierproperty, 2) then return false end
	return true
end
local function qualifierhasvalue(claim, property, value)
	if not claim.qualifiers then return false end
	if not claim.qualifiersproperty then return false end
	for key, snak in pairs(claim.qualifiersproperty) do
		if snak.snaktype == "value" then
			if snak.datavalue.type == "wikibase-entityid" then
				if snak.datavalue.value.id == value then
					return true
				end
			--TODO: elseif other types
			end
		end
	end
	return false
end
local function hassource(claim, sourceproperty)
	if not claim.references then return false end
	if sourceproperty ==  then return true end
	if string.sub(sourceproperty,1,1) ~= "!" then
		for _, source in pairs(claim.references) do
			if source.snakssourceproperty then return true end
		end
		return false
	else
		for _, source in pairs(claim.references) do
			for key in pairs(source.snaks) do
				if key ~= string.sub(sourceproperty,2) then return true end
			end
		end
		return false
	end
end
function atdate(claim, mydate)
	local refdate
	if not mydate or mydate == "" then
		refdate = os.date("!*t")
	else
		if string.match(mydate, "^%d+$") then
			refdate = { year = tonumber(mydate) }
		else
			refdate = datavalueTimeToDateObject({ time = mw.language.getContentLanguage():formatDate("+Y-m-d\\TH:i:s\\Z", mydate) })
		end
	end
	local refjd = julianDay(refdate)
	local mindate = getQualifierSortValue(claim, propertyId"starttime")
	local maxdate = getQualifierSortValue(claim, propertyId"endtime")
	if mindate and mindate > refjd then return false end
	if maxdate and maxdate  2 and key % 2 == 1 then
			-- key = 3, 5, 7 and so on
			local newclaims = {}
			for i, claim in pairs(claims) do
				if qualifierhasvalue(claim, frame.argskey - 1, frame.argskey) then
					table.insert(newclaims, claim)
				end
			end
			claims = newclaims
		end
	end
	
	return claims
end
local p = {}
function p.isSubclass(frame)
	if not frame.args"parent" then return "" end
	local maxDepth
	maxDepth = frame.args"maxDepth" or 5
	if not type(maxDepth) == "number" then maxDepth = 5 end
	local result
	result = isParent(frame.args"id", frame.args"parent", frame.args"exitItem", maxDepth)
	
	if frame.args"returnInt" then
		if result == true then return 1 else return "" end
	else
		if result then return result else return false end
	end
	
end
function p.descriptionIn(frame)
	local langcode = frame.args1
	local id = frame.args2
	-- return description of a Wikidata entity in the given language or the default language of this Wikipedia site
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.descriptions then
		local desc = entity.descriptionslangcode or wiki.langcode
		if desc then return desc.value end
	else
		return "";
	end
end
function p.labelIn(frame)
	local langcode = frame.args1
	local id = frame.args2
	-- return label of a Wikidata entity in the given language or the default language of this Wikipedia site
	local entity = mw.wikibase.getEntity(id)
	if entity and entity.labels then
		local label = entity.labelslangcode or wiki.langcode
		if label then return label.value end
	else
		return "";
	end
end
function p.claim(frame)
	local property = frame.args1 or ""
	local id = frame.args"id"
	local qualifierId = frame.args"qualifier"
	local parameter = frame.args"parameter"
	local language = frame.args"language"
	local list = frame.args"list"
	local includeempty = frame.args"includeempty"
	local listMaxItems = tonumber(frame.args"listMaxItems") or 0
	local references = frame.args"references"
	local sort = frame.args"sort"
	local sortInItem = frame.args"sortInItem"
	local inverse = frame.args"inverse"
	local showerrors = frame.args"showerrors"
	local default = frame.args"default"
	if default then showerrors = nil end
	-- get wikidata entity
	local entity = mw.wikibase.getEntity(id)
	if not entity then
		if showerrors then return printError("entity-not-found") else return default end
	end
	-- fetch the first claim of satisfying the given property
	local claims
	if entity.claims then claims = entity.claimsmw.wikibase.resolvePropertyId(property) end
	if not claims or not claims1 then
		if showerrors then return printError("property-not-found") else return default end
	end
	--filter claims
	claims = filterClaims(frame, claims)
	if not claims1 then return default end
	-- get initial sort indices
	local sortindices = {}
	for idx in pairs(claims) do
		sortindices#sortindices + 1 = idx
	end
	local comparator
	if sort then
		-- sort by time qualifier
		comparator = function(a, b)
			local timea = getQualifierSortValue(claimsa, sort)
			local timeb = getQualifierSortValue(claimsb, sort)
			if type(timea) ~= type(timeb) and not (tonumber(timea) and tonumber(timeb)) then
				if tonumber(timea) then return true
				elseif tonumber(timeb) then return false
				elseif tostring(timea) and tostring(timeb) then
					if inverse then return tostring(timea) > tostring(timeb) else return tostring(timea)  random result to avoid script error
			elseif tonumber(timea) and tonumber(timeb) then
				timea = tonumber(timea)
				timeb = tonumber(timeb)
			end
			if inverse then
				return timea > timeb
			else
				return timea  sortkeys b
			else
				return sortkeysa  0 then
			result = table.concat(result, list, 1, math.min(table.getn(result), listMaxItems))
		else
			result = table.concat(result, list)
		end
	else
		-- return first element
		local claim = claimssortindices1
		if language and claim.mainsnak.datatype == "monolingualtext" then
			-- iterate over claims to find adequate language
			for idx, claim in pairs(claims) do
				if claim.mainsnak.datavalue.value.language == language then
					result, error = getValueOfClaim(claim, qualifierId, parameter)
					break
				end
			end
		else
			result, error = getValueOfClaim(claim, qualifierId, parameter)
		end
		if references == "only" then 
			result = getReferences(frame, claim)
		elseif result and references then 
			result = result .. getReferences(frame, claim) 
		end
	end
	if result then return result else
		if showerrors then return error else return default end
	end
end
function p.getValue(frame)
	local param = frame.args2
	if param == "FETCH_WIKIDATA" or param == i18n"FETCH_WIKIDATA" then return p.claim(frame) else return param end
end
function p.pageId(frame)
	local id = frame.args1
	local entity = mw.wikibase.getEntity(id)
	if not entity then return "" else return entity.id end
end
function p.labelOf(frame)
	local id = frame.args1
	-- returns the label of the given entity/property id
	-- if no id is given, the one from the entity associated with the calling Wikipedia article is used
	if not id then
		local entity = mw.wikibase.getEntity()
		if not entity then return printError("entity-not-found") end
		id = entity.id
	end
	return mw.wikibase.label(id)
end
function p.sitelinkOf(frame)
	local id = frame.args1
	-- returns the Wikipedia article name of the given entity
	-- if no id is given, the one from the entity associated with the calling Wikipedia article is used
	if not id then
		local entity = mw.wikibase.getEntity()
		if not entity then return printError("entity-not-found") end
		id = entity.id
	end
	return mw.wikibase.sitelink(id)
end
function p.badges(frame)
	local site = frame.args1
	local id = frame.args2
	if not site then return printError("site-not-found") end
	local entity = mw.wikibase.getEntity(id)
	if not entity then return printError("entity-not-found") end
	local badges = entity.sitelinkssite.badges
	if badges then
		local result
		for idx = 1, #badges do
			if result then result = result .. "/" .. badgesidx else result = badgesidx end
		end
		return result
	end
end
function p.sitelinkCount(frame)
	local filter = "^.*" .. (frame.args1 or "") .. "$"
	local id = frame.args2
	local entity = mw.wikibase.getEntity(id)
	local count = 0
	if entity and entity.sitelinks then
		for project, _ in pairs(entity.sitelinks) do
			if string.find(project, filter) then count = count + 1 end
		end
	end
	return count
end
-- call this in cases of script errors within a function instead of ... call ...
function p.debug(frame)
	local func = frame.args1
	if func then
		-- create new parameter set, where the first parameter with the function name is removed
		local newargs = {}
		for key, val in pairs(frame.args) do
			if type(key) == "number" then
				if key > 1 then newargskey - 1 = val end
			else
				newargskey = val
			end
		end
		frame.args = newargs
		local status, result = pcall(pfunc, frame)
		-- if status then return tostring(result) or "" else return  .. result ..  end -- revert
		if status then return result else return  .. result ..  end
	else
		return printError("invalid-parameters")
	end
end
function p.printEntity(frame)
	local id = frame.args1
	local entity = mw.wikibase.getEntity(id)
	if entity then return "" .. mw.text.jsonEncode(entity, mw.text.JSON_PRETTY) .. "" end
end
return p