Jump to content

Module:AIC status

From Wikipedia, the free encyclopedia

local getArgs = require('Module:Arguments').getArgs
local p = {}

function p.main(frame)
	local args = getArgs(frame, {
		trim = true,
		removeBlanks = false
	})
	return p._main(args)
end

function p.parseCase(status)
	local frame = mw.getCurrentFrame()
	if status == "r" or status == "requested" or status == "t" or status == "td" or status == "to do" or status == "todo"  then
		return { isOpen = true, level = 1 }
	elseif status == "o" or status == "ongoing" then
		return { isOpen = true, level = 2 }
	elseif status == "c" or status == "completed" then
		return { isOpen = false, level = 3 }
	elseif status == "u" or status == "unneeded" or status == "unnecessary" then
		return { isOpen = false, level = 4 }
	else
		return { isOpen = true, level = 1 } -- Defaults to "to do"
	end
end

function p.listCases(args)
	local frame = mw.getCurrentFrame()
	local headers = { frame:expandTemplate{ title = 'bang' } .. "'''To do'''",
					  frame:expandTemplate{ title = 'cloc' } .. " '''Ongoing'''",
					  frame:expandTemplate{ title = 'yeac' } .. " '''Completed'''",
					  frame:expandTemplate{ title = 'nayc' } .. " '''Unnecessary'''" }
	local caseList = { "", "", "", "" }
	local totalCases = 0
	local openCases = 0
	
	for i = 1, 10 do
		if args["article" .. tostring(i)] then
			local caseStatus = p.parseCase(args["status" .. tostring(i)])
			local link = "* [[" .. args["article" .. tostring(i)] .. "]]"
			
			caseList[caseStatus.level] = caseList[caseStatus.level] .. link .. "\n\n"
			totalCases = totalCases + 1
			if caseStatus.isOpen then
				openCases = openCases + 1
			end
		end
	end
	
	for i = 1, 4 do
		if #caseList[i] > 0 then
			caseList[i] = headers[i] .. "\n\n" .. caseList[i]
		end
	end
	return {list = caseList[1] .. caseList[2] .. caseList[3] .. caseList[4], totalCases = totalCases, openCases = openCases}
end

function p._main(args)
	local frame = mw.getCurrentFrame()
	local status = args.status or args.cleanup_status or args[1] or "error"
	local subpageArg = args.subpage or args.tracking_subpage or args[2] or ""
	local hatnoteIcon = ""
	local hatnote = ""
	local currentTitle = ""
	local subpage = ""
	local subpageLink = ""
	local subpageExists = false
	local count = 0
	local displayError = false
	
	local caseList = p.listCases(args)
	
	-- Status aliases
	if status == "requested" or status == "ongoing" or status == "no" or status == "unnecessary" or status == "completed" then
		status = string.sub( status, 1, 1 )
	end
	if status == "n" then status = "u" end
	
	-- Compute the number of open cases
	if caseList.totalCases > 0 then
		count = caseList.openCases
	else
		count = tonumber(args.count or args.pages_left_to_review or args[3] or "0")
	end
	
	if #subpageArg > 0 then
		currentTitle = mw.title.getCurrentTitle()
		if ( currentTitle.subpageText == subpageArg and currentTitle.namespace % 2 == 1 ) then
			subpage = mw.title.new( currentTitle.text, currentTitle.namespace - 1 )
		elseif mw.ustring.match( currentTitle.subpageText, "Archive %d+" ) then
			subpage = mw.title.new( currentTitle.baseText .. "/" .. subpageArg, currentTitle.namespace )
		else
			subpage = mw.title.new( currentTitle.fullText .. "/" .. subpageArg )
		end
		
		subpageLink = "[[" .. subpage.fullText .. "|/" .. subpageArg .. "]]"
		
		if (status == "r" or status == "o") then -- Minimizes expensive calls
			subpageExists = subpage.exists
			if subpageExists then
				local subpageContent = subpage.content
				if select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC case list", "")) > 0 or select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC article list", "")) > 0 then
					count = select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC case row%s*|[^|}]-|%s*[ort|]", "")) + select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC article row%s*|[^|}]-|%s*[ort|]", "")) + select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC case row%s*|[^|}]-|%s*status%s*=%s*[ort|]", "")) + select(2, mw.ustring.gsub(subpageContent, "{{%s*AIC article row%s*|[^|}]-|%s*status%s*=%s*[ort|]", ""))
				end
			end
		end
	end
	
	-- Building the hatnote
	if status == "r" then
		hatnoteIcon = frame:expandTemplate{ title = 'bang' }
		if #subpageArg > 0 then
			if subpageExists then
				hatnote = "Cleanup has been requested and is being tracked at " .. subpageLink
			else
				hatnote = "Cleanup has been requested and ".. subpageLink .. " can be created for that purpose"
			end
		else
			hatnote = "Cleanup has been requested"
		end
	elseif status == "o" then
		hatnoteIcon = frame:expandTemplate{ title = 'icon', args = { "cleanup" } }
		if #subpageArg > 0 then
			if subpageExists then
				hatnote = " Cleanup efforts are ongoing and being tracked at " .. subpageLink
			else
				hatnote = " Cleanup efforts are ongoing and ".. subpageLink .. " can be created for that purpose"
			end
		else
			hatnote = " Cleanup efforts are ongoing"
		end
	elseif status == "u" then
		hatnoteIcon = frame:expandTemplate{ title = 'nayc' }
		hatnote = " Cleanup is unnecessary"
	elseif status == "c"  then
		hatnoteIcon = frame:expandTemplate{ title = 'yeac' }
		hatnote = " Cleanup has been completed"
	else
		hatnote = frame:expandTemplate{ title = 'error', args = {"Please specify a cleanup status using <code>{{{status}}}</code>." } }
		displayError = true
	end
	
	if status == "r" or status == "o" then
		if subpageExists or caseList.totalCases > 0 or args.count or args.pages_left_to_review or args[3] then
			hatnote = hatnote .. ", " .. count .. " pages need review"
		end
	end
	
	if status == "u" or status == "c" then
		if #subpageArg > 0 then
			hatnote = hatnote .. ", see " .. subpageLink .. " for reference"
		end
	end
	
	-- Building the hatnote. Note that {{terminate sentence}} does not work for a terminated sentence wrapped in another element
	if displayError then
    	return frame:expandTemplate{ title = 'hatnote', args = {  hatnoteIcon .. hatnote } }
    else
    	hatnote = frame:expandTemplate{ title = 'hatnote', args = {  hatnoteIcon .. frame:expandTemplate{ title = 'terminate sentence', args = { hatnote } } } }
	end
	
	if caseList.totalCases > 0 then
		hatnote = '<div class="mw-collapsible mw-collapsed">' .. hatnote .. '<div class="mw-collapsible-content">' .. caseList.list .. '</div></div>'
	end
	
	return hatnote
end

return p