Модуль:Votes — Википедия
Этот модуль оценён как бета-версия. Он готов для широкого применения, но должен применяться с осторожностью. |
Модуль предназначен для подсчёта голосов на страницах голосования.
Функции
Подсчёт количества голосов
{{#invoke:Votes|count|<имя страницы>}}
Параметры
page
— имя страницы голосования (если страница не указана, то подсчёт ведется на текущей странице)level
— уровень секций (по умолчанию 3)support
— имя секции за (по умолчанию «За
»)oppose
— имя секции против (по умолчанию «Против
»)neutral
— имя секции воздержались (по умолчанию «Воздержались
»)namespace
— пространство имён страницы, если оно не указано в параметреpage
format
— формат вывода в виде строки таблицы (row) или строки текста (mini) (по умолчанию «row»)template
— шаблон форматирования результатов, принимающий параметры («за», «против», «воздержались»)unordered
— флаг учета голосов в ненумерованных списках вместо нумерованных (по умолчанию «false»)
Замечания
Подсчитываются только строки нумерованного списка первого уровня. Строки HTML комментариев и зачеркнутые тегом <s></s> не учитываются.
---- Этот модуль подсчитывает голоса в секциях голосования local votes = {} function votes.count(frame) local new_args = votes._getParameters( frame.args, { 'page', 'level', 'support', 'oppose', 'neutral', '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 sections = { support = { name = new_args['support'] or 'За', count = 0}, oppose = { name = new_args['oppose'] or 'Против', count = 0}, neutral = { name = new_args['neutral'] or 'Воздержались', 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.support.count == 0 then percent = "0 %" else percent = mw.ustring.format("%.2f %%", sections.support.count * 100 / (sections.support.count + sections.oppose.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.support.count); targs['против'] = tostring(sections.oppose.count); targs['воздержались'] = tostring(sections.neutral.count); targs['page'] = page; return frame:expandTemplate{ title = template, args = targs } end if fmt == 'mini' then return sections.support.count .. '/' .. sections.oppose.count .. '/' .. sections.neutral.count .. ' — ' .. percent; else return "\n<tr>" .. "<td class=\"criteriaCheck-supportVotesCount\">" .. sections.support.count .. " </td>" .. "<td class=\"criteriaCheck-opposeVotesCount\">" .. sections.oppose.count .. " </td>" .. "<td class=\"criteriaCheck-neutralVotesCount\">" .. sections.neutral.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