Description
This module creates download links based on hashes stored in submodule /hashes. It looks up hashes there based on template calls.
It is highly recommended to also have some JavaScript which removes empty p elements. Those are apparently also generated by the module, yet there doesn't seem to be any good reason to do so.
Individual elements
Table i18n
local i18n = {
process_args = 'Module:ProcessArgs',
hashes = 'Module:Downloadlinks/hashes',
category = 'Categorie:Invalid download link',
version_error = '[[Template:Downloadlink|No or invalid version given]]',
variant_error = '[[Template:Downloadlink|No or invalid variant given]]',
hash_error = '[[Template:Downloadlink|No hash found]]',
client = 'Client',
server = 'Server',
windows_server = '.exe',
json = '.json',
json_package = '.json',
nowiki_template = 'Downloadlink/nw',
}
In here:
process_args: Module where process arguments are located.hashes: Module where hashes are stored.category: (including namespace) Category where pages should be added to when no download link could be constructed.version_error: What to show when no version could be found in the hash module.variant_error: What to show when no variant could be found (e.g.client,server, etc.).hash_error: What to show when no hash could be found.client: Default text for a client download link.server: Default text for a server download link.windows_server: Default text for a Windows server download link.json: Default text for a JSON download link.json_package: Default text for JSON shown in-browser.nowiki_template: Name of template where a self-closingnowikielement is located. This is necessary to prevent the module from generating illogicalpres.
Getting and checking arguments from template call
Getting
Using
-- Checks before constructing
if args[1] then
version = mw.text.trim(tostring(args[1])):lower()
elseif args.v then -- args.v is deprecated, yet still here for backwards compatibility
version = mw.text.trim(tostring(args.v)):lower()
end
if args[2] then
variant = mw.text.trim(tostring(args[2])):lower()
elseif args.s then -- args.s is deprecated, yet still here for backwards compatibility
variant = mw.text.trim(tostring(args.s)):lower()
end
the module gets the mandatory arguments from the template call.
NOTICE: If a previous version of this module has not been used ever before, the deprecated argument checks may be safely removed.
Checking
After the module got the arguments, a further check is done to find out if all arguments are valid, that is, if the given mandatory parameters (version and variant) in the template are in the hashes module.
-- Check if version or variant outputs nil
if version_data[version] == nil then
text = i18n['version_error']
return text .. category
elseif version_data[version][variant] == nil then
text = i18n['variant_error']
return text .. category
end
If version or variant do not pass this test, the module stops, returns an error message and adds the page to the given category (category = if the page is in the main namespace).
If the hash is somehow nil (either set to that or unintended) in the hashes module, the module returns a 'download link' with text explaining no hash could be found (this might be redundant after the version and variant checks, but better safe than sorry).
Downloaded and in-browser JSON
By utilising
-- Start constructing
-- Check if variant also outputs a hash as 'json_package' (only works when force_download is NOT set)
if variant == 'json' and not args.force_download then
hash = version_data[version]['json_package']
if hash ~= nil then
variant = 'json_package'
else
variant = 'json'
hash = version_data[version]['json']
end
end
the module checks if there's also a json_package hash in the hashes module for the specified version. If there's such a hash found, it'll construct a link based on json_package rather than a download link. To prevent this from happening, set parameter |force_download=.
windows shorthand
If, rather than windows_server, windows is used for a Windows server download link, the module converts that to windows_server internally by utilising
-- Check if variant is 'windows' and then change it to 'windows_server'
if variant == 'windows' then
variant = 'windows_server'
end
Custom link text
If default text is unwanted, the module can add custom text using
-- Check if text parameter exists, if not, use default
if args[3] then
text = args[3]
elseif args.description then -- args.description is deprecated, yet still here for backwards compatibility
text = args.description
else
text = i18n[variant]
end
NOTICE: If a previous version of this module has not been used ever before, the deprecated argument check may be safely removed.
Other
When a link is returned by the module, it will be an external link, plus the nowiki template put behind it. This will prevent unwanted pres from happening.
--[[
Create download links to replace the S3 links.
]]
local p = {}
function p.downloadLinkConstructor( f )
local args = f
if f == mw.getCurrentFrame() then
args = require( 'Module:ProcessArgs' ).merge( true )
end
local version = mw.text.trim( tostring( args.v ) or '' ):lower()
local variant = mw.text.trim( args.s or '' ):lower()
-- Load values from granules and define variables
local versiondata = mw.loadData( 'Module:Downloadlinks/hashes' ).hash[version]
local windowsServer = mw.loadData( 'Module:Downloadlinks/windows_server' )[version]
local category = ''
local dlink
local dhash
if (versiondata or windowsServer) and variant then
if (variant:lower() == 'client') and versiondata.clienthash then
dhash = versiondata.clienthash
dlink = 'https://launcher.mojang.com/v1/objects/' .. dhash .. '/client.jar'
if versiondata.clienthash == nil then
dlink = '<abbr title="There is no client version of ' .. version .. '">—</abbr>'
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Invalid download link|' .. title .. ']]'
end
end
elseif (variant:lower() == 'server') and versiondata.serverhash then
dhash = versiondata.serverhash
dlink = 'https://launcher.mojang.com/v1/objects/' .. dhash .. '/server.jar'
if versiondata.serverhash == nil then
dlink = '<abbr title="There is no server version of ' .. version .. '">—</abbr>'
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Invalid download link|' .. title .. ']]'
end
end
elseif (variant:lower() == 'json') and versiondata.jsonhash then
dhash = versiondata.jsonhash
dlink = 'https://launchermeta.mojang.com/v1/objects/' .. dhash .. '/' .. version .. '.json'
if versiondata.jsonhash == nil then
dlink = '<abbr title="No JSON file exists for ' .. version .. '">—</abbr>'
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Invalid download link|' .. title .. ']]'
end
end
elseif (variant:lower() == 'windows') and windowsServer then
dhash = windowsServer
dlink = 'https://launcher.mojang.com/v1/objects/' .. dhash .. '/windows_server.exe'
--[[
Although unlikely to get a Windowsversion 'nil' as
Value (just don't add), anyway added in case.
]]
if windowsServer == nil then
dlink = '<abbr title="There is no Windowsserverversion of ' .. version .. '">—</abbr>'
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Invalid download link|' .. title .. ']]'
end
end
else
dlink = '<abbr title="No valid version and/or variant specified. Check the parameters.">missingno</abbr>'
end
else
dlink = '<abbr title="No valid parameters specified!">Parameterfout</abbr>'
local title = mw.title.getCurrentTitle()
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[Category:Invalid download link|' .. title .. ']]'
end
end
-- Ability to add a description
if args.description then
local description = mw.text.trim( args.description )
dlink = '[' .. dlink .. ' ' .. description .. ']'
else
dlink = dlink
end
return dlink .. category
end
return p