PhantomCaleb (talk | contribs) (prep work for modularizing FileInfo. Will do the actual transition during a quieter time.) |
PhantomCaleb (talk | contribs) (testing something) |
||
Line 161: | Line 161: | ||
return result |
return result |
||
+ | end |
||
+ | |||
+ | -- MediaWiki:UploadField-select-Type |
||
+ | function p.SelectType(frame) |
||
+ | return utilsMarkup.bulletList({ |
||
+ | "screenshot|Screenshot" |
||
+ | }) |
||
end |
end |
||
Revision as of 16:39, 25 July 2020
This is the main module for the following templates:
In addition, this module exports the following functions.
image
image(game, subject, type, [options])
See also utilsMarkup.file.
Parameters
Returns
- A
string
of wikitext that renders a thumbnail.
Examples
# | Input | Output | Result | Status | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | image(
"TWW",
"Great Fairy Figurine",
"Model",
{
size = "100px",
link = "Great Fairy",
}
)
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
If file does not exist, show 'click to upload' thumbnail which links to Special:Upload. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
2 | image(
"TWWHD",
"Great Fairy Figurine",
"Model",
{
size = "150px",
link = "Great Fairy",
}
)
|
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
'No image' thumbnail has minimum 100px width, because it is illegible at smaller sizes. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
3 | image(
"TWWHD",
"Great Fairy Figurine",
"Model",
{
size = "64px",
link = "Great Fairy",
}
)
|
|
icon
icon(game, subject, [options])
Parameters
Returns
- An icon thumbnail for the subject in the given game.
Examples
# | Input | Output | Result | Status | ||||
---|---|---|---|---|---|---|---|---|
4 | icon("LANS", "Pineapple")
|
| ||||||
5 | icon("LADX", "Pineapple")
|
|
local p = {}
local h = {}
local Franchise = require("Module:Franchise")
local Term = require("Module:Term")
local utilsCargo = require("Module:UtilsCargo")
local utilsLayout = require("Module:UtilsLayout")
local utilsMarkup = require("Module:UtilsMarkup")
local utilsPage = require("Module:UtilsPage")
local utilsString = require("Module:UtilsString")
local utilsTable = require("Module:UtilsTable")
local data = mw.loadData("Module:File/Data")
local cargo = {
tableName = "Files",
fields = {
summary = "String",
type = "String",
source = "Wikitext",
game = "String",
licensing = "String",
subjects = "List (,) of String",
width = "Integer",
height = "Integer",
}
}
-- Template:FileInfo
function p.CargoDeclare(frame)
return utilsCargo.declare(cargo.tableName, cargo.fields)
end
function p.Main(frame)
local args, err = utilsArg.parse(frame:getParent().args, p.Templates.FileInfo)
utilsCargo.store(cargo.tableName, args)
local result = p.printFileInfo(args)
if err then
return result .. utilsMarkup..categories(err.categories)
else
return result
end
end
function p.printFileInfo(args)
return h.printFileInfoTable(args) .. h.categories(args.type, args.game, args.subject)
end
function h.printFileInfoTable(args)
local gameDisplay
if args.game then
local gameLogo = Franchise.logo(args.game)
local gameImage = gameLogo and utilsPage.exists(gameLogo) and utilsMarkup.file(gameLogo, { size = "130px" })
local gameLink = Franchise.link(args.game)
local gameText = gameLink and string.format("This is a file pertaining to %s.", gameLink)
if gameImage and gameText then
gameDisplay = gameImage .. " " .. gameText
elseif gameText then
gameDisplay = gameText
else
gameDisplay = ""
end
end
local license = mw.getCurrentFrame():expandTemplate({
title = "FileInfo/" .. args.license,
args = args
})
local html = mw.html.create("table"):attr("id", "fileinfo")
h.row(html, "Summary", args.summary)
h.row(html, "Type", args.type)
h.row(html, "Game", gameDisplay)
h.row(html, "Licensing", license, {
rowspan = args.trademark and "2" or "1"
})
h.row(html, "Trademark", mw.getCurrentFrame():expandTemplate({ title = "FileInfo/Trademark" }))
return tostring(html)
end
function h.row(html, field, value, attributes)
if value then
return html
:tag("tr")
:tag("th")
:content(field)
:done()
:tag("td")
:content(value)
:done()
:done()
end
end
function h.categories(type, game, subjects)
local game = game and Franchise.shortName(game)
local typeCat = type and data.typesByKey[type].cat
local categories = {}
if typeCat and not typeCat.nogame and game then
table.insert(categories, game .. " " .. typeCat)
elseif typeCat then
table.insert(categories, typeCat)
elseif gameCat then
table.insert(categories, game .. " Files")
end
if subjects then
categories = utilsTable.concat(categories, h.subjectCategories(subjects))
end
return utilsMarkup.categories(subjects)
end
function h.subjectCategories(subjects)
local categories = utilsTable.flatMap(subjects, function(subject)
local term, err = Term.fetchTerm(subject, args.game)
if term then
term = string.gsub(term, "#", "") -- strip # from term because categories can't have them in their name
local category = "Images of "..term
if utilsPage.exists(category) then -- only add subject-based categories if they already exist, to avoid spamming Special:WantedCategories
return {}
else
return {category}
end
else
return err.categories -- add term-related maintenance categories, if any
end
end)
return categories
end
-- Various templates
function p.Icon(frame)
local args = frame.args
return p.icon(args[1], args[2], {
size = args.size
})
end
-- Module:File/Data
function p.Data(frame)
local result = ""
result = result .. utilsMarkup.heading(2, "Types")
result = result .. utilsLayout.table({
sortable = true,
headers = {"Type", "Category"},
rows = utilsTable.map(data.types, function(type)
local key = utilsMarkup.code(type.key)
local cat = "[[:Category:"..type.cat.."]]"
return {key, cat}
end)
})
result = result .. utilsMarkup.heading(2, "Licenses")
result = result .. utilsLayout.table({
sortable = true,
headers = {"License", "Template", "Output"},
rows = utilsTable.map(data.licenses, function(license)
local template = "FileInfo/"..license
local templateLink = "[[Template:"..template.."]]"
local templateOutput = mw.getCurrentFrame():expandTemplate({title = template})
return {utilsMarkup.code(license), templateLink, templateOutput}
end)
})
return result
end
-- MediaWiki:UploadField-select-Type
function p.SelectType(frame)
return utilsMarkup.bulletList({
"screenshot|Screenshot"
})
end
-- Utilities
function p.image(game, subject, type, options)
local parts = utilsTable._filter(utilsString.notEmpty)({game, subject, type})
local filename = table.concat(parts, " ") .. ".png"
if utilsPage.exists("File:" .. filename) then
return utilsMarkup.file(filename, options)
else
local uploadUrl = mw.uri.fullUrl("Special:Upload")
uploadUrl:extend({
wpDestFile = filename
})
local options = utilsTable.merge({}, options, {
link = tostring(uploadUrl)
})
-- Make sure thumbnail for 'no image' is no less than 100x100px
local size
for dimension in string.gmatch(options.size or "", "[0-9]+") do
if tonumber(dimension) < 100 then
size = "100px"
end
end
options.size = size or options.size or "100px"
return utilsMarkup.file("File:No Image Upload.png", options)
end
end
function p.icon(game, subject, options)
local type = "Icon"
if Franchise.graphics(game) == "2D" then
type = "Sprite"
end
return p.image(game, subject, type, options)
end
p.Templates = {
FileInfo = {
purpose = "Displays, categorizes, and stores file information. See [[Guidelines:Files]] for further guidance.",
format = "block",
paramOrder = {"summary", "subject", "type", "source", "game", "licensing", "trademark"},
params = {
summary = {
--required = true,
type = "content",
desc = "A short description of the file.",
trim = true,
nilIfEmpty = true,
},
type = {
--required = true,
type = "string",
desc = "The type of file, which determines how it is [[:Category:Files by Type|categorized]].",
enum = data.typesEnum,
trim = true,
nilIfEmpty = true,
},
source = {
--required = true,
type = "string",
desc = "The original source of the file. It may be in the form of a URL or author recognition. [[Template:Source]] exists for this purpose.",
trim = true,
nilIfEmpty = true,
},
subject = {
--required = true,
type = "string",
desc = "Wiki article names of all the subjects depicted in the file. A comma-separated list.",
split = true,
trim = true,
nilIfEmpty = true,
},
game = {
--required = true,
type = "string",
desc = "A [[Data:Franchise|game code]].",
enum = Franchise.enumGames(),
trim = true,
nilIfEmpty = true,
},
licensing = {
--required = true,
type = "string",
desc = "The copyright licensing for the file. For the vast majority of files, <code>Copyright</code> is the correct value here.",
enum = data.licenses,
trim = true,
nilIfEmpty = true,
},
trademark = {
type = "boolean",
desc = "Enter any text to add a trademark notice to the licensing. Use on all [[:Category:Trademarks|trademarks]] (usually denoted by an ® or ™ symbol).",
trim = true,
nilIfEmpty = true,
}
},
examples = {
vertical = true,
{
summary = "{{Term|LADX|Animal Village|link}}",
subject = "Animal Village, Rabbit",
type = "map",
source = "{{Source|Original|MannedTooth}}",
game = "LADX",
licensing = "Copyright",
},
{
summary = "The [[Timeline]]",
source = "{{Cite Book|book= E |page= 10}}",
type = "print",
game = "Series",
licensing = "Copyright"
},
{
summary = "Nintendo's current logo.",
type = "logo",
source = "",
licensing = "PD-Simple",
trademark = "yes",
},
},
}
}
local optionsSchema = {
type = "record",
properties = {
{
name = "size",
type = "string",
desc = "Image size in pixels.",
},
{
name = "link",
type = "string",
desc = "Name of a page on the wiki or an external URL for the image thumbnail to link to.",
},
{
name = "caption",
type = "string",
desc = "[https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img Alt text] for the image.",
},
}
}
p.Schemas = {
image = {
game = {
type = "string",
required = true,
desc = "A [[Data:Franchise|game code]]."
},
subject = {
type = "string",
required = true,
},
type = {
type = "string",
required = true,
enum = {"", "Artwork", "Icon", "Model", "Render", "Screenshot", "Sprite", "Texture"},
},
options = optionsSchema
},
icon = {
game = {
type = "string",
required = true,
desc = "A [[Data:Franchise|game code]]."
},
subject = {
type = "string",
required = true,
},
options = optionsSchema,
}
}
p.Documentation = {
image = {
desc = "See also [[Module:UtilsMarkup#file|utilsMarkup.file]].",
params = {"game", "subject", "type", "options"},
returns = "A <code>string</code> of wikitext that renders a thumbnail.",
cases = {
{
args = {"TWW", "Great Fairy Figurine", "Model", {
link = "Great Fairy",
size = "100px"
}},
expect = "[[File:TWW Great Fairy Figurine Model.png|100px|link=Great Fairy|TWW Great Fairy Figurine Model.png]]",
},
{
desc = "If file does not exist, show 'click to upload' thumbnail which links to [[Special:Upload]].",
args = {"TWWHD", "Great Fairy Figurine", "Model", {
link = "Great Fairy",
size = "150px"
}},
expect = "[[File:No Image Upload.png|150px|link=https://zelda.gamepedia.com/Special:Upload?wpDestFile=TWWHD+Great+Fairy+Figurine+Model.png|File:No Image Upload.png]]",
},
{
desc = "'No image' thumbnail has minimum 100px width, because it is illegible at smaller sizes.",
args = {"TWWHD", "Great Fairy Figurine", "Model", {
link = "Great Fairy",
size = "64px",
}},
expect = "[[File:No Image Upload.png|100px|link=https://zelda.gamepedia.com/Special:Upload?wpDestFile=TWWHD+Great+Fairy+Figurine+Model.png|File:No Image Upload.png]]",
},
}
},
icon = {
params = {"game", "subject", "options"},
returns = "An icon thumbnail for the subject in the given game.",
cases = {
{
args = {"LANS", "Pineapple"},
expect = "[[File:LANS Pineapple Icon.png|LANS Pineapple Icon.png]]"
},
{
args = {"LADX", "Pineapple"},
expect = "[[File:LADX Pineapple Sprite.png|LADX Pineapple Sprite.png]]"
},
}
}
}
return p