Modulo:WikidataLoĝantaroProvo

El Vikipedio, la libera enciklopedio

Dokumentado por ĉi tiu modulo povas esti kreata ĉe Modulo:WikidataLoĝantaroProvo/dokumentado

local p = {}

function p.dateobject(orig, params) -- transforme un snak en un nouvel objet utilisable par Module:Date complexe
	if not params then
		params = {}
	end
	
	local newobj = splitTimestamp(orig.time, orig.calendar) -- initalise l'object en mettant la valeur des dates
	
	newobj.precision = params.precision or orig.precision
	newobj.type = 'dateobject'
	return newobj
end

function p.getQualifier( frame )
	return p.showQualifier(frame.args)
end

function p.lastaDato(frame)
	local qualifs = "p585"
	local claims = p.getClaims2(frame)
	if not claims then
		return 'malvera'
	end
	return claims
end

local function removeblanks2(args)
	for i, j in pairs(args) do
		if j == '' then args[i] = nil end
	end
	return args
end

function p.getClaims2( args ) -- returns a table of the claims matching some conditions given in args
	args = removeblanks2(args)
	if args.claims then -- if claims have already been set, return them
		return args.claims
	end
	if args.claims then -- if claims have already been set, return them
		return args.claims
	end
    if not args.property then
        return 'paramètre propriété non fourni'
    end
    if type(args.property) == 'table' then
    	return getMultipleClaims(args)
    end
    return args
end

local function getQualifiers(statement, qualifs, params)
	if not statement.qualifiers then
		return nil
	end
	local vals = {}
	for i, j in pairs(qualifs) do
		if statement.qualifiers[j] then
			for k, l in pairs(statement.qualifiers[j]) do
				table.insert(vals, l)
			end
		end
	end
	if #vals == 0 then
		return nil
	end
	return vals
end

function p.conj(args, conjtype)
	if (not args) then
		return nil
	end
	local newargs = {}
	for i, j in pairs(args) do
		if type(j) ~= 'nil' then
			table.insert(newargs, j)
		end
	end
	if #newargs == 0 then
		return nil
	end
	return p.quickconj(newargs, conjtype, lang)
end

function p.getFormattedQualifiers(statement, qualifs, params)
	if not params then params = {} end
	local qualiftable = getQualifiers(statement, qualifs)
	if not qualiftable then
		return nil
	end
	for i, j in pairs(qualiftable) do
		qualiftable[i] = p.formatSnak(j, params)
	end
	return qualiftable[1]  -- nur unu valoro
end


function p.objecttotext(obj, params)
	if obj.type == 'dateobject' then
		return formatDate.simplestring(obj, params)
	elseif obj.type == 'rangeobject' then
		return formatDate.daterange(obj.begin, obj.ending, params)
	end
end


function p.getDatavalue(snak, params)
	if not params then
		params = {}
	end
	local speciallabels = params.speciallabels -- parfois on a besoin de faire une liste d'éléments pour lequel le libellé doit être changé, pas très pratique d'utiliser une fonction pour ça
	if snak.snaktype ~= 'value' then
		return nil
	end

	local datatype = snak.datavalue.type
	local value = snak.datavalue.value
	local displayformat = params.format or params.displayformat -- params.format is deprecated
	if type(displayformat) == 'function' then
        	return displayformat(snak, params)
 	end
 	
	if datatype == 'wikibase-entityid' then
		if displayformat == 'raw' then
			return "Q" .. tostring(value['numeric-id'])
		elseif speciallabels and speciallabels['Q' .. value['numeric-id']] then
			return speciallabels['Q' .. value['numeric-id']]
		else
			return p.formatEntity('Q' .. value['numeric-id'], params)
		end

	elseif datatype == 'string' then
		if params.displayformat == 'weblink' then
			return require('Module:Weblink').makelink(value, params.showntext)
		elseif params.urlpattern then
			value = '[' .. mw.ustring.gsub(params.urlpattern, '$1', value) .. ' ' .. (params.text or value) .. ']'
		end
		return value

	elseif datatype == 'time' then -- format example: +00000001809-02-12T00:00:00Z
		local precision = year -- degré de précision à afficher ('day', 'month', 'year'), inférieur ou égal à value.precision
		if displayformat == 'raw' then
			return value.time
		else
			return value.time
		end

	elseif datatype == 'globecoordinate' then
		-- retourne une table avec clés latitude, longitude, précision et globe à formater par un autre module (à changer ?)
		value.globe = require('Module:Wikidata/Globes')[value.globe] -- transforme l'ID du globe en nom anglais utilisable par geohack
		if formatting == 'latitude' then
			return value.latitude
		elseif formatting == 'longitude' then
			return value.longitude
		else
			return value -- note : les coordonnées Wikidata peuvent être utilisée depuis Module:Coordinates. Faut-il aussi autoriser à appeler Module:Coordiantes ici ?
		end


    elseif datatype == 'quantity' then -- todo : gérer les paramètre précision
        if displayformat == 'raw' then
            return value.amount
        else
            local str = string.sub(value.amount,2) --
            return formatText.do_formatnum({str})
        end
	elseif datatype == 'monolingualtext' then
		return langmodule.langue({value.language, value.text})
    else
        return formatError('unknown-datavalue-type' )
    end
end

function p.formatSnak( snak, params )
    if not args then args = {} end -- pour faciliter l'appel depuis d'autres modules
    if snak.snaktype == 'somevalue' then
        return formatTheUnknown()
    elseif snak.snaktype == 'novalue' then
        return i18n['novalue'] --todo
    elseif snak.snaktype == 'value' then
        return p.getDatavalue( snak, params)
    else
        return formatError( 'unknown-snak-type' )
    end
end

function p.showQualifier( args )
	return args
	--local qualifs = args.qualifiers or args.qualifier
	--if type(qualifs) == 'string' then
	--	qualifs = mw.text.split(qualifs, ',')
	--end
	--if not qualifs then
	--	return formatError( 'property-param-not-provided' )
	--end
	--local claims = p.getClaims(args)
	--if not claims then
	--	return nil
	--end
	--local str = ''
	--for i, j in pairs(claims) do
	--	local new = p.getFormattedQualifiers(j, qualifs, args) or ''
	--	str = str ..  new
	--end
	--str=string.gsub( str,"+", "" )
	--str=string.sub(str,1,4) -- nur la ano
	--return str
end

local function removeblanks(args)
	for i, j in pairs(args) do
		if j == '' then args[i] = nil end
	end
	return args
end

local function getEntity( val )
	if type(val) == 'table' then
		return val
	end
	return mw.wikibase.getEntityObject(val)
end

local function hasqualifier(claim, qualifier, qualifiervalues)
    qualifier = string.upper(qualifier)

    if not qualifier then -- si aucun qualificatif est demandé, ça passe
    	return true
    end
   	if not claim.qualifiers or not claim.qualifiers[qualifier] then
   		return false
   	end

   	if (not qualifiervalues) or (qualifiervalues == {}) then
   		return true -- si aucune valeur spécifique n'est exigée
   	end

   if type(qualifiervalues) == 'string' then
   		qualifiervalues = {qualifiervalues}
   	end

   	for i, j in pairs(claim.qualifiers[qualifier]) do
   		local val = p.getRawvalue(j)
   		for k, l in pairs(qualifiervalues) do
   			if l == val then
   				return true
   			end
   		end
   end
   return false
 end
 
 local function hasrank(claim, target)
	if target == 'valid' then
		return hasrank(claim, 'preferred') or hasrank(claim, 'normal')
	end
	if claim.rank == target then
		return true
	end
	return false
end

local function bestranked(claims)
	if not claims then
		return nil
	end
	local preferred, normal = {}, {}
	for i, j in pairs(claims) do
		if j.rank == 'preferred' then
			table.insert(preferred, j)
		elseif j.rank == 'normal' then
			table.insert(normal, j)
		end
	end
	if #preferred > 0 then
		return preferred
	else
		return normal
	end
end

function p.getClaims( args ) -- returns a table of the claims matching some conditions given in args
	args = removeblanks(args)
	if args.claims then -- if claims have already been set, return them
		return args.claims
	end
    if not args.property then
        return formatError( 'property-param-not-provided' )
    end
    if type(args.property) == 'table' then
    	return getMultipleClaims(args)
    end
    --Get entity
    if args.item then args.entity = args.item end -- synonyms
    local entity = args.entity
    if type(entity) ~= 'table' then
        entity = getEntity( args.entity )
    end
	if (not entity) or (not entity.claims) then
		return nil
	end
	local property = string.upper(args.property)
	if not entity.claims[property] then
		return nil
	end
	if not args.rank then
		args.rank = 'best'
	end
	local claims = {}
    -- ~= '' lorsque le paramètre est écrit mais laissé blanc dans une fonction frame
    for i, statement in pairs(entity.claims[property]) do
    	if
    		(
    		not args.excludespecial
    		or
    		not (isSpecial(statement.mainsnak))
    		)
    	and
    		(
			not args.targetvalue
			or
			hastargetvalue(statement, args.targetvalue)
			)
    	and
    		(
    		not args.qualifier
    		or
    		hasqualifier(statement, args.qualifier, args.qualifiervalue or args.qualifiervalues)
    		)
		and
			(
			not args.source
			or
			hassource(statement, args.source, args.sourceproperty)
			)
		--and
		--	not args.isinlanguage
		--	or
		--	isinlanguage(statement.mainsnak, args.isinlanguage)
		and
			args.rank == 'best' -- rank == best est traité à a fin
			or
			hasrank(statement, rank)
		then
			table.insert(claims, statement)
		end
	end
	if #claims == 0 then
		return nil
	end
    if args.rank == 'best' then
    	claims = bestranked(claims)
    end
    if args.sorttype then
    	claims = p.sortclaims(claims, args.sorttype)
    end

	if args.numval then
		return numval(claims, args.numval)
	end
	return claims
end


return p