Module:File

From Zelda Wiki, the Zelda encyclopedia
Jump to navigation Jump to search
This module exports the following functions.

image

image(filename, [options])

A higher-level version of Module:Util/markup/file with awareness of whether the file exists or not.

Parameters

  • filename
    Filename of the image, with or without the namespace prefix.
  • [options]
    [size]
    Image size in pixels.
    [scale]
    Image scaling factor — the original image size is multitplied by scale. If both scale and size are present, the value which results in the smaller image will be used. By default this uses an expensive parser function.
    [scaleUsingCargo]
    If set to true, then a Cargo query is used to determine the original image size for the scale option above. You can use this to avoid hitting expensive parser function limit. This option has an additional performance cost of roughly 2-5 milliseconds per image.
    [link]
    Name of a page on the wiki or an external URL for the image thumbnail to link to.
    [caption]
    Alt text for the image.
    [notPageImage]
    If true, the image will never appear as the page's representative image in page previews.
    [checkExists=true]

    If set to false then the function skips the file existence check. A red link is returned instead of the 'please upload' placeholder.

    This may be needed to prevent too many expensive parser function calls from occurring on a page.

    [isPixelArt]
    If true the image will be rendered using nearest-neighbor interpolation, which prevents pixel art (sprites, 2D game screenshots) from appearing blurry in thumbnails.

Returns

  • Wikitext rendering an image thumbnail.
  • A boolean — true if the image exists, false otherwise.

Examples

#InputOutputResultStatus
1
image(
  "File:TWW Great Fairy Figurine Model.png",
  {
    size = "100px",
    link = "Great Fairy",
  }
)
"[[File:TWW Great Fairy Figurine Model.png|100px|link=Great Fairy]]"
true
true
If file does not exist, show 'click to upload' thumbnail which links to Special:Upload.
2
image(
  "File:TWWHD Great Fairy Figurine Model.png",
  {
    size = "150px",
    link = "Great Fairy",
  }
)
"[[File:No Image Upload.png|150px|link=//zeldawiki.wiki/w/index.php?title=Special%3AUpload&wpDestFile=TWWHD+Great+Fairy+Figurine+Model.png|class=notpageimage]]"
false
false
'No image' thumbnail has minimum 100px width, because it is illegible at smaller sizes.
3
image(
  "File:TWWHD Great Fairy Figurine Model.png",
  { size = "64px" }
)
"[[File:No Image Upload.png|100px|link=//zeldawiki.wiki/w/index.php?title=Special%3AUpload&wpDestFile=TWWHD+Great+Fairy+Figurine+Model.png|class=notpageimage]]"
false
false
checkExists = false skips the existence check and simply render a red link
4
image(
  "File:TWWHD Great Fairy Figurine Model.png",
  {
    checkExists = false,
    size = "100px",
    link = "Great Fairy",
  }
)
"[[File:TWWHD Great Fairy Figurine Model.png|100px|link=Great Fairy]]"
File:TWWHD Great Fairy Figurine Model.png
nil
Scaling factor.
5
image(
  "File:TMC Vaati Sprite.png",
  {
    scale = 2,
    checkExists = false,
  }
)
"[[File:TMC Vaati Sprite.png|48x56px]]"
nil
The isPixelArt options ensures that sprites do not appear blurry.
6
image(
  "File:TMC Vaati Sprite.png",
  {
    checkExists = false,
    isPixelArt = true,
    size = "100px",
  }
)
"[[File:TMC Vaati Sprite.png|100px|class=pixel-art]]"
nil
Scaling using Cargo instead of an expensive parser function.
7
image(
  "File:TMC Vaati Sprite.png",
  {
    scale = 2,
    isPixelArt = true,
    checkExists = false,
    scaleUsingCargo = true,
  }
)
"[[File:TMC Vaati Sprite.png|48x56px|class=pixel-art]]"
nil
If both scale and size are specified, the one resulting in the smaller image is used.
8
image(
  "File:TMC Vaati Sprite.png",
  {
    scale = 2,
    isPixelArt = true,
    checkExists = false,
    size = "80px",
  }
)
"[[File:TMC Vaati Sprite.png|48x56px|class=pixel-art]]"
nil
9
image(
  "File:TMC Vaati Sprite.png",
  {
    scale = 10,
    isPixelArt = true,
    checkExists = false,
    size = "80px",
  }
)
"[[File:TMC Vaati Sprite.png|80px|class=pixel-art]]"
nil
Applying notpageimage.
10
image(
  "File:TMC Vaati Sprite.png",
  {
    notPageImage = true,
    checkExists = false,
    isPixelArt = true,
  }
)
"[[File:TMC Vaati Sprite.png|class=notpageimage pixel-art]]"
nil

gameImage

gameImage(game, subject, type, [options])

A specialized version of image that infers the filename from game, subject, and type.

Parameters

  • game
    A franchise code.
  • subject
  • type
  • [options]
    [size]
    Image size in pixels.
    [scale]
    Image scaling factor — the original image size is multitplied by scale. If both scale and size are present, the value which results in the smaller image will be used. By default this uses an expensive parser function.
    [scaleUsingCargo]
    If set to true, then a Cargo query is used to determine the original image size for the scale option above. You can use this to avoid hitting expensive parser function limit. This option has an additional performance cost of roughly 2-5 milliseconds per image.
    [link]
    Name of a page on the wiki or an external URL for the image thumbnail to link to.
    [caption]
    Alt text for the image.
    [notPageImage]
    If true, the image will never appear as the page's representative image in page previews.
    [checkExists=true]

    If set to false then the function skips the file existence check. A red link is returned instead of the 'please upload' placeholder.

    This may be needed to prevent too many expensive parser function calls from occurring on a page.

Returns

  • A string of wikitext that renders a thumbnail.
  • A boolean — true if the image exists, false otherwise.

Examples

#InputResultStatus
11
gameImage(
  "TWW",
  "Great Fairy Figurine",
  "Model",
  {
    size = "100px",
    link = "Great Fairy",
  }
)
true

icon

icon(game, subject, [options])

Parameters

  • game
    A franchise code.
  • subject
  • [options]
    [size]
    Image size in pixels.
    [scale]
    Image scaling factor — the original image size is multitplied by scale. If both scale and size are present, the value which results in the smaller image will be used. By default this uses an expensive parser function.
    [scaleUsingCargo]
    If set to true, then a Cargo query is used to determine the original image size for the scale option above. You can use this to avoid hitting expensive parser function limit. This option has an additional performance cost of roughly 2-5 milliseconds per image.
    [link]
    Name of a page on the wiki or an external URL for the image thumbnail to link to.
    [caption]
    Alt text for the image.
    [notPageImage]
    If true, the image will never appear as the page's representative image in page previews.
    [checkExists=true]

    If set to false then the function skips the file existence check. A red link is returned instead of the 'please upload' placeholder.

    This may be needed to prevent too many expensive parser function calls from occurring on a page.

Returns

  • An icon thumbnail for the subject in the given game.

Examples

#InputResultStatus
12
icon("LANS", "Pineapple")
13
icon("LADX", "Pineapple")

local p = {}
local h = {}

local Franchise = require("Module:Franchise")
local Util = {
	cargo = {
		query = require("Module:Util/cargo/query")
	},
	markup = {
		file = require("Module:Util/markup/file"),
	},
	pages = {
		exists = require("Module:Util/pages/exists"),
	},	
	strings = {
		isEmpty = require("Module:Util/strings/isEmpty"),
		notEmpty = require("Module:Util/strings/notEmpty"),
	},
	tables = {
		compact = require("Module:Util/tables/compact"),
		filter = require("Module:Util/tables/filter"),
		merge = require("Module:Util/tables/merge"),
	}
}

local CARGO_TABLE = "Files" 

-- Various templates
function p.Icon(frame)
	local args = frame.args
	local img = p.icon(args[1], args[2], {
		size = args.size,
	})
	return img
end

-- Utilities
function p.image(filename, options)
	if not string.find(filename, "^File:") then
		filename = "File:"..filename
	end
	options = options or {}
	local sizeWidth, sizeHeight = p.dimensions(options.size)
	local checkExists = options.checkExists ~= false
	
	-- If the file is a redirect and we are doing an existence check or getting width/height from Cargo, then we need to get the redirectTarget.
	-- This is a somewhat expensive operation to do at scale so we only do it when needed
	local originalFilename = filename
	if checkExists or options.scale then
		local redirectTarget = mw.title.new(filename).redirectTarget
		redirectTarget = redirectTarget and redirectTarget.prefixedText
		if redirectTarget then
			filename = redirectTarget
		end
	end
	
	if checkExists and not Util.pages.exists(filename, true) then
		-- We check again with mw.title To work around a bug where moved files are marked non-existant until null-edited
		-- This happens because of how we use Cargo to query for existence to save on expensive parser functions
		local title = mw.title.new(filename)
		if not title.fileExists then
			return h.noimage(filename, sizeWidth, sizeHeight, options), false
		end
	end
	
	if options.scale then
		local file
		if options.scaleUsingCargo then
			results = Util.cargo.query(CARGO_TABLE, "width, height", {
				where = {
					["_pageName"] = filename,
				},
				limit = 1,
			})
			file = results[1]
		end
		if not file or Util.strings.isEmpty(file.width) or Util.strings.isEmpty(file.height) then -- if scaleUsingCargo = false or data does not return from Cargo query for some reason, then use the title object (an expensive parser function)
			file = mw.title.new(filename).file
		end
		
		if file.width == nil or file.height == nil then -- can happen when a file is moved 
			return h.noimage(filename, sizeWidth, sizeHeight, options), false
		end

		local width = math.floor(tonumber(file.width) * options.scale)
		local height = math.floor(tonumber(file.height) * options.scale)
		if (sizeWidth and sizeWidth < width) or (sizeHeight and sizeHeight < height) then
			width = sizeWidth
			height = sizeHeight
		end
		size = ""
		if width then
			size = width
		end
		if height then
			size = size .. "x" .. height
		end
		size = size .. "px"
		options = Util.tables.merge({}, options, {
			size = size,
		})
	end
	local classes = Util.tables.compact({options.notPageImage and "notpageimage", options.isPixelArt and "pixel-art"})
	if #classes > 0 then
		options.class = table.concat(classes, " ")
	end
	return Util.markup.file(originalFilename, options), checkExists and true or nil
end
function p.dimensions(size)
	if not size then
		return nil
	end
	local s, e
	s, e = size:find("^[0-9]+")
	local width = s and size:sub(s, e) or ""
	
	s, e = size:find("x[0-9]+")
	local height = s and size:sub(s+1, e) or ""
	
	return tonumber(width), tonumber(height)
end
function h.noimage(filename, sizeWidth, sizeHeight, options)
	filename = string.gsub(filename, "^File:", "")
	local uploadUrl = mw.uri.fullUrl("Special:Upload", {
		wpDestFile = filename
	})
	local options = Util.tables.merge({}, options, {
		link = tostring(uploadUrl),
		class = "notpageimage",
	})
	-- Make sure thumbnail for 'no image' is no less than 100x100px
	if (sizeWidth and sizeWidth < 100) or (sizeHeight and sizeHeight < 100) then
		options.size = "100px"
	end
	return Util.markup.file("File:No Image Upload.png", options)
end

function p.gameImage(game, subject, type, options)
	options = options or {}
	local parts = {game, subject, type}
	parts = Util.tables.filter(parts, Util.strings.notEmpty)
	local filename = table.concat(parts, " ") .. ".png"
	if Franchise.graphics(game) == "2D" then
		options.isPixelArt = true
	end
	return p.image(filename, options)
end

function p.icon(game, subject, options)
	local type = "Icon"
	if Franchise.graphics(game) == "2D" then
		type = "Sprite"
	end
	options = options or {}
	options.isPixelArt = true
	return p.gameImage(game, subject, type, options)
end

return p