Moduł:Dopracować

 Dokumentacja modułu[zobacz] [edytuj] [historia] [odśwież]

Moduł techniczny wspierający szablon {{dopracować}}. Wszelkie instrukcje użycia są przedstawione na stronie dokumentacji szablonu.

Funkcja do sprawdzania czy szablon {{dopracować}} jest wywołany z parametrami określającymi powody jego umieszczenia. Jeśli żadnych powodów nie podano to zwraca pusty tekst.

Funkcja do generowania treści wyświetlanej przez szablon {{dopracować}}.
local resources = mw.loadData( 'Moduł:Dopracować/resources' )--[[Spr. czy tekst zawiera potwierdzenie.Uwaga! Zakłada, że ew. trim już wykonano.]]function checkYes(str)if #str > 3 thenreturn falseendlocal strings = {"1", "y", "yes", "t", "tak"}for i = 1, #strings doif str == strings[i] thenreturn trueendendreturn falseend--[[Główne funkcje:test -- do sprawdzania czy szablon {{dopracować}} jest wywołany z parametrami określającymi powody jego umieszczeniatext -- do generowania treści ramki (interepretacja parametrów)]]return {test = function(frame)local pf = frame:getParent()-- scan reasons without dateslocal sectionParamIndex = resources.data.sectionParamIndexlocal sectionParamValue = resources.data.sectionParamValuefor i = 1, 10 dolocal object = pf:getArgument(i)if object thenlocal reason = object:expand()if reason and ((i ~= sectionParamIndex) or (reason ~= sectionParamValue)) thenif #mw.text.trim(reason) > 0 thenreturn "true"endendendend-- scan well known reasons with optional datefor _, v in ipairs(resources.reasons) dofor _, k in ipairs(v.aliases) dolocal object = pf:getArgument(k)if object thenlocal currentDate = object:expand()if currentDate thenreturn "true"endendendendreturn nilend,icon = function(frame)local pf = frame:getParent() local onlyReason = nil local mapParamToReason = {}for i, v in ipairs(resources.reasons) dofor _, a in ipairs(v.aliases) domapParamToReason[a] = iendend -- scan well known reasons with datefor k, i in pairs(mapParamToReason) dolocal object = pf:getArgument(k)if object thenlocal currentDate = object:expand()if currentDate thenif not onlyReason thenonlyReason = ielseif onlyReason ~= i thenonlyReason = 0breakendendendend -- scan reasons without datesif not onlyReason thenfor i = 1, 10 dolocal object = pf:getArgument(i)if object thenlocal reason = object:expand()if reason and ((i ~= resources.data.sectionParamIndex) or (reason ~= resources.data.sectionParamValue)) thenif mw.text.trim(reason) ~= "" thenlocal known = mapParamToReason[reason]if known thenif not onlyReason thenonlyReason = knownelseif onlyReason ~= known thenonlyReason = 0breakendendendendendendend local icon = nilif onlyReason thenlocal reasonData = resources.reasons[onlyReason]if reasonData thenicon = reasonData.iconendend return icon or resources.default.iconend,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 == "Wiki") or (switch == "Portal") or (switch == "Wikiproject") then--table.insert(result, "[[Kategoria:WP?|"..title.text.."]]")--elseif switch == "Kategoria" thentable.insert(result, "[[Kategoria:?|"..title.text.."]]")elseif title.namespace == 0 thentable.insert(result, "[[Kategoria:?]]")endend,}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) dotemplates[k] = vendtemplates[resources.data.dateTemplate] = date or ""if title.talkPageTitle thentemplates[resources.data.talkNameTemplate] = title.talkPageTitle.fullTextendtemplates["{{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.fullTexttemplates["{{PAGENAME}}"] = title.texttemplates["{{BASEPAGENAME}}"] = title.baseTexttemplates["{{SUBPAGENAME}}"] = title.subpageTexttemplates[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" hintlocal talk = ""if title.talkPageTitle and title.talkPageTitle.exists thentalk, _ = string.gsub(reason.talk or "", resources.data.templatePattern, templates)endtemplates[resources.data.talkTemplate] = talkreturn templatesendlocal mapParamToReason = {}for i, v in ipairs(resources.reasons) dofor _, a in ipairs(v.aliases) domapParamToReason[a] = iendendlocal reasonDates = {}-- scan well known reasons with datefor k, i in pairs(mapParamToReason) dolocal object = pf:getArgument(k)if object thenlocal currentDate = object:expand()if currentDate thenlocal previousDate = reasonDates[i]if not previousDate or (previousDate == "") or ((currentDate ~= "") and (currentDate < previousDate)) thenreasonDates[i] = currentDateendendendendlocal onlyReason = nillocal allMessages = {}local customMessages = {}local allGrowthItems = {}-- collect messages with datesfor i, v in ipairs(resources.reasons) dolocal date = reasonDates[i]if date thentable.insert(allMessages, (#date > 0) and string.format(resources.data.messageWithDatePrefix, date, v.message) or v.message)for _, g in ipairs(v.growth or {}) doallGrowthItems[g] = trueendif not onlyReason thenonlyReason = ielseonlyReason = 0endendend-- scan reasons without dateslocal scanReasonWithoutDate = function(index)local object = pf:getArgument(index)if object thenlocal reason = object:expand()if reason thenreason = mw.text.trim(reason)-- support for section=1/y/tif index == resources.data.sectionParamValue thenif checkYes(reason) thenreturn ""endendif (index ~= resources.data.sectionParamIndex) or (reason ~= resources.data.sectionParamValue) thenif #reason > 0 thenlocal known = mapParamToReason[reason]if not known thentable.insert(allMessages, reason)table.insert(customMessages, reason)elsefor _, g in ipairs(resources.reasons[known].growth or {}) doallGrowthItems[g] = trueendif not onlyReason thenonlyReason = knownelseif onlyReason ~= known thenonlyReason = 0endif not reasonDates[known] thenreasonDates[known] = defaultDatelocal v = resources.reasons[known].messagetable.insert(allMessages, (#defaultDate > 0) and string.format(resources.data.messageWithDatePrefix, defaultDate, v) or v)endendendendendreturn reasonendendlocal sectionParam = scanReasonWithoutDate(resources.data.sectionParamValue)for i = 1, 10 doscanReasonWithoutDate(i)end-- transclude "growth" templatesif title.namespace == 0 thenfor g, _ in pairs(allGrowthItems) dolocal sitelink = mw.wikibase.getSitelink(g)if sitelink thenpcall(frame.expandTemplate, frame, {title = sitelink, args = {}})endendend-- collect categorieslocal categories = {}for i, v in ipairs(resources.reasons) dolocal date = reasonDates[i]if date and v.cat thenlocal complexCat = complexCategories[v.cat]if complexCat thencomplexCat(categories, date)elseif title.namespace == 0 thentable.insert(categories, (v.catDate and (#date > 0)) and string.format(v.catDate, date) or v.cat)endendend if (#categories == 0) and (title.namespace == 0) thentable.insert(categories, resources.data.defaultCategory)end-- prepare date hintslocal hints = {}local currentDate = nilfor i, v in ipairs(resources.reasons) dolocal date = reasonDates[i]if date and (#date == 0) and v.catDate thenif not currentDate thencurrentDate = mw.getContentLanguage():formatDate(resources.data.dateFormat, nil, true)endtable.insert(hints, string.format(resources.data.dateExample, v.aliases[1], currentDate))endend local result = {}local section = (pf.args[resources.data.sectionParamIndex] == resources.data.sectionParamValue) or sectionParamlocal onlyReasonData = resources.reasons[onlyReason]local mainText = nillocal templates = {}if onlyReasonData and onlyReasonData.simple and ((#customMessages == 0) or onlyReasonData["?"]) thenlocal date = reasonDates[onlyReason]if #date > 0 thendate = string.format(resources.data.dateInfix, date)endtemplates = prepareTemplates(onlyReasonData, date, section, customMessages)mainText, _ = string.gsub(onlyReasonData.simple, resources.data.templatePattern, templates)if onlyReasonData.processTemplates then-- Transclude templates in the main text only if neededmainText = frame:preprocess(mainText)endendif not mainText and not onlyReason thenlocal date = pf.args[resources.data.defaultDateIndex] or ""if #date > 0 thendate = string.format(resources.data.dateInfix, date)endtemplates = prepareTemplates(resources.default, date, section, customMessages)mainText, _ = string.gsub(resources.default.simple, resources.data.templatePattern, templates)endif not mainText thentemplates = prepareTemplates(resources.default, "", section, allMessages)mainText, _ = string.gsub(resources.default.message, resources.data.templatePattern, templates) endtable.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, ", ")))endlocal result = table.concat(result, "")for i, v in ipairs(resources.compact or {}) dolocal r, c = mw.ustring.gsub(result, v.pattern, v.replace)if c > 0 thenresult = rbreakendendreturn resultend,}