Module:IsAdmin
Jump to navigation
Jump to search
| This module is rated as beta. It is considered ready for widespread use, but as it is still relatively new, it should be applied with some caution to ensure results are as expected. |
Usage
[edit source]render
[edit source]{{#invoke:IsAdmin|render|<username>}}
- Returns yes if <username> is an admin, no otherwise.
isAdmin
[edit source]{{#invoke:IsAdmin|isAdmin|<username>}}
- Returns 1 if <username> is an admin (usable in #if), blank otherwise.
Examples
[edit source]{{#invoke:IsAdmin|render|L235}}→ no{{#invoke:IsAdmin|render|ExampleUser}}→ no
Notes
[edit source]- This module relies on the page User:AmoryBot/crathighlighter.js/sysop.json being updated.
-- Module:AdminCheck
-- Determines whether a supplied username is an administrator.
--
-- USAGE
-- {{#invoke:AdminCheck|render|Example}}
-- → "yes" / "no"
--
-- {{#invoke:AdminCheck|_isAdmin|Example}}
-- → returns a boolean when called from Lua
--
-- {{#invoke:AdminCheck|isAdmin|Example}}
-- → same as |render but returns boolean-ish wikitext ("1" / "")
--
--------------------------------------------------------------------------------
local p = {}
-------------------------------------------------------------------------------
-- Helper: load the JSON list of sysops once per page‑view
-------------------------------------------------------------------------------
local function loadAdminTable()
-- Fetch the raw JSON text
local title = mw.title.new('User:AmoryBot/crathighlighter.js/sysop.json')
if not title then
return {}
end
local jsonText = title:getContent()
if not jsonText or jsonText == '' then
return {}
end
-- Convert JSON → Lua table
local ok, data = pcall(mw.text.jsonDecode, jsonText)
if ok and type(data) == 'table' then
return data
end
-- Fallback – return an empty table on any error
return {}
end
-- Cached for this invocation
local ADMINS = loadAdminTable()
-------------------------------------------------------------------------------
-- Internal utility – normalise usernames
-------------------------------------------------------------------------------
local function normalise(name)
if not name then
return ''
end
-- Trim whitespace, convert underscores to spaces, capitalise first letter
name = name:gsub('_', ' '):gsub('^%s*(.-)%s*$', '%1')
-- MediaWiki forces first‑letter capitalisation on usernames
name = mw.ustring.upper(mw.ustring.sub(name, 1, 1)) .. mw.ustring.sub(name, 2)
return name
end
-------------------------------------------------------------------------------
-- Core boolean check
-------------------------------------------------------------------------------
function p._isAdmin(username)
username = normalise(username)
return not not ADMINS[username] -- coerce to boolean
end
-------------------------------------------------------------------------------
-- Interface for templates / #invoke –
-- returns “1” for true, “” for false (so it works in #if), or explicit text.
-------------------------------------------------------------------------------
function p.isAdmin(frame)
local name = frame.args[1] or frame:getParent().args[1] or ''
return p._isAdmin(name) and '1' or ''
end
-- Friendly renderer that outputs “yes” / “no”
function p.render(frame)
local name = frame.args[1] or frame:getParent().args[1] or ''
return p._isAdmin(name) and 'yes' or 'no'
end
return p