Moduł:Dopracować
Wygląd
Moduł wykorzystywany przez {{Dopracować}}.
local resources = mw.loadData( 'Moduł:Dopracować/resources' )
return {
test = function(frame)
local pf = frame:getParent()
-- scan reasons without dates
local sectionParamIndex = resources.data.sectionParamIndex
local sectionParamValue = resources.data.sectionParamValue
for i = 1, 10 do
local object = pf:getArgument(i)
if object then
local reason = object:expand()
if reason and ((i ~= sectionParamIndex) or (reason ~= sectionParamValue)) then
if #mw.text.trim(reason) > 0 then
return "true"
end
end
end
end
-- scan well known reasons with optional date
for _, v in ipairs(resources.reasons) do
for _, k in ipairs(v.aliases) do
local object = pf:getArgument(k)
if object then
local currentDate = object:expand()
if currentDate then
return "true"
end
end
end
end
return nil
end,
icon = function(frame)
local pf = frame:getParent()
local onlyReason = nil
local mapParamToReason = {}
for i, v in ipairs(resources.reasons) do
for _, a in ipairs(v.aliases) do
mapParamToReason[a] = i
end
end
-- scan well known reasons with date
for k, i in pairs(mapParamToReason) do
local object = pf:getArgument(k)
if object then
local currentDate = object:expand()
if currentDate then
if not onlyReason then
onlyReason = i
elseif onlyReason ~= i then
onlyReason = 0
break
end
end
end
end
-- scan reasons without dates
if not onlyReason then
for i = 1, 10 do
local object = pf:getArgument(i)
if object then
local reason = object:expand()
if reason and ((i ~= resources.data.sectionParamIndex) or (reason ~= resources.data.sectionParamValue)) then
if mw.text.trim(reason) ~= "" then
local known = mapParamToReason[reason]
if known then
if not onlyReason then
onlyReason = known
elseif onlyReason ~= known then
onlyReason = 0
break
end
end
end
end
end
end
end
local icon = nil
if onlyReason then
local reasonData = resources.reasons[onlyReason]
if reasonData then
icon = reasonData.icon
end
end
return icon or resources.default.icon
end,
text = function(frame)
local title = mw.title.getCurrentTitle()
local pf = frame:getParent()
local defaultDate = pf.args[resources.data.defaultDateIndex] or ""
local complexCategories = {
categories = function(result, date)
local switch = title.nsText
-- if (switch == "Pomoc") or (switch == "Wikinews") or (switch == "Portal") or (switch == "Wikiproject") then
-- table.insert(result, "[[Kategoria:Wikinews?|"..title.text.."]]")
-- else
if switch == "Kategoria" then
table.insert(result, "[[Kategoria:?|"..title.text.."]]")
elseif title.namespace == 0 then
table.insert(result, "[[Kategoria:?]]")
end
end,
}
function prepareTemplates(reason, date, section, comments)
local c = reason["?"] or {}
local sourceTemplates = section and resources.sectionTemplates or ((title.namespace == 0) and resources.articleTemplates or resources.otherTemplates)
local templates = {}
for k, v in pairs(sourceTemplates) do
templates[k] = v
end
templates[resources.data.dateTemplate] = date or ""
if title.talkPageTitle then
templates[resources.data.talkNameTemplate] = title.talkPageTitle.fullText
end
templates["{{FULLPAGENAMEE}}"] = mw.uri.encode(title.fullText, "WIKI")
templates["{{PAGENAMEE}}"] = mw.uri.encode(title.text, "WIKI")
templates["{{BASEPAGENAMEE}}"] = mw.uri.encode(title.baseText, "WIKI")
templates["{{SUBPAGENAMEE}}"] = mw.uri.encode(title.subpageText, "WIKI")
templates["{{FULLPAGENAME}}"] = title.fullText
templates["{{PAGENAME}}"] = title.text
templates["{{BASEPAGENAME}}"] = title.baseText
templates["{{SUBPAGENAME}}"] = title.subpageText
templates[resources.data.pageUrlTemplate] = title:fullUrl()
templates[resources.data.talkSectionTemplate] = pf.args[resources.data.talkPageSectionParamIndex] or ""
local defaultComment, _ = string.gsub(c.default or "", resources.data.templatePattern, templates)
templates[resources.data.infoTemplate] = #comments > 0 and ((c.prefix or "")..table.concat(comments, c.separator or ", ")..(c.suffix or "")) or defaultComment
-- prepare "talk" hint
local talk = ""
if title.talkPageTitle and title.talkPageTitle.exists then
talk, _ = string.gsub(reason.talk or "", resources.data.templatePattern, templates)
end
templates[resources.data.talkTemplate] = talk
return templates
end
local mapParamToReason = {}
for i, v in ipairs(resources.reasons) do
for _, a in ipairs(v.aliases) do
mapParamToReason[a] = i
end
end
local reasonDates = {}
-- scan well known reasons with date
for k, i in pairs(mapParamToReason) do
local object = pf:getArgument(k)
if object then
local currentDate = object:expand()
if currentDate then
local previousDate = reasonDates[i]
if not previousDate or (previousDate == "") or ((currentDate ~= "") and (currentDate < previousDate)) then
reasonDates[i] = currentDate
end
end
end
end
local onlyReason = nil
local allMessages = {}
local customMessages = {}
-- collect messages with dates
for i, v in ipairs(resources.reasons) do
local date = reasonDates[i]
if date then
table.insert(allMessages, (#date > 0) and string.format(resources.data.messageWithDatePrefix, date, v.message) or v.message)
if not onlyReason then
onlyReason = i
else
onlyReason = 0
end
end
end
-- scan reasons without dates
local scanReasonWithoutDate = function(index)
local object = pf:getArgument(index)
if object then
local reason = object:expand()
if reason then
reason = mw.text.trim(reason)
if (index ~= resources.data.sectionParamIndex) or (reason ~= resources.data.sectionParamValue) then
if #reason > 0 then
local known = mapParamToReason[reason]
if not known then
table.insert(allMessages, reason)
table.insert(customMessages, reason)
else
if not onlyReason then
onlyReason = known
elseif onlyReason ~= known then
onlyReason = 0
end
if not reasonDates[known] then
reasonDates[known] = defaultDate
local v = resources.reasons[known].message
table.insert(allMessages, (#defaultDate > 0) and string.format(resources.data.messageWithDatePrefix, defaultDate, v) or v)
end
end
end
end
end
return reason
end
end
local sectionParam = scanReasonWithoutDate(resources.data.sectionParamValue)
for i = 1, 10 do
scanReasonWithoutDate(i)
end
-- collect categories
local categories = {}
for i, v in ipairs(resources.reasons) do
local date = reasonDates[i]
if date and v.cat then
local complexCat = complexCategories[v.cat]
if complexCat then
complexCat(categories, date)
elseif title.namespace == 0 then
table.insert(categories, (v.catDate and (#date > 0)) and string.format(v.catDate, date) or v.cat)
end
end
end
if (#categories == 0) and (title.namespace == 0) then
table.insert(categories, resources.data.defaultCategory)
end
-- prepare date hints
local hints = {}
local currentDate = nil
for i, v in ipairs(resources.reasons) do
local date = reasonDates[i]
if date and (#date == 0) and v.catDate then
if not currentDate then
currentDate = mw.getContentLanguage():formatDate(resources.data.dateFormat, nil, true)
end
table.insert(hints, string.format(resources.data.dateExample, v.aliases[1], currentDate))
end
end
local result = {}
local section = (pf.args[resources.data.sectionParamIndex] == resources.data.sectionParamValue) or sectionParam
local onlyReasonData = resources.reasons[onlyReason]
local mainText = nil
local templates = {}
if onlyReasonData and onlyReasonData.simple and ((#customMessages == 0) or onlyReasonData["?"]) then
local date = reasonDates[onlyReason]
if #date > 0 then
date = string.format(resources.data.dateInfix, date)
end
templates = prepareTemplates(onlyReasonData, date, section, customMessages)
mainText, _ = string.gsub(onlyReasonData.simple, resources.data.templatePattern, templates)
end
if not mainText and not onlyReason then
local date = pf.args[resources.data.defaultDateIndex] or ""
if #date > 0 then
date = string.format(resources.data.dateInfix, date)
end
templates = prepareTemplates(resources.default, date, section, customMessages)
mainText, _ = string.gsub(resources.default.simple, resources.data.templatePattern, templates)
end
if not mainText then
templates = prepareTemplates(resources.default, "", section, allMessages)
mainText, _ = string.gsub(resources.default.message, resources.data.templatePattern, templates)
end
table.insert(result, mainText)
table.insert(result, table.concat(categories,""))
if title.talkPageTitle and title.talkPageTitle.exists and ((#templates[resources.data.talkTemplate] == 0) or (#templates[resources.data.talkSectionTemplate] > 0)) then
local talkPageMessage, _ = string.gsub((#templates[resources.data.talkSectionTemplate] == 0) and resources.data.talkPage or resources.data.talkSection, resources.data.templatePattern, templates)
table.insert(result, talkPageMessage)
end
local articleInstruction, _ = string.gsub(resources.data.articleInstruction, resources.data.templatePattern, templates)
table.insert(result, articleInstruction)
if #hints > 0 then
table.insert(result, string.format(resources.data.templateInstruction, table.concat(hints, ", ")))
end
local result = table.concat(result, "")
for i, v in ipairs(resources.compact or {}) do
local r, c = mw.ustring.gsub(result, v.pattern, v.replace)
if c > 0 then
result = r
break
end
end
return result
end,
}