Jump to content

Module:Votings

Permanently protected module
From Meta, a Wikimedia project coordination wiki
Module documentation

Usage

See Template:Votings.


-- For attribution: [[:vi:Module:CurrentCandidateList]]
local p = {}

local title = 'Meta:Requests for adminship' -- Title here...
local content = mw.title.new(title):getContent() -- ...and content here. Let's go.

local perm = { -- Data. "i" stands for `i`nitial; "f" stands for `f`ull word; "s" stands for `s`ection.
	{
		i = 'A',
		f = 'adminship',
		s = 'regular adminship'
	},
	{
		i = 'LA',
		f = 'limited adminship'
	},
	{
		i = 'IA',
		f = 'interface adminship'
	},
	{
		i = 'B',
		f = 'bureaucratship'
	},
	{
		i = 'CU',
		f = 'checkuser',
		s = 'CheckUser access'
	},
	{
		i = 'OS',
		f = 'oversight',
		s = 'Oversight access'
	},
	{
		i = 'TA',
		f = 'translation adminship'
	},
	{
		i = 'CN',
		f = 'CentralNotice adminship'
	},
	{
		i = 'BF',
		f = 'bot status',
		s = 'bot flags'
	}
}

function p.main() -- Main function.
	local list = {} -- 
	
	-- These may be caught as false positives. Get rid of them.
	local ex = {'none', 'None', 'rF', 'RF', 'Meta:Requests for bot status/Examplebot', 'Meta:Requests for translation adminship/ExampleUser'}
	-- Lua pattern to catch a page name inside double braces.
	local re = '%{%{([^#|<>{}%[%]]+)%}%}'
	
	for l in mw.text.gsplit(content, '\n') do -- Iterate all `l`ines of WM:RfA.
		if mw.ustring.match(l, re) and not p.inc(mw.ustring.match(l, re), ex) then -- If the line is caught and is not one of those listed above,
			table.insert(list, mw.ustring.match(l, re)) -- ...that's exactly what we're looking for. Add it to our table.
		end
	end
	-- Our "list" is now an array-like table with page names: 'M:RfA/Foo', 'M:RfLA/Baz', 'M:RfB/Bar' etc.
	return p.join(p.list(list)) -- See p.list and p.join below for explanations.
end

function p.list(t) -- Unlike [[Module:Votings-global]], this p.list require a parameter that cannot be provided using wikitext syntax.
	local l = {} -- "l" stands for `l`ist.
	local n = {} -- "n" stands for `n`umber.
	local re = function(g) -- Lua pattern to catch a page name that contains Meta:Requests for <privilege>/<username>
		return '[%s_]*[Mm][Ee][Tt][Aa][%s_]*:[%s_]*[Rr]equests[%s_]*for[%s_]*' .. mw.ustring.gsub(g, '%s', '[%s_]*') .. '/(.+)'
	end
	
	for a = 1, 3 do -- We're looping through "perm" array three times consecutively. Just ignore this one.
		for k1 = 1, #perm do -- Here's the main loop, as explained right above.
			if a == 1 then -- For the first loop,
				l[perm[k1].i] = {} -- ...declare all l[<privilege>] as tables,
				l[perm[k1].i].n = 0 -- ...and all l[<privilege>].n as numbers. Remember, "perm[k1].i" == <privilege>.
			elseif a == 2 then -- Second loop,
				for k2 = 1, #t do -- Iterate table "t" (provided by p.main).
					if mw.ustring.match(t[k2], re(perm[k1].f)) then -- If the page name we're dealing with is caught by one of the patterns,
						l[perm[k1].i].p = mw.ustring.match(t[k2], re(perm[k1].f)) -- Add <username> to l[<privilege>].p. "p" stands for `p`age.
						l[perm[k1].i].n = tonumber(l[perm[k1].i].n) + 1 -- Also, add 1 to its number respectively.
					end
				end
			else -- Third loop, add all (precisely 9) numbers to "n", an array-like table.
				table.insert(n, l[perm[k1].i].n)
			end
		end
	end
	-- Join numbers in "n" with pipe as separator, so that the output will look like this: 0|0|0|0|0|0|0|0|0.
	return table.concat(n, '|')
end

function p.inc(e, a) -- "inc" stands for `inc`lude. Return true if `e`lement "e" is one of the keys or values of table "a".
	local b = false
	for k, v in pairs(a) do
		if k == e or v == e then b = true end
	end
	return b
end

function p.join(l) -- Join a pipe-separated list of 9 numbers to a human-readable value that includes linking.
	local num = mw.text.split(l, '|') -- Split the list up.
	local req = {} -- Array-like table for links to `req`uests.
	local i = { -- "i" stands for `i`ndex. Just ignore this as it doesn't affect our output.
		t = {
			0
		},
		f = {
			0
		}
	}
	
	for n = 1, #perm do -- Iterate "perm".
		if tonumber(num[n]) ~= nil and tonumber(num[n]) > 0 then -- Add a link to "req" if there is at least one of them.
			-- As said above, "s" stands for `s`ection. If .s was specified, use it. Otherwise, use .f.
			table.insert(
				req,
				'[[' .. title .. '#Requests for ' .. (perm[n].s ~= nil and perm[n].s or perm[n].f) .. '|' .. num[n] .. '&nbsp;Rf' .. perm[n].i .. ']]'
			)
			table.insert(i.t, num[n]) -- Ignore this.
		else
			table.insert(i.f, num[n]) -- Idem.
		end
	end
	
	if #req > 0 then -- If there is something in "req", return it with bullets and bold tags.
		return '&nbsp;&bull; <b>' .. table.concat(req, '</b>&nbsp;&bull; <b>') .. '</b>'
	else -- Otherwise, nothing.
		return ''
	end
end

return p