Module:File: Difference between revisions
From Neodyland Wiki
More actions
m return "unknown" for file types where the limit is not known |
adding tests, woExtension |
||
| Line 3: | Line 3: | ||
local funcs = {} | local funcs = {} | ||
-- =p.File("Foo.bar.svg").extension() | -- =p.File("Foo.bar.svg").extension() | ||
-- @return "svg" | |||
funcs.extension = function() | funcs.extension = function() | ||
local parts = mw.text.split( title, '.', true ) | local parts = mw.text.split( title, '.', true ) | ||
return parts[#parts] | return parts[#parts] | ||
end | |||
-- =p.File("Foo.bar.svg").woExtension() | |||
-- Original author: Bawolff at [[Module:FileName]] | |||
-- @return "Foo.bar" | |||
funcs.woExtension = function() | |||
local parts = mw.text.split( title , '.', true ) | |||
local upTo = #parts - 1 | |||
if upTo == 0 then upTo = 1 end | |||
return table.concat( parts, '.', 1, upTo ) | |||
end | end | ||
-- mapping file extensions to MIME-types | -- mapping file extensions to MIME-types | ||
| Line 15: | Line 25: | ||
TIF = "image/tiff", | TIF = "image/tiff", | ||
JPEG = "image/jpeg", | JPEG = "image/jpeg", | ||
JPG = "image/jpeg" | |||
} | } | ||
-- =p.File("Foo.bar.svg").mime() | -- =p.File("Foo.bar.svg").mime() | ||
| Line 36: | Line 46: | ||
end | end | ||
-- Helpers | |||
local h = {} | |||
h.getArg = function(f) | |||
return f.args[1] or f.args["file"] or f.args["title"] | |||
end | |||
-- @exports | |||
local p = {} | local p = {} | ||
p.File = File | p.File = File | ||
p.extension = function(frame) | p.extension = function(frame) | ||
return File( | return File( h.getArg(frame) ).extension():lower() | ||
end | end | ||
p.extensionUpper = function(frame) | p.extensionUpper = function(frame) | ||
return File( | return File( h.getArg(frame) ).extension():upper() | ||
end | |||
p.woExtension = function(frame) | |||
return File( h.getArg(frame) ).woExtension() | |||
end | end | ||
p.mime = function(frame) | p.mime = function(frame) | ||
return File( | return File( h.getArg(frame) ).mime() | ||
end | end | ||
p.maxthumb = function(frame) | p.maxthumb = function(frame) | ||
return File(frame. | return File( h.getArg(frame) ).maxthumb() | ||
end | |||
p.runTests = function() | |||
local toTest = require('Module:File/tests/all') | |||
for i, t in ipairs(toTest) do | |||
local f = File(t.fileName) | |||
local stringResult = '' | |||
local ret = true | |||
local results = { | |||
extension = (t.extension == f.extension():lower()), | |||
extensionUpper = (t.extensionUpper == f.extension():upper()), | |||
woExtension = (t.woExtension == f.woExtension()), | |||
mime = (t.mime == f.mime()), | |||
maxthumb = (not (tonumber(f.maxthumb()) == nil) == t.maxthumbIsNumber) | |||
} | |||
for k, v in pairs(results) do | |||
stringResult = stringResult .. k .. ': ' .. (v and 'ok ' or 'failed') .. ' ' | |||
ret = ret and v | |||
end | |||
mw.log(i, ret and 'passed' or 'FAILED', t.typeOfFileName, (not ret) and ('\n >>' .. stringResult .. '\n >> ' .. t.fileName) or '') | |||
end | |||
end | end | ||
return p | return p | ||
Revision as of 10:05, 4 December 2013
This documentation is transcluded from Module:File/doc.
Scope
File title and file information. If the code here grows beyond a border line, this should be converted to an interface and the processing code should be outsourced.[Please clarify – October 2024]
Usage
Note that an error is thrown if the title can't be constructed (e.g. a title containing invalid characters is supplied) or metadata isn't available (e.g. due to missing file or extraction error). It is your responsibility to catch them; in Lua through pcall(); from Wikitext as {{#iferror: {{#invoke:File|function|file=File:P%bar]baz.qqq}} | meaningful message}}.
From Wikitext
Examples:
{{#invoke:File|woExtension|file=File:Test.C.JpeG}}→File:Test.C{{#invoke:File|extension|file=File:Test.C.JpeG}}→jpeg{{#invoke:File|csExtension|file=File:Test.C.JpeG}}→Script error: The function "csExtension" does not exist.{{#invoke:File|mime|file=File:DOESNOTEXIST-0a8de8c83.Png}}→image/pngdeprecated (but works even if the file does not exist and has no metadata){{#invoke:File|mimeType|file=File:DOESNOTEXIST-0a8de8c83.Png}}→Script error: The function "mimeType" does not exist.preferred (but will not work if the file does not exist to parse its metadata){{#invoke:File|fileExists|file=File:Commons-logo.png}}→Script error: The function "fileExists" does not exist.{{#invoke:File|fileExists|file=File:DOESNOTEXIST-0a8de8c83.png}}→Script error: The function "fileExists" does not exist.{{#invoke:File|fileExistsRelaxed|file=File:Ba[!#llot-sprite.png}}→Script error: The function "fileExistsRelaxed" does not exist.
{{#invoke:File|width|file=File:Commons-logo.png}}→Script error: The function "width" does not exist.{{#invoke:File|height|file=File:Commons-logo.png}}→Script error: The function "height" does not exist.{{#invoke:File|dimensions|file=File:Commons-logo.png}}→Script error: The function "dimensions" does not exist.{{#invoke:File|size|file=File:Commons-logo.png}}→Script error: The function "size" does not exist.
-- Module for handling Media files (Origin: Wikimedia Commons)
local File = function(title)
local funcs = {}
-- =p.File("Foo.bar.svg").extension()
-- @return "svg"
funcs.extension = function()
local parts = mw.text.split( title, '.', true )
return parts[#parts]
end
-- =p.File("Foo.bar.svg").woExtension()
-- Original author: Bawolff at [[Module:FileName]]
-- @return "Foo.bar"
funcs.woExtension = function()
local parts = mw.text.split( title , '.', true )
local upTo = #parts - 1
if upTo == 0 then upTo = 1 end
return table.concat( parts, '.', 1, upTo )
end
-- mapping file extensions to MIME-types
funcs.extensionMap = {
PNG = "image/png",
GIF = "image/gif",
SVG = "image/svg+xml",
TIFF = "image/tiff",
TIF = "image/tiff",
JPEG = "image/jpeg",
JPG = "image/jpeg"
}
-- =p.File("Foo.bar.svg").mime()
funcs.mime = function()
local extension = funcs.extension():upper()
return funcs.extensionMap[extension] or "unknown"
end
-- =p.File("Foo.bar.tiff").maxthumb()
funcs.maxthumb = function()
local mime = funcs.mime()
if mime == "image/png" then
return mw.getCurrentFrame():preprocess( '{{LargePNG/limit}}' )
elseif mime == "image/tiff" or mime == "image/gif" then
return mw.getCurrentFrame():preprocess( '{{LargeTIFF/limit}}' )
else
return 'unknown @Module:File'
end
end
return funcs
end
-- Helpers
local h = {}
h.getArg = function(f)
return f.args[1] or f.args["file"] or f.args["title"]
end
-- @exports
local p = {}
p.File = File
p.extension = function(frame)
return File( h.getArg(frame) ).extension():lower()
end
p.extensionUpper = function(frame)
return File( h.getArg(frame) ).extension():upper()
end
p.woExtension = function(frame)
return File( h.getArg(frame) ).woExtension()
end
p.mime = function(frame)
return File( h.getArg(frame) ).mime()
end
p.maxthumb = function(frame)
return File( h.getArg(frame) ).maxthumb()
end
p.runTests = function()
local toTest = require('Module:File/tests/all')
for i, t in ipairs(toTest) do
local f = File(t.fileName)
local stringResult = ''
local ret = true
local results = {
extension = (t.extension == f.extension():lower()),
extensionUpper = (t.extensionUpper == f.extension():upper()),
woExtension = (t.woExtension == f.woExtension()),
mime = (t.mime == f.mime()),
maxthumb = (not (tonumber(f.maxthumb()) == nil) == t.maxthumbIsNumber)
}
for k, v in pairs(results) do
stringResult = stringResult .. k .. ': ' .. (v and 'ok ' or 'failed') .. ' '
ret = ret and v
end
mw.log(i, ret and 'passed' or 'FAILED', t.typeOfFileName, (not ret) and ('\n >>' .. stringResult .. '\n >> ' .. t.fileName) or '')
end
end
return p