Module:Database reports/Hot articles by category

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

local Report = require('Module:Database report')
local Arguments = require('Module:Arguments')

local p = {}

p.main = function(frame)
	local args = Arguments.getArgs(frame)
	local report = Report:new()
	
	local category = args.category and args.category:gsub(' ', '_') 
	local articles = args.articles or '10'
	local days = args.days or '7'
	local red = args.red or '30'
	local orange = args.orange or '15'

	if category == nil then
		return invalid('|category= parameter is required')	
	end
	if tonumber(articles) == nil or tonumber(articles) < 1 then
		return invalid('articles must be a number >= 1')
	end
	if tonumber(days) == nil or tonumber(days) < 1 or tonumber(days) > 30 then
		return invalid('days must be between 1 and 30')
	end
	if tonumber(red) == nil or tonumber(red) < 1 then
		return invalid('red: threshold must be a number >= 1')
	end
	if tonumber(orange) == nil or tonumber(orange) < 1 then
		return invalid('orange: threshold must be a number >= 1')
	end

	report:setSQL([[
		WITH hotarticles_presort AS (
			WITH incategoryarticleedits AS (
				SELECT main.page_title AS Article, rev_id
				FROM linktarget
				JOIN categorylinks on cl_target_id = lt_id
				JOIN page AS talk ON talk.page_id = cl_from AND talk.page_namespace = 1
				JOIN page AS main ON main.page_namespace = 0 AND main.page_title = talk.page_title
				JOIN revision ON rev_page = main.page_id
				WHERE lt_namespace = 14
				  AND lt_title = ']]..category..[['
				  AND rev_timestamp >= DATE_SUB(NOW(), INTERVAL ]]..days..[[ DAY)
			)
			SELECT COUNT(*) AS Edits, Article
			FROM incategoryarticleedits
			GROUP BY Article
			ORDER BY Edits DESC
			FETCH FIRST ]]..articles ..[[ ROWS WITH TIES
		)
		SELECT 
			Edits, 
			Article,
			ROW_NUMBER() OVER(ORDER BY Edits DESC, Article ASC) AS Row, 
			IF(EDITS > ]]..red..[[, 'c60d27', IF(EDITS > ]]..orange..[[, 'f75a0d', 'ff9900')) AS Color
		FROM hotarticles_presort
		ORDER BY Edits DESC, Article ASC	
	]])
	report:useWikilinks(2)
	report:setWidth(3, '3em')
	report:setTableStyle('overflow-wrap: anywhere; border-width: 0; border-collapse: separate; border-spacing: 0 1px')
	report:setTableClass('wikitable')
	report:setInterval(1)
	report:setHeaderTemplate('Hot articles by category header')
	report:setRowTemplate('Hot articles by category row')
	report:useNamedParamsInRowTemplate(true)

	return report:generate()
end 

function invalid(text)
	return require('Module:Error').error{ 'Invalid configuration: '.. text, tag = 'p' }
end

return p