コンテンツにスキップ

モジュール:ブロック議論

出典: へっぽこ実験ウィキ『八百科事典(トンチンペディア)』

このモジュールについての説明文ページを モジュール:ブロック議論/doc に作成できます

-- 投稿ブロックの議論の票の集計(テンプレート:ブロック議論 から呼ばれる)
-- {{BL|種類|条件|期間}} の票を数える。期限・経過時間はモジュール:削除議論 と同じ 168 時間
-- 票の行の読み方(署名・日時・取り消し線・版の履歴)は モジュール:削除議論 の readVote を使う(モジュール:投票 と同じ)
local d = require('Module:削除議論')
local p = {}

local PERIOD = 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

-- 依頼の種類ごとに、どの票が「依頼に賛成」「依頼に反対」か
local SIDES = {
	['ブロック'] = { yes = { '賛成' }, no = { '反対' }, median = true },
	['追認'] = { yes = { '追認', '賛成' }, no = { '反対' }, median = true },
	['延長'] = { yes = { '延長', '賛成' }, no = { '維持', '反対' } },
	['解除'] = { yes = { '解除', '賛成' }, no = { '継続', '反対' } },
	['短縮'] = { yes = { '短縮', '賛成' }, no = { '継続', '反対' } },
}
-- 集計に表示する票の種類(この順で並べる)
local ORDER = { '賛成', '追認', '延長', '解除', '短縮', '反対', '維持', '継続', '保留', '中立', '棄権', '即時終了', '終了提案' }
local KNOWN = {}
for _, k in ipairs(ORDER) do KNOWN[k] = true end

local SEC = { ['秒'] = 1, ['分'] = 60, ['時間'] = 3600, ['日'] = 86400, ['週間'] = 604800, ['週'] = 604800,
	['か月'] = 2592000, ['ヶ月'] = 2592000, ['ヵ月'] = 2592000, ['カ月'] = 2592000, ['箇月'] = 2592000, ['月'] = 2592000, ['年'] = 31536000 }
local UNITS = { '時間', '週間', 'か月', 'ヶ月', 'ヵ月', 'カ月', '箇月', '秒', '分', '日', '週', '月', '年' }
local FIFTEEN = 15 * 31536000

-- 単位の前の漢数字(「一週間」「二十日」「三か月」など)を算用数字に
local KANJI = { ['〇'] = 0, ['一'] = 1, ['二'] = 2, ['三'] = 3, ['四'] = 4, ['五'] = 5, ['六'] = 6, ['七'] = 7, ['八'] = 8, ['九'] = 9 }
local function kanjiNumber(k)
	local total, cur = 0, 0
	for c in mw.ustring.gmatch(k, '.') do
		if c == '百' then total, cur = total + (cur == 0 and 1 or cur) * 100, 0
		elseif c == '十' then total, cur = total + (cur == 0 and 1 or cur) * 10, 0
		else cur = cur * 10 + KANJI[c] end
	end
	return tostring(total + cur)
end

-- 「1週間」「3ヶ月」「1年6か月」「一週間」「半年」「無期限」などを秒に。無期限は 15 年として返し、2 番目の値を true にする
local function seconds(s)
	if not s then return nil end
	s = mw.ustring.gsub(s, '[0-9]', function(c) return tostring(mw.ustring.codepoint(c) - 0xFF10) end)
	s = mw.ustring.gsub(s, '半年', '6か月')
	for _, u in ipairs(UNITS) do
		s = mw.ustring.gsub(s, '([〇一二三四五六七八九十百]+)(%s*' .. u .. ')', function(k, rest) return kanjiNumber(k) .. rest end)
	end
	if s:find('無期') then return FIFTEEN, true end
	local total, rest = 0, s
	local found = false
	while true do
		local num, after = rest:match('^%D-(%d+%.?%d*)%s*(.*)$')
		if not num then break end
		local unit
		for _, u in ipairs(UNITS) do
			if after:sub(1, #u) == u then unit = u; break end
		end
		if not unit then break end
		total = total + tonumber(num) * SEC[unit]
		found = true
		rest = after:sub(#unit + 1)
	end
	if found then return total end
	return nil
end

local function human(t)
	if t >= FIFTEEN then return '15 年' end
	if t < 60 then return string.format('%d 秒', math.floor(t + 0.5)) end
	local parts = {}
	local steps = { { 31536000, '年' }, { 2592000, 'か月' }, { 604800, '週間' }, { 86400, '日' }, { 3600, '時間' }, { 60, '分' } }
	local rest = t
	for _, st in ipairs(steps) do
		local n = math.floor(rest / st[1])
		if n > 0 then
			parts[#parts + 1] = n .. ' ' .. st[2]
			rest = rest - n * st[1]
		end
		if #parts == 2 then break end
	end
	return table.concat(parts, ' ')
end

-- 行の中の {{BL|...}} を読む。返り値:種類、条件、期間、撤回か
local function readBL(line)
	local inner = line:match('{{%s*[Bb][Ll]%s*|([^{}]*)}}')
	if not inner then return nil end
	local pos = {}
	local named = {}
	for part in (inner .. '|'):gmatch('([^|]*)|') do
		local k, v = part:match('^%s*([^=]-)%s*=%s*(.-)%s*$')
		if k and k ~= '' and not k:match('^%d+$') then named[k] = v else pos[#pos + 1] = mw.text.trim(part) end
	end
	local kind = named['クラス'] or pos[1]
	if not kind or kind == '' then return nil end
	local withdrawn = false
	if kind:sub(-1) == 'r' and KNOWN[kind:sub(1, -2)] then kind, withdrawn = kind:sub(1, -2), true end
	if not KNOWN[kind] then return nil end
	return kind, (pos[2] or ''), (pos[3] or ''), withdrawn
end

function p.tally(frame)
	local s = d._parse(arg(frame, 1))
	local section = arg(frame, 2)
	local mode = arg(frame, 3) or 'ブロック'
	local side = SIDES[mode] or SIDES['ブロック']
	local title = mw.title.getCurrentTitle()
	local text = title:getContent() or ''
	if section then text = d._sectionText(text, section) end
	-- 票の行を readVote で読む(書いた本人が期限までに書いた版・署名・日時・取り消し線)。同じ利用者は日時がいちばん新しい票
	local function voteIn(str)
		local kind, cond, period, withdrawn = readBL(str)
		if kind then return { kind = kind, cond = cond ~= '', period = period, withdrawn = withdrawn } end
		return nil
	end
	local newest, order, pos = {}, {}, 0
	local skip = { late = 0, unsigned = 0, ip = 0, notime = 0, struck = 0, forged = 0, review = {}, pending = 0, fixed = 0 }
	for line in (text .. '\n'):gmatch('(.-)\n') do
		pos = pos + 1
		local v, st, user, t, info = d._readVote(title, line, s, s and s + PERIOD, voteIn)
		if st == 'struck' then
			skip.struck = skip.struck + 1
		elseif v 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 = newest[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 newest[user] = { v = v, 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 last = {}
	for _, key in ipairs(order) do
		local v = newest[key].v
		if v.withdrawn then last[key] = false else last[key] = { kind = v.kind, cond = v.cond, period = v.period } end
	end
	local count, condc = {}, {}
	local yesN, noN = 0, 0
	local isYes, isNo = {}, {}
	for _, k in ipairs(side.yes) do isYes[k] = true end
	for _, k in ipairs(side.no) do isNo[k] = true end
	local durations, periods, unread = {}, {}, 0
	for _, key in ipairs(order) do
		local v = last[key]
		if v then
			count[v.kind] = (count[v.kind] or 0) + 1
			if v.cond then condc[v.kind] = (condc[v.kind] or 0) + 1 end
			if isYes[v.kind] then
				yesN = yesN + 1
				local sec, indef = seconds(v.period)
				if sec then
					durations[#durations + 1] = sec
					local key = indef and FIFTEEN + 1 or sec
					periods[key] = (periods[key] or 0) + 1
				else
					unread = unread + 1
				end
			elseif isNo[v.kind] then
				noN = noN + 1
				durations[#durations + 1] = 1
			end
		end
	end
	local out = {}
	for _, k in ipairs(ORDER) do
		if (count[k] or 0) > 0 or isYes[k] and k == side.yes[1] or isNo[k] and k == side.no[1] then
			local c = (count[k] or 0) .. ''
			if (condc[k] or 0) > 0 then c = c .. '(うち条件付 ' .. condc[k] .. ')' end
			out[#out + 1] = k .. ' ' .. c
		end
	end
	local res = table.concat(out, '・')
	local note = '(期間内の票。同じ利用者はいちばん新しい票だけ数え、撤回した票は数えていません。日時は、版の履歴で確かめた票は履歴の時刻、ほかは署名の日時です'
	res = res .. note .. d._skippedNote(skip) .. ')'
	local keys, plist = {}, {}
	for k in pairs(periods) do keys[#keys + 1] = k end
	table.sort(keys)
	for _, k in ipairs(keys) do plist[#plist + 1] = (k > FIFTEEN and '無期限' or human(k)) .. ' ' .. periods[k] end
	if #plist > 0 then res = res .. '<br />期間:' .. table.concat(plist, '・') end
	if side.median then
		local ref = '<br /><small>参考([[トンチンペディア:投稿ブロックの議論の方針|私論]]による 2 段の集計):'
		if yesN == 0 and noN == 0 then
			ref = ref .. 'まだ票がありません'
		elseif yesN <= noN then
			ref = ref .. 'ブロックしない(賛成 ' .. yesN .. '・反対 ' .. noN .. ')'
		elseif unread > 0 then
			-- 期間を読み取れない賛成の票を除いて中央値をとると、反対の票の 1 秒が中央値になったり、期間が一つも無くてエラーになったりするので出さない
			ref = ref .. 'ブロックする(賛成 ' .. yesN .. '・反対 ' .. noN .. ')。期間を読み取れない賛成の票が ' .. unread .. ' 件あるので、期間の中央値は出していません(その票の期間を確かめてください)'
		else
			table.sort(durations)
			local n = #durations
			local med
			if n % 2 == 1 then med = durations[(n + 1) / 2] else med = (durations[n / 2] + durations[n / 2 + 1]) / 2 end
			ref = ref .. 'ブロックする(賛成 ' .. yesN .. '・反対 ' .. noN .. ')。期間の中央値は ' .. human(med)
			if med >= FIFTEEN then ref = ref .. '(無期限が相当な場合は無期限)' end
		end
		res = res .. ref .. '</small>'
	end
	return res
end

return p
Cookieは私達のサービスを提供するのに役立ちます。このサービスを使用することにより、お客様はCookieの使用に同意するものとします。