Documentation for this module may be created at Module:Sandbox/Danski454/SPI report/doc

require('strict')
local yesno = require('Module:Yesno')
local getArgs = require('Module:Arguments').getArgs
local TableTools = require('Module:TableTools')
local p = {}

local newPageHeader = '<noinclude>__TOC__</noinclude>\n{{SPIarchive notice|__SUBPAGENAME__}}\n{{SPIpriorcases}}\n'

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local outText = ''
	local master = mw.title.getCurrentTitle().subpageText
	local socks = {}
	local ips = {}
	
	--Wikipedia:Sockpuppet investigations/SPI/Blank report template header
	if not mw.title.getCurrentTitle().exists then
		outText = string.gsub(newPageHeader, '__SUBPAGENAME__', master)
	end
	
	local status = ''
	if args.status then
		status = args.status
	elseif args.admincomment then
		status = 'close'
	elseif yesno(args.checkuser) then
		status = 'CUrequest'
	end
	
	outText = outText .. '===' .. mw.language.getContentLanguage():formatDate('d F Y') .. 
		'===\n{{SPI case status|' .. status .. '}}\n'
	
	--SPI report
	outText = outText .. '====Suspected sockpuppets====\n'
	if args.socksraw then
		outText = outText .. args.socksraw .. '\n'
		-- Twinkle uses this to pass in reports (and loads are filed this way),
		-- so parse the reports for use by the tools section
		local socksraw = mw.ustring.gsub(args.socksraw, '<!%-%-.-%-%->', '') --rm comments
		local sockMatches = mw.ustring.gmatch(socksraw, '{{%s*[Cc]heckuser%s*|%s*1%s*=%s*(.-)%s*}}')
		local ipMatches = mw.ustring.gmatch(socksraw, '{{%s*[Cc]heckip%s*|%s*1%s*=%s*(.-)%s*}}')
		for i in sockMatches do
			socks[#socks + 1] = i
		end
		for i in ipMatches do
			ips[#ips + 1] = i
		end
	else
		-- loop over ip# and sock# args (copied from [[Module:Old XfD multi]])
		for k, v in pairs(args) do
			if type(k) == 'string' then
				local prefix, num = k:match('^(.-)([1-9][0-9]*)$')
				if prefix and num then
					num = tonumber(num)
					if prefix == 'sock' then
						socks[num] =  v
					elseif prefix == 'ip' then
						ips[num] =  v
					end
				end
			end
		end
		socks = TableTools.compressSparseArray(socks)
		ips = TableTools.compressSparseArray(ips)
		for i=1,#socks do
			outText = outText .. '* {{checkuser|1=' .. socks[i] .. '}}\n'
		end
		for i=1,#ips do
			outText = outText .. '* {{checkip|1=' .. ips[i] .. '}}\n'
		end
	end
	outText = outText .. '<!-- You may duplicate the templates above ({{checkuser}} and {{checkIP}}) to list more accounts-->\n'
	
	--Tools
	outText = outText .. '*\'\'\'Tools\'\'\': <span class="plainlinks">[https://tools.wmflabs.org/sigma/editorinteract.py?users=' ..
		mw.uri.encode(master)
	for i=1,#socks do
		outText = outText .. '&users=' .. mw.uri.encode(socks[i])
	end
	for i=1,#ips do
		outText = outText .. '&users=' .. mw.uri.encode(ips[i])
	end
	outText = outText .. ' Editor interaction utility] • [https://tools.wmflabs.org/interaction-timeline?wiki=enwiki&user=' ..
		mw.uri.encode(master)
	if socks[1] then
		outText = outText .. '&user=' .. mw.uri.encode(socks[1])
	end
	outText = outText .. ' Interaction Timeline] • [https://tools.wmflabs.org/betacommand-dev/UserCompare/' ..
		mw.uri.encode(master) .. '.html User compare report]</span> <small>\'\'Auto-generated every hour.\'\'</small>\n\n'
	
	--evidence
	if args.evidence then
		outText = outText .. args.evidence
	else
		outText = outText .. "''No evidence submitted''"
	end
	outText = outText .. ' ~~~~\n\n' ..
		'====<big>Comments by other users</big>====\n' ..
		'<small>\'\'Accused parties may also comment/discuss in this section below. See [[Wikipedia:Sockpuppet investigations/SPI/Guidance#Defending yourself against claims|Defending yourself against claims]].\'\'</small>' ..
		'\n\n====<big>Clerk, CheckUser, and/or patrolling admin comments</big>====\n'
	
	if args.admincomment then
		outText = outText .. args.admincomment
	end
	outText = outText .. '\n\n----<!-- All comments go ABOVE this line, please. -->'
	
	return outText
end

return p