Module:Install

-- -- Installation template module for FANDOM. -- Support for JS and CSS at this time. -- @module     install -- @version    1.1.2 -- @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 local bool = require('Module:Yesno')

-- 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')

-- 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 } 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

-- Extraction subroutines for code pages. -- @section extract local extract = {}

-- Code page path extractor for JavaScript. -- @param      {string} source Source code (a JavaScript import). -- @return     {string} Extracted path of code page. function extract.js(source) -- Quote style extraction. local q = string.match(source, '[\'\"]')   -- Export variable.    local c    -- Script data extraction from window.importScriptPage    if string.find(source, 'importScriptPage') then        -- Cache variables.        local script -- script page path        local server -- wiki subdomain name        -- Pattern extraction.        script, server = string.match(source, 'importScriptPage%(%s*' .. q .. '?([^' .. q .. ']+)[' .. q .. ',%s]+([^' .. q .. ')]+)')        -- Post-processing the data.        server = type(server) == 'nil' and 'dev' or server        script = string.gsub(script, '[Mm]edia[Ww]iki:', '')        -- Create export        c = server .. ':' .. script    -- Script data extraction from window.importArticles    elseif string.find(source, 'importArticle') then        -- Cache variable.        local _        -- Pattern extraction.        c, _ = string.match(source, 'article[s%s:'.. q .. ']+([^' .. q .. ']+)')       -- Post-processing the data.        c = string.gsub(c, 'u:', )        c = string.gsub(c, 'w:c:', )        c = string.gsub(c, '[Mm]edia[Ww]iki:', '')        c = type(string.find(c, ':')) == 'nil' and            'dev:' .. c or c    end    -- Return extracted codepage.    return c end

-- Installation exports. -- @section exports

-- Installation label generator. -- @param      {table} frame Frame invocation object. -- @param      {string} frame.args[1] Code language - 'js' or 'css'. -- @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 = frame.args[1] local usage = use(frame.args[1], frame.args[3]) local scope = frame.args[2] -- Label generation from I18n. local label = i18n:msg('use-' .. scope) if conf[usage].label then label = label .. ' (' .. i18n: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 - 'js' or 'css'. -- @param      {string} frame.args[2] Scope of template field. -- @param      {string} frame.args[3] Code usage scope. -- @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 = frame.args[1] local usage = use(frame.args[1], frame.args[3]) local scope = frame.args[2] -- Link generation. local link = '' if conf[usage][code][scope] then link =  .. conf[usage][code][scope] ..  end return link else error('not enough arguments supplied to install.link') end end

-- Installation code page extractor for JS scripts. -- Introduces support of the legacy Source parameter. -- @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] and frame.args[2] then -- Frame arguments. local codepage = mw.text.trim(mw.text.nowiki(frame.args[1])) or '' local source  = mw.text.unstrip(frame.args[2]) -- @todo remove this       -- Conditional source extraction.        if source ~=  and codepage ==  then            codepage = extract.js(source)        elseif codepage ==  and source ==  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            bool(frame.args.mw, false) 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 ' ' .. source .. ' ' -- @todo remove this else error('not enough arguments supplied to install.script') end end

-- Export package. return install

--