Saltar para o conteúdo

Módulo:Testes/TiagoLubiana/Wikidata

Origem: Wikipédia, a enciclopédia livre.
--[=[ 
Why creating a new module to process wikidata?
Modules have their own versions throughout Wikipedia projects. I want to have a version which I can use anywhere, regardless of the project.

Some implementation decisions also will be changed too. Functions in the original modules have too many parameters, they do too much. 
This new module will focus a lot on interpretability. 

(it does not mean it will be better, and probably it won't. It is just a different approach to try and solve the same issues.)

Following Test Driven Development standards, all the functions here will be tested in the page :
* https://pt.wikipedia.org/wiki/Usu%C3%A1rio(a):TiagoLubiana/WikidataTDD

]=]--

-- The home variable holds all externally accessible functions.
local home = {};

----------------------
-- Local functions --
----------------------

-- Wrappers for the wikibase client 
-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua#mw.wikibase.getEntity

-- https://www.mediawiki.org/wiki/Extension:Wikibase_Client/Lua#mw.wikibase.getLabel

home.get_qid_value_for_statement = function(frame, position_in_value_list)
	local qid = mw.text.trim(frame.args["qid"])
	local pid = mw.text.trim(frame.args["pid"])
	local position_to_return = position_in_value_list
	local wikibase_entity = mw.wikibase.getEntity(qid)
	local snak = wikibase_entity['claims'][pid]

	if position_in_value_list == "last" then
		position_to_return = #snak
	end
		local text_to_return =  snak[position_to_return]["mainsnak"]["datavalue"]["value"]["id"]
	return text_to_return
end

home.get_first_reference_url_for_statement = function(frame, position_in_value_list)
	local qid = mw.text.trim(frame.args["qid"])
	local pid = mw.text.trim(frame.args["pid"])
	local position_to_return = position_in_value_list
	local wikibase_entity = mw.wikibase.getEntity(qid)
	local snak = wikibase_entity['claims'][pid]

	if position_in_value_list == "last" then
		position_to_return = #snak
	end
	local text_to_return =  snak[position_to_return]["references"][1]["snaks"]["P854"][1]["datavalue"]["value"]
	return text_to_return
end


----------------------
-- Public functions --
----------------------

home.render_item_label = function(frame)
	local qid = mw.text.trim(frame.args[1])
	local label = mw.wikibase.getLabel(qid)
	return label
end

home.get_oldest_qid_value_for_statement = function(frame)
	local text_to_return = home.get_qid_value_for_statement(frame, 1 )
	return(text_to_return)
end
home.get_newest_qid_value_for_statement = function(frame)
	local text_to_return = home.get_qid_value_for_statement(frame, "last" )
	return(text_to_return)
end


home.render_label_of_oldest_value_for_statement = function(frame)
	local value_qid = home.get_oldest_qid_value_for_statement(frame)
	return mw.wikibase.getLabel(value_qid)
end
home.render_label_of_newest_value_for_statement = function(frame)
	local value_qid = home.get_newest_qid_value_for_statement(frame)
	return  mw.wikibase.getLabel(value_qid)
end

home.render_reference_URL_of_oldest_value_for_statement = function(frame)
	local reference_url = home.get_first_reference_url_for_statement(frame, 1)
	return reference_url
end

home.render_reference_URL_of_newest_value_for_statement = function(frame)
	local reference_url = home.get_first_reference_url_for_statement(frame, "last")
	return reference_url
end

----------------------------
-- End Return Statement--
----------------------------

return home