Модуль:Votes/count
Перейти к навигации
Перейти к поиску
Версия модуля для большего числа заголовков (пока доступен только текстовый режим число/число/число... — процент). Пример использования. h1-h7 — заголовки разделов с голосами, percent — раздел, процент голосов которого относительно суммы всех голосов в разделах h1-h7 будет показан.
---- Этот модуль подсчитывает голоса в секциях голосования
local votes = {}
function votes.count(frame)
local new_args = votes._getParameters( frame.args, { 'page', 'level', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'percent', 'namespace', 'format', 'template', 'unordered' } );
local page = mw.ustring.gsub(new_args['page'] or '', '_', ' ');
local namespace = new_args['namespace'] or '';
local level = new_args['level'] or 3;
local fmt = new_args['format'] or 'row';
local template = new_args['template'] or '';
local unordered = new_args['unordered'] or false;
local leader = new_args['percent'] or 0;
local sections = {
h1 = { name = new_args['h1'] or 'h1', count = 0},
h2 = { name = new_args['h2'] or 'h2', count = 0},
h3 = { name = new_args['h3'] or 'h3', count = 0},
h4 = { name = new_args['h4'] or 'h4', count = 0},
h5 = { name = new_args['h5'] or 'h5', count = 0},
h6 = { name = new_args['h6'] or 'h6', count = 0},
h7 = { name = new_args['h7'] or 'h7', count = 0}
}
local pagepointer;
local pattern = "\n#[^#*:][^\n]+"; -- подсчёт нумерованных списков
if page == '' then
pagepointer=mw.title.getCurrentTitle()
assert(pagepointer,"failed to access getCurrentTitle")
else
pagepointer=mw.title.new(page, namespace)
assert(pagepointer,"failed to access mw.title.new("..tostring(page)..")")
end
if type( unordered ) == 'string' and unordered ~= 'false' and unordered ~= 'no' and unordered ~= '0' then
pattern = "\n%*[^#*:][^\n]+"; -- подсчёт ненумерованных списков
end
local text=pagepointer.getContent(pagepointer);
if text ~= nil then
text= mw.ustring.gsub( text, "<!%-%-.-%-%->", "" ); -- убираем HTML комментарии
local hpref = mw.ustring.rep("=", tonumber(level));
for k, v in pairs(sections) do
local name = mw.ustring.gsub( v.name, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" );
local t = mw.ustring.match(text, "\n" .. hpref .. "[^=]-" .. name .. "[^=]-" .. hpref .."(\n.-)\n=");
if t ~= nil then
t, v.count = mw.ustring.gsub(t, pattern, ""); -- количество голосов
end
end
end
local percent;
if sections.h1.count == 0 then
percent = "0 %"
else
percent = mw.ustring.format("%.2f %%", sections[leader].count * 100 / (sections.h1.count + sections.h2.count + sections.h3.count + sections.h4.count + sections.h5.count + sections.h6.count + sections.h7.count))
end
if template ~= '' then
local targs = {};
local pframe = frame:getParent();
if pframe ~= nil then
for key, value in pairs(pframe.args) do
targs[key] = value;
end
end
targs['за'] = tostring(sections.h1.count);
targs['против'] = tostring(sections.h2.count);
targs['воздержались'] = tostring(sections.h3.count);
targs['page'] = page;
return frame:expandTemplate{ title = template, args = targs }
end
if fmt == 'mini' then
local extra = ''
if sections.h5.count>0 then extra = extra .. '/' .. sections.h5.count end
if sections.h6.count>0 then extra = extra .. '/' .. sections.h6.count end
if sections.h7.count>0 then extra = extra .. '/' .. sections.h7.count end
local scriptTag = "<div class=\"criteriaCheck-adminElection\">"
local scriptTagEnd = "</div>"
return scriptTag .. scriptTagEnd .. sections.h1.count .. '/' .. sections.h2.count .. '/' .. sections.h3.count .. '/' .. sections.h4.count .. extra .. ' — ' .. percent;
else
return "\n<tr>" ..
"<td class=\"criteriaCheck-supportVotesCount\">" .. sections.h1.count .. " </td>" ..
"<td class=\"criteriaCheck-opposeVotesCount\">" .. sections.h2.count .. " </td>" ..
"<td class=\"criteriaCheck-neutralVotesCount\">" .. sections.h3.count .. " </td>" ..
"<td class=\"criteriaCheck-supportPercent\">" .. percent .. " </td>" ..
"</tr>";
end
end
-- Таблица параметров
function votes._getParameters( frame_args, arg_list )
local new_args = {};
local index = 1;
local value;
for i,arg in ipairs( arg_list ) do
value = frame_args[arg]
if value == nil then
value = frame_args[index];
index = index + 1;
end
new_args[arg] = value;
end
return new_args;
end
return votes