Aller au contenu

Module:Propriétés

Une page de Wikiquote, le recueil des citations libres.

La documentation pour ce module peut être créée à Module:Propriétés/Documentation

local p = {
	instance_de= "P31",
    sous_classe_de = "P279",
    prenom = "P735",
    nomdefamille = "P734", 
    isbn10 = "P957",
    publie_dans = "P1433",
    langue = "P407" 
}
-- this module gives solutions to get the Pnumber of a property by its name


-- could be automatized, but for now just defining a muanual mapping


p.p_from_name = {
	["country"] = 17 ;
	["gender"] = 21 ;

	["instance of"] = 31 ;
	
	["field"] = 101 ; 
	["occupation"] = 106 ;
	
	["genre"] = 136 ;
	
	["named after"] = 138 ;
	
	
	["preceded by"] = 155;
	["succeeded by"] = 156;
	["collection"] = 195;	
	["subclass of"] = 279 ;

	["category's topic"] = 301;

	["list of"] = 360 ; 
	["part of"] = 361 ;

	["commons category"] = 373;
	
	["terminus"] = 559;
	["direction"] = 560;
	
	["start date"] = 580 ;
	["begin date"] = 580 ;
	
	["end date"] = 582 ;
	
	["date"] = 585 ;
	
	["of"] = 642 ;
	
	["participant"] = 710 ;
	
	["edition"] = 747 ;
	["edition of"] = 629 ;
	
	['key event'] = 793 ;
	['significant event'] = 793 ;
	
	["topic's category"] = 910;
	["subject"] = 921; -- work subject
	["inspired by"] = 941 ;	
	
	["fictional analog of"] = 1074 ;

	["statement disputed by"] = 1310 ;
	["disputed by"] = 1310 ;
	
	["of"] = 642 ;
	
	["history"] = 2184 ;
	
	["property for this type"] = 1963 ;
    ["metasubclass of"] = 2445 ;
    
	["number"] = 1114 ;
	
	["union of"] = 2737;
	["disjoint union of"] = 2738;
	
	["has part of the type"] = 2670 ;
}



local function string_to_propnumber(label)
	local res = p.p_from_name[label] ;
	if res ~= nil then
		return tostring( res) ;
	else
		res = mw.wikibase.resolvePropertyId(label)
		if res then
			return string.sub(res, 2)
		else
			return nil
		end
    end
end

p.numberFromPropertylabel = function(frame)  
	local label = frame.args[1] ;
	mw.log(label)
	return string_to_propnumber(label)
end;

-- returns a pid of the form "Pnnn..." where n is a character in "123456789"
--from either 
--* a number representing a property wikidata number
--* a string number representing a property wikidata number
--* a pid of the form "Pnnn"
--* a "property name" corresponding to a property, where the names are listed in the array p.p_from_name
-- or "nil" if the id does not match to any of the cases above
p.normalize = function(input_pid)
	local input_pid = tostring(input_pid)
	if mw.ustring.match(input_pid, "P%d+") then
		return input_pid
	elseif mw.ustring.match(input_pid, "%d+") then
		return "P" .. input_pid
	else
		return "P" .. tostring(string_to_propnumber(input_pid))
	end
end

-- function that takes a string with a set of pids and returns an array of properties
p.expandPropList = function(plist)
	local props = mw.text.split(plist,",")
	for ind, val in ipairs(props) do
		props[ind] = p.normalize(val)
	end
	return props
end

function p.datatype(property)
	if type(property) ~= "table" then
		property = mw.wikibase.getEntityObject(p.normalize(property))
	end
	return property.datatype
end

---------------------------------------------------------------
-- wikitemplate interface
---------------------------------------------------------------

p.Pid = function(frame)  
	local label = frame.args[1] ;
	return p.normalize(label)
end;

p.Pdatatype = function(frame)
	return p.datatype(frame.args[1])
end;


p.of = p.normalize("of")
p.union_of = p.normalize("union of")
p.disjoint_union_of = p.normalize("disjoint union of")

return p