跳转到内容

模組:SecurePoll

维基百科,自由的百科全书
local p = {}

local util = require 'libraryUtil'

local wgSecurePollUseNamespace = false
local NS_SECUREPOLL = 830

local wgSecurePollUseMediaWikiNamespace = true

local function to_bool(val)
	return val == '1' and true or false
end

local function get_data_page(name)
	if wgSecurePollUseNamespace then
		return mw.title.new(name, NS_SECUREPOLL)
	end
	if wgSecurePollUseMediaWikiNamespace then
		return mw.title.new('SecurePoll/' .. name, 8)
	end
	error('None of wgSecurePollUseNamespace or wgSecurePollUseMediaWikiNamespace is true.')
end

local json_loaded_cache = {}
local function get_data_json(name)
	local title = get_data_page(name)
	if json_loaded_cache[title.prefixedText] then
		return json_loaded_cache[title.prefixedText]
	end
	if not title.exists then
		error('Title ' .. title.prefixedText .. ' doesn\'t exists.')
	elseif title.contentModel ~= 'SecurePoll' then
		error('Title ' .. title.prefixedText .. ' is NOT a SecurePoll.')
	end
	-- 註:為何這裡不用 mw.loadJsonData?因為 mw.loadJsonData 只接受內容模型等於 json 的頁面,而不是繼承了 JsonContentHandler 的類別,哎...
	json = mw.text.jsonDecode(title.content)
	json_loaded_cache[title.prefixedText] = json
	return json
end
p._get_data_json = get_data_json -- debug

local list = {}
list.__index = list
list.lists = {
	voter = 'need-list',
	include = 'include-list',
	exclude = 'exclude-list',
}
function list.get_list_name(type)
	if list.lists[type] ~= nil then
		return list.lists[type]
	end
	for _, v in pairs(list.lists) do
		if v == type then
			return type
		end
	end
	error('Unknown list type "' .. type .. '".')
end
function list.new(main_id, type)
	list_name = list.get_list_name(type)
	return list.new_from_title(tostring(main_id) .. '/list/' .. type) 
end
function list.new_from_title(title_name)
	local obj = {}
	local checkSelf = util.makeCheckSelfFunction('securePoll.list', 'spl', obj, 'spl object')
	
	local members_cache
	function obj:get_members()
		checkSelf(self, 'get_members')
		if not members_cache then
			members_cache = get_data_json(title_name)
		end
		return members_cache
	end
	
	return obj
end

local msg = {}
function list.new(main_id, lang)
	if not mw.language.isSupportedLanguage(lang) then
		error('Unknown language "' .. lang .. '".')
	end

	return list.new_from_title(tostring(main_id) .. '/msg/' .. lang) 
end
function msg.new_from_title(title_name)
	local obj = {}
	local checkSelf = util.makeCheckSelfFunction('securePoll.msg', 'spm', obj, 'spm object')

	local data_cache
	function get_data()
		if not data_cache then
			data_cache = get_data_json(title_name)
		end
		return data_cache
	end

	function obj:get_messages()
		checkSelf(self, 'get_messages')
		return get_data().messages
	end

	function obj:get_questions()
		checkSelf(self, 'get_questions')
		return get_data().questions
	end

	-- #TODO

	return obj
end

local securePoll = {}
function securePoll.new(id)
	if not tonumber(id) then
		error('Bad SecurePoll ID "' .. id .. '".')
	end

	local data = get_data_json(tostring(id))
	
	local function may_get_list(type)
		name = list.lists[type]
		if data.properties[name] then
			return list.new_from_title(data.properties[name])
		end
		return nil
	end

	local obj = {
		-- data = data,
		title = title,
		admins = data.properties.admins and mw.text.split(data.properties.admins, "|", true) or {}, -- 投票管理員
		start_date = data.startDate,
		end_date = data.endDate,
		voter_privacy = to_bool(data.properties['voter-privacy']), -- 不公開投票者列表
		shuffle_questions = to_bool(data.properties['shuffle-questions']), -- 問題順序隨機
		shuffle_options = to_bool(data.properties['shuffle-options']), -- 選項順序隨機
		voters = may_get_list('voters'), -- 資格者列表
		include = may_get_list('include'), -- 包含名單
		exclude = may_get_list('exclude'), -- 排除名單
	}

	local checkSelf = util.makeCheckSelfFunction('securePoll', 'sp', obj, 'sp object')

	-- #TODO
	return obj
end

p.securePoll = securePoll
p.securePoll.list = list -- # internal
p.securePoll.msg = msg -- # internal

return p