Module:Util
From Another Eden Wiki
Documentation for this module may be created at Module:Util/doc
local p = {}
local cargo = mw.ext.cargo
p.getMaterial = function(itemName)
local tables = 'Materials'
local queryFields = '_pageName=page,Name,Type'
local queryArgs = {
where = 'Name="' .. itemName .. '"',
limit = 1
}
local query = cargo.query(tables, queryFields, queryArgs)
return query[1]
end
function p.fetchPath(frame, image, thumb)
local search_title
if frame.args.image ~= nil then
search_title = mw.text.decode(frame.args.image, true)
else
search_title = image
end
local thumb_size
if frame.args.size ~= nil then
thumb_size = frame.args.size
else
thumb_size = thumb
end
if (thumb_size ~= nil) and (string.match(thumb_size, '^%d+px$') == nil) then
error('thumb_size value not valid', 1)
end
local title = p.fetchFinalTitle(mw.title.makeTitle('Image', search_title))
if title == nil then
return ''
end
--mw.log(title)
local filename = title:partialUrl()
--mw.log(filename)
local hash_path = p.calcHashPath(filename)
--mw.log(md5)
if thumb ~= nil then
local encoded_filename = mw.uri.encode(filename, 'WIKI')
return '/images/thumb/' .. hash_path .. '/' .. encoded_filename .. '/' .. thumb_size .. '-' .. encoded_filename;
else
return '/images/' .. hash_path .. '/' .. mw.uri.encode(filename, 'WIKI');
end
end
function p.fetchURL(frame, image, thumb)
return 'https://anothereden.wiki' .. p.fetchPath(frame, image, thumb)
end
function p.calcHashPath(filename)
local md5 = mw.hash.hashValue('md5', filename)
return string.sub(md5, 1, 1) .. '/' .. string.sub(md5, 1, 2)
end
function p.fetchFinalTitle(title)
if title == nil then
return nil
elseif title.redirectTarget == false then
return title
else
return p.fetchFinalTitle(title.redirectTarget)
end
end
return p