Module:IsAdmin

-- 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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.