Module:Sandbox/Ahecht/Xviews

This is an old revision of this page, as edited by Ahecht (talk | contribs) at 02:15, 15 July 2025 (Add). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- HELPER FUNCTIONS --
local function notblank(v) return (v or '' ) ~= '' end
local function ifblank(v, alt) return notblank(v) and v or alt end
local function red(v) return '<span style="color: red;">' .. v .. '</span>' end
local function getContent(title) return mw.ustring.gsub(mw.title.new(title).content, '^Date,"[^"]+"', '') end
local function diffDays(dargs)
	return require('Module:diff days')['diff_days'](
		mw.getCurrentFrame():newChild{ title = 'Template:Diff_days', args = dargs }
	)
end

-- EXPORTED CONSTANTS --
local p = {
	gSTALE = function() return 30 end, --global for representing the age in days after which a pageviews file, and any chart based on it, becomes stale and no longer appropriate for display on a Talk page. Stale pages should have their pageviews file updated.
	gCOUNT = function() return 75 end, --global for representing the default number of days to display in the chart; overridable with param {{para|ct}}.
}

function p._xviews(args)														-- {{xviews}}
	local title = ifblank(args[1], mw.title.getCurrentTitle().fullText .. '/pageviews')
	if mw.title.new(title).exists then
		local stale = ifblank(args.stale, p.gSTALE())
		if p._age(title) > stale then --stale:
			return red(mw.ustring.format(
				'The pageviews file [[%s]] is more than %d days old; please see [[Template:Xviews#Instructions|Instructions]].',
				title, stale
			))
		else --not stale:
			return p._xvmain(title, ifblank(args.days, ifblank(args.ct, p.gCOUNT()))) ..
				((notblank(args.log) or notblank(args.mode)) and p._xAxis(title) or '')
		end
	else
		return red('Missing required pageviews file.') ..
			' See [[Template:Xviews#Instructions|Instructions]].'
	end
	
end


function p._age(title)															-- {{xviews/age}}
	local nthview = p._nthView(title, p._viewCount(title))
	nthview = mw.text.split(nthview, ':', true)[1]
	return diffDays({nthview, mw.getContentLanguage():formatDate('j F Y'), precision=0})
end

function p._nthView(title, number)												-- {{xviews/nth view}}
	local views = mw.ustring.gsub(getContent(title), '.?(202%d%-%d%d%-%d%d)%,?(%d+)', '%1:%2,')
	return mw.text.split(views, ',', true)[tonumber(number)]
end

function p._viewCount(title)													-- {{xviews/view count}}
		local str = mw.ustring.gsub(getContent(title), '.?202%d%-%d%d%-%d%d', '')
		str = mw.ustring.gsub(str, ',', '', 1)
		-- Remove leading and trailing delimiters (along with any surrounding whitespace)
        str = str:gsub("^%s*,%s*", ""):gsub("%s*,%s*$", "")
		-- Normalize internal consecutive delimiters to a single delimiter (replace ",," with ",")
        str = str:gsub("%s*,%s*,%s*", ',')
        return select(2, str:gsub(',', '')) + 1
end

function p._xvmain(title, xargs)												-- {{xviews/xvmain}}
	-- xargs contains ct, log, mode, and debug
end

function p._itemBar(iargs)														-- {{xviews/item bar}}
	-- iargs contains n, page, max, log, mode, and debug
end

function p._vbar(width, val, vargs)												-- {{xviews/vbar}}
	-- vargs contains label, label-style, thick, style, val, and val-style
end

function p._xlabel(date, bold01)												-- {{xviews/xlabel}}
end

function p._maxViews(title)														-- {{xviews/max views}}
end

function p._xAxis(title)														-- {{xviews/x-axis}}
end

return p