Module:GenerateID: Difference between revisions
From TANGOWIKI-TITAF
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
-- Module:GenerateID | -- Module:GenerateID | ||
-- This module generates the next available TITAF-P-xxxxxxx ID for Person pages | -- This module generates the next available TITAF-P-xxxxxxx ID for Person pages | ||
-- and TITAF-G-xxxxxxx ID for Musical Groups | -- and a random unused TITAF-G-xxxxxxx ID for Musical Groups | ||
local p = {} | local p = {} | ||
| Line 7: | Line 7: | ||
function p.personID() | function p.personID() | ||
return p. | return p._generateSequentialID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%-P%-(%d+)") | ||
end | end | ||
function p.groupID() | function p.groupID() | ||
return p. | return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%-G%-(%d+)") | ||
end | end | ||
function p. | function p._generateSequentialID(prefix, category, pattern) | ||
local allPages = mw.smw.ask { | local allPages = mw.smw.ask { | ||
category, | category, | ||
| Line 37: | Line 37: | ||
local newId = maxId + 1 | local newId = maxId + 1 | ||
return prefix .. string.format("%0" .. digits .. "d", newId) | return prefix .. string.format("%0" .. digits .. "d", newId) | ||
end | |||
function p._generateRandomID(prefix, category, pattern) | |||
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(1, 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 | end | ||
return p | return p | ||
Revision as of 21:44, 12 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
function p.personID()
return p._generateSequentialID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%-P%-(%d+)")
end
function p.groupID()
return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%-G%-(%d+)")
end
function p._generateSequentialID(prefix, category, pattern)
local allPages = mw.smw.ask {
category,
"?=page",
"limit=5000"
}
local maxId = 0
if allPages then
for _, result in ipairs(allPages) do
local title = result["page"] or ""
local idNum = title:match(pattern)
if idNum then
local num = tonumber(idNum)
if num and num > maxId then
maxId = num
end
end
end
end
local newId = maxId + 1
return prefix .. string.format("%0" .. digits .. "d", newId)
end
function p._generateRandomID(prefix, category, pattern)
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(1, 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