Difference between revisions of "Module:IDs"

From Wikibase Personal data
Jump to navigation Jump to search
(Created page with " local entities_prefixes = "PQ" ; local pq_pattern = "[" .. entities_prefixes .. "][1-9][0-9]*" ; local q_pattern = "[" .. "Q" .. "][1-9][0-9]*" ; local pattern = "^" .. pq_pa...")
(No difference)

Revision as of 19:52, 23 October 2019

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

local entities_prefixes = "PQ" ;
local pq_pattern = "[" .. entities_prefixes .. "][1-9][0-9]*" ;
local q_pattern = "[" .. "Q" .. "][1-9][0-9]*" ;
local pattern = "^" .. pq_pattern .."$" ;

local entities_uri_pattern = { "https?://www.wikidata.org/wiki/", "https?://www.wikidata.org/entity/"};
local entities_wikilink_pattern_bounds = { "[[][[]", "|.*[]][]]"};

p = {
	entities_prefixes = entities_prefixes;
	pq_pattern = pq_pattern ;
    pattern = pattern ;
    entities_uri_pattern = entities_uri_pattern;
}

p.is_validID = function(id)
	return mw.ustring.match(mw.text.trim(id), p.pattern) ~= nil
end

p.is_numberID = function(id)
	return mw.ustring.match(mw.text.trim(id), "^[1-9][0-9]*$") ~= nil
end

p.is_uriID = function(id)
	
	return mw.ustring.match(mw.text.trim(id), 
		"^https?://www.wikidata.org/(entity|wiki)/" .. p.pq_pattern .. "$" ) ~= nil;
end

p.is_wikilinkQID = function(id)
	
	return mw.ustring.match(mw.text.trim(id), 
		entities_wikilink_pattern_bounds[1] .. q_pattern .. entities_wikilink_pattern_bounds[2] ) ~= nil;
end

p.raw_number_from_StringID = function(id)
	-- string_id assumed to be valid
	return mw.ustring.gmatch(mw.text.trim(id), "[0-9]*$")()
end

p.id_normalize = function(idstring)
	id = mw.text.trim(idstring)
	id = mw.ustring.gsub(id, p.entities_uri_pattern[1], "")
	id = mw.ustring.gsub(id, p.entities_uri_pattern[2], "")
	if p.is_wikilinkQID(id) then
		id = mw.ustring.gsub(id, "^" .. entities_wikilink_pattern_bounds[1], "")
		id = mw.ustring.gsub(id, entities_wikilink_pattern_bounds[2] .. "$", "")
	end
	return id
end


p.number = function(frame)
 	return p.raw_number_from_StringID(frame.args[1])
end

p.valid = function(frame)
	id = p.id_normalize(frame.args[1])
 	if p.is_validID(id) then
 		return "OK"
 	else
 		return nil
 	end
end

-- returns a P/Q number from a supposed to be entity id

p.isNumberID = function(frame)
 	if p.is_numberID(frame.args[1]) then
 		return "OK"
 	else
 		return nil
 	end
end

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

return p;