Module:GenerateID: Difference between revisions

From TANGOWIKI-TITAF
Created page with "-- Module:GenerateID -- This module generates the next available TITAF-P-xxxxxxx ID for Person pages local p = {} local idPrefix = "TITAF-P-" local digits = 7 function p.getNextId() local allPages = mw.smw.ask { "Category:TangoPeople", "?=page", "limit=5000" } local maxId = 0 if allPages then for _, result in ipairs(allPages) do local title = result["page"] or "" local idNum = title:match("TIT..."
 
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 1: Line 1:
-- Module:GenerateID
-- Module:GenerateID
-- This module generates the next available TITAF-P-xxxxxxx ID for Person pages
-- This module generates IDs for Persons, Groups, GroupMemberships, Tunes, and Recordings


local p = {}
local p = {}
local idPrefix = "TITAF-P-"
local digits = 7
local digits = 7


function p.getNextId()
-- Reserved Ranges:
-- Person:        9958802 - 9988802
-- Group:        0031181 - 0099999
-- Membership:    0090001 - 0900000
-- Tune (import): 0000150 - 0024999
-- Tune (future): 0025000 - 0049999
-- Tune (form):  0050000+
-- Recording (import): 0000150 - 0099999
-- Recording (reserve): 0100000 - 0250000
-- Recording (form):  0250001+
 
function p.personID()
    return p._generateRandomID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%%-P%%-(%d+)", 9958802, 9988802)
end
 
function p.groupID()
    return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%%-G%%-(%d+)", 31181, 9999999)
end
 
function p.membershipID()
    return p._generateRandomID("TITAF-R-", "[[Category:PersonGroupMemberships]]", "TITAF%%-R%%-(%d+)", 90001, 900000)
end
 
function p.tuneID()
    return p._generateRandomID("TITAF-T-", "[[Category:Tune]]", "TITAF%%-T%%-(%d+)", 50000, 999999)
end
 
function p.recordingID()
    return p._generateRandomID("TITAF-RC-", "[[Category:Recording]]", "TITAF%%-RC%%-(%d+)", 250001, 999999)
end
 
function p._generateRandomID(prefix, category, pattern, min, max)
    math.randomseed(os.time())
    local usedIds = {}
     local allPages = mw.smw.ask {
     local allPages = mw.smw.ask {
         "[[Category:TangoPeople]]",
         category,
         "?=page",
         "?=page",
         "limit=5000"
         "limit=5000"
     }
     }


    local maxId = 0
     if allPages then
     if allPages then
         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("TITAF%-P%-(%d+)")
             local idNum = title:match(pattern)
             if idNum then
             if idNum then
                 local num = tonumber(idNum)
                 usedIds[idNum] = true
                if num and num > maxId then
                    maxId = num
                end
             end
             end
         end
         end
     end
     end


     local newId = maxId + 1
     local attempts = 0
     return idPrefix .. string.format("%0" .. digits .. "d", newId)
     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
 
function p.roleID()
    return p._generateRandomID("TITAF-R-", "[[Has group::+]]", "TITAF%%-R%%-(%d+)", 90001, 900000)
end
end


return p
return p

Latest revision as of 14:50, 23 April 2025

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

-- Module:GenerateID
-- This module generates IDs for Persons, Groups, GroupMemberships, Tunes, and Recordings

local p = {}
local digits = 7

-- Reserved Ranges:
-- Person:        9958802 - 9988802
-- Group:         0031181 - 0099999
-- Membership:    0090001 - 0900000
-- Tune (import): 0000150 - 0024999
-- Tune (future): 0025000 - 0049999
-- Tune (form):   0050000+
-- Recording (import): 0000150 - 0099999
-- Recording (reserve): 0100000 - 0250000
-- Recording (form):   0250001+

function p.personID()
    return p._generateRandomID("TITAF-P-", "[[Category:TangoPeople]]", "TITAF%%-P%%-(%d+)", 9958802, 9988802)
end

function p.groupID()
    return p._generateRandomID("TITAF-G-", "[[Category:MusicalGroups]]", "TITAF%%-G%%-(%d+)", 31181, 9999999)
end

function p.membershipID()
    return p._generateRandomID("TITAF-R-", "[[Category:PersonGroupMemberships]]", "TITAF%%-R%%-(%d+)", 90001, 900000)
end

function p.tuneID()
    return p._generateRandomID("TITAF-T-", "[[Category:Tune]]", "TITAF%%-T%%-(%d+)", 50000, 999999)
end

function p.recordingID()
    return p._generateRandomID("TITAF-RC-", "[[Category:Recording]]", "TITAF%%-RC%%-(%d+)", 250001, 999999)
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

function p.roleID()
    return p._generateRandomID("TITAF-R-", "[[Has group::+]]", "TITAF%%-R%%-(%d+)", 90001, 900000)
end

return p