Module:Project statistics

From Meta, a Wikimedia project coordination wiki
Jump to navigation Jump to search
Module documentation
local p = {}
local dataByWiki = require("Module:Project portal/wikis")

local contentLang = mw.getContentLanguage()

local newTableRowPattern = "|%-.-\n|.-\n|.-\n|.-\n| -%[%[[^|]+|([%a-]+)%]%]\n|[^[]-%[[^ ]+ '''([%d,]+)%s*'''%]\n| -([%d,]+)\n| -%[%[[^|]+|([%d,]+)%]%]\n| -%[%[[^|]+|([%d,]+)%]%]\n| -%[%[[^|]+|([%d,]+)%]%]\n| -%[%[[^|]+|([%d,]+)%]%]\n| -%[%[[^|]+|([%d,]+)%]%]"
local oldTableRowPattern = "|%-.-\n|.-\n|.-\n|.-\n| -%[[^ ]+ ([%a-]+)%]\n| -%[[^ ]+ '''(%d+)%s*'''%]\n| -(%d+)\n| -%[%D+(%d+)%s*%]\n| -%[%D+(%d+)%s*%]\n| -%[%D+(%d+)%s*%]\n| -%[%D+(%d+)%s*%]\n| -%[%D+(%d+)%s*%]"
local newDatePattern = "%* Statistics at %d%d:%d%d, (%d%d? %a+ %d%d%d%d) %(UTC%)"
local oldDatePattern = "%d%d%d%d-%d%d-%d%d %d%d:%d%d:%d%d"

-- Returns a title object for the page that contains statistics about the given
-- project.
function p.statsTitleForProject(project)
	if project == "wikipedia" then
		return mw.title.new("List of Wikipedias/Table", 0)
	end
	return mw.title.new(project .. "/Table", 0)
end

function p.getStatistics(project)
	project = project or "wikipedia"
	local title = p.statsTitleForProject(project)
	local page = title:getContent()
	assert(page, "[[%s]] not found", title.fullText)
	
	-- Metadata
	local timestamp = mw.ustring.match(page, newDatePattern) or mw.ustring.match(page, oldDatePattern)
	local date = mw.text.split(contentLang:formatDate("Y-m-d", dateStr), "-")
	
	local tableRowPattern = newTableRowPattern
	if not mw.ustring.match(page, tableRowPattern) then
		tableRowPattern = oldTableRowPattern
	end
	
	local wikis = {}
	for wiki, articles, pages, edits, admins, users, activeUsers, files
			in mw.ustring.gmatch(page, tableRowPattern) do
		local stats = {
			articles = contentLang:parseFormattedNumber(articles),
			pages = contentLang:parseFormattedNumber(pages),
			edits = contentLang:parseFormattedNumber(edits),
			admins = contentLang:parseFormattedNumber(admins),
			users = contentLang:parseFormattedNumber(users),
			activeUsers = contentLang:parseFormattedNumber(activeUsers),
			files = contentLang:parseFormattedNumber(files),
		}
		local data = dataByWiki[wiki] and dataByWiki[wiki][project]
		if data and data.closed then
			stats.closed = true
		end
		-- [[Wikipedia article depth]]
		stats.depth = stats.edits / stats.pages * ((stats.pages - stats.articles) / stats.articles) ^ 2
		wikis[wiki] = stats
	end
	
	-- Sometimes the statistics pages don’t get updated right after a wiki is
	-- created. The workaround is to temporarily set [project].numArticles in
	-- /wikis to the number of articles.
	for wiki, data in pairs(dataByWiki) do
		local data = dataByWiki[wiki] and dataByWiki[wiki][project]
		if not wikis[wiki] and data and not data.closed then
			wikis[wiki] = {
				manual = true,
				articles = data.numArticles,
				pages = data.numPages,
				edits = data.numEdits,
				admins = data.numAdmins,
				users = data.numUsers,
				activeUsers = data.numActiveUsers,
				files = data.numFiles,
			}
		end
	end
	
	return {
		project = project,
		source = title,
		date = date,
		wikis = wikis,
	}
end

return p