Modul:Multiple issues

Dokumentaciju za ovaj modul možete napraviti na stranici Modul:Multiple issues/dok

-- This powers {{multiple issues}}.

p = {}

-- Constants
local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs
local lang = mw.language.getContentLanguage()

function generateOutput(frame, data)
	local text = [=[<table class="mw-collapsible%s" style="width:95%%; background:transparent;">
<tr><th style="text-align:left; padding:0.2em 2px 0.2em 0;">This %s has multiple issues. <span style="font-weight: normal;">Please help '''[%s improve it]''' or discuss these issues on the '''[[%s|talk page]]'''.</span></th></tr>
<tr><td>%s</td></tr></table>]=]
	text = mw.ustring.format(text, data.collapsed, data.section, data.editlink, data.talkpage, data.contents)
	local args = {
		name = 'Multiple issues',
		doc = 'no',
		subst = '<includeonly>{{subst:</includeonly><includeonly>substcheck}}</includeonly>',
		class = 'ambox-multiple_issues compact-ambox',
		type = 'content',
		text = text
	}
	return frame:expandTemplate{title = 'Ambox', args = args}
end

function processDepreciated(frame, tableargs, noticetype, demospace)
	-- Generates the templates for args provided using the depreciated
	-- {{multiple issues|unreferenced=January 2013|advert=March 2012}} method

	local depreciated = {} -- {templatename: date} for all matched depreciated templates
	local outputTable = {} -- List of the final templates, in wikicode form
	local depreciatedTemplates = { -- {param: template} List of all possible parameters to convert to templates
		['abbreviations'] = 'Abbreviations';
		['advert'] = 'Advert';
		['autobiography'] = 'Autobiography';
		['BLP IMDb refimprove'] = 'BLP IMDb refimprove';
		['BLP IMDB refimprove'] = 'BLP IMDb refimprove';
		['BLP IMDb-only refimprove'] = 'BLP IMDb-only refimprove';
		['BLP IMDB-only refimprove'] = 'BLP IMDb-only refimprove';
		['BLP sources'] = 'BLP sources';
		['BLP unsourced'] = 'BLP unsourced';
		['BLPsources'] = 'BLP sources';
		['BLPunsourced'] = 'BLP unsourced';
		['citation style'] = 'Citation style';
		['citation-style'] = 'Citation style';
		['citationstyle'] = 'Citation style';
		['cite check'] = 'Cite check';
		['citecheck'] = 'Cite check';
		['cleanup-laundry'] = 'Cleanup-laundry';
		['cleanup-link rot'] = 'Cleanup-link rot';
		['cleanup-reorganize'] = 'Cleanup-reorganize';
		['cleanup-rewrite'] = 'Cleanup-rewrite';
		['cleanup-spam'] = 'Cleanup-spam';
		['cleanup'] = 'Cleanup';
		['coi'] = 'COI';
		['COI'] = 'COI';
		['colloquial'] = 'Colloquial';
		['confusing'] = 'Confusing';
		['context'] = 'Context';
		['contradict'] = 'Contradict';
		['copy edit'] = 'Copy edit';
		['copyedit'] = 'Copy edit';
		['criticism section'] = 'Criticism section';
		['criticisms'] = 'Criticism section';
		['crystal'] = 'Crystal';
		['dead end'] = 'Dead end';
		['deadend'] = 'Dead end';
		['disputed'] = 'Disputed';
		['essay-like'] = 'Essay-like';
		['essay'] = 'Essay-like';
		['example farm'] = 'Example farm';
		['examplefarm'] = 'Example farm';
		['external links'] = 'External links';
		['fancruft'] = 'Overly detailed';
		['fanpov'] = 'Fanpov';
		['fansite'] = 'Fanpov';
		['fiction'] = 'Fiction';
		['game guide'] = 'Game guide';
		['gameguide'] = 'Game guide';
		['globalize'] = 'Globalize';
		['histinfo'] = 'Historical information needed';
		['hoax'] = 'Hoax';
		['howto'] = 'Howto';
		['in-universe'] = 'In-universe';
		['inappropriate person'] = 'Inappropriate person';
		['inappropriate tone'] = 'Tone';
		['incomplete'] = 'Incomplete';
		['intro length'] = 'Lead too long';
		['intro-toolong'] = 'Lead too long';
		['intro-tooshort'] = 'Lead too short';
		['intromissing'] = 'Lead missing';
		['introrewrite'] = 'Lead rewrite';
		['jargon'] = 'Technical';
		['laundry'] = 'Cleanup-laundry';
		['laundrylists'] = 'Cleanup-laundry';
		['lead missing'] = 'Lead missing';
		['lead rewrite'] = 'Lead rewrite';
		['lead too long'] = 'Lead too long';
		['lead too short'] = 'Lead too short';
		['like resume'] = 'Like resume';
		['likeresume'] = 'Like resume';
		['linkrot'] = 'Cleanup-link rot';
		['long'] = 'Very long';
		['more footnotes'] = 'More footnotes';
		['news release'] = 'News release';
		['newsrelease'] = 'News release';
		['no footnotes'] = 'No footnotes';
		['notability'] = 'Notability';
		['notable'] = 'Notability';
		['npov'] = 'POV';
		['NPOV'] = 'POV';
		['one source'] = 'One source';
		['onesource'] = 'One source';
		['organize'] = 'Cleanup-reorganize';
		['original research'] = 'Original research';
		['orphan'] = 'Orphan';
		['out of date'] = 'Update';
		['over-quotation'] = 'Over-quotation';
		['overly detailed'] = 'Overly detailed';
		['peacock'] = 'Peacock';
		['plot'] = 'Plot';
		['pov-check'] = 'POV-check';
		['POV-check'] = 'POV-check';
		['pov'] = 'POV';
		['POV'] = 'POV';
		['primary sources'] = 'Primary sources';
		['primarysources'] = 'Primary sources';
		['prose'] = 'Prose';
		['quote farm'] = 'Over-quotation';
		['recentism'] = 'Recentism';
		['refimprove'] = 'Refimprove';
		['reorganisation'] = 'Cleanup-reorganize';
		['restructure'] = 'Cleanup-reorganize';
		['review'] = 'Review';
		['rewrite'] = 'Cleanup-rewrite';
		['sections'] = 'Sections';
		['self-published'] = 'Self-published';
		['spam'] = 'Cleanup-spam';
		['story'] = 'Story';
		['synthesis'] = 'Synthesis';
		['technical'] = 'Technical';
		['tone'] = 'Tone';
		['travel guide']  = 'Travel guide';
		['travelguide'] = 'Travel guide';
		['trivia'] = 'Trivia';
		['unbalanced'] = 'Unbalanced';
		['unref'] = 'Unreferenced';
		['unreferenced'] = 'Unreferenced';
		['unreliable sources'] = 'Unreliable sources';
		['unreliable'] = 'Unreliable sources';
		['update'] = 'Update';
		['very long'] = 'Very long';
		['verylong'] = 'Very long';
		['weasel'] = 'Weasel'
	}

	for template, date in pairs(tableargs) do
		if depreciatedTemplates[template] then
			-- Store the template name and value (date)
			depreciated[depreciatedTemplates[template]] = date
		end
	end

	-- Handle templates that spawn other templates
	if depreciated['BLP unsourced'] and not depreciated['Unreferenced'] then
		depreciated['Unreferenced'] = depreciated['BLP unsourced']
	end
	if depreciated['BLP sources'] and not depreciated['Refimprove'] then
		depreciated['Refimprove'] = depreciated['BLP sources']
	end

	-- Now generate the html for each template
	for templatename, date in pairs(depreciated) do
		local templateArgs = {}

		-- Section or article
 		templateArgs[1] = noticetype

		-- Individualized template handling
		if templatename == 'Cleanup' then
			templateArgs.reason = tableargs.reason
			templateArgs.nocat = tableargs.nocat
		end

		-- Add date if possible, if invalid date then skip silently
		if #date > 0 then
			pcall(function () templateArgs.date = lang:formatDate('F Y', date) end)
		end

		-- Demospace flag if set
		if demospace then
			templateArgs.demospace = demospace
		end

		table.insert(outputTable, frame:expandTemplate{title = templatename, args = templateArgs})
	end

	return outputTable
end

function p.main(frame)
	local args = getArgs(frame)
	local title = mw.title.getCurrentTitle()
	local templatecontents = {} -- List of templates that will be wrapped by Multiple issues

	-- Process arguments
	local noticetype = args.section and 'section' or 'article'
	local maintemplatecode, demospace, collapsed = args[1], args.demospace, args.collapsed
	args[1], args.section, args.demospace, args.collapsed = nil, nil, nil, nil

	-- If there are additional arguments, the template is using the depreciated format,
	-- so run processDepreciated() to find them
	for k, v in pairs(args) do
		templatecontents = processDepreciated(frame, args, noticetype, demospace)
		break
	end

	-- Add maintemplatecode ({{multiple issues|1=xyz}} => xyz) to the final template list
	table.insert(templatecontents, maintemplatecode)

	-- Make the code to be inserted
	local finaltemplatecode = table.concat(templatecontents)

	-- All templates should be provided as parameter 1. If they aren't, add a tracking category
	if args[2] then
		finaltemplatecode = finaltemplatecode + '[[Category:Pages using multiple issues with incorrect parameters]]'
	end

	local output = generateOutput(frame,
					{
						collapsed = yesno(collapsed) and ' collapsed' or '',
						section = noticetype,
						editlink = title:fullUrl('action=edit'),
						talkpage = tostring(title.talkPageTitle),
						contents = finaltemplatecode
					})

	return output
end

return p