「モジュール:File」の版間の差分
ナビゲーションに移動
検索に移動
require strict per TPER
細 (1版 をインポートしました) |
bsd>Jonesey95 (require strict per TPER) |
||
1行目: | 1行目: | ||
-- Module for handling Media files (Origin: Wikimedia Commons) | -- Module for handling Media files (Origin: Wikimedia Commons) | ||
require('strict') | |||
-- ================================================== | |||
-- === helper Lookup table ========================== | |||
-- ================================================== | |||
local extensionMap = { | |||
-- | |||
-- | |||
-- File types with full support in Commons (See [[c:Commons:File types]]). | -- File types with full support in Commons (See [[c:Commons:File types]]). | ||
DJVU = 'image/vnd.djvu', | DJVU = 'image/vnd.djvu', | ||
61行目: | 46行目: | ||
FLV = 'video/x-flv', -- (deprecated, replaced by MP4) please convert to OGV or WEBM | FLV = 'video/x-flv', -- (deprecated, replaced by MP4) please convert to OGV or WEBM | ||
ICO = 'image/vnd.microsoft.icon', -- used in MediaWiki resources for 'website icons' | ICO = 'image/vnd.microsoft.icon', -- used in MediaWiki resources for 'website icons' | ||
MP4 = 'video/mp4', | MP4 = 'video/mp4', | ||
QT = 'video/quicktime', -- (deprecated, replaced by MP4) please convert to OGV or WEBM | QT = 'video/quicktime', -- (deprecated, replaced by MP4) please convert to OGV or WEBM | ||
RA = 'audio/vnd.rn-realaudio', -- (deprecated, replaced by MP3) please convert to OGA | RA = 'audio/vnd.rn-realaudio', -- (deprecated, replaced by MP3) please convert to OGA | ||
69行目: | 54行目: | ||
XLS = 'application/vnd.ms-excel', -- please convert to PDF, DJVU, or Wiki | XLS = 'application/vnd.ms-excel', -- please convert to PDF, DJVU, or Wiki | ||
} | } | ||
local | -- ================================================== | ||
-- === Local functions ============================== | |||
-- ================================================== | |||
local function filename(frame) | |||
return frame.args[1] or frame.args["file"] or frame.args["title"] | |||
end | |||
local function getMetadata(frame) | |||
local fname = filename(frame) | |||
local title | |||
if fname then | |||
return | title = mw.title.new(fname, 6) | ||
else | |||
title = mw.title.getCurrentTitle() | |||
end | |||
if title then | |||
return title.file | |||
else | |||
return {exists=false} | |||
end | end | ||
end | |||
-- ================================================== | |||
-- === External functions =========================== | |||
-- ================================================== | |||
local p = {} | |||
-- p.csExtension("Foo.bar.svg") | |||
-- @return "svg" | |||
function p.csExtension(frame) | |||
local fname = filename(frame) or mw.title.getCurrentTitle().text | |||
local parts = mw.text.split(fname, '.', true) | |||
if #parts>1 then | |||
return parts[#parts] | return parts[#parts] | ||
else | |||
return '' | |||
end | end | ||
end | |||
function p.extension(frame) | |||
return p.csExtension(frame):lower() | |||
end | |||
function p.extensionUpper(frame) | |||
return p.csExtension(frame):upper() | |||
end | |||
-- p.woExtension("Foo.bar.svg") | |||
-- @return "Foo.bar" | |||
-- Original author: Bawolff at [[Module:FileName]] | |||
function p.woExtension(frame) | |||
local fname = filename(frame) or mw.title.getCurrentTitle().text | |||
local parts = mw.text.split(fname , '.', true) | |||
local upTo = #parts - 1 | |||
if upTo == 0 then upTo = 1 end | |||
return table.concat(parts, '.', 1, upTo) | |||
end | |||
function p.mime(frame) | |||
local meta = getMetadata(frame) | |||
if meta.exists then | |||
return meta.mimeType | |||
else | |||
return extensionMap[p.extensionUpper(frame)] or 'unknown' | |||
end | end | ||
end | |||
return | function p.mimeType(frame) | ||
return getMetadata(frame).mimeType | |||
end | end | ||
return | function p.fileExists(frame) | ||
return getMetadata(frame).exists or '' | |||
end | end | ||
-- This one won't throw errors at you | -- This one won't throw errors at you | ||
p.fileExistsRelaxed | function p.fileExistsRelaxed(frame) | ||
local ok, metadata = pcall( | local ok, metadata = pcall(getMetadata(frame)) | ||
if ok then | if ok then | ||
return metadata.exists or '' | return metadata.exists or '' | ||
185行目: | 140行目: | ||
end | end | ||
end | end | ||
p. | |||
function p.maxthumb(frame) | |||
-- Limits in megapixels are currently stored in Commons templates, they could be in this module. | |||
-- There may be more limits for other supported mime types (djvu, flac, ogv, pdf, svg, webm, xcf). | |||
local maxthumbMap = { | |||
['image/gif'] = 1000, | |||
['image/png' ] = 2500, -- see Template:LargePNG/limit | |||
['image/tiff'] = 1000, -- see Template:LargeTIFF/limit | |||
} | |||
local mime = p.mime(frame) | |||
return maxthumbMap[mime] or 'unknown @Module:File' | |||
end | end | ||
p. | |||
return | function p.dateWorkCreated(frame) | ||
return '' -- no longer supported | |||
end | end | ||
p. | |||
return | function p.width(frame) | ||
return getMetadata(frame).width | |||
end | end | ||
p. | |||
return | function p.height(frame) | ||
return getMetadata(frame).height | |||
end | end | ||
p. | |||
return | function p.dimensions(frame) | ||
local d = getMetadata(frame) | |||
if d.exists then | |||
return d.width .. ' × ' .. d.height | |||
else | |||
return '' | |||
end | |||
end | end | ||
p. | |||
function p.size(frame) | |||
return getMetadata(frame).size | |||
end | end | ||
function p.pageCount(frame) | |||
local pages = getMetadata(frame).pages or {1} | |||
p.pageCount | |||
local pages = | |||
return #pages | return #pages | ||
end | end | ||
return p | return p |