-- 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, ct) -- [{xviews/xvmain}]
end
function p._xAxis(title) -- {{xviews/x-axis}}
end
return p