Module:Sandbox/Nettrom/SuggestBot

Source: Wikipedia, the free encyclopedia.
local p = {}

-- List of all columns and their respective header titles
local table_columns = {'Views/Day', 'Quality', 'Title', 'Content', 'Headings',
	'Images', 'Links', 'Sources', 'Tagged with…'}

-- Mapping of shortnames for task categories and their respective text
-- For more information, see User:SuggestBot/Documentation/Task categories
local task_map = {source='Add sources', cleanup='Cleanup', expand='Expand',
	unenc='Unencyclopaedic', merge='Merge', wikify='Wikify', orphan='Orphan',
	stub='Stub'}

-- Mapping of assessment class names to indexes for sorting
local rating_sort = {FA=7, A=6, GA=5, B=4, C=3, Start=2, Stub=1, NA=0}

-- Mapping of quality levels into images
local rating_level = {FA='High', A='High', GA='High', B='Medium', C='Medium',
	Start='Low', Stub='Low', NA='Unassessed'}
local rating_images = {High='File:Stars330.svg', Medium='File:Stars320.svg',
	Low='File:Stars310.svg', Unassessed='File:Stars300.svg'}

-- List of task columns in order so we can flag if in need of improvement
local task_list = {'content', 'headings', 'images', 'links', 'sources'}
-- Mapping of tasks to explanations (used in the image alt-text)
local task_explanations = {content='Please add more content',
	headings='Please create proper section headings',
	images='Please add more images', links='Please add more wikilinks',
	sources='Please add more sources'}
-- Title of the image file we use to flag tasks
local task_image = 'Flag icon red 3.svg'
-- [[File:Flag icon red 3.svg|18px|alt=Please add more content|link=Yamaha electric guitar models|Please add more content]]

-- Hello world to test that the module works...
function p.hello_world()
	return "Hello, world!"
end

function translate_quality(tbl_row, title, rating, prediction)
	-- Add a table cell with quality information
	local qual_lvl = rating_level[prediction]
	local qual_text = '[[' .. rating_images[qual_lvl] ..
		'|60px|alt=Quality: ' .. qual_lvl .. ', Assessed class: ' ..
		rating .. ', Predicted class: ' .. prediction .. '|link=' ..
		title .. '|Quality: ' .. qual_lvl .. ', Assessed class: ' ..
		rating .. ', Predicted class: ' .. prediction .. ']]'

	-- Create a table cell with information about article quality
	local qualcell = tbl_row:tag('td')
	-- Add the sortkey from mapping predicted class to a number
	add_sortkey(qualcell, rating_sort[prediction])
	qualcell:wikitext(qual_text)
end

function add_sortkey(tblcell, value)
	return tblcell:tag('span')
		:addClass('sortkey')
		:wikitext(value)
end

function add_tasks(tbl_row, tasks, title)
	-- For each task we now about, see if it's in task and act accordingly
	for i, task in ipairs(task_list) do
		local taskcell = tbl_row:tag('td')
		if string.find(tasks, task) then
			add_sortkey(taskcell, '0')
			taskcell:wikitext('[[File:' .. task_image .. '|18px|alt=' ..
				task_explanations[task] .. '|link=' .. title .. '|' .. 
				task_explanations[task] .. ']]')
		else
			add_sortkey(taskcell, '2')
		end
	end
end

function build_header(tbl)
	local t_row = tbl:tag('tr')
	for i, title in ipairs(table_columns) do
		t_row:tag('th')
			:attr('scope', 'col')
			:wikitext(title)
			:newline()
	end
end

function add_row(tbl)
	-- Adds a suggestion row to the table
	return tbl:tag('tr')
		:css('text-align', 'center')
		:css('height', '30px')
		:newline()
end

function add_cells(tbl_row, tag, title, views, rating, prediction, tasks)
	-- 	Add the table cells for the suggestion based on the given parameters
	local tag_text = task_map[tag]
	
	-- First column is number of views
	tbl_row:tag('td')
		:css('text-align', 'right')
		:wikitext(views)
		
	-- Second column is article quality
	translate_quality(tbl_row, title, rating, prediction)
	
	-- Third column is a link to the article and its talk page
	tbl_row:tag('td')
		:css('text-align', 'left')
		:wikitext('[[' .. title .. ']] <small>([[Talk:' .. title .. '|talk]])</small>')
		
	-- Fourth through eighth columns are the tasks
	add_tasks(tbl_row, tasks, title)
	
	-- Last column is the article's task category
	tbl_row:tag('td'):wikitext(task_map[tag])
end

function p.build_table(frame)
	-- Build the entire suggestion table.
	-- Expects a set of 'n' suggestions, where each suggestion has
	-- a total of six parameters, in order as follows:
	-- 1: Task category
	-- 2: Article title
	-- 3: Avg. no. of article views/day for past 14 days
	-- 4: Assessed quality class
	-- 5: Predicted quality class
	-- 6: Comma-separated list of tasks to flag
	
	-- What language are we talking?
	local lang = mw.language.getContentLanguage()
	
	local table = mw.html.create('table')
		:addClass('wikitable')
		:addClass('sortable')
		:css('text-align', 'center')
		:attr('border', '1')
	build_header(table)

	-- 0-based index of the current article being processed	
	local cur_art = 0

	while frame.args[(cur_art * 6) + 1] do
		local cur_idx = cur_art * 6
		local newrow = add_row(table)
		-- Use the formatnum parser function to format number of views
		local n_views = lang:formatNum(tonumber(frame.args[cur_idx + 3]))
		add_cells(newrow, frame.args[cur_idx + 1], frame.args[cur_idx + 2],
			n_views, frame.args[cur_idx + 4],
			frame.args[cur_idx + 5], frame.args[cur_idx + 6])
		cur_art = cur_art + 1
	end

	return tostring(table)
end

function p.nargs(frame)
	local i = 0
	local output = ''
	while frame.args[(i * 6) + 1] do
		output = output .. 'Processing article ' .. i .. '... '
		i = i + 1
	end
	return output
end

return p