Moduł:Brudnopis/Paweł Ziemian/Wikidane

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Wikidane/opis

local moduleData = mw.loadData("Module:Brudnopis/Paweł Ziemian/Wikidane/data")

local function loadArg(frame,id)
	local result = frame.args[id]
	if not result or (#result == 0) then
		result = frame:getParent().args[id]
		if not result or (#result == 0) then
			return nil
		end
	end
 
	return result
end

local function getEntityData(frame)
	local qid = loadArg(frame, "id")
	local data = mw.wikibase.getEntityObject(qid)
	if not data then
		return
	end

	local f = frame.args[1] and frame or frame:getParent()
	local i = 1
	if (f.args[1] == "bestclaims") or (f.args[1] == "claims") then
		local pid = f.args[2]
		if not pid then
			return f, {}, 2
		end
		
		local sd = require("Module:Brudnopis/Paweł Ziemian/Wikidane/select")
		local _, qid, prop = sd.selectProperty(pid, sd.prepareFilters(frame), qid)
		if not qid then
			return nil
		end
		
		data = prop
		i = 3
	end

	return f, data, i
end

return {

	T = function(frame)
		local qid = loadArg(frame, "id")
		local pid = loadArg(frame, 1)
		if not pid then
			return moduleData.errorMissingPropertyId
		end
		
		local sd = require("Module:Brudnopis/Paweł Ziemian/Wikidane/select")
		local pid, qid, prop = sd.selectProperty(pid, sd.prepareFilters(frame), qid, false, loadArg(frame, "ranga"))
		if qid then
			return #prop
		end
	end,
	
	P = function(frame)
		local qid = loadArg(frame, "id")
		local pid = loadArg(frame, 1)
		if not pid then
			return moduleData.errorMissingPropertyId
		end
		
		local sd = require("Module:Brudnopis/Paweł Ziemian/Wikidane/select")
		local pid, qid, prop = sd.selectProperty(pid, sd.prepareFilters(frame), qid, tonumber(loadArg(frame, "ile")),  loadArg(frame, "ranga"))
		if qid then
			return require("Moduł:Brudnopis/Paweł Ziemian/Wikidane/format").run(frame, pid, prop)
		end
	end,
	
	etykieta = function(frame)
		local qid = loadArg(frame, "id")
		local entity = mw.wikibase.getEntityObject(qid)
		if entity then
			local i = 1
			local lang = true
			while lang do
				lang = frame.args[i]
				local result = entity:getLabel(lang)
				if result and (#result > 0) then
					return result
				end
				
				i = i + 1
			end
		end
	
		return mw.title.getCurrentTitle().text .. moduleData.itemWithoutLabelCat
	end,

	id = function(frame)
		local title = loadArg(frame, 1)
		return title
			and mw.wikibase.getEntityIdForTitle(mw.getContentLanguage():ucfirst(title))
			or mw.wikibase.getEntityIdForCurrentPage()
	end,
	
	label = function(frame)
		local id = loadArg(frame, 1)
		if id and string.match(id, "^[PQ]%d+$") then
			return mw.wikibase.label(id)
		end
	end,
	
	page = function(frame)
		local id = loadArg(frame, 1)
		if id then
			return mw.wikibase.sitelink(id)
		else
			return nil
		end
	end,

	V = function(frame)
		local f, data, i = getEntityData(frame)
		if not data then
			return nil
		end

		while true do
			local index = f.args[i]
			if not index then
				return tostring(data)
			end

			data = data[index] or data[tonumber(index)]
			if not data then
				return
			end

			i = i + 1
		end
	end,

	Dump = function(frame)
		local f, data, i = getEntityData(frame)
		if not data then
			return moduleData.warnDump
		end

		while true do
			local index = f.args[i]
			if not index then
				return "<pre>"..mw.dumpObject(data).."</pre>"..moduleData.warnDump
			end

			data = data[index] or data[tonumber(index)]
			if not data then
				return moduleData.warnDump
			end

			i = i + 1
		end
	end,
}