Documentation for this module may be created at Module:PageViews/doc
-- Module:PageViews (preprocess variant)
-- Live page view count via Extension:HitCounters' {{NUMBEROFVIEWS:Title}}.
-- Usage:
-- {{#invoke:PageViews|get}} -- current page, localized
-- {{#invoke:PageViews|get|page=Main Page}} -- specific page, localized
-- {{#invoke:PageViews|get|page=Main Page|plain=yes}}-- just the number
-- {{#invoke:PageViews|get|page=Foo|fallback=0}} -- fallback if unavailable
local p = {}
local function normalizeTitle(txt)
if not txt or txt == '' then
return mw.title.getCurrentTitle()
end
return mw.title.new(txt)
end
local function fetchViews(title)
local frame = mw.getCurrentFrame()
-- Try raw flag if your HitCounters build supports it, then fall back.
local wikitext = string.format('{{NUMBEROFVIEWS:%s|R}}', title.prefixedText)
local v = frame:preprocess(wikitext)
if not v or v == '' or v:match('^⧼') then
v = frame:preprocess(string.format('{{NUMBEROFVIEWS:%s}}', title.prefixedText))
end
if not v or v == '' then return nil end
local digits = tostring(v):gsub('[^0-9]', '')
if digits == '' then return nil end
return tonumber(digits)
end
function p.get(frame)
-- collect args (support parent template)
local a = frame.args or {}
local parent = frame:getParent()
if parent and parent.args then
for k,v in pairs(parent.args) do
if a[k] == nil or a[k] == '' then a[k] = v end
end
end
local t = normalizeTitle(a.page or a[1])
if not t then return a.fallback or '' end
local n = fetchViews(t)
if not n then return a.fallback or '' end
if (a.plain or ''):lower():match('^(yes|true|1)$') then
return tostring(n)
end
return mw.language.getContentLanguage():formatNum(n)
end
return p