Module:Properties/sandbox

From Wikidata
Jump to navigation Jump to search
Lua
CodeDiscussionLinksLink count SubpagesDocumentationTestsResultsSandboxLive code All modules

Documentation for this module may be created at Module:Properties/sandbox/doc

Code

-- 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

local 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 ;
		
		["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 ;
		["dissolution date"] = 576;
		
		["list item"] = 11260;
	}
}


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

p.numberFromPropertylabel = function(frame)  
	local label = frame.args[1]
	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)

	if tonumber(input_pid) then
		return "P" .. tostring(input_pid)
	end
	input_pid = tostring(input_pid)
	if mw.ustring.find(input_pid, 'P%d+') then
		return mw.ustring.match(input_pid, '(P%d+)') .. ''
	end
	input_pid = string_to_propnumber(input_pid)
	if input_pid then
		return "P" .. input_pid
	end
	return nil
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) or val
	end
	return props
end

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

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

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

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


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

return p