Modul:SimpleDataAccess

aus Wikipedia, der freien Enzyklopädie
--[=[ SimpleDataAccess 2022-07-03
Data Management Module for simplified Access from Within Other Modules
Author: Vollbracht
* MainSnackValue(qualifier, property)		wikiData first main snack value
* MainSnackTime(qualifier, property)		wikiData first main snack value
* indirectMSValue(qualifier, properties)	wikiData first main snack value
* qualifiersLineup(qualifier, property)	sequence of wikiData qualifiers
]=]
	
--Module globals
local service = {}

--[[
	qualifyingValue(statement, propQual)
	simplified view on data
	Parameters:
		statement:	statement given for an entity
		propQual:	qualifier for a statement value, defaults to MainSnack
	returns: a string value of MainSnack or property qualifier if available
]]
service.qualifyingValue = function(statement, propQual)
	if statement == nil or statement == "" then
		mw.log('no qualifier in pQV')
		return ""
	end
	local result = ""
	if propQual and propQual ~= "" and statement["qualifiers"]
	            and type(statement["qualifiers"][propQual]) == "table" then
		result = statement["qualifiers"][propQual][1]["datavalue"]["value"]
	else
		result = statement["mainsnak"]["datavalue"]["value"]
	end
	if type(result) == 'table' then
		if result["id"] then return mw.wikibase.getLabel(result["id"]) end
		return ""
	end
	return result
end

--[[
	MainSnackValue(qualifier, property)
	simplified view on data
	Parameters:
		qualifier:	case 1: wikiData qualifier of an element with a property
					case 2: wikiData element with a property
		property:	Property of the element having result as value
	returns:	a wikiData qualifier or a string value of MainSnack if available
				limited: no regard of anything but first statement
				limited: only label if id
				limited: no regard of text language if text
]]

local get1stStatement = function(qualifier, property)
	if qualifier == nil or qualifier == "" then
		mw.log('no qualifier in MSV')
		return ""
	end
	local statementList = {}
	if type(qualifier) == "table" then
		statementList = qualifier:getBestStatements(property)
	else
		statementList = mw.wikibase.getBestStatements(qualifier, property)
	end
	if statementList and type(statementList) == "table" then
		local result = statementList[1]
		if type(result) == "table" then
			return result
		end
	end
	mw.log('no data: (' .. qualifier .. '.' .. property .. '):')
	return ""
end

service.MainSnackValue = function(qualifier, property)
	local result = get1stStatement(qualifier, property)
	if result == "" then return "" end
	result = result["mainsnak"]["datavalue"]["value"]
	if type(result) == 'table' then
		local id = result["id"]
		if id then
			local label = mw.wikibase.getLabel(id)
			if label then return label
			else
				mw.log('result entity w/o label')
				mw.logObject(mw.wikibase.getEntity(id))
				return id
			end
		end
		local text = result["text"]
		if text and text ~= "" then return text end
		mw.log('result table w/o id or text')
		mw.logObject(result)
		return ""
	end
	return result
end

service.MainSnackTime = function(qualifier, property)
	local result = get1stStatement(qualifier, property)
	if result == "" then return "" end
	result = result["mainsnak"]["datavalue"]["value"]
	if type(result) == 'table' then
		local test = result["time"]
		if test then return result end
		mw.log('result table w/o time')
		mw.logObject(result)
		return ""
	end
	mw.log('result w/o time object: ' .. result)
	return ""
end

--[[
	indirectMSValue(qualifier, properties)
	Parameters:
		qualifier:	case 1: wikiData qualifier of an element with a property
					case 2: wikiData element with a property
		properties:	sequence of properties in a string, optionally separated
	returns:
		first MainSnackValue of last property of element given in seccond last
		property ... of element given in first property of given element
]]
service.indirectMSValue = function(qualifier, properties)
	if qualifier == nil or qualifier == "" then
		mw.log('no qualifier')
		return ""
	end
	local statementList = {}
	local t = type(qualifier)
	local qual = qualifier
	local result = ""
	mw.log(properties)
	local props = {}
	for prop in properties:gmatch('[pP]%d+') do
		table.insert(props, prop)
	end
	if #props == {} then return "" end
	if t == "table" then
		statementList = qual:getBestStatements(props[1])
	else
		statementList = mw.wikibase.getBestStatements(qual, props[1])
	end
	local i = 1
	-- process all but last properties
	while i < #props do
		if statementList == nil or statementList[1] == nil then
			-- revoke result
			mw.log('no ' .. props[i] .. ' in ' .. qual)
			return ""
		end
		result = statementList[1]["mainsnak"]["datavalue"]["value"]
		t = type(result)
		if t ~= 'table' then
			-- revoke result
			mw.log(result .. ' is ' .. props[1] .. ' in ' .. qual .. ' but ' .. t)
			return ""
		end
		result = result["id"]
		if result == nil or result:find('[qQ]%d+') == nil then
			mw.log(result .. ' is ' .. props[1] .. ' in ' .. qual .. ' but no id.')
			return ""
		end
		qual = result
		mw.log('next for ' .. mw.wikibase.getLabel(qual))
		i = i + 1
		statementList = mw.wikibase.getBestStatements(qual, props[i])
	end
	-- process last property
	if statementList == nil or statementList[1] == nil then
		-- revoke result
		mw.log('no ' .. props[i] .. ' in ' .. qual)
		return ""
	end
	result = statementList[1]["mainsnak"]["datavalue"]["value"]
	if type(result) == 'table' then
		if result["id"] then return mw.wikibase.getLabel(result["id"]) end
		return ""
	end
	return result
end

--[[
	qualifiersLineup(qualifier, property)
	sequence of wikiData qualifiers
	Parameters:
		qualifier:	case 1: wikiData qualifier of an element with a property
					case 2: wikiData element with a property
		property:	Property of the element having result qualifiers as values
	returns:
		best statement's values as long as they are wikiData qualifiers lined up
		in a table with respect on series ordinals if available
	constraint:
		It's in the responsibility of wikiData to provide correct data. In case
		of corruption this function might return an empty table or one that
		apears empty by starting with index ~= 1.
]]
service.qualifiersLineup = function(qualifier, property)
	if qualifier == nil or qualifier == "" then
		return {}
	end
	local statementList = {}
	local t = type(qualifier)
	if t == "table" then
		statementList = qualifier:getBestStatements(property)
	else
		statementList = mw.wikibase.getBestStatements(qualifier, property)
	end
	if statementList == nil then
		return {}
	end
	local result = {}
	for i, elm in ipairs(statementList) do
		local eVal = elm["mainsnak"]["datavalue"]["value"]
		if eVal == nil then return result end
		t = type(eVal)
		if t ~= "table" then return result end
		local eQual = eVal["id"]
		if eQual == nil then return result end
		local iQual = elm["qualifiers"]
		if iQual == nil then
			mw.log('kein qualifier in ' .. eQual)
			table.insert(result, eQual)
		else
			iQual = iQual["P1545"]
			if iQual == nil then
				mw.log('keine Ornungsnummer in ' .. eQual)
				table.insert(result, eQual)
			else
				local eNum = iQual[1]["datavalue"]["value"]
				table.insert(result, eNum, eQual)
			end
		end
	end
	return result
end

return service