Module:User:AnomieBOT/TFDTemplateSubster/row

local bit32 = require( 'bit32' )

local p = {}

local function getStatus( frame, tmpl, logpage )
	local t = mw.title.new( tmpl )
	if not t then
		return frame:expandTemplate{ title = 'Not done', args = { 'Invalid' } } .. ' The title "' .. mw.text.nowiki( tmpl ) .. '" is invalid.'
	end
	if t.id <= 0 then
		return frame:expandTemplate{ title = 'Done', args = { 'Deleted' } } .. ' Please remove this row.'
	end
	tmpl = t.fullText -- Normalize

	t = mw.title.new( logpage )
	if not t or not t.exists then
		return frame:expandTemplate{ title = 'Not done', args = { 'Invalid' } } .. ' The XfD page [[:' .. logpage .. ']] is invalid or does not seem to exist.'
	end

	local status = mw.loadJsonData( 'User:AnomieBOT/TFDTemplateSubster/status' )
	local s = status[tmpl] or 0
	local buts = {}
	if bit32.band( s, 0x01 ) ~= 0 then
		buts[#buts+1] = 'page protection'
	end
	if bit32.band( s, 0x02 ) ~= 0 then
		buts[#buts+1] = 'bot exclusion'
	end
	if bit32.band( s, 0x04 ) ~= 0 then
		buts[#buts+1] = 'demo/nosubst transclusions'
	end
	if bit32.band( s, 0x08 ) ~= 0 then
		buts[#buts+1] = 'unrecognized wikitext'
	end
	if bit32.band( s, 0x4000 ) ~= 0 then
		buts[#buts+1] = 'errors while attempting to edit'
	end
	if bit32.band( s, bit32.bnot( 0xc00f ) ) ~= 0 then
		buts[#buts+1] = 'unknown reasons'
	end
	if #buts > 0 then
		buts = ', however transclusions may remain due to ' .. mw.text.listToText( buts )
	else
		buts = ''
	end

	return frame:expandTemplate{ title = bit32.band( s, 0x8000 ) ~= 0 and 'Done' or 'Working' } .. buts
end

function p.main( frame )
	local args = frame:getParent().args
	local tmpl = args[1]
	if tmpl == nil then
		return '* <strong class="error">Row template missing target template name!</strong>\n'
	end
	
	-- Note that the bot has equivalent logic for determining `link`, `logpage`, and `reason`.
	-- The bot and this module need to be updated in sync with each other.
	local link, logpage
	if args['link'] then
		link = args['link']
		logpage = string.gsub( link, '#.*', '' )
	else
		if args[2] == nil then
			return '* <strong class="error">Row template missing link or TFD date!</strong>\n'
		end
		logpage = 'WP:Templates for discussion/Log/' .. args[2]
		link = logpage .. '#' .. ( args['section'] or args[1] )
	end
	local reason = ''
	if args['reason'] then
		reason = '|' .. args['reason']
	end
	
	local ret
	if string.sub( tmpl, 1, 9 ) == 'Template:' then
		ret = '{{[[' .. tmpl .. '|' .. string.sub( tmpl, 10 ) .. ']]}}'
	else
		ret = '{{[[:' .. tmpl .. ']]}}'
	end
	return '* ' .. ret .. ' per [[' .. link .. reason .. ']]<br><small>(' ..
		'<span class="plainlinks">[https://en.wikipedia.org/w/index.php?title=Special:WhatLinksHere/' .. mw.uri.encode( tmpl ) .. '&limit=999 links]</span>' ..
		'&nbsp;&#124; <span class="plainlinks">[https://en.wikipedia.org/w/index.php?title=Special:WhatLinksHere/' .. mw.uri.encode( tmpl ) .. '&limit=999&hidelinks=1 transclusions]</span>' ..
		'&nbsp;&#124; [[' .. string.gsub( tmpl, ':', ' talk:', 1 ) .. '|talk]]' ..
		'&nbsp;&#124; [[' .. tmpl .. '/doc|doc]]' ..
		'&nbsp;&#124; [[' .. tmpl .. '/sandbox|sandbox]]' ..
		'&nbsp;&#124; [[' .. tmpl .. '/testcases|testcases]]' ..
		')</small><br>' .. getStatus( frame, tmpl, logpage )
end

return p