Module:GenerateID: Difference between revisions
From TANGOWIKI-TITAF
No edit summary |
No edit summary |
||
| Line 6: | Line 6: | ||
local digits = 7 | local digits = 7 | ||
-- Generate a random ID for a Person in the range TITAF-P-9958802 to TITAF-P-9988802 | |||
function p.personID() | function p.personID() | ||
return p. | return p._generateRandomID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%%-P%%-(%d+)", 9958802, 9988802) | ||
end | end | ||
-- Generate a random ID for a Group in the range TITAF-G-00011235 to TITAF-G-9999999 | |||
function p.groupID() | function p.groupID() | ||
return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%%-G%%-(%d+)", 11235) | return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%%-G%%-(%d+)", 11235, 9999999) | ||
end | end | ||
function p. | function p._generateRandomID(prefix, category, pattern, min, max) | ||
math.randomseed(os.time()) | math.randomseed(os.time()) | ||
local usedIds = {} | local usedIds = {} | ||
| Line 60: | Line 37: | ||
local attempts = 0 | local attempts = 0 | ||
while attempts < 1000 do | while attempts < 1000 do | ||
local randomNum = math.random( | local randomNum = math.random(min or 1, max or 9999999) | ||
local formatted = string.format("%0" .. digits .. "d", randomNum) | local formatted = string.format("%0" .. digits .. "d", randomNum) | ||
if not usedIds[formatted] then | if not usedIds[formatted] then | ||
Revision as of 21:32, 15 April 2025
Documentation for this module may be created at Module:GenerateID/doc
-- Module:GenerateID
-- This module generates the next available TITAF-P-xxxxxxx ID for Person pages
-- and a random unused TITAF-G-xxxxxxx ID for Musical Groups
local p = {}
local digits = 7
-- Generate a random ID for a Person in the range TITAF-P-9958802 to TITAF-P-9988802
function p.personID()
return p._generateRandomID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%%-P%%-(%d+)", 9958802, 9988802)
end
-- Generate a random ID for a Group in the range TITAF-G-00011235 to TITAF-G-9999999
function p.groupID()
return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%%-G%%-(%d+)", 11235, 9999999)
end
function p._generateRandomID(prefix, category, pattern, min, max)
math.randomseed(os.time())
local usedIds = {}
local allPages = mw.smw.ask {
category,
"?=page",
"limit=5000"
}
if allPages then
for _, result in ipairs(allPages) do
local title = result["page"] or ""
local idNum = title:match(pattern)
if idNum then
usedIds[idNum] = true
end
end
end
local attempts = 0
while attempts < 1000 do
local randomNum = math.random(min or 1, max or 9999999)
local formatted = string.format("%0" .. digits .. "d", randomNum)
if not usedIds[formatted] then
return prefix .. formatted
end
attempts = attempts + 1
end
return prefix .. "ERROR"
end
return p