Module:Install

-- -- Installation template module for FANDOM. -- Support for JS and CSS at this time. -- @module     install -- @version    1.4.5 -- @usage      require("Dev:Install") -- @author     Speedit -- @author     Fngplg -- @see        Template:Script Install; Template:CSS Install -- @release    stable; tested

-- Module package. local install = {}

-- Variables and utilities. -- @section utils

-- Dependencies. local title = mw.title.getCurrentTitle

-- Module configuration. -- @see        Module:Install/config local conf = mw.loadData('Module:Install/config')

-- Module message data. -- @see        Module:Install/i18n local i18n = require('Module:I18n').loadMessages('Install', 'Common')

-- Utility for usage validation. -- @param      {string} code Coding language supported by module. -- @param      {string} val Valid usage scope in configuration. -- @return     {string} Valid usage to map against configuration. function use(code, val) local usage = 'default' if code and val then -- Code support validation. local codedir = { ['js'] = 1, ['css'] = 1, ['html'] = 1 } if codedir[code] == nil then error('unrecognised code language "' + code + '" supplied') end -- Configuration validation. if val ~= '' and type((conf[val] or {})[code]) ~= 'nil' then usage = val end end return usage end

-- Utility for message key shorthand expansion. -- @param      {string} val Key shorthand to expand. -- @return     {string} Expanded value key for i18n. function expand(val) if val then local msg = { C = 'chat', G = 'global', L = 'local', P = 'personal', S = 'sitewide', T = 'template' }       return msg[val] or '' end end

-- Installation exports. -- @section exports

-- 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 -- @return     {string} Localised installation header. function install.header(frame) if frame.args and frame.args[1] then -- Frame arguments and variables. local usage = mw.text.trim(frame.args[1]) i18n:useUserLang -- Return message. return i18n:msg( 'title', (mw.ustring.gsub(i18n:msg( expand(usage), '1' ), '^%l', mw.ustring.upper )), expand(usage) ) else error('not enough arguments supplied to install.header') end 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 -- @return     {string} Localised instruction for installation. function install.text(frame) if frame.args and frame.args[1] then -- Frame arguments and variables. 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 = sub .. i18n:inUserLang:msg('installation-merge') if mw.ustring.find(sub, '%s') then sub = ' ' .. sub end end -- Return message. return i18n:inUserLang:msg('installation-text', lang[code], sub) else error('not enough arguments supplied to install.text') end end

-- Installation label generator. -- @param      {table} frame Frame invocation object. -- @param      {string} frame.args[1] Code language. -- @param      {string} frame.args[2] Scope of template field. -- @param      {string} frame.args[3] Code usage scope. -- @raise      'not enough arguments supplied to install.label' -- @usage -- @return     {string} Localised label describing scope. function install.label(frame) if frame.args and frame.args[1] and frame.args[2] and frame.args[3] then -- Frame arguments. local code = mw.text.trim(frame.args[1]) local usage = use(           mw.text.trim(frame.args[1]),            mw.text.trim(frame.args[3])        ) local scope = mw.text.trim(frame.args[2]) -- Label generation from I18n. local label = i18n:inUserLang:fromSource('Common'):msg( expand(scope) ) if conf[usage].label then label = label .. ' (' .. i18n:inUserLang:msg(conf[usage].label.key) .. ')' end return label else error('not enough arguments supplied to install.label') end end -- Installation link generator. -- @param      {table} frame Frame invocation object. -- @param      {string} frame.args[1] Code language. -- @param      {string} frame.args[2] Scope of template field. -- @param      {string} frame.args[3] Code usage scope. -- @param      {string} frame.args[4] Page name (or empty string). -- @usage -- @raise      'not enough arguments supplied to install.link' -- @return     {string} Wikitext link or empty string. function install.link(frame) if frame.args and frame.args[1] and frame.args[2] and frame.args[3] then -- Frame arguments. local code = mw.text.trim(frame.args[1]) local usage = use(           mw.text.trim(frame.args[1]),            mw.text.trim(frame.args[3])        ) local scope = mw.text.trim(frame.args[2]) -- Link generation. local link = '' if conf[usage][code][scope] then link = conf[usage][code][scope] if code == 'html' then local page = #mw.text.trim(frame.args[4] or '') ~= 0 and frame.args[4] or mw.language.fetchLanguageName(title.subpageText) == '' and title.subpageText or title.baseText:match('/*([^/]+)$') page = (mw.ustring.gsub(page, '^%l', mw.ustring.upper)) link = (mw.ustring.gsub(link, '$1', page )) end link =  .. link ..  end return link else error('not enough arguments supplied to install.link') end 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[2] Source import code. -- @param      {string} frame.args.mw MediaWiki prefix boolean. -- @usage -- @return     {string} Path to code page. function install.script(frame) if frame.args and frame.args[1] then -- Frame arguments. local codepage = mw.text.trim(frame.args[1]) -- Conditional source extraction. if codepage == '' then codepage = title.baseText .. '.js' end -- Post-processing for code page paths. codepage = mw.ustring.gsub(codepage, 'MediaWiki:', '') if type(mw.ustring.find(codepage, ':')) == 'nil' then codepage = 'dev:' .. codepage end -- Namespace prefix ('MediaWiki'). local is_mw = frame.args.mw and ({               ['1']    = true,                ['true'] = true            })[frame.args.mw or ''] or            false if is_mw then local resource = mw.text.split(codepage, ':') table.insert(resource, 2, mw.site.namespaces[8].name) codepage = table.concat(resource, ':') end -- Return script path. return codepage else error('not enough arguments supplied to install.script') end end

-- Installation notice list generator. -- @param      {table} frame Frame invocation object. -- @param      {string} frame.args[1] Code language. -- @param      {string} frame.args[2] Code usage scope. -- @raise      'not enough arguments supplied to install.notice' -- @usage -- @return     {string} Localised installation notice list. function install.notice(frame) if frame.args and frame.args[1] and frame.args[2] then -- Frame arguments and variables. local code = mw.text.trim(frame.args[1]) local usage = use(           mw.text.trim(frame.args[1]),            mw.text.trim(frame.args[2])        ) local import = mw.text.trim(frame.args.import or '1') -- Message conditionals. local list = { -- Import combination message (CSS/JS). ["import-combination"] = { bool = function(code, usage) return type(({ css = 1, js = 1 })[code]) ~= 'nil' and tonumber(import) == 1 and (                           type(conf[usage][code].G) ~= 'nil' or                            type(conf[usage][code].P) ~= 'nil' or                            conf[usage][code].S ~= 'MediaWiki:ImportJS'                        ) end, sub = { i18n:inUserLang:msg(({ ['css'] = 'stylesheet', ['html'] = 'template', ['js']  = 'script' })[code], '2') }           },            -- Common.css inclusion notice (CSS). ["include-common-css"] = { bool = function(code, usage) return code == 'css' end, sub = {} },           -- Personal JS preferences notice (JS). ["enable-personal-js"] = { bool = function(code, usage) return code == 'js' and type(conf[usage][code].P) ~= 'nil' end, sub = {} },           -- Wikitext source mode notice (HTML). ["source-mode-html"] = { bool = function(code, usage) return code == 'html' and type(conf[usage][code]) ~= 'nil' 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(k, unpack(m.sub)) end end -- Generate list of notices. local notices = '' if #msg > 1 then notices = '' .. table.concat(msg, '') .. '' elseif #msg == 1 then notices = msg[1] end -- Return notice list. return notices else error('not enough arguments supplied to install.notice') end end

-- Export package. return install

--