Dokumentācijas ikona Moduļa dokumentācija[izveidot]
local moduleData = mw.loadData("Module:Wikidata/pl/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 loadClaimsByName(claims)
	local result = {}
	for k, v in pairs(claims) do
		if (type(k) == "string") and k:match"^[Pp]%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?$" then
			local name = mw.wikibase.label(k)
			if name and (#name ~= 0) then
				result[name] = v
			end
		end
	end
 
	return result
end
 
local function selectProperty(pid)
	local entity = mw.wikibase.getEntityObject()
	if not entity then
		mw.log(moduleData.warnNoEntity)
		return false
	end
 
	local claims = entity.claims
	if not claims then
		mw.log(moduleData.warnNoClaims)
		return false
	end
 
	local P, ddd = pid:match"^([Pp]?)(%d%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?%d?)$"
	if not P then
		mw.log(string.format(moduleData.warnPropertyByName, pid))
		claims = loadClaimsByName(claims)
	elseif #P == 0 then
		pid = "P" .. pid
	elseif P == "p" then
		pid = "P" .. ddd
	end
 
	local prop = claims[pid]
	if not prop then
		mw.log(string.format(moduleData.warnNoStatements, pid))
		return false
	end
 
	-- load preferred statements
	local result = {}
	for i, p in ipairs(prop) do
		if (p.rank == "preferred") then
			table.insert(result, p)
		end
	end
 
	if #result ~= 0 then
		return pid, result
	end
 
	mw.log("Ładowanie '"..mw.wikibase.entity.claimRanks.RANK_NORMAL.."'")
	for i, p in ipairs(prop) do
		if p.rank == "normal" then
			table.insert(result, p)
		end
	end
 
	if #result ~= 0 then
		mw.log(string.format(moduleData.warnUsingNormalRank, pid))
		return pid, result
	end
 
	mw.log(string.format(moduleData.warnNoAcceptableRank, pid))
	return false
end
 
return {
	zawiera = function(frame)
		local pid = loadArg(frame, 1)
		local qualifier = loadArg(frame, "qualifier")
		if not pid then
			return moduleData.errorMissingPropertyId
		end
	 
		local status, prop = selectProperty(pid)
		if not status then
			return
		end

		if not qualifier then
			return #prop
		end
		
		local count = 0
		for _, p in ipairs(prop) do
			if p.qualifiers then
				local qualifiers = p.qualifiers[qualifier]
				if qualifiers then
					count = count + #qualifiers
				end
			end
		end
		
		return count > 0 and count or nil
	end,
	
	P = function(frame)
		local pid = loadArg(frame, 1)
		if not pid then
			return moduleData.errorMissingPropertyId
		end
		
		local pid, prop = selectProperty(pid)
		if pid then
			return require("Module:Wikidata/pl/format").run(frame, pid, prop)
		end
	end,
	
	P0 = function(frame)
		local pid = loadArg(frame, 1)
		local expectedType = loadArg(frame, "type")
		local member = loadArg(frame, "member")
		local index = tonumber(loadArg(frame, "index")) or 1
		local default = loadArg(frame, "default")
		local qualifier = loadArg(frame, "qualifier")
		local qualifierIndex = tonumber(loadArg(frame, "qindex")) or 1
		if not pid then
			return default
		end
	 
		local status, prop = selectProperty(pid)
		if not status then
			return default
		end
	 
		local p = prop[index]
		if not p then
			return default
		end
	 
		if p.type ~= "statement" then
			return default
		end
	 
		local snak = false
		if not qualifier then
			snak = p.mainsnak
		elseif p.qualifiers and p.qualifiers[qualifier] then
			snak = p.qualifiers[qualifier][qualifierIndex]
		end
		
		if not snak then
			return default
		end
	 
		if (snak.snaktype ~= "value") or not snak.datavalue then
			return default
		end
	 
		local value = snak.datavalue.value
		if not value then
			return default
		end
	 
		if expectedType and snak.datavalue.type ~= expectedType then
			return default
		end
	 
		if member then
			return value[member]
		else
			return value
		end
	end,
	
	etykieta = function(frame)
		local entity = mw.wikibase.getEntityObject()
		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 entity = mw.wikibase.getEntityObject()
		if entity then
			return entity.id
		else
			return nil
		end
	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,

	voyage = function(frame)
		local entity = mw.wikibase.getEntityObject()
		if not entity then
			return nil
		end
	 
		local sitelinks = entity.sitelinks
		if not sitelinks then
			return nil
		end
	 
		local voyage = sitelinks.plwikivoyage
		if not voyage then
			return nil
		end
	 
		return voyage.title
	end,

	commons = function (frame)
		local entity = mw.wikibase.getEntityObject()
		if not entity then
			return nil
		end
	 
		local sitelinks = entity.sitelinks
		if not sitelinks then
			return nil
		end
	 
		local result = sitelinks.commonswiki
		if not result then
			return nil
		end
	 
		return result.title
	end,

	V = function(frame)
		local data = mw.wikibase.getEntityObject()
		if not data then
			return nil
		end

		local f = frame.args[1] and frame or frame:getParent()

		local i = 1
		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 data = mw.wikibase.getEntityObject()
		if not data then
			return moduleData.warnDump
		end

		local f = frame.args[1] and frame or frame:getParent()

		local i = 1
		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,
}