ICT:FinalContent - ASSET - LUA
Appearance
-- Module:DigitalAssetID
local p = {}
local cargo = mw.ext.cargo
-- Helper: zero-pad sequence number
local function pad(num)
return string.format("%04d", tonumber(num))
end
-- Main function: compute identifier
function p.generate(frame)
local args = frame.args
local chapterPage = args.chapter_id
local placePage = args.place_id
local orgPage = args.organisation_id
-- 1. Enforce exclusivity
if (placePage ~= "" and orgPage ~= "") then
return mw.text.jsonEncode({
error = "Select either a Place OR an Organisation, not both."
})
end
if (placePage == "" and orgPage == "") then
return mw.text.jsonEncode({
error = "You must select a Place OR an Organisation."
})
end
-- 2. Fetch Chapter code
local chapterRes = cargo.query(
"ResearchChapters",
"code",
{ where = string.format("_pageName='%s'", chapterPage) }
)
if not chapterRes[1] then
return mw.text.jsonEncode({ error = "Invalid chapter selected." })
end
local chapterCode = chapterRes[1].code
-- 3. Fetch context code (Place or Organisation)
local contextCode = nil
local contextTable = nil
local contextPage = nil
if placePage ~= "" then
contextTable = "Places"
contextPage = placePage
else
contextTable = "Organisations"
contextPage = orgPage
end
local ctxRes = cargo.query(
contextTable,
"code",
{ where = string.format("_pageName='%s'", contextPage) }
)
if not ctxRes[1] then
return mw.text.jsonEncode({ error = "Invalid context selected." })
end
contextCode = ctxRes[1].code
-- 4. Determine next sequence number
local existing = cargo.query(
"DigitalAssets",
"sequence_number",
{
where = string.format(
"chapter_id='%s' AND (place_id='%s' OR organisation_id='%s')",
chapterPage, placePage, orgPage
),
orderBy = "sequence_number DESC",
limit = 1
}
)
local nextSeq = 1
if existing[1] and existing[1].sequence_number then
nextSeq = tonumber(existing[1].sequence_number) + 1
end
-- 5. Format identifier
local identifier = string.format(
"%s-%s-%s",
chapterCode,
contextCode,
pad(nextSeq)
)
-- 6. Return result
return mw.text.jsonEncode({
identifier = identifier,
sequence_number = nextSeq
})
end
return p