「モジュール:削除依頼ログ/sandbox」の版間の差分
表示
削除された内容 追加された内容
編集の要約なし |
|||
(同じ利用者による、間の14版が非表示) | |||
1行目: | 1行目: | ||
local yesno = require('Module:Yesno') |
local yesno = require('Module:Yesno') |
||
local mMessageBox = require('Module:Message box') |
|||
local TEMPLATE_PAGE = 'Template:Old XfD multi' |
|||
------------------------------------------------------------------------------- |
------------------------------------------------------------------------------- |
||
-- |
-- AfDLog class |
||
------------------------------------------------------------------------------- |
------------------------------------------------------------------------------- |
||
local |
local AfDLog = {} |
||
AfDLog.__index = AfDLog |
|||
local success, exists = pcall(function () |
|||
local title = mw.title.new(page) |
|||
return title.exists |
|||
end) |
|||
return success and exists |
|||
end |
|||
---Create a new AfDLog object instance. |
|||
local function getAfdPage(page) |
|||
---@param args table |
|||
if page and mw.title.new(page) then |
|||
---@return AfDLog |
|||
if mw.title.new(page).namespace ~= 0 then |
|||
function AfDLog.new(args) |
|||
return page |
|||
else |
|||
return 'Wikipedia:Articles for deletion/' .. page |
|||
end |
|||
else return nil |
|||
end |
|||
end |
|||
local |
local self = setmetatable({}, AfDLog) |
||
if page and mw.title.new(page) then |
|||
if mw.title.new(page).rootPageTitle.fullText == 'Wikipedia:Votes for deletion' then |
|||
return page |
|||
else |
|||
return 'Wikipedia:Votes for deletion/' .. page |
|||
end |
|||
else return nil |
|||
end |
|||
end |
|||
-- Set non-data parameters |
|||
local function makeWikilink(page, display) |
|||
self.talk = not not yesno(args.talk) |
|||
display = display or 'discussion' |
|||
self.collapse = tonumber(args.collapse) or not not yesno(args.collapse) |
|||
if page then |
|||
self.numbered = true --yesno(args.numbered) == nil or not not yesno(args.numbered) -- Disabled parameter |
|||
return string.format('[[%s|%s]]', page, display) |
|||
self.reversed = yesno(args.reversed) == nil or not not yesno(args.reversed) |
|||
else |
|||
self.currentTitle = mw.title.getCurrentTitle() |
|||
return display --probably a bad title |
|||
self.omitCat = (function() |
|||
end |
|||
local patterns = { |
|||
end |
|||
'^Template:削除依頼ログ', |
|||
'^Template:削除済みノート', |
|||
'^Template:不削除ノート' |
|||
} |
|||
for _, v in ipairs(patterns) do |
|||
if string.find(self.currentTitle.prefixedText, v) then |
|||
return true |
|||
end |
|||
end |
|||
return false |
|||
end)() |
|||
self.pageList = {} |
|||
self.pageInvalid = false |
|||
self._suppressPageError = args._suppressPageError == 'true' -- Private parameter |
|||
-- Get page type |
|||
local function makeUrlLink(page, display) |
|||
local thisTitle |
|||
display = display or 'discussion' |
|||
if self.talk then |
|||
return string.format('[%s %s]', page, display) |
|||
thisTitle = self.currentTitle.talkPageTitle.prefixedText |
|||
end |
|||
self.pageType = 'ノート' |
|||
else |
|||
local subjTitle = self.currentTitle.subjectPageTitle |
|||
thisTitle = subjTitle.prefixedText |
|||
local pageTypes = { |
|||
[0] = '記事', |
|||
[2] = '利用者ページ', |
|||
[4] = 'プロジェクトページ', |
|||
[6] = 'ファイル', |
|||
[8] = 'インターフェースページ', |
|||
[10] = 'テンプレート', |
|||
[12] = 'ヘルプページ', |
|||
[14] = 'カテゴリ', |
|||
[100] = 'ポータル', |
|||
[102] = 'プロジェクト', |
|||
[828] = 'モジュール' |
|||
} |
|||
self.pageType = pageTypes[subjTitle.namespace] |
|||
end |
|||
-- Process data paremeters (result, page, date) |
|||
local function pageTypeName(title) |
|||
self.rowData = {} |
|||
local display = mw.ustring.lower(title.subjectNsText) |
|||
self.badParameters = {} |
|||
local pageTypes = { |
|||
[''] = 'article', |
|||
['user'] = 'user page', |
|||
['wikipedia'] = 'project page', |
|||
['mediawiki'] = 'interface page', |
|||
['help'] = 'help page' |
|||
} |
|||
if pageTypes[display] then display = pageTypes[display] end |
|||
return display |
|||
end |
|||
---@param prefix "result" | "page" | "date" |
|||
local function cleanupTitle(title) |
|||
---@param num number |
|||
if not title then return title end |
|||
---@param val string |
|||
title = mw.uri.decode(title, 'PATH') |
|||
local function setRowData(prefix, num, val) |
|||
title = string.gsub(title, '|.*', '') |
|||
if not self.rowData[num] then -- Set default data |
|||
title = string.gsub(title, '[%[%]{}]', '') |
|||
self.rowData[num] = { |
|||
return title |
|||
result = '削除', |
|||
end |
|||
page = 'Wikipedia:削除依頼/' .. thisTitle, |
|||
date = nil |
|||
} |
|||
end |
|||
if prefix == 'page' then |
|||
local m = string.match(val, '^%[*%s*(.-)%s*%]*$') -- Remove leading/trailing brackets and spaces |
|||
if m then val = m end |
|||
val = 'Wikipedia:削除依頼/' .. val |
|||
end |
|||
self.rowData[num][prefix] = val -- Set the specified data |
|||
end |
|||
---@param paramKey string | number |
|||
------------------------------------------------------------------------------- |
|||
local function setBadParameter(paramKey) |
|||
-- OldAfdMulti class |
|||
self.badParameters[#self.badParameters + 1] = paramKey |
|||
------------------------------------------------------------------------------- |
|||
end |
|||
-- List of accepted parameter names; true for ordered parameters and false for unordered ones |
|||
local OldAfdMulti = {} |
|||
local acceptedPrefixes = { |
|||
OldAfdMulti.__index = OldAfdMulti |
|||
result = true, |
|||
date = true, |
|||
page = true, |
|||
talk = false, |
|||
collapse = false, |
|||
reversed = false, |
|||
_suppresspageerror = false, -- Private parameter |
|||
-- numbered = false -- Disabled parameter |
|||
} |
|||
function OldAfdMulti.new(args) |
|||
local self = setmetatable({}, OldAfdMulti) |
|||
self.currentTitle = mw.title.getCurrentTitle() |
|||
-- Preprocess the row args for easier looping. |
|||
self.rowData = {} |
|||
for k, v in pairs(args) do |
for k, v in pairs(args) do |
||
if type(k) == 'string' then |
if type(k) == 'string' then |
||
local key = mw.ustring.lower(k) |
|||
local prefix, num = k:match('^(.-)([1-9][0-9]*)$') |
|||
local prefix, num = key:match('^(.-)(%d*)$') |
|||
if prefix and acceptedPrefixes[prefix] ~= nil and num and num ~= '0' then |
|||
num = tonumber(num) |
|||
num = tonumber(num) or 1 |
|||
if acceptedPrefixes[prefix] == true then |
|||
prefix == 'date' or |
|||
setRowData(prefix, num, v) |
|||
prefix == 'page' or |
|||
elseif acceptedPrefixes[prefix] == false then |
|||
prefix == 'link' or |
|||
-- Do nothing |
|||
prefix == 'caption' or |
|||
else |
|||
prefix == 'votepage' or |
|||
setBadParameter(k) |
|||
prefix == 'merge' |
|||
end |
|||
then |
|||
else |
|||
self.rowData[num] = self.rowData[num] or {} |
|||
setBadParameter(k) |
|||
self.rowData[num][prefix] = v |
|||
end |
|||
if v and v ~= '' and prefix=='merge' then |
|||
else |
|||
self.isMerge = true |
|||
setBadParameter(k) |
|||
end |
|||
end |
|||
end |
|||
end |
end |
||
end |
end |
||
-- Set aliases for parameters ending in "1". |
|||
---Remove any gaps in the array we made. |
|||
if self.rowData[1] then |
|||
---@param t table |
|||
self.rowData[1].result = self.rowData[1].result or args.result |
|||
---@return table |
|||
self.rowData[1].date = self.rowData[1].date or args.date |
|||
self.rowData[1].page = self.rowData[1].page or args.page |
|||
self.rowData[1].votepage = self.rowData[1].votepage or args.votepage |
|||
self.rowData[1].link = self.rowData[1].link or args.link |
|||
self.rowData[1].caption = self.rowData[1].caption or args.caption |
|||
self.rowData[1].merge = self.rowData[1].merge or args.merge |
|||
elseif args.result or |
|||
args.date or |
|||
args.page or |
|||
args.votepage or |
|||
args.link or |
|||
args.caption or |
|||
args.merge |
|||
then |
|||
self.rowData[1] = { |
|||
result = args.result, |
|||
date = args.date, |
|||
page = args.page, |
|||
votepage = args.votepage, |
|||
link = args.link, |
|||
caption = args.caption, |
|||
merge = args.merge |
|||
} |
|||
end |
|||
-- Remove any gaps in the array we made. |
|||
local function compressSparseArray(t) |
local function compressSparseArray(t) |
||
local ret, nums = {}, {} |
local ret, nums = {}, {} |
||
147行目: | 138行目: | ||
end |
end |
||
self.rowData = compressSparseArray(self.rowData) |
self.rowData = compressSparseArray(self.rowData) |
||
self.hasNoData = not self.rowData[1] |
|||
-- Set aliases that apply to all of the data tables. |
|||
for i, data in ipairs(self.rowData) do |
|||
data.page = data.page or data.votepage |
|||
data.page = cleanupTitle(data.page) |
|||
data.votepage = nil |
|||
end |
|||
-- Set collapsedness |
|||
self.collapse = tonumber(args.collapse) |
|||
if not self.collapse then |
|||
self.collapse = yesno(args.collapse) |
|||
end |
|||
-- Set other properties |
|||
self.isNumbered = yesno(args.numbered) |
|||
self.isSmall = yesno(args.small) |
|||
self.pageType = args.type or pageTypeName(self.currentTitle) |
|||
if args.merge and args.merge ~= '' then |
|||
self.isMerge = true |
|||
end |
|||
self.deletion = args.deletion |
|||
return self |
return self |
||
end |
|||
function OldAfdMulti:renderResult(result) |
|||
return result or "'''Keep'''" |
|||
end |
end |
||
---Render the AfDLog instance as a message box. |
|||
function OldAfdMulti:renderDate(date) |
|||
---@return string |
|||
if date then |
|||
function AfDLog:renderBox() |
|||
return date |
|||
return require('Module:Message box').main('tmbox', { |
|||
else |
|||
type = 'notice', |
|||
self.hasMissingDate = true |
|||
image = '[[File:Clipboard.svg|35px|削除依頼]]', |
|||
return string.format( |
|||
text = self:_renderBoxText() |
|||
'<sup>%s[[%s|date missing]]%s</sup>', |
|||
}) |
|||
mw.text.nowiki('['), |
|||
TEMPLATE_PAGE, |
|||
mw.text.nowiki(']') |
|||
) |
|||
end |
|||
end |
end |
||
function AfDLog:_renderBoxText() |
|||
function OldAfdMulti:renderPageText(linkFunc, page, caption) |
|||
return string.format(', see %s.', linkFunc(page, caption)) |
|||
end |
|||
local nRows = #self.rowData |
|||
function OldAfdMulti:renderRow(result, date, link, merge) |
|||
local |
local ret = {} |
||
if nRows == 1 and self.rowData[1].date then |
|||
ret[#ret + 1] = string.format( |
|||
local mergeText = '' |
|||
'この%sは%sに[[Wikipedia:削除依頼|削除依頼]]の審議対象になりました。', |
|||
if merge and merge ~= '' then |
|||
self.pageType, |
|||
mergeText = string.format('Merge with [[:%s]]: ', merge) |
|||
self.rowData[1].date |
|||
) |
|||
else |
|||
ret[#ret + 1] = string.format( |
|||
'この%sは過去に[[Wikipedia:削除依頼|削除依頼]]の審議対象になりました。', |
|||
self.pageType |
|||
) |
|||
end |
|||
if nRows > 1 then |
|||
ret[#ret + 1] = '新しく依頼を提出する場合、以下を参考にしてください。' |
|||
ret[#ret + 1] = '\n' |
|||
ret[#ret + 1] = self:_renderMultipleRows() |
|||
elseif nRows == 1 then |
|||
ret[#ret + 1] = self:_renderFirstRow() |
|||
else -- There's no rowData |
|||
-- Do nothing |
|||
end |
end |
||
if link then |
|||
return string.format('%s%s, %s, see %s.', mergeText, result, date, link) |
|||
else |
|||
return string.format('%s%s, %s', mergeText, result, date) |
|||
end |
|||
end |
|||
local errorMessage = self:_renderErrors() |
|||
function OldAfdMulti:renderFirstRow(data) |
|||
ret[#ret + 1] = #errorMessage > 0 and table.concat(errorMessage) or '' |
|||
local link |
|||
if data.link then |
|||
return table.concat(ret) |
|||
link = makeUrlLink(data.link, data.caption) |
|||
else |
|||
local page = data.page or self.currentTitle.text |
|||
link = makeWikilink(getAfdPage(page), data.caption) |
|||
end |
|||
return self:renderRow(data.result, data.date, link, data.merge) |
|||
end |
|||
function OldAfdMulti:renderSubsequentRow(data) |
|||
local link |
|||
if data.page then |
|||
link = makeWikilink(getAfdPage(data.page), data.caption) |
|||
elseif data.link then |
|||
link = makeUrlLink(data.link, data.caption) |
|||
end |
|||
return self:renderRow(data.result, data.date, link, data.merge) |
|||
end |
end |
||
function |
function AfDLog:_renderMultipleRows() |
||
local root = mw.html.create() |
local root = mw.html.create() |
||
local nRows = #self.rowData |
local nRows = #self.rowData |
||
local i = nRows |
local i = self.reversed and nRows or 1 |
||
local nCollapsedRows |
local nCollapsedRows |
||
259行目: | 218行目: | ||
:tag('tr') |
:tag('tr') |
||
:tag('td') |
:tag('td') |
||
:tag(self. |
:tag(self.numbered and 'ol' or 'ul') |
||
end |
|||
local function renderRow(html, method, data) |
|||
html |
|||
:tag('li') |
|||
:attr('value', self.isNumbered and i or nil) |
|||
:wikitext(self[method](self, data)) |
|||
end |
end |
||
272行目: | 224行目: | ||
if hasNormalRows then |
if hasNormalRows then |
||
local normalList = makeList(false) |
local normalList = makeList(false) |
||
if self.reversed then |
|||
while i > 1 and i > nCollapsedRows do |
|||
while i >= 1 and i > nCollapsedRows do |
|||
renderRow(normalList, 'renderSubsequentRow', self.rowData[i]) |
|||
self:_renderRow(i, normalList) |
|||
i = i - 1 |
|||
i = i - 1 |
|||
end |
|||
end |
|||
if i == 1 and i > nCollapsedRows then |
|||
else |
|||
renderRow(normalList, 'renderFirstRow', self.rowData[i]) |
|||
while i <= nRows - nCollapsedRows do |
|||
i = i - 1 |
|||
self:_renderRow(i, normalList) |
|||
end |
|||
i = i + 1 |
|||
end |
|||
end |
|||
end |
end |
||
286行目: | 241行目: | ||
local header |
local header |
||
if hasNormalRows then |
if hasNormalRows then |
||
header = ' |
header = '過去の削除依頼:' |
||
elseif nRows > 1 then |
|||
header = 'Deletion discussions:' |
|||
else |
else |
||
header = ' |
header = '削除依頼:' |
||
end |
end |
||
local collapsedList = makeList(true, header) |
local collapsedList = makeList(true, header) |
||
if self.reversed then |
|||
while i > 1 do |
|||
while i >= 1 do |
|||
renderRow(collapsedList, 'renderSubsequentRow', self.rowData[i]) |
|||
self:_renderRow(i, collapsedList) |
|||
i = i - 1 |
|||
i = i - 1 |
|||
end |
|||
end |
|||
renderRow(collapsedList, 'renderFirstRow', self.rowData[i]) |
|||
else |
|||
while i <= nRows do |
|||
self:_renderRow(i, collapsedList) |
|||
i = i + 1 |
|||
end |
|||
end |
|||
end |
end |
||
return tostring(root) |
return tostring(root) |
||
end |
end |
||
function |
function AfDLog:_renderRow(rowNum, html) |
||
local data = self.rowData[rowNum] |
|||
local link = self:_addPage(data) |
|||
local caption = data.caption or 'the discussion' |
|||
local wkt |
|||
if data.date then |
|||
wkt = string.format('<b>%s</b> %s (%s)', data.result, link, data.date) |
|||
else |
|||
wkt = string.format('<b>%s</b> %s', data.result, link) |
|||
local page = data.page or self.currentTitle.text |
|||
end |
|||
if exists(getAfdPage(page)) then |
|||
html |
|||
link = makeWikilink(getAfdPage(page), caption) |
|||
:tag('li') |
|||
elseif exists(getVfdPage(page)) then |
|||
:attr('value', self.numbered and rowNum or nil) |
|||
link = makeWikilink(getVfdPage(page), caption) |
|||
:wikitext(wkt) |
|||
else |
|||
end |
|||
link = caption -- Make this an error? |
|||
end |
|||
function AfDLog:_renderFirstRow() |
|||
end |
|||
local |
local data = self.rowData[1] |
||
return string.format( |
return string.format( |
||
'%sの結果、<b>%s</b>となりました。', |
|||
'The result of %s was %s.', |
|||
self:_addPage(data, '議論'), |
|||
link, result |
|||
data.result |
|||
) |
) |
||
end |
end |
||
---Clean up pagetitle, check its existence, and set the 'pageInvalid' parameter to true if there's a problem. |
|||
function OldAfdMulti:renderBannerText() |
|||
---@param data table rowData object |
|||
---@param caption? string If nil, same as data.page |
|||
---@return string wikilink |
|||
function AfDLog:_addPage(data, caption) |
|||
local title = mw.title.new(data.page) |
|||
if title then |
|||
data.page = title.prefixedText |
|||
if self.pageList[data.page] == nil and not self._suppressPageError then |
|||
self.pageList[data.page] = title.exists |
|||
else |
|||
self.pageList[data.page] = true |
|||
end |
|||
else |
|||
self.pageList[data.page] = false |
|||
end |
|||
if not self.pageInvalid and not self.pageList[data.page] then |
|||
self.pageInvalid = true |
|||
end |
|||
if title then |
|||
return string.format( |
|||
'[[%s|%s]]', |
|||
data.page, |
|||
caption or data.page |
|||
) |
|||
else |
|||
return string.format( |
|||
'%s%s%s', |
|||
mw.text.nowiki('[['), |
|||
data.page, |
|||
mw.text.nowiki(']]') |
|||
) |
|||
end |
|||
end |
|||
function AfDLog:_renderErrors() |
|||
local ret = {} |
local ret = {} |
||
local errorMessage = '<strong class="error" style="display:block;">エラー: %s</strong>' |
|||
if self.deletion then |
|||
if self.hasNoData then |
|||
ret[#ret + 1] = string.format( |
|||
errorMessage, |
|||
'This %s was previously nominated for %s.', |
|||
'必須引数が指定されていません' |
|||
self.pageType, |
|||
) |
|||
self.deletion |
|||
end |
|||
) |
|||
if #self.badParameters > 0 then |
|||
ret[#ret + 1] = string.format( |
|||
errorMessage, |
|||
'This %s was nominated for %s on %s.', |
|||
string.format( |
|||
self.pageType, |
|||
'不正な引数が指定されています (%s)', |
|||
self.deletion, |
|||
table.concat(self.badParameters, ', ') |
|||
self.rowData[1].date |
|||
) |
|||
) |
|||
) |
|||
else |
|||
end |
|||
ret[#ret + 1] = string.format( |
|||
if self.pageInvalid then |
|||
'This %s was nominated for %s.', |
|||
ret[#ret + 1] = string.format( |
|||
self.pageType, |
|||
errorMessage, |
|||
self.deletion |
|||
'不正な削除依頼サブページ名が指定されています' |
|||
) |
|||
) |
|||
end |
|||
end |
|||
elseif self.isMerge then |
|||
-- Add cat if the caller isn't a template invoking this module |
|||
if nRows < 1 or not self.rowData[1].date then |
|||
if #ret > 0 then |
|||
ret[#ret + 1] = self:renderCat() |
|||
'This %s was considered for [[Wikipedia:Deletion policy#Merging|merging]] with %s.', |
|||
self.pageType, |
|||
self.rowData[1].merge |
|||
) |
|||
elseif nRows == 1 and self.rowData[1].date then |
|||
ret[#ret + 1] = string.format( |
|||
'This %s was considered for [[Wikipedia:Deletion policy#Merging|merging]] with [[:%s]] on %s.', |
|||
self.pageType, |
|||
self.rowData[1].merge, |
|||
self.rowData[1].date |
|||
) |
|||
else |
|||
ret[#ret + 1] = string.format( |
|||
'This %s was nominated for [[Wikipedia:Deletion policy|deletion]] or considered for [[Wikipedia:Deletion policy#Merging|merging]].', |
|||
self.pageType |
|||
) |
|||
end |
|||
else |
|||
if nRows < 1 or not self.rowData[1].date then |
|||
ret[#ret + 1] = string.format( |
|||
'This %s was previously nominated for [[Wikipedia:Deletion policy|deletion]].', |
|||
self.pageType |
|||
) |
|||
elseif nRows == 1 and self.rowData[1].date then |
|||
ret[#ret + 1] = string.format( |
|||
'This %s was nominated for [[Wikipedia:Deletion policy|deletion]] on %s.', |
|||
self.pageType, |
|||
self.rowData[1].date |
|||
) |
|||
else |
|||
ret[#ret + 1] = string.format( |
|||
'This %s was nominated for [[Wikipedia:Deletion policy|deletion]].', |
|||
self.pageType |
|||
) |
|||
end |
|||
end |
end |
||
return ret |
|||
if nRows > 1 then |
|||
ret[#ret + 1] = ' ' |
|||
if self.isSmall then |
|||
ret[#ret + 1] = 'Review prior discussions if considering re-nomination:' |
|||
else |
|||
ret[#ret + 1] = 'Please review the prior discussions if you are considering re-nomination:' |
|||
end |
|||
ret[#ret + 1] = '\n' |
|||
ret[#ret + 1] = self:renderRows() |
|||
else |
|||
ret[#ret + 1] = ' ' |
|||
ret[#ret + 1] = self:renderFirstRowOnly() |
|||
end |
|||
return table.concat(ret) |
|||
end |
end |
||
function |
function AfDLog:renderCat() |
||
return self.omitCat and '' or '[[Category:テンプレート呼び出しエラーのあるページ/Template:削除依頼ログ]]' |
|||
return mMessageBox.main('tmbox', { |
|||
small = self.isSmall, |
|||
type = 'notice', |
|||
image = '[[File:Clipboard.svg|35px|Articles for deletion]]', |
|||
smallimage = 'none', |
|||
text = self:renderBannerText() |
|||
}) |
|||
end |
end |
||
function |
function AfDLog:renderRawTemplate() |
||
local ret = {} |
|||
ret[#ret + 1] = '{{削除依頼ログ/sandbox' |
|||
if self.hasMissingDate and self.currentTitle.isTalkPage then |
|||
for i, _ in ipairs(self.rowData) do |
|||
ret[#ret + 1] = '[[Category:Old XfD multi templates with errors]]' |
|||
local data = self.rowData[i] |
|||
end |
|||
ret[#ret + 1] = '\n' |
|||
return table.concat(ret) |
|||
ret[#ret + 1] = string.format('|result%s=%s', i, data.result) |
|||
end |
|||
ret[#ret + 1] = string.format('|page%s=%s', i, string.gsub(data.page, '^Wikipedia:削除依頼/', '')) |
|||
ret[#ret + 1] = string.format('|date%s=%s', i, data.date or '') |
|||
function OldAfdMulti:__tostring() |
|||
end |
|||
return self:renderBanner() .. self:renderTrackingCategories() |
|||
ret[#ret + 1] = '\n' |
|||
ret[#ret + 1] = '|talk=' .. tostring(self.talk) |
|||
ret[#ret + 1] = '\n' |
|||
ret[#ret + 1] = '|collapse=' .. tostring(self.collapse) |
|||
-- ret[#ret + 1] = '\n' |
|||
-- ret[#ret + 1] = '|numbered=' .. tostring(self.numbered) |
|||
ret[#ret + 1] = '\n' |
|||
ret[#ret + 1] = '}}' |
|||
return table.concat(ret) |
|||
end |
end |
||
435行目: | 385行目: | ||
function p._main(args) |
function p._main(args) |
||
local afd = OldAfdMulti.new(args) |
|||
local afd = AfDLog.new(args) |
|||
return tostring(afd) |
|||
local caller = mw.getCurrentFrame():getParent():getTitle() |
|||
local callerTitle = caller:match('^Template:(削除済みノート)') or caller:match('^Template:(不削除ノート)') |
|||
if mw.isSubsting() then |
|||
return afd:renderRawTemplate() |
|||
elseif callerTitle then |
|||
local ret = {} |
|||
ret[#ret + 1] = '<strong class="error">エラー: ' |
|||
ret[#ret + 1] = '[[Help:テンプレート#テンプレートの内容で置き換える|subst:]] がありません。' |
|||
ret[#ret + 1] = string.format('「%s」ではなく「subst:%s」としてください。', callerTitle, callerTitle) |
|||
ret[#ret + 1] = '</strong>' |
|||
ret[#ret + 1] = afd:renderCat() |
|||
return table.concat(ret) |
|||
else |
|||
return afd:renderBox() |
|||
end |
|||
end |
end |
||
function p.main(frame) |
function p.main(frame) |
||
local args = require('Module:Arguments').getArgs(frame, { |
local args = require('Module:Arguments').getArgs(frame, { |
||
wrappers = |
wrappers = { |
||
'Template:削除依頼ログ' |
|||
} |
|||
}) |
}) |
||
return p._main(args) |
return p._main(args) |
2023年4月20日 (木) 03:53時点における版
これはモジュール:削除依頼ログ (差分)のモジュール・サンドボックスページです。 |
モジュールの解説[作成]
local yesno = require('Module:Yesno')
-------------------------------------------------------------------------------
-- AfDLog class
-------------------------------------------------------------------------------
local AfDLog = {}
AfDLog.__index = AfDLog
---Create a new AfDLog object instance.
---@param args table
---@return AfDLog
function AfDLog.new(args)
local self = setmetatable({}, AfDLog)
-- Set non-data parameters
self.talk = not not yesno(args.talk)
self.collapse = tonumber(args.collapse) or not not yesno(args.collapse)
self.numbered = true --yesno(args.numbered) == nil or not not yesno(args.numbered) -- Disabled parameter
self.reversed = yesno(args.reversed) == nil or not not yesno(args.reversed)
self.currentTitle = mw.title.getCurrentTitle()
self.omitCat = (function()
local patterns = {
'^Template:削除依頼ログ',
'^Template:削除済みノート',
'^Template:不削除ノート'
}
for _, v in ipairs(patterns) do
if string.find(self.currentTitle.prefixedText, v) then
return true
end
end
return false
end)()
self.pageList = {}
self.pageInvalid = false
self._suppressPageError = args._suppressPageError == 'true' -- Private parameter
-- Get page type
local thisTitle
if self.talk then
thisTitle = self.currentTitle.talkPageTitle.prefixedText
self.pageType = 'ノート'
else
local subjTitle = self.currentTitle.subjectPageTitle
thisTitle = subjTitle.prefixedText
local pageTypes = {
[0] = '記事',
[2] = '利用者ページ',
[4] = 'プロジェクトページ',
[6] = 'ファイル',
[8] = 'インターフェースページ',
[10] = 'テンプレート',
[12] = 'ヘルプページ',
[14] = 'カテゴリ',
[100] = 'ポータル',
[102] = 'プロジェクト',
[828] = 'モジュール'
}
self.pageType = pageTypes[subjTitle.namespace]
end
-- Process data paremeters (result, page, date)
self.rowData = {}
self.badParameters = {}
---@param prefix "result" | "page" | "date"
---@param num number
---@param val string
local function setRowData(prefix, num, val)
if not self.rowData[num] then -- Set default data
self.rowData[num] = {
result = '削除',
page = 'Wikipedia:削除依頼/' .. thisTitle,
date = nil
}
end
if prefix == 'page' then
local m = string.match(val, '^%[*%s*(.-)%s*%]*$') -- Remove leading/trailing brackets and spaces
if m then val = m end
val = 'Wikipedia:削除依頼/' .. val
end
self.rowData[num][prefix] = val -- Set the specified data
end
---@param paramKey string | number
local function setBadParameter(paramKey)
self.badParameters[#self.badParameters + 1] = paramKey
end
-- List of accepted parameter names; true for ordered parameters and false for unordered ones
local acceptedPrefixes = {
result = true,
date = true,
page = true,
talk = false,
collapse = false,
reversed = false,
_suppresspageerror = false, -- Private parameter
-- numbered = false -- Disabled parameter
}
for k, v in pairs(args) do
if type(k) == 'string' then
local key = mw.ustring.lower(k)
local prefix, num = key:match('^(.-)(%d*)$')
if prefix and acceptedPrefixes[prefix] ~= nil and num and num ~= '0' then
num = tonumber(num) or 1
if acceptedPrefixes[prefix] == true then
setRowData(prefix, num, v)
elseif acceptedPrefixes[prefix] == false then
-- Do nothing
else
setBadParameter(k)
end
else
setBadParameter(k)
end
else
setBadParameter(k)
end
end
---Remove any gaps in the array we made.
---@param t table
---@return table
local function compressSparseArray(t)
local ret, nums = {}, {}
for num, data in pairs(t) do
nums[#nums + 1] = num
end
table.sort(nums)
for i, num in ipairs(nums) do
ret[i] = t[num]
end
return ret
end
self.rowData = compressSparseArray(self.rowData)
self.hasNoData = not self.rowData[1]
return self
end
---Render the AfDLog instance as a message box.
---@return string
function AfDLog:renderBox()
return require('Module:Message box').main('tmbox', {
type = 'notice',
image = '[[File:Clipboard.svg|35px|削除依頼]]',
text = self:_renderBoxText()
})
end
function AfDLog:_renderBoxText()
local nRows = #self.rowData
local ret = {}
if nRows == 1 and self.rowData[1].date then
ret[#ret + 1] = string.format(
'この%sは%sに[[Wikipedia:削除依頼|削除依頼]]の審議対象になりました。',
self.pageType,
self.rowData[1].date
)
else
ret[#ret + 1] = string.format(
'この%sは過去に[[Wikipedia:削除依頼|削除依頼]]の審議対象になりました。',
self.pageType
)
end
if nRows > 1 then
ret[#ret + 1] = '新しく依頼を提出する場合、以下を参考にしてください。'
ret[#ret + 1] = '\n'
ret[#ret + 1] = self:_renderMultipleRows()
elseif nRows == 1 then
ret[#ret + 1] = self:_renderFirstRow()
else -- There's no rowData
-- Do nothing
end
local errorMessage = self:_renderErrors()
ret[#ret + 1] = #errorMessage > 0 and table.concat(errorMessage) or ''
return table.concat(ret)
end
function AfDLog:_renderMultipleRows()
local root = mw.html.create()
local nRows = #self.rowData
local i = self.reversed and nRows or 1
local nCollapsedRows
if type(self.collapse) == 'number' then
nCollapsedRows = self.collapse
elseif self.collapse then
nCollapsedRows = nRows
else
nCollapsedRows = 0
end
local hasNormalRows = nRows - nCollapsedRows > 0
local function makeList(isCollapsed, header)
local tableRoot = root:tag('table')
tableRoot
:addClass(isCollapsed and 'mw-collapsible mw-collapsed' or nil)
:css('width', '100%')
:css('background-color', '#f8eaba')
if header then
tableRoot
:tag('tr')
:tag('th')
:wikitext(header)
end
return tableRoot
:tag('tr')
:tag('td')
:tag(self.numbered and 'ol' or 'ul')
end
-- Render normal rows
if hasNormalRows then
local normalList = makeList(false)
if self.reversed then
while i >= 1 and i > nCollapsedRows do
self:_renderRow(i, normalList)
i = i - 1
end
else
while i <= nRows - nCollapsedRows do
self:_renderRow(i, normalList)
i = i + 1
end
end
end
-- Render collapsed rows
if nCollapsedRows > 0 then
local header
if hasNormalRows then
header = '過去の削除依頼:'
else
header = '削除依頼:'
end
local collapsedList = makeList(true, header)
if self.reversed then
while i >= 1 do
self:_renderRow(i, collapsedList)
i = i - 1
end
else
while i <= nRows do
self:_renderRow(i, collapsedList)
i = i + 1
end
end
end
return tostring(root)
end
function AfDLog:_renderRow(rowNum, html)
local data = self.rowData[rowNum]
local link = self:_addPage(data)
local wkt
if data.date then
wkt = string.format('<b>%s</b> %s (%s)', data.result, link, data.date)
else
wkt = string.format('<b>%s</b> %s', data.result, link)
end
html
:tag('li')
:attr('value', self.numbered and rowNum or nil)
:wikitext(wkt)
end
function AfDLog:_renderFirstRow()
local data = self.rowData[1]
return string.format(
'%sの結果、<b>%s</b>となりました。',
self:_addPage(data, '議論'),
data.result
)
end
---Clean up pagetitle, check its existence, and set the 'pageInvalid' parameter to true if there's a problem.
---@param data table rowData object
---@param caption? string If nil, same as data.page
---@return string wikilink
function AfDLog:_addPage(data, caption)
local title = mw.title.new(data.page)
if title then
data.page = title.prefixedText
if self.pageList[data.page] == nil and not self._suppressPageError then
self.pageList[data.page] = title.exists
else
self.pageList[data.page] = true
end
else
self.pageList[data.page] = false
end
if not self.pageInvalid and not self.pageList[data.page] then
self.pageInvalid = true
end
if title then
return string.format(
'[[%s|%s]]',
data.page,
caption or data.page
)
else
return string.format(
'%s%s%s',
mw.text.nowiki('[['),
data.page,
mw.text.nowiki(']]')
)
end
end
function AfDLog:_renderErrors()
local ret = {}
local errorMessage = '<strong class="error" style="display:block;">エラー: %s</strong>'
if self.hasNoData then
ret[#ret + 1] = string.format(
errorMessage,
'必須引数が指定されていません'
)
end
if #self.badParameters > 0 then
ret[#ret + 1] = string.format(
errorMessage,
string.format(
'不正な引数が指定されています (%s)',
table.concat(self.badParameters, ', ')
)
)
end
if self.pageInvalid then
ret[#ret + 1] = string.format(
errorMessage,
'不正な削除依頼サブページ名が指定されています'
)
end
-- Add cat if the caller isn't a template invoking this module
if #ret > 0 then
ret[#ret + 1] = self:renderCat()
end
return ret
end
function AfDLog:renderCat()
return self.omitCat and '' or '[[Category:テンプレート呼び出しエラーのあるページ/Template:削除依頼ログ]]'
end
function AfDLog:renderRawTemplate()
local ret = {}
ret[#ret + 1] = '{{削除依頼ログ/sandbox'
for i, _ in ipairs(self.rowData) do
local data = self.rowData[i]
ret[#ret + 1] = '\n'
ret[#ret + 1] = string.format('|result%s=%s', i, data.result)
ret[#ret + 1] = string.format('|page%s=%s', i, string.gsub(data.page, '^Wikipedia:削除依頼/', ''))
ret[#ret + 1] = string.format('|date%s=%s', i, data.date or '')
end
ret[#ret + 1] = '\n'
ret[#ret + 1] = '|talk=' .. tostring(self.talk)
ret[#ret + 1] = '\n'
ret[#ret + 1] = '|collapse=' .. tostring(self.collapse)
-- ret[#ret + 1] = '\n'
-- ret[#ret + 1] = '|numbered=' .. tostring(self.numbered)
ret[#ret + 1] = '\n'
ret[#ret + 1] = '}}'
return table.concat(ret)
end
-------------------------------------------------------------------------------
-- Exports
-------------------------------------------------------------------------------
local p = {}
function p._main(args)
local afd = AfDLog.new(args)
local caller = mw.getCurrentFrame():getParent():getTitle()
local callerTitle = caller:match('^Template:(削除済みノート)') or caller:match('^Template:(不削除ノート)')
if mw.isSubsting() then
return afd:renderRawTemplate()
elseif callerTitle then
local ret = {}
ret[#ret + 1] = '<strong class="error">エラー: '
ret[#ret + 1] = '[[Help:テンプレート#テンプレートの内容で置き換える|subst:]] がありません。'
ret[#ret + 1] = string.format('「%s」ではなく「subst:%s」としてください。', callerTitle, callerTitle)
ret[#ret + 1] = '</strong>'
ret[#ret + 1] = afd:renderCat()
return table.concat(ret)
else
return afd:renderBox()
end
end
function p.main(frame)
local args = require('Module:Arguments').getArgs(frame, {
wrappers = {
'Template:削除依頼ログ'
}
})
return p._main(args)
end
return p