Jump to content

Module:User:SuggestBot/WikiProjects

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Nettrom (talk | contribs) at 20:45, 16 August 2015 (iterate over the right table variable name). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local wp = {}

-- BTW: Lua allows literal strings declarations [[ ]], but the syntax
-- highlighter doesn't support them properly, fails if the string contains
-- another set of square brackets...

-- List start example, showing what we're aiming to do...
-- Note that the intro-parameter always ends with a '<br />'
local list_start = "{{WPX list start|color={{{2|#086}}}<includeonly>|constrained=yes</includeonly>" ..
	"|title=Add sources|intro=See [[Wikipedia:Citing sources]] for more information.<br />" ..
	"{{WPX last updated|Wikipedia:WikiProject_Ghana/Tasks/SuggestBot}}}}"
local list_item = "{{WPX article recommendation|color={{{1|#37f}}}|title = Jay Q | rating = 1" ..
	"| rating_description = Quality: Low, Assessed class: Start, Predicted class: Stub" ..
	"| pageviews = 4 | needs = More content, more images, more wikilinks, more sources}}"
local list_end= "{{WPX list end|more=Wikipedia:WikiProject_Ghana/Tasks/SuggestBot#Add_sources}}"

-- Mapping of SuggestBot's task categories (see [[User:SuggestBot/Documentation/Task categories]])
-- to section titles and descriptions, and the order in which they will be presented
local taskcat_order = {'source', 'cleanup', 'expand', 'unenc',
	'merge', 'wikify', 'orphan', 'stub'}
local taskcat_map = {
	source={
		title='Add sources',
		descr='See [[Wikipedia:Citing sources]] for more information.'
	},
	cleanup={
		title='Cleanup',
		descr='Improve the presentation of content.'
	},
	expand={
		title='Expand',
		descr='Add more information to these articles.'
	},
	unenc={
		title='Unencyclopaedic',
		descr='Remove content that is not relevant for an encyclopaedia.'
	},
	merge={
		title='Merge',
		descr='Should this article be merged with another article?'
	},
	wikify={
		title='Wikify',
		descr='Improve the wiki-formatting on this article.'
	},
	orphan={
		title='Orphan',
		descr='Few or no articles link to this article.'
	},
	stub={
		title='Stub',
		descr='Expand on this brief article.'
	}
}

-- Local support functions follow below

local function has_key(tbl, key)
	-- Check if the given table has the given key
	return tbl[key] ~= nil	
end

local function sort_articles(arglist, task_cats)
	-- Sort the given argument list into a table mapping task categories
	-- to a list of articles in that task category.
	local sorted_arts = {}
	for i, task_cat in pairs(task_cats) do
		sorted_arts[task_cat] = {}
	end

	-- What language are we talking?
	local lang = mw.language.getContentLanguage()
	
	-- 0-based index of the current article being processed	
	local cur_art = 0
	while arglist[(cur_art * 6) + 1] do
		local cur_idx = cur_art * 6
		local task_cat = cur_idx + 1
		-- Do we have this task category in our table?
		if sorted_arts[task_cat] then
			table.insert(sorted_arts[task_cat], {
				tag=task_cat, title=arglist[cur_idx + 2],
				views=lang:formatNum(tonumber(arglist[cur_idx + 3])),
				cur_qual=arglist[cur_idx + 4],
				pred_qual=arglist[cur_idx + 5],
				tasks=arglist[cur_idx + 6]
				})
		end
		cur_art = cur_art + 1
	end
	return sorted_arts
end

-- Globally exposed functions follow below
function wp.suggestions(frame)
	-- Process the suggested articles given in the frame's arguments
	-- and build lists of articles for each task category defined in the global
	-- list of categories above.
	
	-- Keyword arguments: 
	-- * is_included=yes: passed in to limit lists to two items each when
	--                    the page of the call is transcluded
	-- * list_col=[colour value]: defines the CSS colour value of list headers
	-- * item_col=[colour value]: defines the CSS colour value of list items
	--
	-- The rest of the arguments are sets of six parameters describing each
	-- suggested article, as follows:
	-- 1: task category
	-- 2: article title
	-- 3: number of average views/day (14 days prior to the update)
	-- 4: assessed quality
	-- 5: predicted quality
	-- 6: comma-separated list of specific tasks for improving this article
	
	local list_col = '#086'
	local item_col = '#37f'
	if has_key(frame, 'list_col') then
		list_col = frame['list_col']
	end
	if has_key(frame, 'item_col') then
		item_col = frame['item_col']
	end
	
	local is_included = false
	if has_key(frame, 'is_included') then
		is_included = true
	end
	
	local x = "{{WPX list start|color={{{2|#086}}}<includeonly>|constrained=yes</includeonly>" ..
	"|title=Add sources|intro=See [[Wikipedia:Citing sources]] for more information.<br />" ..
	"{{WPX last updated|Wikipedia:WikiProject_Ghana/Tasks/SuggestBot}}}}"
	
	-- When was the page this is invoked from last updated?
	local last_update = frame:expandTemplate{title='WPX last updated',
		args={frame:getTitle()}}
	
	local result = ''
	
	-- sort the supplied suggested articles and process into lists…
	local sorted_articles = sort_articles(frame.args, taskcat_order)
	for i, task_cat in pairs(taskcat_order) do
		-- write list start
		
		local list_params = {color=list_col,
			title=taskcat_map[task_cat]['title'],
			intro=taskcat_map[task_cat]['descr'] .. "<br />" .. last_update
		}
		if is_included then
			list_params['constrained'] = 'yes'
		end
		result = result .. frame:expandTemplate{title='WPX list start',
								args=list_params}
		-- write some articles
		
		-- write list end
		result = result .. frame:expandTemplate{title='WPX list end',
			{more=frame:getTitle() .. '#' .. string.gsub(taskcat_map[task_cat]['title'], ' ', '_')}}
	end
	return result
end

function wp.hello(frame)
	return "Hello World!"	
end

function wp.test_args(frame)
	result = "The method got the following arguments:"
	if has_key(frame, 'is_included') then
		result = result .. "\n* is_included" .. frame.args['is_included']
	end
	for k, v in pairs(frame.args) do
		result = result .. "\n* " .. k .. "=" .. v				
	end
	return result
end

return wp