This is a module to implement logic for MediaWiki:Proofreadpage index template


--[=[
This is a module to implement logic for [[MediaWiki:Proofreadpage index template]]

It doesn't do everything yet, but over time it can accrete functions from
the template, which will become simpler and simpler until it's just an invoke.

Implemented so far:
 * Status categorisation and field
 * Some auxiliary data fields
]=]

local getArgs = require('Module:Arguments').getArgs

local p = {} --p stands for package

-- mapping of field ID to field properties
headings = {
	transclusion = {
    	txt = 'Transclusion',
    },
    validation_date = {
    	txt = 'Validated in',
    }
}

-- construct a basic "field" row
function construct_field(id, content)
	
	local title = headings[id]['txt']
	
	local s = "<tr id=\"index-field-" .. id .. "\">\n"
	s = s .. "<th scope=\"row\" style=\"padding-right:0.50em; text-align:right; vertical-align:top;\">" .. title .. "</th>\n"
	s = s .. "<td style=\"vertical-align:top;\">" .. content .. "</td>\n"
	s = s .. "</tr>\n"
	return s
end

function construct_link(url, text)
	return '[' .. url .. ' ' .. text .. ']'
end

-- handy URL generators (remember to use mw.uri.encode on the bits you need to)
local url_gens = {
	oclc = function(id)
		return 'https://www.worldcat.org/oclc/' .. id	
	end,
	ark = function(id)
		return 'https://n2t.net/' .. mw.uri.encode(id)
	end
}

-- construct a span which might be classed "error"
function maybe_error_span(text, error) 
	span = mw.html.create('span')
		:wikitext(text)
	if error then
		span:addClass('error')
	end
	return span
end

-- construct the status (proofread/validated/...) field
function construct_status_field(status)

	local out = ''
	local sd = index_status_data[status]
	
	if not sd then
		sd = index_status_data['_default']
	end
	
	out = out .. '[[Category:' .. sd['cat'] .. ']]'

	local link_text = tostring(maybe_error_span(sd['txt'], sd['error']))
	local catlink = '[[:Category:' .. sd['cat'] .. '|' .. link_text .. ']]'
	out = out .. construct_field("progress", catlink)

	return out
end

local transclusion_data = {
	yes = {
		cat = 'Fully transcluded',
		text = 'Fully transcluded',
		help = 'The work is fully transcluded, including front and end matter and images'
	},
	notimg = {
		cat = 'Images not fully transcluded',
		text = 'Images not fully transcluded',
		help = 'The work is fully transcluded, but that some images are still missing or need improvement',
	},
	notadv = {
		cat = 'Advertising not transcluded',
		text = 'Advertising not transcluded',
		help = 'The main body of the work is properly transcluded, though front- or end-matter advertising material is not'
	},
	held = {
		cat = 'Transclusion on hold',
		text = 'Transclusion on hold',
		help = 'There is a problem with transcluding the work (which should be explained on the talk page)'
	},
	check = {
		cat = 'Transclusion check required',
		text = 'Transclusion check required',
		help = ' The transclusion of the work is incomplete or needs checking'
	},
	no = {
		cat = 'Index not transcluded',
		text = 'Index not transcluded or unreviewed',
		help = ' This work is not transcluded or has not been reviewed for transclusion'
	}
}

function construct_cat(cat)
	return "[[Category:" .. cat .. "]]"	
end

-- construct the transclusion status
function construct_transclusion_status(status, date)
	
	-- handle templates with no explicit status
	if status == nil then
		status = "no"
	end
	
	local td = transclusion_data[status]
	
	if td == nil then
		error("Unknown index status: " .. status)
	end

	local catlink = '[[:Category:' .. td['cat'] .. '|' .. td['text'] .. ']]'
	local out = construct_field("transclusion", catlink)
	out = out .. construct_cat( td['cat'])
	return out
end

function construct_validated_date(date)
	local cat = 'Indexes validated in ' .. date
	local indicator_text = 'Validated in ' .. date
	
	local out = '[[Category:' .. cat .. ']]'
	
	local catlink = '[[:Category:' .. cat .. '|' .. date .. ']]'
	out = out .. construct_field("validation_date", catlink)
	
	out = out .. '<indicator name="validated-index-date">[[File:Yes Check Circle.svg|15px|link=Category:' .. cat .. '|' .. indicator_text .. '|alt=Validated index page.]]</indicator>'
	return out
end

function p.fields(frame)
	
	local args = getArgs(frame)

	local s = ""
	
	s = s .. construct_status_field(args['Progress'])
	
	-- always do this, even if the arg is not set
	s = s .. construct_transclusion_status(args["Transclusion"], args['Transclusion_date'])
	
	if args["Validation_date"] then
		s = s .. construct_validated_date(args["Validation_date"])	
	end

	if args["OCLC"] then
		local link = construct_link(url_gens['oclc'](args["OCLC"]), args['OCLC'])
		s = s .. construct_field("oclc", link)
	end

	
	return s
	
end

return p