Last edited 4 days ago
by Redaktion

Module:PageViews: Difference between revisions

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)..."
 
No edit summary
 
Line 1: Line 1:
-- Module:PageViews (compact)
-- Module:PageViews (preprocess variant)
-- Live page view count via Extension:HitCounters' NUMBEROFVIEWS.
-- Live page view count via Extension:HitCounters' {{NUMBEROFVIEWS:Title}}.
-- Usage:
-- Usage:
--  {{#invoke:PageViews|get}}                        -- current page, localized
--  {{#invoke:PageViews|get}}                        -- current page, localized
--  {{#invoke:PageViews|get|page=Main Page}}          -- specific 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=Main Page|plain=yes}}-- just the number
--  {{#invoke:PageViews|get|page=Foo|fallback=0}}    -- 0 if not available
--  {{#invoke:PageViews|get|page=Foo|fallback=0}}    -- fallback if unavailable


local p = {}
local p = {}
Line 18: Line 18:
local function fetchViews(title)
local function fetchViews(title)
     local frame = mw.getCurrentFrame()
     local frame = mw.getCurrentFrame()
     -- Try raw flags (some HitCounters builds support them), then fallback.
     -- Try raw flag if your HitCounters build supports it, then fall back.
     local v = frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText .. '|R')
     local wikitext = string.format('{{NUMBEROFVIEWS:%s|R}}', title.prefixedText)
          or frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText .. '|raw')
    local v = frame:preprocess(wikitext)
          or frame:callParserFunction('NUMBEROFVIEWS', title.prefixedText)
    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
     if not v or v == '' then return nil end
     local digits = tostring(v):gsub('[^0-9]', '')
     local digits = tostring(v):gsub('[^0-9]', '')
Line 28: Line 30:
end
end


-- {{#invoke:PageViews|get|page=...|plain=yes|fallback=...}}
function p.get(frame)
function p.get(frame)
     -- collect args (support parent template)
     -- collect args (support parent template)

Latest revision as of 16:20, 19 March 2026

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