mediawiki

tooltip for pdf size (automatically found)


I have tables listing various pdf documents in my wiki. I use [[Image:Pdficon.png|16px|link={{filepath:MyFileName.pdf}}]] in the wiki table to display a small icon (Pdficon.png) that when clicked opens the pdf file.

enter image description here

I want to get a popup showing the size of the pdf file when I mouse over the icon. I know I can do it "manually" with the RegularTooltips extension like this: {{#inline-tooltip: | 350 kB}}.

However, I would like to have some wiki code that finds the filesize (e.g. 350 kB) and populates that popup for me automatically.

Is there anything remotely close to this already available?


Solution

  • If you have Scribunto installed, you can use the file attribute of the mw.title object by defining Module:File data:

    -- Convert a property to custom unit of measurement:
    local bit32 = require 'bit32'
    local conversions = {
        size = function (bytes, unit)
            local shifts = { b = 0, kb = 10, mb = 20, gb = 30 }
            return bit32.rshift (bytes, shifts [(unit or 'b'):lower()] or 0)
        end
    }
    
    -- Function that returns a function returning certain file attribute:
    local function attribute_function (attr)
        return function (frame)
            local text = frame.args [1]
            local title = mw.title.new (text, 6) -- 6 is File:.
            local attrs = title.file
            if not attrs.exists then
                return nil
            end
            -- Take multi-page files into account (| page = n):
            if frame.args.page and attrs.pages then
                attrs = attrs.pages[tonumber (frame.args.page) or 1]
            end
            local value = attrs [attr]
            -- Process optional parameters if defined for this property:
            if conversions [attr] then
                -- Normal table methods do not work on frame.args:
                local optional = {}
                for key, arg in ipairs (frame.args) do
                    if key ~= 1 then
                        optional [key - 1] = arg
                    end
                end
                value = conversions [attr] (value, unpack (optional))
            end
            return value
        end
    end
    
    local module = {}
    
    for _, property in ipairs {
        'width',
        'height',
        'size',
        'mimeType',
        'length'
    } do
        module [property] = attribute_function (property)
    end
    
    return module
    

    Get the file size by adding to wikitext: