Module:IDs

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?://gratisdata.miraheze.org/wiki/", "https?://gratisdata.miraheze.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?://gratisdata.miraheze.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

-- handle the case where just a number is given as id and an item number is required p.qid_normalize = function(idstring) if p.is_numberID(idstring) then return "Q" .. idstring else return p.id_normalize(idstring) end 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

-- case where a Qid is expected p.normalizeQ = function(frame) local normedID = p.qid_normalize(frame.args[1]); if p.is_validID(normedID) then return normedID else error("«" .. frame.args[1] .. "» cannot be transformed into a Qid") end end

return p;