FANDOM


--  <nowiki>
--- Installation template module for FANDOM.
--  Support for JS and CSS at this time.
--  @module             install
--  @alias              p
--  @version            1.6.0
--  @usage              {{#invoke:install|function}}
--  @author             [[User:MACH-59330|MACH-59330]]
--  @author             [[User:Fngplg|Fngplg]]
--  @author             [[User:DarthKitty|DarthKitty]]
--  @see                [[:Category:Installation templates]]
--  @release            stable
local p = {}
require('Dev:No interwiki access')
 
--  Dependencies.
local yesno = require('Dev:Yesno')
local title = mw.title.getCurrentTitle()
local conf = mw.loadData('Dev:Install/config')
 
--- Module message data.
--  @variable       {table} i18n
--  @see            [[Module:Install/i18n]]
local i18n = require('Dev:I18n').loadMessages('Install', 'Common')
 
--- Utility for usage validation.
--  @function       use
--  @param          {string} code Coding language supported by module.
--  @param          {string} val Valid usage scope in configuration.
--  @return         {string} Valid usage to map against configuration.
--  @local
local function use(code, val)
    code = mw.text.trim(code)
    val = mw.text.trim(val)
 
    -- Code support validation.
    if
        code ~= 'js' and
        code ~= 'css' and
        code ~= 'html'
    then
        error('unrecognised code language "' + code + '" supplied')
    end
 
    -- Configuration validation.
    if (conf[val] or {})[code] then
        return val
    end
 
    return 'default'
end
 
--- Usage parameter getter from child frame.
--  Relies on @{frame:getParent} to function correctly.
--  @param          {table} frame Invocation frame object.
--  @return         {string} `Use` template parameter, or empty string.
--  @local
local function getUse(frame)
    local parentFrame = frame:getParent() or {}
    local parentArgs = parentFrame.args or {}
    return parentArgs.Use or ''
end
 
--- Utility for message key shorthand expansion.
--  @param          {string} val Key shorthand to expand.
--  @return         {string} Expanded value key for i18n.
local function expand(val)
    local msg = {
        C = 'chat',
        G = 'global',
        L = 'local',
        P = 'personal',
        S = 'sitewide',
        T = 'template'
    }
 
    return msg[val] or ''
end
 
--- Installation header generator.
--  @param          {table} frame Frame invocation object.
--  @param          {string} frame.args[1] Section usage scope.
--  @raise          'not enough arguments supplied to install.header'
--  @usage          {{#invoke:install|header|usage}}
--  @return         {string} Localised installation header.
function p.header(frame)
    if not frame.args or not frame.args[1] then
        error('not enough arguments supplied to install.header')
    end
 
    local usage = expand(mw.text.trim(frame.args[1]))
    local lang = mw.language.new(i18n:useUserLang():getLang())
    local kind = lang:ucfirst(i18n:msg(usage, '1'))
 
    return i18n:msg('title', kind, usage)
end
 
--- Installation text generator.
--  @param          {table} frame Frame invocation object.
--  @param          {string} frame.args[1] Code language.
--  @raise          'not enough arguments supplied to install.text'
--  @usage          {{#invoke:install|text|code}}
--  @return         {string} Localised instruction for installation.
function p.text(frame)
    if not frame.args or not frame.args[1] then
        error('not enough arguments supplied to install.text')
    end
 
    local code = mw.text.trim(frame.args[1])
    local lang = {
        css = 'CSS',
        js = 'JavaScript',
        html = i18n:inUserLang():msg('template', '1')
    }
 
    -- I18n logic.
    local sub = ''
 
    if code == 'js' then
        sub = i18n:inUserLang():msg('installation-merge')
 
        if mw.ustring.find(sub, '%s') then
            sub = ' ' .. sub
        end
    end
 
    return i18n:inUserLang():msg('installation-text', lang[code], sub)
end
 
--- Installation label generator.
--  @param          {table} frame Frame invocation object.
--  @param          {string} frame.args[1] Code language.
--  @param          {string} frame.args[2] Scope of installation label.
--  @raise          'not enough arguments supplied to install.label'
--  @usage          {{#invoke:install|label|code|scope}}
--  @return         {string} Localised label describing scope.
function p.label(frame)
    if
        not frame.args or
        not frame.args[1] or
        not frame.args[2]
    then
        error('not enough arguments supplied to install.label')
    end
 
    local code = mw.text.trim(frame.args[1])
    local usage = conf[use(frame.args[1], getUse(frame))].label
    local scope = mw.text.trim(frame.args[2])
    local label = i18n:inUserLang():fromSource('Common'):msg(expand(scope))
 
    return usage
        and (label .. ' (' .. i18n:inUserLang():msg(usage.key) .. ')')
        or label
end
 
--- Installation link generator.
--  @param           {table} frame Frame invocation object.
--  @param           {string} frame.args[1] Code language.
--  @param           {string} frame.args[2] Scope of installation link.
--  @param           {string} frame.args[3] Template page name (or empty string).
--  @usage           {{#invoke:install|label|code|scope|usage}}
--  @raise           'not enough arguments supplied to install.link'
--  @return          {string} Wikitext link or empty string.
function p.link(frame)
    if
        not frame.args or
        not frame.args[1] or
        not frame.args[2]
    then
        error('not enough arguments supplied to install.link')
    end
 
    local code = mw.text.trim(frame.args[1])
    local usage = use(frame.args[1], getUse(frame))
    local scope = mw.text.trim(frame.args[2])
    local link = conf[usage][code][scope]
 
    if not link then
        return ''
    end
 
    -- Special logic for HTML Install
    if code == 'html' then
        local page = frame.args[3]
        local subpagePtn
 
        -- Strip language subpage for install link.
        if #mw.language.fetchLanguageName(title.subpageText) ~= 0 then
            page = page:gsub('/[%w-]+$', '')
        end
 
        page = mw.ustring.gsub(page, '^%l', mw.ustring.upper)
        link = mw.ustring.gsub(link, '$1', page)
    end
 
    return '[[' .. link .. ']]'
end
 
--- Installation code page formatter for JS scripts.
--  @param          {table} frame Frame invocation object.
--  @param          {string} frame.args[1] Path to code page.
--  @param          {string} frame.args.mw MediaWiki prefix boolean.
--  @raise          'not enough arguments supplied to install.script'
--  @usage          {{#invoke:install|script|codepage|source|mw=1}}
--  @return         {string} Path to code page.
function p.script(frame)
    if not frame:getParent() then
        error('no parent frame available to install.script')
    end
 
    local codepage = mw.text.trim(frame:getParent().args.codepage or '')
 
    -- Conditional source extraction.
    if codepage == '' then
        codepage = title.baseText .. '.js'
    end
 
    -- Post-processing for code page paths.
    codepage = mw.ustring.gsub(codepage, 'MediaWiki:', '')
 
    if mw.ustring.find(codepage, ':') == nil then
        codepage = 'dev:' .. codepage
    end
 
    -- Namespace prefix ('MediaWiki').
    if yesno(frame.args.mw) then
        local resource = mw.text.split(codepage, ':')
 
        table.insert(resource, 2, mw.site.namespaces[8].name)
 
        codepage = table.concat(resource, ':')
    end
 
    return codepage
end
 
--- Installation notice list generator.
--  @param       {table} frame Frame invocation object.
--  @param       {string} frame.args[1] Code language.
--  @raise       'not enough arguments supplied to install.notice'
--  @usage       {{#invoke:install|label|code|usage}}
--  @return      {string} Localised installation notice list.
function p.notice(frame)
    if not frame.args or not frame.args[1] then
        error('not enough arguments supplied to install.notice')
    end
 
    local code = mw.text.trim(frame.args[1])
    local usage
 
    if code ~= 'lua' then
        usage = use(frame.args[1], getUse(frame))
    end
 
    local import = mw.text.trim(frame.args.import or '1')
 
    -- Message conditionals.
    local type_messages = {
        css = 'stylesheet',
        html = 'template',
        js = 'script',
        lua = 'module'
    }
    local list = {}
 
    -- Devmodule alternative notice (Lua).
    list['devmodule-template'] = {
        bool = function (code, usage)
            return code == 'lua' and mw.text.split(title.text, '/')[2] ~= 'Devmodule'
        end,
        sub = {}
    }
 
    -- Import combination message (CSS/JS).
    list['import-combination'] = {
        bool = function (code, usage)
            return
                (code == 'css' or code == 'js') and
                tonumber(import) == 1 and (
                    conf[usage][code].G ~= nil or
                    conf[usage][code].P ~= nil or
                    conf[usage][code].S ~= 'MediaWiki:ImportJS'
                )
        end,
        sub = {
            i18n:inUserLang():msg(type_messages[code], '2')
        }
    }
 
    -- Common.css inclusion notice (CSS).
    list['include-common-css'] = {
        bool = function (code, usage)
            return code == 'css'
        end,
        sub = {}
    }
 
    -- Personal JS preferences notice (JS).
    list['enable-personal-js'] = {
        bool = function (code, usage)
            return code == 'js' and conf[usage][code].P ~= nil
        end,
        sub = {}
    }
 
    -- Wikitext source mode notice (HTML).
    list['source-mode-html'] = {
        bool = function (code, usage)
            return code == 'html' and conf[usage][code] ~= nil
        end,
        sub = {}
    }
 
    -- Lua templating guidance notice (Lua).
    list['lua-templating'] = {
        bool = function (code, usage)
            return code == 'lua'
        end,
        sub = {}
    }
 
    -- Message cache building.
    local msg = {}
 
    for k, m in pairs(list) do
        if m.bool(code, usage) == true then
            msg[#msg+1] = i18n:inUserLang():msg{ key = k, args = m.sub }
        end
    end
 
    local notices = table.concat(msg, '</li><li>')
 
    if #msg > 1 then
        return '<ul><li>' .. notices .. '</li></ul>'
    end
 
    return notices
end
 
return p
--  </nowiki>
Community content is available under CC-BY-SA unless otherwise noted.

Fandom may earn an affiliate commission on sales made from links on this page.

Stream the best stories.

Fandom may earn an affiliate commission on sales made from links on this page.

Get Disney+