「モジュール:削除依頼ログ/sandbox」の版間の差分
表示
削除された内容 追加された内容
編集の要約なし |
fullpage引数を追加 |
||
66行目: | 66行目: | ||
self.badParameters = {} |
self.badParameters = {} |
||
---@param prefix "result" | "page" | "date" |
---@param prefix "result" | "page" | "fullpage" | "date" |
||
---@param num number |
---@param num number |
||
---@param val string |
---@param val string |
||
74行目: | 74行目: | ||
result = '削除', |
result = '削除', |
||
page = 'Wikipedia:削除依頼/' .. thisTitle, |
page = 'Wikipedia:削除依頼/' .. thisTitle, |
||
fullpage = nil, |
|||
date = nil |
date = nil |
||
} |
} |
||
end |
end |
||
if prefix == 'page' then |
if prefix == 'page' or prefix == 'fullpage' then |
||
local m = string.match(val, '^%[*%s*(.-)%s*%]*$') -- Remove leading/trailing brackets and spaces |
local m = string.match(val, '^%[*%s*(.-)%s*%]*$') -- Remove leading/trailing brackets and spaces |
||
if m then |
if m then |
||
val = m |
|||
end |
|||
if prefix == 'page' then |
|||
val = 'Wikipedia:削除依頼/' .. val |
|||
end |
|||
end |
end |
||
self.rowData[num][prefix] = val -- Set the specified data |
self.rowData[num][prefix] = val -- Set the specified data |
||
95行目: | 100行目: | ||
date = true, |
date = true, |
||
page = true, |
page = true, |
||
fullpage = true, |
|||
talk = false, |
talk = false, |
||
collapse = false, |
collapse = false, |
||
280行目: | 286行目: | ||
function AfDLog:_renderFirstRow() |
function AfDLog:_renderFirstRow() |
||
local data = self.rowData[1] |
local data = self.rowData[1] |
||
if data.fullpage then |
|||
return string.format( |
|||
⚫ | |||
'詳細は%sをご覧ください。', |
|||
⚫ | |||
data |
self:_addPage(data) |
||
) |
|||
) |
|||
else |
|||
return string.format( |
|||
⚫ | |||
⚫ | |||
data.result |
|||
) |
|||
end |
|||
end |
end |
||
292行目: | 305行目: | ||
---@return string wikilink |
---@return string wikilink |
||
function AfDLog:_addPage(data, caption) |
function AfDLog:_addPage(data, caption) |
||
local key = data.fullpage and 'fullpage' or 'page' -- Override 'page' by 'fullpage' if the latter is specified |
|||
local title = mw.title.new(data |
local title = mw.title.new(data[key]) |
||
if title then |
if title then |
||
data |
data[key] = title.prefixedText |
||
if self.pageList[data |
if self.pageList[data[key]] == nil and not self._suppressPageError then |
||
self.pageList[data |
self.pageList[data[key]] = title.exists |
||
else |
else |
||
self.pageList[data |
self.pageList[data[key]] = true |
||
end |
end |
||
else |
else |
||
self.pageList[data |
self.pageList[data[key]] = false |
||
end |
end |
||
if not self.pageInvalid and not self.pageList[data |
if not self.pageInvalid and not self.pageList[data[key]] then |
||
self.pageInvalid = true |
self.pageInvalid = true |
||
end |
end |
||
if title then |
if title then |
||
return string.format( |
return string.format( |
||
'[[%s|%s]]', |
'[[:%s|%s]]', |
||
data |
data[key], |
||
caption or data |
caption or data[key] |
||
) |
) |
||
else |
else |
||
316行目: | 330行目: | ||
'%s%s%s', |
'%s%s%s', |
||
mw.text.nowiki('[['), |
mw.text.nowiki('[['), |
||
data |
data[key], |
||
mw.text.nowiki(']]') |
mw.text.nowiki(']]') |
||
) |
) |
||
343行目: | 357行目: | ||
ret[#ret + 1] = string.format( |
ret[#ret + 1] = string.format( |
||
errorMessage, |
errorMessage, |
||
'不正な |
'不正なページ名が指定されています' |
||
) |
) |
||
end |
end |
2023年7月28日 (金) 06:03時点における版
これはモジュール:削除依頼ログ (差分)のモジュール・サンドボックスページです。 |
モジュールの解説[作成]
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" | "fullpage" | "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,
fullpage = nil,
date = nil
}
end
if prefix == 'page' or prefix == 'fullpage' then
local m = string.match(val, '^%[*%s*(.-)%s*%]*$') -- Remove leading/trailing brackets and spaces
if m then
val = m
end
if prefix == 'page' then
val = 'Wikipedia:削除依頼/' .. val
end
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,
fullpage = 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]
if data.fullpage then
return string.format(
'詳細は%sをご覧ください。',
self:_addPage(data)
)
else
return string.format(
'%sの結果、<b>%s</b>となりました。',
self:_addPage(data, '議論'),
data.result
)
end
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 key = data.fullpage and 'fullpage' or 'page' -- Override 'page' by 'fullpage' if the latter is specified
local title = mw.title.new(data[key])
if title then
data[key] = title.prefixedText
if self.pageList[data[key]] == nil and not self._suppressPageError then
self.pageList[data[key]] = title.exists
else
self.pageList[data[key]] = true
end
else
self.pageList[data[key]] = false
end
if not self.pageInvalid and not self.pageList[data[key]] then
self.pageInvalid = true
end
if title then
return string.format(
'[[:%s|%s]]',
data[key],
caption or data[key]
)
else
return string.format(
'%s%s%s',
mw.text.nowiki('[['),
data[key],
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