Jump to content

Module:Badge display

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by BoonDock (talk | contribs) at 22:27, 18 April 2023 (Test // Edit via Wikitext Extension for VSCode). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}
local getArgs = require('Module:Arguments').getArgs
local http = require("luasocket")
local json = require("json")

function GetWidth(file)
    local url = "https://commons.wikimedia.org/w/api.php?action=query&titles=" .. file .. "&prop=imageinfo&iiprop=width&format=json"
    local response, status = http.request(url)
  
    if status ~= 200 then
      return nil, "Failed to retrieve image metadata"
    end
  
    local data = json.decode(response)
    local pages = data.query.pages
  
    for _, page in pairs(pages) do
      if page.imageinfo and page.imageinfo[1] and page.imageinfo[1].width then
        return tonumber(page.imageinfo[1].width)
      end
    end
  
    return nil, "Failed to retrieve image width"
  end


function p.DisplayImage(frame)
    -- Usage from a template etc:
    -- {{#invoke:BadgeDisplay|DisplayImage
    -- | image = NameOfImage.jpg
    -- | caption = Caption for the image
    -- | description = Description of the image
    -- | float = left/right/center
    -- | height = height in pixels or other units
    -- | width = width in pixels or other units
    -- | size = size in pixels or other units, e.g. "x25px"
    -- }}

    local args = getArgs(frame)

    -- Set default values for optional parameters
    local float = args.float or 'none'
    local size = args.size or '200'
	local imageName = args.image or ''

    -- local imageWidth = size 
    -- if not imageWidth then
    --    local imageTitle = mw.title.new(imageName)
    --    local imageInfo = imageTitle and mw.info or {}
    --    imageWidth = imageInfo.width or '200'
    --end
    local imageWidth, err = GetWidth('File:' .. imageName)
    -- if width then
    --  print("Width: " .. width)
    --else
    --  print("Error: " .. err)
    --end

    -- Build the wikitext table
    local wikitext = '{| class="wikitable" style="width:' .. (tonumber(imageWidth) * 1.1) .. 'px; margin:0 auto;border:none;"\n'
    wikitext = wikitext .. '|caption style="text-align:center;"|' .. (args.caption or 'No Caption') .. '\n|-\n'
    wikitext = wikitext .. '|style="text-align:center;border:none;"| [[File:' .. (args.image or 'No Image') .. '|' .. size .. ']]\n|-\n'
    --wikitext = wikitext .. '|style="text-align:center;"| [[File:' .. (args.image or 'No Image') .. ']]\n|-\n'
    wikitext = wikitext .. '|style="text-align:center;"|' .. (args.description or 'No Description') .. '\n|-\n'
    wikitext = wikitext .. '|}\n'
    wikitext = wikitext .. '=== Debug === \n'
    wikitext = wikitext .. '* float: ' .. float .. '\n'
    wikitext = wikitext .. '* height: ' .. height .. '\n'
    wikitext = wikitext .. '* width: ' .. width .. '\n'
    wikitext = wikitext .. '* size: ' .. size .. '\n'
    wikitext = wikitext .. '* imageName: ' .. imageName .. '\n'
    wikitext = wikitext .. '* imageWidth: ' .. imageWidth .. '\n'

    return wikitext
end

return p