Module:Accession numbering
From CHIPPS
More actions
Documentation for this module may be created at Module:Accession numbering/doc
require("Module:Verhoeff")
id_to_n = { R=1, L=2, S=3, A=3, E=4 }
function wrm_idproc(str_r, func, pp, gc)
str_stripped = str_r:upper():gsub("^WRM%-?", "")
prefix = str_stripped:match("^[RLSAE]")
res = func(str_stripped:gsub("([RLSAE]?)(%d+)",
function(p, ds, x)
return (gc and "%u%05u" or "%u%06u"):format(id_to_n[p] or 0, ds)
end))
if pp
then
return ("WRM-%s%04u"):format(prefix or "", res:sub(2))
else
return res
end
end
function wrm_gencheck(str_r)
return wrm_idproc(str_r, verhoeff_gen, true, true)
end
function wrm_verifycheck(str_r)
return wrm_idproc(str_r, verhoeff_verify, false, false)
end
function wrm_fmtcheck(idcheck)
idnocheck = idcheck:sub(1,-2)
check = idcheck:sub(-1,-1)
invalid_check = wrm_verifycheck(idcheck) and "" or " is invalid!"
return "<code>"..idnocheck.."<span class=\"check_digit\">"..
check.."</span></code>"..invalid_check
end
function wrm_fmtcheck_f(frame)
return wrm_fmtcheck(wrm_getcheck_f(frame))
end
function wrm_getcheck_f(frame)
pargs = frame:getParent().args
return pargs[1] or wrm_gencheck(pargs[2] or "WRM-R999")
end
function wrm_getcheckforsmw_f(frame)
c = wrm_getcheck_f(frame)
return c .. (wrm_verifycheck(c) and "" or "-invalid")
end
function wrm_next_id()
return mw.smw.getQueryResult(
"[[Has AM-ID::+]] [[:+]]|?Has AM-ID=id|sort=Has AM-ID|order=desc|limit=1"
).results[1].printouts.id[1]:sub(6,-2)+1
end
function wrm_next_an(frame)
prepend = type(frame) == "string" and frame or frame.args[1]
return wrm_gencheck(prepend..wrm_next_id())
end
return
{
wrm_fmtcheck = wrm_fmtcheck,
wrm_gencheck = wrm_gencheck,
next_id = wrm_next_id,
next_an = wrm_next_an,
fmt = wrm_fmtcheck_f,
get = wrm_getcheckforsmw_f,
gen = function (frame) return wrm_gencheck(frame.args[1]) end,
}