Jump to content

Module:Qnotstats

From Wikipedia, the free encyclopedia

local p = {}

function p.sums()

	local function add_days_to_date(date_str, days)
    	local y, m, d = date_str:match("(%d+)%-(%d+)%-(%d+)")
    	local ts = os.time{ year = y, month = m, day = d, hour = 12 }
    	local new_ts = ts + days * 24 * 60 * 60
    	return os.date("%Y-%m-%d", new_ts)
	end

    -- Initialise language
    local lang = mw.language.getContentLanguage()
    
    -- Load the data array
    -- https://commons.wikimedia.org/wiki/Data:Notability_statistics_data.tab
    local t = mw.ext.data.get('Notability_statistics_data.tab')
    local rows = t.data

    -- get the necessary rows
	local day0 = rows[#rows]    -- today
	local day1 = rows[#rows-1]  -- yesterday
	local dayz = rows[1]        -- when records began

    -- Extract numeric fields (columns 2–6)
    local a0 = tonumber(day0[2]) or 0
    local b0 = tonumber(day0[3]) or 0
    local c0 = tonumber(day0[4]) or 0
    local d0 = tonumber(day0[5]) or 0

    local a1 = tonumber(day1[2]) or 0
    local b1 = tonumber(day1[3]) or 0
    local c1 = tonumber(day1[4]) or 0
    local d1 = tonumber(day1[5]) or 0
    
    local az = tonumber(dayz[2]) or 0
    local bz = tonumber(dayz[3]) or 0
    local cz = tonumber(dayz[4]) or 0
    local dz = tonumber(dayz[5]) or 0
    
    -- Compute sums
    local def0 = a0 + b0 + c0 + d0
    local def1 = a1 + b1 + c1 + d1
    local defz = az + bz + cz + dz

	-- Compute rate
	local articles_per_day = math.floor(((defz - def0) / (#rows - 1)))
	local days_to_clear_backlog = math.ceil((def0 / articles_per_day))

    -- Retrieve dates and format them nicely
	local date0 = lang:formatDate( "j F Y", day0[1], nil )
	local finish_date = lang:formatDate( "j F Y", add_days_to_date(day0[1], days_to_clear_backlog), nil)

	-- Compose return string
    return string.format(
    	"At the beginning of the day (UTC) on %s there were:\n" ..
        "* '''%s articles''' with topics of unclear notability," ..
		"a change of '''%s''' since the day before\n" ..
        "At the current [[arithmetic mean|average]] rate of '''%s articles per day''', " ..
        "it will take '''%s days to clear the backlog''', " ..
        "finishing on '''%s'''.",
        date0, 
        lang:formatNum(def0),
		lang:formatNum(def0 - def1), 
        lang:formatNum(articles_per_day),
		lang:formatNum(days_to_clear_backlog),
		finish_date
    )
end

return p