Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.
Revision as of 03:10, 5 November 2025 by Tucker R. Twomey (talk | contribs)

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(frame)
  str_r = type(frame) == "table" and frame.args[1] or frame
  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(frame)
  idcheck = type(frame) == "table" and wrm_getcheck_f(frame) or frame

  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_getcheck_f(frame)
  pargs = frame:getParent().args
  return pargs[1] or wrm_gencheck(pargs[2] or "WRM-R999")
end

function wrm_getcheckforsmw(frame)
  c = type(frame) == "table" and wrm_getcheck_f(frame) or 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) == "table" and frame.args[1] or frame
  return wrm_gencheck(prepend..wrm_next_id())
end

return
{
	wrm_fmtcheck = wrm_fmtcheck,
	next_id = wrm_next_id,
	next_an = wrm_next_an,
	fmt = wrm_fmtcheck,
	get = wrm_getcheckforsmw,
	gen = wrm_gencheck,
}