Last edited 3 days ago
by Redaktion

PageViews

Revision as of 16:17, 19 March 2026 by Redaktion (talk | contribs) (Created page with "-- Module:PageViews (compact) -- Live page view count via Extension:HitCounters' NUMBEROFVIEWS. -- 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}} -- 0 if not available local p = {} local function normalizeTitle(txt)...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

-- Module:PageViews (compact)
-- Live page view count via Extension:HitCounters' NUMBEROFVIEWS.
-- 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}}     -- 0 if not available

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 flags (some HitCounters builds support them), then fallback.
    local v = frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText .. '|R')
           or frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText .. '|raw')
           or frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText)
    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

-- {{#invoke:PageViews|get|page=...|plain=yes|fallback=...}}
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