Module:Downloadlinks
Description[edit source]
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[edit source]
Table i18n
[edit source]
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-closingnowiki
element is located. This is necessary to prevent the module from generating illogicalpre
s.
Getting and checking arguments from template call[edit source]
Getting[edit source]
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[edit source]
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[edit source]
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[edit source]
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[edit source]
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[edit source]
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 pre
s from happening.
local p = {}
local i18n = {
process_args = 'Module:ProcessArgs',
hashes = 'Module:Downloadlinks/hashes',
category = 'Category: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',
}
function p.downloadlink_constructor(f)
local args = f
if f == mw.getCurrentFrame() then
args = require(i18n['process_args']).merge(true)
end
-- Construct category
local title = mw.title.getCurrentTitle()
local category = ''
if not args.nocat and title.namespace == 0 and not title.isSubpage then
category = '[[' .. i18n['category'] .. '|' .. title.text .. ']]'
end
local version
local variant
-- 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
-- change 'windows' to 'windows_server'
if variant == 'windows' then
variant = 'windows_server'
end
local nw = f:expandTemplate{title = i18n['nowiki_template']}
local version_data = mw.loadData(i18n['hashes'])
-- 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
local hash = version_data[version][variant]
local link
local download_link
local text
-- 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 then
variant = 'json_package'
elseif hash == nil then
variant = 'json'
hash = version_data[version]['json']
end
end
-- 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
-- And now finally actually start contructing links
-- Client
if variant == 'client' then
-- Check if hash exists
if hash then
download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/client.jar'
link = '[' .. download_link .. ' ' .. text .. ']' .. nw
return link
else
download_link = i18n['hash_error']
return download_link .. category
end
-- Server
elseif variant == 'server' then
-- Check if hash exists
if hash then
download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/server.jar'
link = '[' .. download_link .. ' ' .. text .. ']' .. nw
return link
else
download_link = i18n['hash_error']
return download_link .. category
end
-- Windows server
elseif variant == 'windows_server' then
-- Check if hash exists
if hash then
download_link = 'https://launcher.mojang.com/v1/objects/' .. hash .. '/windows_server.exe'
link = '[' .. download_link .. ' ' .. text .. ']' .. nw
return link
else
download_link = i18n['hash_error']
return download_link .. category .. hash
end
-- JSON
elseif variant == 'json' then
-- Check if hash exists
if hash then
download_link = 'https://launchermeta.mojang.com/mc/game/' .. hash .. '/' .. version .. '.json'
link = '[' .. download_link .. ' ' .. text .. ']' .. nw
return link
else
download_link = i18n['hash_error']
return download_link .. category
end
-- JSON in browser
elseif variant == 'json_package' then
-- Check if hash exists
if hash ~= nil then
download_link = 'https://launchermeta.mojang.com/v1/packages/' .. hash .. '/' .. version .. '.json'
link = '[' .. download_link .. ' ' .. text .. ']' .. nw
return link
else
download_link = i18n['hash_error']
return download_link .. category
end
end
end
return p