モジュール:削除議論
表示
このモジュールについての説明文ページを モジュール:削除議論/doc に作成できます
-- 削除依頼・削除議論の期限と票の集計(テンプレート:削除依頼・テンプレート:削除議論 から呼ばれる)
-- 期限(トンチンペディア:削除の方針「ふつうの削除」):
-- コンテンツ空間のページ: 改稿期間 = 作成から 1 週間以内に貼ったら 1 週間、それより後なら 30 日(720 時間)。議論の期限 = 改稿期間の終わり + 1 週間
-- それ以外の空間: 議論の期限 = 貼ってから 1 週間
local p = {}
local WEEK = 168 * 3600
local MONTH = 720 * 3600 -- 30 日(月によって長さが変わらないように)
local JST = 9 * 3600
-- os.time{...} は表をウィキの地方時(このウィキは日本時間)として読むので、日本時間の日時を UTC の秒に直すための差を求める
local LOCAL_OFFSET = (function()
local t = os.time()
local u = os.date('!*t', t)
return JST - (t - os.time{ year = u.year, month = u.month, day = u.day, hour = u.hour, min = u.min, sec = u.sec })
end)()
-- 「2026-10-11 00:02」「2026年10月11日 00:02」などを UTC の秒に(日本時間として読む)
local function parse(s)
if not s or s == '' then return nil end
local y, m, d = s:match('(%d%d%d%d)%-(%d%d?)%-(%d%d?)')
if not y then y, m, d = s:match('(%d%d%d%d)年(%d%d?)月(%d%d?)日') end
if not y then return nil end
local H, M = s:match('(%d%d?):(%d%d)')
return os.time{ year = tonumber(y), month = tonumber(m), day = tonumber(d),
hour = tonumber(H or 0), min = tonumber(M or 0), sec = 0 } - LOCAL_OFFSET
end
local function fmt(t)
return (os.date('!%Y年%m月%d日 %H:%M', t + JST):gsub('年0', '年'):gsub('月0', '月'))
end
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
-- 削除の対象のページ(トークページに貼られたときは本体のページ)。一覧(row)からは page= で別のページを指す
local function subject(frame)
local name = frame and frame.args.page
local t = (name and name ~= '') and mw.title.new(name) or mw.title.getCurrentTitle()
if t.isTalkPage then return t.subjectPageTitle end
return t
end
-- ページの作成日時(最初の版。DynamicPageList で取る。日本時間の文字列)
local function created(frame, t)
local out = frame:preprocess('{{#dpl:title=' .. t.text .. '|namespace=' .. t.nsText ..
'|skipthispage=no|ordermethod=firstedit|addeditdate=true|mode=userformat|listseparators=,%DATE%,,|userdateformat=Y-m-d H:i}}')
return parse(mw.text.trim(out))
end
-- 開始が書かれていないとき(削除議論)は、本体のページの {{削除依頼|...|開始=...}} から読む
local function start_of(frame)
-- 1 番目の引数はテンプレートが渡す開始の日時だけを見る(親テンプレートの 1 番目は削除の理由なので読まない)
local s = frame.args[1]
if s == nil or s == '' then s = frame:getParent().args['開始'] end
if s ~= nil and s ~= '' then return parse(s) end
local t = subject(frame)
if t == mw.title.getCurrentTitle() and not (frame.args.page and frame.args.page ~= '') then return nil end
local text = t:getContent() or ''
local v = text:match('{{%s*削除依頼[^}]-開始%s*=%s*([^|}]+)')
return v and parse(v) or nil
end
-- 期限の計算: 戻り値は 改稿期間の終わり(無ければ nil)、議論の期限、改稿期間の長さの表示
local function schedule(frame)
local s = start_of(frame)
if not s then return nil end
local t = subject(frame)
local ns = mw.site.namespaces[t.namespace]
if not (ns and ns.isContent) then
return s, nil, s + WEEK, nil
end
local c = created(frame, t)
local rev = (c and s - c > WEEK) and MONTH or WEEK
return s, s + rev, s + rev + WEEK, rev == MONTH and '30 日' or '1 週間'
end
-- 「改稿期間:…まで(1 週間) 議論の期限:…」
function p.periods(frame)
local s, revEnd, deadline, label = schedule(frame)
if not s then return '' end
local out = ''
if revEnd then out = '改稿期間:' .. fmt(revEnd) .. ' まで(' .. label .. ') ' end
return out .. '議論の期限:' .. fmt(deadline) .. '(日本時間)'
end
-- 期限(議論の期限)
function p.deadline(frame)
local _, _, deadline = schedule(frame)
if not deadline then return '' end
return fmt(deadline) .. '(日本時間)'
end
-- 議論の期限を過ぎていれば "1"
function p.expired(frame)
local _, _, deadline = schedule(frame)
if deadline and os.time() > deadline then return '1' end
return ''
end
-- 今の段階: 「改稿期間中」「議論中」「期限切れ」
function p.phase(frame)
local s, revEnd, deadline = schedule(frame)
if not s then return '' end
local now = os.time()
if now > deadline then return '期限切れ' end
if revEnd and now <= revEnd then return '改稿期間中' end
return '議論中'
end
-- 開始からの経過(時間)
function p.elapsed(frame)
local s = start_of(frame)
if not s then return '' end
return tostring(math.floor((os.time() - s) / 3600))
end
-- 節の中の行だけ(同じか浅い見出しが来るまで)。モジュール:投票 も使う
local function sectionText(text, section)
local lines, inside, level = {}, false, nil
for line in (text .. '\n'):gmatch('(.-)\n') do
local eq, title = line:match('^(=+)%s*(.-)%s*=+%s*$')
if eq then
if inside and #eq <= level then break end
if title == section then inside, level = true, #eq end
elseif inside then
lines[#lines + 1] = line
end
end
return table.concat(lines, '\n')
end
-- 行の署名の利用者名(行の中の最後の [[利用者:…]])
local function signer(line)
local user
for u in line:gmatch('%[%[%s*利用者:([^|%]]+)') do user = u end
return user and mw.text.trim(user) or nil
end
-- 行の署名の日時(「2026年10月16日 (金) 10:42 (JST)」を UTC の秒に)
local function sigTime(line)
local y, mo, d, H, M = line:match('(%d%d%d%d)年(%d%d?)月(%d%d?)日 %(.-%) (%d%d):(%d%d) %(JST%)')
return y and (os.time{ year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(H), min = tonumber(M), sec = 0 } - LOCAL_OFFSET)
end
-- 行から、表示されない部分(nowiki・code・pre など)と、取り消し線の部分(s・del・strike)を除く
-- 閉じていないタグは、行の終わりまでを除く
local HIDDEN = { 'nowiki', 'code', 'pre', 'syntaxhighlight', 'source' }
local STRUCK = { 's', 'del', 'strike' }
local function stripTags(line, tags)
for _, t in ipairs(tags) do
for _, tag in ipairs({ t, t:upper() }) do
line = line:gsub('<' .. tag .. '>.-</' .. tag .. '>', '')
line = line:gsub('<' .. tag .. '%s[^>]*>.-</' .. tag .. '>', '')
line = line:gsub('<' .. tag .. '>.*$', '')
line = line:gsub('<' .. tag .. '%s[^>]*>.*$', '')
end
end
return line
end
local function stripHidden(line) return (stripTags(line, HIDDEN)) end
local function stripStruck(line)
-- {{無効票|…}} で無効にされた行は、行ごと取り消し線の票として扱う
if line:find('{{%s*無効票%s*|') then return '' end
return (stripTags(line, STRUCK))
end
-- 行の中の署名の利用者(重なりを除いた並び。[[利用者:X/…]] は X とする)
local function signers(line)
local seen, list = {}, {}
local function add(u)
u = mw.text.trim(((u:match('^[^/]+') or u):gsub('_', ' ')))
local t = mw.title.new(u, 2)
u = t and t.text or u
if u ~= '' and not seen[u] then seen[u] = true; list[#list + 1] = u end
end
for u in line:gmatch('%[%[%s*利用者:([^|%]]+)') do add(u) end
for u in line:gmatch('%[%[%s*[Uu]ser:([^|%]]+)') do add(u) end
return list
end
-- 行の中の署名の日時(すべて)
local function sigTimes(line)
local ts = {}
for y, mo, d, H, M in line:gmatch('(%d%d%d%d)年(%d%d?)月(%d%d?)日 %(.-%) (%d%d):(%d%d) %(JST%)') do
ts[#ts + 1] = os.time{ year = tonumber(y), month = tonumber(mo), day = tonumber(d), hour = tonumber(H), min = tonumber(M), sec = 0 } - LOCAL_OFFSET
end
return ts
end
-- 票の行を調べる(表示されない部分を除いた行で見る)。戻り値は 状態, 利用者, 日時
-- 'ok' … 数える(署名が一人で、日時がすべて期間の内。日時はいちばん新しいもの)
-- 'unsigned' … 署名が無い
-- 'ip' … ログインしていない人の署名(IP の [[特別:投稿記録/…]]、または仮アカウント=名前が ~ で始まる利用者)
-- 'notime' … 署名に日時が無い
-- 'late' … 日時がすべて期間の外
-- 'review' … 履歴で確かめる(署名が二人以上ある、または期間の内と外の日時が混じる=期限の後に書き換えられた可能性)
-- ログインしていない人の名前か(IP アドレス、または仮アカウント=名前が ~ で始まる)
local function isIP(user)
return user:match('^%d+%.%d+%.%d+%.%d+$') or user:find(':') or user:sub(1, 1) == '~'
end
local function classify(line, s, e)
local shown = stripHidden(line)
local users = signers(shown)
if #users == 0 then
if shown:find('%[%[%s*特別:投稿記録/') or shown:find('%[%[%s*[Ss]pecial:[Cc]ontributions/') then return 'ip' end
return 'unsigned'
end
if #users > 1 then return 'review', table.concat(users, '・') end
if isIP(users[1]) then return 'ip', users[1] end
local ts = sigTimes(shown)
if #ts == 0 then return 'notime', users[1] end
local inside, outside, tmax = 0, 0, nil
for _, t in ipairs(ts) do
if (s and t < s) or (e and t > e) then outside = outside + 1 else inside = inside + 1 end
if not tmax or t > tmax then tmax = t end
end
if inside == 0 then return 'late', users[1] end
if outside > 0 then return 'review', users[1] end
return 'ok', users[1], tmax
end
-- 票の行の履歴(モジュール:削除議論/履歴.json)。ジョブが版の履歴から、行ごとに 書いた利用者(a)・その利用者が書いた版の
-- 時刻(UTC の秒)と行(v)・ほかの人が書き換えたか(o)を出す。行は、行の文字の md5 で引く
local HISTORY
local function history(title, line)
if HISTORY == nil then
local ok, data = pcall(mw.loadJsonData, 'Module:削除議論/履歴.json')
HISTORY = ok and data.pages or false
end
local page = HISTORY and HISTORY[title.prefixedText]
return page and page[mw.hash.hashValue('md5', line)] or nil
end
-- 票の行を一つ読む。kindOf は、取り消し線を除いた行から票の中身を返す関数
-- 戻り値は 票の中身・状態(classify と同じもの、ほかに 'struck' と 'forged')・利用者・日時・履歴の注記
-- 履歴があれば、その行を書いた利用者が期限までに書いた最後の版で読み、日時は版の本当の時刻を使う(期間は分の単位で比べる)
-- 'forged' … その版の署名に書いた本人の名前が無い(ほかの人の名前の署名)か、本人の版に票が無い(ほかの人が書き足した票)
-- 履歴がまだ無い行(ジョブが次に回るまで)は、行の文字だけで読む
-- 版の時刻(秒)を分に切り捨てる。期間は署名の日時と同じ分の単位で見る(期限の分のうちに書いた票は期間内)
local function minuteOf(t) return t - t % 60 end
local function readVote(title, line, s, e, kindOf)
local shown = stripHidden(line)
local kind = kindOf(stripStruck(shown))
local struck = false
if not kind then
kind = kindOf(shown)
if not kind then return nil end
struck = true
end
local h = history(title, line)
if not h then
if struck then return nil, 'struck' end
local st, user, t = classify(line, s, e)
return kind, st, user, t, { pending = true }
end
local info = { other = h.o == 1 }
local pick, n = nil, 0
for i, v in ipairs(h.v) do
n = i
if not e or minuteOf(v[1]) <= e then pick = i end
end
if pick and pick ~= n then info.rewritten = true end
-- 取り消し線の票は数えない。ただし、今の行が本人の最後の版のままで、その版が期限の後なら、
-- 本人が期限の後に引いた取り消し線なので、期限の後の書き換えと同じく、期限までの版で読む
if struck and not (info.rewritten and h.v[n][2] == line) then return nil, 'struck' end
if not pick or (s and minuteOf(h.v[pick][1]) < s) then return kind, 'late', h.a, nil, info end
local t = h.v[pick][1]
local vshown = stripHidden(h.v[pick][2])
local vkind = kindOf(stripStruck(vshown))
if not vkind then
if kindOf(vshown) then return nil, 'struck' end
return kind, 'forged', h.a, nil, info
end
if isIP(h.a) then return vkind, 'ip', h.a, nil, info end
local mine = false
for _, u in ipairs(signers(vshown)) do
if u == h.a then mine = true end
end
if not mine then
if #signers(vshown) == 0 then return vkind, 'unsigned', h.a, nil, info end
return vkind, 'forged', h.a, nil, info
end
if #sigTimes(vshown) == 0 then return vkind, 'notime', h.a, nil, info end
return vkind, 'ok', h.a, t, info
end
-- Reboot版へ移す票(ユーモアだけれどナンセンスではない記事を、アンサイクロペディア Reboot版へ移す)。
-- 削除議論では、このウィキから外す側(削除に賛成の側)に数える。移動議論では、賛成・反対とは別に数える
local REBOOT = 'Reboot版へ移動'
local VOTES = {
{ '条件付賛成', '条件付き賛成' }, { '条件付反対', '条件付き反対' },
{ '賛成', '賛成' }, { '反対', '反対' }, { '保留', '保留' },
{ REBOOT, REBOOT },
}
-- 遊びの票(en.uncyclopedia の VFD の Nuke・Smite などにならったもの)と、数える側
local ALIASES = {
{ '爆破', '賛成' }, { '天誅', '賛成' }, { '浄化', '賛成' }, { '抹殺', '賛成' },
{ '延命', '反対' }, { '守護', '反対' }, { '移動', '反対' },
}
-- このページ(または「節」の範囲)の票を数える
-- 数えるのは、署名が一人で日時がすべて期間の内の票だけ。同じ利用者は、日時がいちばん新しい票
-- 履歴(readVote)がある行は、書いた本人が期限までに書いた版で読み、日時は版の時刻
-- 取り消し線の票・署名の無い票・IP の票・日時の無い票・期間の外の票・履歴で確かめる票は数えず、件数を返す
-- 依頼者は、引数「依頼者」(一覧からはトークページの {{削除議論|依頼者=…}})。書かれていなければ判定しない
local function count(frame)
local s, _, deadline = schedule(frame)
local section = arg(frame, 2)
local text, title
local proposer
if frame.args.page and frame.args.page ~= '' then
-- 一覧から: 本体のページのトークページの議論を、{{削除議論|節=…}} の節で数える
title = subject(frame).talkPageTitle
text = title:getContent() or ''
section = text:match('{{%s*削除議論[^}]-節%s*=%s*([^|}]+)')
section = section and mw.text.trim(section) or nil
proposer = text:match('{{%s*削除議論[^}]-依頼者%s*=%s*([^|}]+)')
else
title = mw.title.getCurrentTitle()
text = title:getContent() or ''
proposer = frame:getParent().args['依頼者']
end
proposer = proposer and mw.text.trim(proposer) or nil
if proposer == '' then proposer = nil end
if section then text = sectionText(text, section) end
local last, order = {}, {}
local c = { late = 0, unsigned = 0, ip = 0, notime = 0, struck = 0, forged = 0, review = {}, proposer = proposer, others = 0, othersCond = 0,
pending = 0, fixed = 0 }
local pos = 0
local function kindIn(str)
for _, v in ipairs(VOTES) do
if str:find('{{%s*' .. v[1] .. '%s*[|}]') then return v[1] end
end
for _, v in ipairs(ALIASES) do
if str:find('{{%s*' .. v[1] .. '%s*[|}]') then return v[2] end
end
return nil
end
for line in (text .. '\n'):gmatch('(.-)\n') do
pos = pos + 1
local kind, st, user, t, info = readVote(title, line, s, deadline, kindIn)
if st == 'struck' then
c.struck = c.struck + 1
elseif kind then
if info.pending then c.pending = c.pending + 1 end
if info.other or info.rewritten then c.fixed = c.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] = { kind = kind, t = t, pos = pos }
end
elseif st == 'review' then
c.review[#c.review + 1] = user
else
c[st] = c[st] + 1
end
end
end
for _, k in ipairs(order) do
local kind = last[k].kind
c[kind] = (c[kind] or 0) + 1
if k ~= proposer then
if kind == '賛成' then c.others = c.others + 1 end
if kind == '条件付賛成' then c.othersCond = c.othersCond + 1 end
if kind == REBOOT then c.others = c.others + 1 end
end
end
return c
end
-- 履歴で読み替えた票と、履歴がまだ無い票の注記
local function historyNote(c)
local out = ''
if c.fixed > 0 then
out = out .. '。ほかの人が書き換えた票と、期限の後に書き換えた票 ' .. c.fixed .. ' 件は、書いた本人が期限までに書いた版で読んでいます'
end
if c.pending > 0 then
out = out .. '。履歴をまだ確かめていない票 ' .. c.pending .. ' 件は、行の文字だけで読んでいます(履歴は後から確かめます)'
end
return out
end
-- 数えなかった票の注記
local function skippedNote(c)
local out = {}
if c.late > 0 then out[#out + 1] = '期間外の票 ' .. c.late .. ' 件' end
if c.struck > 0 then out[#out + 1] = '取り消し線の票 ' .. c.struck .. ' 件' end
if c.unsigned > 0 then out[#out + 1] = '署名のない票 ' .. c.unsigned .. ' 件' end
if c.ip > 0 then out[#out + 1] = 'IP・仮アカウントの票 ' .. c.ip .. ' 件' end
if c.notime > 0 then out[#out + 1] = '日時のない署名の票 ' .. c.notime .. ' 件' end
if c.forged > 0 then out[#out + 1] = 'ほかの人の名前の署名か、本人が書いていない票 ' .. c.forged .. ' 件' end
if #c.review > 0 then
out[#out + 1] = '履歴で確かめる票 ' .. #c.review .. ' 件(署名が二人以上か、期間の外の日時が混じる行:' .. table.concat(c.review, '、') .. ')'
end
local note = historyNote(c)
if #out == 0 then return note end
return '。数えていない票:' .. table.concat(out, '・') .. note
end
function p.tally(frame)
local c = count(frame)
local out = {}
for _, v in ipairs(VOTES) do
if (c[v[1]] or 0) > 0 or v[1] == '賛成' or v[1] == '反対' then
out[#out + 1] = v[2] .. ' ' .. (c[v[1]] or 0)
end
end
local note = '(期間内の票。同じ利用者は、いちばん新しい票だけ数えています。日時は、版の履歴で確かめた票は履歴の時刻、ほかは署名の日時です' .. skippedNote(c)
return table.concat(out, '・') .. note .. ')'
end
-- 削除の条件: 依頼者以外の削除賛成が 1 票以上、かつ賛成が反対より多い。満たさなければ残す
-- 条件付の票は、条件が満たされればその側に数え、満たされなければ保留と同じ(数えない)。条件は票ごとに見て、まとめたり多数決にしたりしない
-- モジュールは条件の中身を判断できないので、両端(条件付賛成がすべて成立・条件付反対がすべて不成立/その逆)で結論が変わるかだけを見る
local function judge(frame)
local c = count(frame)
local r = c[REBOOT] or 0
local y, cy, n, cn = (c['賛成'] or 0) + r, c['条件付賛成'] or 0, c['反対'] or 0, c['条件付反対'] or 0
local okMax = c.others + c.othersCond >= 1 and y + cy > n
local okMin = c.others >= 1 and y > n + cn
local detail = '依頼者以外の賛成 ' .. c.others .. (c.othersCond > 0 and ('(ほかに条件付 ' .. c.othersCond .. ')') or '') ..
'・賛成 ' .. y .. (r > 0 and ('(うち Reboot版へ移動 ' .. r .. ')') or '') .. (cy > 0 and ('+条件付 ' .. cy) or '') ..
' 対 反対 ' .. n .. (cn > 0 and ('+条件付 ' .. cn) or '')
-- このウィキから外す票(賛成と Reboot版へ移動)のうち、Reboot版へ移動が半分以上なら、削除せずに Reboot版へ移す
c.toReboot = r > 0 and r * 2 >= y
local _, _, deadline = schedule(frame)
local over = deadline and os.time() > deadline
local code = okMin and 'yes' or (okMax and 'maybe' or 'no')
if not c.proposer then code = 'unknown' end
detail = detail .. skippedNote(c)
return code, detail, c, over
end
function p.verdict(frame)
local code, detail, c, over = judge(frame)
detail = detail .. (c.proposer and ('。依頼者:' .. c.proposer) or '。依頼者:不明')
if code == 'unknown' then return '依頼者が書かれていないので、判定していません(' .. detail .. ')。{{削除議論}}に「依頼者=」を書いてください' end
if code == 'yes' and c.toReboot then
return '削除の条件を満たしています(' .. detail .. ')。外す票の半分以上が Reboot版へ移動なので、アンサイクロペディア Reboot版が受け入れを決めれば移します。受け入れられなければ削除します'
end
if code == 'yes' then return '削除の条件を満たしています(' .. detail .. ')' end
if code == 'maybe' then return '条件付の票の条件しだいです(' .. detail .. ')。管理者が、それぞれの条件が満たされたかを確かめて判断します' end
if over then return '削除の条件を満たしていません(' .. detail .. ')。残します' end
return '削除の条件をまだ満たしていません(' .. detail .. ')。このまま期限を迎えると残します'
end
-- 一覧(トンチンペディア:削除依頼)の 1 行: ページ・理由・依頼の日時・改稿期間の終わり・議論の期限・段階・判定・議論へのリンク
function p.row(frame)
local t = subject(frame)
local text = t:getContent() or ''
local reason = text:match('{{%s*削除依頼%s*|%s*([^|}]*)') or ''
reason = mw.text.trim(reason)
if mw.ustring.len(reason) > 60 then reason = mw.ustring.sub(reason, 1, 60) .. '…' end
local s, revEnd, deadline, label = schedule(frame)
local cells = { '[[:' .. t.prefixedText .. ']]', reason ~= '' and reason or '(記入なし)' }
if s then
local code, detail, c = judge(frame)
local word = ({ yes = '削除の条件を満たす', maybe = '条件付の票しだい', no = '満たしていない', unknown = '依頼者が不明' })[code]
if code == 'yes' and c.toReboot then word = '削除の条件を満たす(Reboot版へ移動)' end
cells[#cells + 1] = fmt(s)
cells[#cells + 1] = revEnd and (fmt(revEnd) .. '(' .. label .. ')') or '—'
cells[#cells + 1] = fmt(deadline)
cells[#cells + 1] = p.phase(frame)
cells[#cells + 1] = word .. '<br /><small>' .. detail .. '</small>'
else
for _ = 1, 5 do cells[#cells + 1] = '—' end
cells[5] = '(開始の日時が分かりません)'
end
cells[#cells + 1] = '[[' .. t.talkPageTitle.prefixedText .. '|議論]]'
return '|-\n| ' .. table.concat(cells, ' || ') .. '\n'
end
-- ほかのモジュール(モジュール:投票・モジュール:ブロック議論)と共有する部品
p._parse, p._fmt, p._sectionText, p._signer, p._sigTime = parse, fmt, sectionText, signer, sigTime
p._stripHidden, p._stripStruck, p._signers, p._sigTimes, p._classify = stripHidden, stripStruck, signers, sigTimes, classify
p._readVote, p._historyNote, p._skippedNote = readVote, historyNote, skippedNote
return p