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 | |||
local p = {} | local p = {} | ||
local digits = 7 | local digits = 7 | ||
function p.personID() | function p.personID() | ||
return p._generateID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%-P%-(%d+)") | |||
end | |||
function p.groupID() | |||
return p._generateID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%-G%-(%d+)") | |||
end | |||
function p._generateID(prefix, category, pattern) | |||
local allPages = mw.smw.ask { | local allPages = mw.smw.ask { | ||
category, | |||
"?=page", | "?=page", | ||
"limit=5000" | "limit=5000" | ||
| Line 17: | Line 25: | ||
for _, result in ipairs(allPages) do | for _, result in ipairs(allPages) do | ||
local title = result["page"] or "" | local title = result["page"] or "" | ||
local idNum = title:match( | local idNum = title:match(pattern) | ||
if idNum then | if idNum then | ||
local num = tonumber(idNum) | local num = tonumber(idNum) | ||
| Line 28: | Line 36: | ||
local newId = maxId + 1 | local newId = maxId + 1 | ||
return | return prefix .. string.format("%0" .. digits .. "d", newId) | ||
end | end | ||
return p | return p | ||
Revision as of 18:10, 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 TITAF-G-xxxxxxx ID for Musical Groups
local p = {}
local digits = 7
function p.personID()
return p._generateID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%-P%-(%d+)")
end
function p.groupID()
return p._generateID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%-G%-(%d+)")
end
function p._generateID(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
return p