Logo Voyage

Module:User rights nominations Voyage Tips and guide

You can check the original Wikivoyage article Here

Documentation for this module may be created at Module:User rights nominations/doc

-- Partially based on [[:m:Module:Votings-global]].

require('strict')

local p = {}

local page_name = 'Wikivoyage:User rights nominations'
local content = mw.title.new(page_name):getContent()

-- Return the level and the text of the heading if the given line can be parsed as such.
-- Return nil otherwise.
local function _parse_heading(line)
	for level = 6, 1, -1 do
		local equal_signs = mw.ustring.rep('=', level)
		
		if (
			#line > level * 2 and
			equal_signs == mw.ustring.sub(line, 1, level) and
			equal_signs == mw.ustring.sub(line, -level)
		) then
			return level, mw.ustring.sub(line, level + 1, -level - 1)
		end
	end
	
	return nil
end

-- Return the heading text if the given line is a level 2 heading.
-- Return nil otherwise.
local function _level_2_heading_text(line)
	local level, text = _parse_heading(line)
	
	if level ~= 2 then
		return nil
	end
	
	return text
end

local function _line_is_nominations_heading(line)
	local heading_text = _level_2_heading_text(line)
	
	return heading_text ~= nil and mw.text.trim(heading_text) == 'Nominations'
end

local function _line_is_level_2_heading(line)
	return _level_2_heading_text(line) ~= nil
end

local function _nominations_lines()
	local lines = {}
	local in_nominations_section = false
	
	for line in mw.text.gsplit(content, '\n') do
		if _line_is_nominations_heading(line) then
			in_nominations_section = true
		elseif in_nominations_section then
			if _line_is_level_2_heading(line) then
				break
			end
			
			table.insert(lines, line)
		end
	end
	
	return lines
end

-- Extract the user name from the given text.
-- There must be a link of the form [[User:Example]] or [[User:Example|Foobar]].
local function _extract_user_name(text)
	local user_name_in_link = mw.ustring.match(text, '%[%[%s*[Uu][Ss][Ee][Rr]%s*:%s*(..-)%]%]')
	local user_name = mw.ustring.match(user_name_in_link, '.*%f[|]') or user_name_in_link
	
	local user_page = mw.title.new('User:' .. user_name)
	local normalized_user_name = user_page.text
	
	return normalized_user_name
end

-- Return a table of users being nominated.
function p._extract_data()
	local users = {}
	
	for _, line in ipairs(_nominations_lines()) do
		local level, text = _parse_heading(line)
		
		if level == 3 then
			local user = _extract_user_name(text)
			
			if user ~= nil then
				table.insert(users, user)
			end
			
			-- Heading is malformed.
		end
	end
	
	return users
end

function p._count()
	local data = p._extract_data()
	return #data
end

function p.count(frame)
	return p._count()
end

return p


Discover



Powered by GetYourGuide