Modul:Wikidata/Sorters/haslabel

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Wikidata/Sorters/haslabel/Dokumentace

require 'Modul:No globals'

local p = {}

local lib = require 'Modul:Wikidata/lib'
local Formatters = require 'Modul:Wikidata/Formatters'

local cache = {}
local function getLabel(value)
	if not cache[value] then
		cache[value] = mw.wikibase.label(value) or false
	end
	return cache[value] or nil
end

local function hasLabel(snak)
	return lib.IsSnakValue(snak) and getLabel(Formatters.getRawValue(snak)) and true
end

function p.isCompleteSnak(snak)
	return true
end

function p.isCompleteStatement(statement)
	return p.isCompleteSnak(statement.mainsnak)
end

function p.mayCompareSnak(snak)
	return lib.datatypeToValueType[snak.datatype] == 'wikibase-entityid'
end

function p.mayCompareStatement(statement)
	return p.mayCompareSnak(statement.mainsnak)
end

function p.compareSnaks(first, second)
	local first_has_label = hasLabel(first)
	local second_has_label = hasLabel(second)
	if first_has_label == second_has_label then
		return 0
	elseif first_has_label then
		return -1
	else
		return 1
	end
end

function p.compareStatements(first, second)
	return p.compareSnaks(first.mainsnak, second.mainsnak)
end

return p