Moduł:Wikidane/select: Różnice pomiędzy wersjami

Z Czarnobyl Wiki
Skocz do: nawigacja, szukaj
m (1 wersja)
m (1 wersja)
 
(Nie pokazano 1 wersji utworzonej przez jednego użytkownika)
Linia 7: Linia 7:
 
if not result or (#result == 0) then
 
if not result or (#result == 0) then
 
return nil
 
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
 
end
 
end
Linia 66: Linia 52:
  
 
return true
 
return true
 +
end
 +
 +
local function take(sequence, maxCount)
 +
if not maxCount or (type(sequence) ~= "table") or (maxCount < 1) or (#sequence < maxCount) then
 +
return sequence
 +
end
 +
 +
local result = {}
 +
for i = 1, maxCount do
 +
result[i] = sequence[i]
 +
end
 +
 +
return result
 
end
 
end
  
Linia 91: Linia 90:
 
end,
 
end,
  
selectProperty = function(pid, filters, qid)
+
selectProperty = function(pid, filters, qid, maximumNumberOfItems)
 
local entity = mw.wikibase.getEntityObject(qid)
 
local entity = mw.wikibase.getEntityObject(qid)
 
if not entity then
 
if not entity then
Linia 109: Linia 108:
 
if not P then
 
if not P then
 
mw.log(string.format(moduleData.warnPropertyByName, pid))
 
mw.log(string.format(moduleData.warnPropertyByName, pid))
claims = loadClaimsByName(claims)
+
P = mw.wikibase.resolvePropertyId(pid)
 +
if not P then
 +
return pid, false
 +
end
 +
pid = P
 
elseif #P == 0 then
 
elseif #P == 0 then
 
pid = "P" .. pid
 
pid = "P" .. pid
Linia 136: Linia 139:
  
 
if #preferred > 0 then
 
if #preferred > 0 then
return pid, qid, preferred
+
return pid, qid, take(preferred, tonumber(maximumNumberOfItems))
 
elseif #normal > 0 then
 
elseif #normal > 0 then
 
mw.log(string.format(moduleData.warnUsingNormalRank, pid))
 
mw.log(string.format(moduleData.warnUsingNormalRank, pid))
return pid, qid, normal
+
return pid, qid, take(normal, tonumber(maximumNumberOfItems))
 
else
 
else
 
mw.log(string.format(moduleData.warnNoAcceptableRank, pid))
 
mw.log(string.format(moduleData.warnNoAcceptableRank, pid))

Aktualna wersja na dzień 20:56, 29 paź 2017

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

Błąd skryptu: Błąd Lua: Błąd wewnętrzny: Proces interpretera został zakończony z sygnałem "-129".

local moduleData = mw.loadData("Module: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 checkFilters(item, filters)
	for k, v in pairs(filters) do
		mw.log(k)
		local qualifiers = item.qualifiers and item.qualifiers[k]
		if not qualifiers and not v.missing then
			mw.log("missing not available")
			return false
		end
		
		local accepted = false
		for _, w in ipairs(qualifiers) do
			mw.logObject(w, "w")
			if (w.snaktype == "novalue") and v.novalue then
				accepted = true
				break
			end
			
			if (w.snaktype == "somevalue") and v.somevalue then
				accepted = true
				break
			end
			
			if w.snaktype == "value" then
				if (w.datatype == "wikibase-item") and (w.datavalue.type == "wikibase-entityid") and (w.datavalue.value["entity-type"] == "item") and v["Q"..w.datavalue.value["numeric-id"]] then
					accepted = true
					break
				elseif (w.datatype == "globe-coordinate") and (w.datavalue.type == "globecoordinate") and v.globe then
					accepted = true
					break
				end
			end
		end
	
		if not accepted then
			return false
		end
	end

	return true
end

local function take(sequence, maxCount)
	if not maxCount or (type(sequence) ~= "table") or (maxCount < 1) or (#sequence < maxCount) then
		return sequence
	end
	
	local result = {}
	for i = 1, maxCount do
		result[i] = sequence[i]
	end
	
	return result
end

return {
	
prepareFilters = function(frame)
	local result = {}
	for i, v in ipairs(moduleData.qualifiedFilters) do
		local selectors = frame.args[v]
		if selectors and (#selectors > 0) then
			local map = {}
			local any = false
			for _, w in ipairs(mw.text.split(selectors, "%s")) do
				map[w] = true
				any = true
			end
			
			if any then
				result[v] = map
			end
		end
	end
	
	return result
end,

selectProperty = function(pid, filters, qid, maximumNumberOfItems)
	local entity = mw.wikibase.getEntityObject(qid)
	if not entity then
		mw.log(moduleData.warnNoEntity)
		return pid, false
	end
 
 	qid = entity.id
 	
	local claims = entity.claims
	if not claims then
		mw.log(moduleData.warnNoClaims)
		return pid, 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))
		P = mw.wikibase.resolvePropertyId(pid)
		if not P then
			return pid, false
		end
		pid = P
	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 pid, false
	end
	
	-- load best statements
	local preferred = {}
	local normal = {}
	for i, p in ipairs(prop) do
		if checkFilters(p, filters) then
			if p.rank == "preferred" then
				table.insert(preferred, p)
			elseif p.rank == "normal" then
				table.insert(normal, p)
			end
		end
	end

	if #preferred > 0 then
		return pid, qid, take(preferred, tonumber(maximumNumberOfItems))
	elseif #normal > 0 then
		mw.log(string.format(moduleData.warnUsingNormalRank, pid))
		return pid, qid, take(normal, tonumber(maximumNumberOfItems))
	else
		mw.log(string.format(moduleData.warnNoAcceptableRank, pid))
		return pid, false
	end
end,

}