Module:Pagestats: Difference between revisions

From The Satanic Wiki
Jump to navigation Jump to search
Mediawiki>Magiczocker
m (test)
 
m (1 revision imported)

Revision as of 02:24, 30 April 2021

Documentation for this module may be created at Module:Pagestats/doc

--- Shows some stats about a page.
--  
--  @module             pagestats
--  @release            unmaintained
--  @author             [[User:Dessamator|Dessamator]]
--  @require            Module:Utility
--  <nowiki>

local p = {}
local u = require("Dev:Utility")

local function getMatches(contentPage, sRegex, sMatchType)
	local tLinks = contentPage:gmatch(sRegex)
	local count = 0
	local tReports = {}
	
	for matchvalue in tLinks do
		count = count + 1
		if matchvalue and not tReports[matchvalue] then
			tReports[matchvalue] = 0
		end
		tReports[matchvalue] = tReports[matchvalue] + 1
	end
	return count, tReports
end

local function getData(tDataTypes, contentPage)
	local tStatRegex = {
		links = "%[.-%]",
		templates = "{{.-}}",
		tables = "{|.-\n|}",
		references = "%<ref[^e].-%s*%>",
		words = "(%a+)",
	}
	local statTable, tReports = {}, {}
	
	for _, sMatchType in pairs(tDataTypes) do
		sMatchType = mw.text.trim(sMatchType or "")
		if tStatRegex[sMatchType] then
			statTable[sMatchType], tReports[sMatchType] = getMatches(contentPage, tStatRegex[sMatchType], sMatchType)
		end
	end
	return statTable, tReports
end

--- Pagestat string output.
--  @function           Pagestats:main
--  @return             {string} Wikitable with pagestats.
--  @usage              p.main(page='Fandom Developers Wiki',stats='templates') == '<table class="wikitable"><th>Stats</th><th>Count</th><tr></tr><td>templates</td><td>3</td></table>'
function p.main(frame)
	local args = u.getArgs(frame)
	local tStats = {}
	local hTable = mw.html.create("table")
	local mwPage
	
	hTable:addClass("wikitable")
	hTable:tag("th")
		:wikitext("Stats")
	hTable:tag("th")
		:wikitext("Count")
	
	if args then
		local sPage = args["page"] or tostring(mw.title.getCurrentTitle())
		local sStats = args["stats"]
		tStats = mw.text.split(sStats, ",")
		
		if sPage then
			mwPage = mw.title.new(sPage)
			local contentPage = mwPage:getContent()
			tStats = getData(tStats, contentPage)
			
			for stats, count in pairs(tStats) do
				hTable:tag("tr")
				hTable:tag("td")
					:wikitext(stats)
				hTable:tag("td")
					:wikitext(count)
			end
		end
	end
	hTable:done()
	
	return hTable
end

return p