モジュール:投票
表示
このモジュールについての説明文ページを モジュール:投票/doc に作成できます
-- 投票の集計(テンプレート:投票集計 から呼ばれる)。票は {{票|選択肢}} を書いて署名した行
-- 署名の読み取り・日時・節の切り出しは モジュール:削除議論 と共有する
local d = require('Module:削除議論')
local p = {}
local WEEK = 168 * 3600
local function arg(frame, k)
local v = frame.args[k]
if v == nil or v == '' then v = frame:getParent().args[k] end
if v == '' then return nil end
return v
end
-- 「B, C, D」「B、C、D」を表に
local function options(s)
local out = {}
for o in (s or ''):gmatch('[^,、,]+') do
o = mw.text.trim(o)
if o ~= '' then out[#out + 1] = o end
end
return out
end
-- 期間内の票を数える。数えるのは、署名が一人で日時がすべて期間の内の票。同じ利用者は、署名の日時がいちばん新しい票
-- 戻り値は 選択肢ごとの票(利用者の並び)・その他の選択肢の並び・数えなかった票の件数・開始・期限
local function count(frame)
local opts = options(arg(frame, '選択肢'))
local s = d._parse(arg(frame, '開始'))
local e = d._parse(arg(frame, '期限')) or (s and s + WEEK)
local page = arg(frame, 'ページ')
local title = page and mw.title.new(page) or mw.title.getCurrentTitle()
local text = title:getContent() or ''
local section = arg(frame, '節')
if section then text = d._sectionText(text, section) end
local last, order = {}, {}
local skip = { late = 0, unsigned = 0, ip = 0, notime = 0, struck = 0, forged = 0, review = {}, pending = 0, fixed = 0 }
local pos = 0
local pat = '{{%s*票%s*|%s*([^|}]-)%s*[|}]'
local function choiceIn(str)
local c = str:match(pat)
if c and c ~= '' then return c end
return nil
end
for line in (text .. '\n'):gmatch('(.-)\n') do
pos = pos + 1
local choice, st, user, t, info = d._readVote(title, line, s, e, choiceIn)
if st == 'struck' then
skip.struck = skip.struck + 1
elseif choice then
if info.pending then skip.pending = skip.pending + 1 end
if info.other or info.rewritten then skip.fixed = skip.fixed + 1 end
if st == 'ok' then
local prev = last[user]
if not prev then order[#order + 1] = user end
if not prev or t > prev.t or (t == prev.t and pos > prev.pos) then
last[user] = { choice = choice, t = t, pos = pos }
end
elseif st == 'review' then
skip.review[#skip.review + 1] = user
else
skip[st] = skip[st] + 1
end
end
end
local known, by, others = {}, {}, {}
for _, o in ipairs(opts) do known[o], by[o] = true, {} end
for _, u in ipairs(order) do
local c = last[u].choice
if not known[c] then
known[c], by[c] = true, {}
others[#others + 1] = c
end
table.insert(by[c], u)
end
return opts, by, others, skip, s, e
end
-- 集計の表
function p.tally(frame)
local opts, by, others, skip, s, e = count(frame)
local rows = { '{| class="wikitable"', '! 選択肢 !! 票 !! 投票した利用者' }
local function row(o, label)
local users = {}
for _, u in ipairs(by[o]) do users[#users + 1] = '[[利用者:' .. u .. '|' .. u .. ']]' end
rows[#rows + 1] = '|-'
rows[#rows + 1] = '| ' .. label .. ' || ' .. #by[o] .. ' || ' .. (#users > 0 and table.concat(users, '、') or '—')
end
for _, o in ipairs(opts) do row(o, o) end
for _, o in ipairs(others) do row(o, o .. '(その他)') end
rows[#rows + 1] = '|}'
-- いちばん多い選択肢
local best, top, tie = 0, nil, false
for _, o in ipairs(opts) do
if #by[o] > best then best, top, tie = #by[o], o, false elseif #by[o] == best and best > 0 then tie = true end
end
for _, o in ipairs(others) do
if #by[o] > best then best, top, tie = #by[o], o, false elseif #by[o] == best and best > 0 then tie = true end
end
local notes = {}
if s then notes[#notes + 1] = '期間:' .. d._fmt(s) .. ' から ' .. d._fmt(e) .. ' まで(日本時間)' end
if best == 0 then
notes[#notes + 1] = 'まだ票はありません'
elseif tie then
notes[#notes + 1] = '最多の票が並んでいます(' .. best .. ' 票)'
else
notes[#notes + 1] = '今の最多:' .. top .. '(' .. best .. ' 票)'
end
notes[#notes + 1] = '期間内の票だけを数え、同じ利用者はいちばん新しい票だけ数えています。日時は、版の履歴で確かめた票は履歴の時刻、ほかは署名の日時です'
if skip.late > 0 then notes[#notes + 1] = '期間外の票 ' .. skip.late .. ' 件は数えていません' end
if skip.struck > 0 then notes[#notes + 1] = '取り消し線の票 ' .. skip.struck .. ' 件は数えていません' end
if skip.unsigned > 0 then notes[#notes + 1] = '署名のない票 ' .. skip.unsigned .. ' 件は数えていません' end
if skip.ip > 0 then notes[#notes + 1] = 'IP・仮アカウントの票 ' .. skip.ip .. ' 件は数えていません' end
if skip.notime > 0 then notes[#notes + 1] = '日時のない署名の票 ' .. skip.notime .. ' 件は数えていません' end
if skip.forged > 0 then notes[#notes + 1] = 'ほかの人の名前の署名か、本人が書いていない票 ' .. skip.forged .. ' 件は数えていません' end
if #skip.review > 0 then notes[#notes + 1] = '履歴で確かめる票 ' .. #skip.review .. ' 件(署名が二人以上か、期間の外の日時が混じる行:' .. table.concat(skip.review, '、') .. ')は数えていません' end
local hn = d._historyNote(skip)
if hn ~= '' then notes[#notes + 1] = hn:sub(4) end
notes[#notes + 1] = '投票権([[TP:GVOTE]])は、締めるときに集計する人が確かめます'
if s and e and os.time() > e then notes[#notes + 1] = "'''期間は終わりました。'''" end
return table.concat(rows, '\n') .. '\n<small>' .. table.concat(notes, '。') .. '。</small>'
end
return p