Módulo:DependencyList

De RuneScape Wiki
Ir para: navegação, pesquisa
Documentação do módulo
Esta documentação é transcluída de Predefinição:Sem documentação/doc. [editar] [atualizar]
Este módulo não possui nenhuma documentação. Por favor, considere adicionar uma documentação em Módulo:DependencyList/doc. [editar]
Módulo:DependencyList's a função main é invocada por Predefinição:DependencyList.
Módulo:DependencyList é solicitado por Módulo:Documentação.

-- <nowiki>
local p = {}
local libraryUtil = require( 'libraryUtil' )
local enum = require( 'Módulo:Enum' )
local yn = require( 'Módulo:Yesno' )
local param = require( 'Módulo:Paramtest' )
local dpl = require( 'Módulo:DPLlua' )
local tooltip = require( 'Módulo:Tooltip' )
local moduleIsUsed = false
local COLLAPSE_LIST_LENGTH_THRESHOLD = 5
local MAX_DYNAMIC_REQUIRE_LIST_LENGTH = 30
local dynamicRequireListQueryCache = {}

--- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'.
[email protected] content string    @The content of the module to look in as a string
[email protected] varName string
[email protected] string
local function substVarValue( content, varName )
    if varName:find( "%b''" ) or varName:find( '%b""' ) then -- Look for balanced quotes
        return varName -- Is already a string
    else
        local res = content:match( varName .. '%s*=%s*(%b""%s-%.*)' ) or content:match( varName .. "%s*=%s*(%b''%s-%.*)" ) or ''
        if res:find( '^(["\'])[Mm]ódulo:[%S]+%1' ) and not res:find( '%.%.' ) then
            return mw.text.trim( res )
        else
            return ''
        end
    end
end

[email protected] str string
[email protected] string
local function formatModuleName( str )
    return (str:gsub( '^([\'\"])(.-)%1$', function(_, x) return x end ) -- Only remove quotes at start and end of string if both are the same type
    	:gsub( '_', ' ' )
    	:gsub( '^.', string.upper )
    	:gsub( ':(.)', function(x) return ':'..x:upper() end ))
end

--- Used in case a construct like 'require( "Módulo:wowee/" .. isTheBest )' is found.
--- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value.
[email protected] query string
[email protected] string[]     @Sequence of strings
local function getDynamicRequireList( query )
    query = mw.text.split( query, '..', true )
    query = enum.map( query, function(x) return mw.text.trim(x) end )
    query = enum.map( query, function(x) return (x:match('^[\'\"]([^\'\"]+)[\'\"]$') or '%') end )
    query = table.concat( query )
    query = query:gsub( '^[Mm]ódulo:', '' )

    if query:find( '^[Mm]ercado/' ) or query:find( '^[Dd]ata/' ) then
        return { 'Módulo:' .. query }   -- This format will later be used by formatDynamicQueryLink()
    end

    if dynamicRequireListQueryCache[ query ] then
        return dynamicRequireListQueryCache[ query ]
    end

    local list = dpl.ask{
        namespace = 'Módulo',
        titlematch = query,
        nottitlematch = '%/doc|%sandbox%%|'..query..'/%',
        distinct = 'strict',
        ignorecase = true,
        ordermethod = 'title',
        count = MAX_DYNAMIC_REQUIRE_LIST_LENGTH + 1,
        skipthispage = 'no',
        allowcachedresults = true,
        cacheperiod = 604800 -- One week
    }

    if #list > MAX_DYNAMIC_REQUIRE_LIST_LENGTH then
        list = { 'Módulo:' .. query }
    end

    dynamicRequireListQueryCache[ query ] = list

    return list
end

--- Returns a list of modules loaded and required by module 'moduleName'.
[email protected] moduleName string
[email protected] searchForUsedTemplates boolean
[email protected] string[], string[], string[]
local function getRequireList( moduleName, searchForUsedTemplates )
    local content = mw.title.new( moduleName ):getContent()
    local requireList = {}
    local loadDataList = {}
    local usedTemplateList = {}
    local dynamicRequirelist = {}
    local dynamicLoadDataList = {}

    assert( param.has_content( content ), string.format( '%s não existe', moduleName ) )

    content = content:gsub( '%-%-%[(=-)%[.-%]%1%]', '' ):gsub( '%-%-[^\n]*', '' ) -- Strip comments

    local function dualGmatch( str, pat1, pat2 )
        local f1 = string.gmatch( str, pat1 )
        local f2 = string.gmatch( str, pat2 )
        return function()
            return f1() or f2()
        end
    end

    for match in dualGmatch( content, 'require%s*%(([^%)]+)', 'require%s*((["\'])%s*[Mm]ódulo:.-%2)' ) do
        match = mw.text.trim( match )
        match = substVarValue( content, match )

        if match:find( '%.%.' ) then
            for _, x in ipairs( getDynamicRequireList( match ) ) do
                table.insert( dynamicRequirelist, x )
            end
        elseif match ~= '' then
            match = formatModuleName( match )

            if match == 'LibraryUtil' then
                match = 'Módulo:LibraryUtil'
            end

            table.insert( requireList, match )
        end
    end

    for match in dualGmatch( content, 'mw%.loadData%s*%(([^%)]+)', 'mw%.loadData%s*((["\'])%s*[Mm]ódulo:.-%2)' ) do
        match = mw.text.trim( match )
        match = substVarValue( content, match )

        if match:find( '%.%.' ) then
            for _, x in ipairs( getDynamicRequireList( match ) ) do
                table.insert( dynamicLoadDataList, x )
            end
        elseif match ~= '' then
            match = formatModuleName( match )
            table.insert( loadDataList, match )
        end
    end

    for func, match in string.gmatch( content, 'pcall%s*%(([^,]+),([^%),]+)' ) do
        func = mw.text.trim( func )
        match = mw.text.trim( match )

        if func == 'require' then
            for _, x in ipairs( getDynamicRequireList( match ) ) do
                table.insert( dynamicRequirelist, x )
            end
        elseif func == 'mw.loadData' then
            for _, x in ipairs( getDynamicRequireList( match ) ) do
                table.insert( dynamicLoadDataList, x )
            end
        end
    end

    if searchForUsedTemplates then
        for preprocess in string.gmatch( content, ':preprocess%s*(%b())' ) do
            local function recursiveGMatch( str, pat )
                local list = {}
                local i = 0
                repeat
                    for match in string.gmatch( list[i] or str, pat ) do
                        table.insert( list, match )
                    end
                    i =  i + 1
                until i > #list or i > 100

                i = 0
                return function()
                    i = i + 1
                    return list[i]
                end
            end

            for template in recursiveGMatch( preprocess, '{(%b{})}' ) do
                local name = string.match( template, '{(.-)[|{}]' )
                if name ~= '' then
                    if name:find( ':' ) then
                        local ns = name:match( '^(.-):' )
                        if enum.contains( {'', 'predefinição', 'calculadora', 'usuário'}, ns:lower() ) then
                            table.insert( usedTemplateList, name )
                        elseif ns == ns:upper() then
                            table.insert( usedTemplateList, ns ) -- Probably a magic word
                        end
                    else
                        if name:match( '^%u+$' ) or name == '!' then
                            table.insert( usedTemplateList, name ) -- Probably a magic word
                        else
                            table.insert( usedTemplateList, 'Predefinição:'..name )
                        end
                    end
                end
            end
        end
    end

    dynamicRequirelist = enum.reject( dynamicRequirelist, function(x) return (enum.contains(requireList, x) or enum.contains(loadDataList, x)) end )
    dynamicLoadDataList = enum.reject( dynamicLoadDataList, function(x) return (enum.contains(requireList, x) or enum.contains(loadDataList, x)) end )
    requireList = enum.insert( requireList, dynamicRequirelist )
    requireList = enum.unique( requireList )
    loadDataList = enum.insert( loadDataList, dynamicLoadDataList )
    loadDataList = enum.unique( loadDataList )
    usedTemplateList = enum.unique( usedTemplateList )
    table.sort( requireList )
    table.sort( loadDataList )
    table.sort( usedTemplateList )

    return requireList, loadDataList, usedTemplateList
end

--- Returns a list with module and function names used in all '{{#Invoke:moduleName|funcName}}' found on page 'templateName'.
[email protected] templateName string
[email protected] table<string, string>[]
local function getInvokeCallList( templateName )
    local content = mw.title.new( templateName ):getContent()
    local invokeList = {}

    assert( param.has_content( content ), string.format( '%s não existe', templateName ) )

    for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do
        moduleName = string.format( 'Módulo:%s', mw.text.trim( moduleName ) )
        moduleName = formatModuleName( moduleName )
        funcName = mw.text.trim( funcName )
        table.insert( invokeList, {moduleName=moduleName, funcName=funcName} )
    end

    invokeList = enum.unique( invokeList, function(x) return x.moduleName..x.funcName end )
    table.sort( invokeList, function(x, y) return x.moduleName..x.funcName < y.moduleName..y.funcName end )

    return invokeList
end

[email protected] pageName string
[email protected] addCategories boolean
[email protected] string
local function messageBoxUnused( pageName, addCategories )
    local html = mw.html.create( 'table' ):addClass( 'messagebox obsolete plainlinks' )
    html:tag( 'td' )
        :attr( 'width', '40xp' )
        :wikitext( '[[Ficheiro:Elmo completo de ferro detalhe antigo.png|center|30px|link=]]' )
    :done()
    :tag( 'td' )
        :wikitext( "'''Este módulo não é utilizado'''" )
        :tag( 'div' )
            :css{ ['font-size']='0.85em', ['line-height']='1.45em' }
            :wikitext( string.format( 'Este módulo não é invocado por nenhuma predefinição ou não é necessário/carregado por outro módulo. Se isto for um erro, adicione <code>{{[[Predefinição:Documentação|Documentação]]}}</code>/<code>{{[[Predefinição:Sem documentação|Sem&nbsp;documentação]]}}</code> à documentação da predefinição ou módulo-pai.', pageName ) )
            :wikitext( addCategories and '[[Categoria:Módulos não utilizados]]' or '' )
        :done()
    :done()

    return tostring( html )
end

local function collapseList( list, id, listType )
    local text = string.format( '%d %s', #list, listType )
    local button = tooltip._span{ name=id, alt=text }
    list = enum.map( list, function(x) return '\n# '..x end )
    local content = tooltip._div{ name=id, content='\n'..table.concat( list )..'\n\n' }

    return { tostring( button ) .. tostring( content ) }
end

--- Creates a link to [[Special:Search]] showing all pages found by getDynamicRequireList() in case it found more than MAX_DYNAMIC_REQUIRE_LIST_LENGTH pages.
[email protected] query string      @This will be in a format like 'Módulo:Wowee/%' or 'Módulo:Wowee/%/data'
[email protected] string
local function formatDynamicQueryLink( query )
    local prefix = query:match( '^([^/]+)' )
    local linkText = query:gsub( '%%', '&lt; ... &gt;' )

    query = query:gsub( '^Módulo:',  '' )

    query = query:gsub( '([^/]+)/?', function ( match )
        if match == '%' then
            return '\\/[^\\/]+'
        else
            return '\\/"' .. match .. '"'
        end
    end )

    query = query:gsub( '^\\/', '' )

    query = string.format(
        'intitle:/%s%s/i -intitle:/%s\\/""/i -intitle:doc prefix:"%s"',
        query,
        query:find( '"$' ) and '' or '""',
        query,
        prefix
    )

    return string.format( '<span class="plainlinks">[%s %s]</span>', tostring( mw.uri.fullUrl( 'Especial:Busca', { search = query } ) ), linkText )
end

[email protected] templateName string
[email protected] addCategories boolean
[email protected] invokeList table<string, string>[]    @This is the list returned by getInvokeCallList()
[email protected] string
local function formatInvokeCallList( templateName, addCategories, invokeList )
    local category = addCategories and '[[Categoria:Predefinições baseadas em Lua]]' or ''
    local res = {}

    for _, item in ipairs( invokeList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' invoca a função '''%s''' em [[%s]] usando [[RuneScape:Lua|Lua]]''.</div>",
            templateName,
            item.funcName,
            item.moduleName
        ) )
    end

    if #invokeList > 0 then
        table.insert( res, category )
    end

    return table.concat( res )
end

[email protected] moduleName string
[email protected] addCategories boolean
[email protected] whatLinksHere string    @A list generated by a dpl of pages in the Predefinição or Calculadora namespace which link to moduleName.
[email protected] string
local function formatInvokedByList( moduleName, addCategories, whatLinksHere )
    local templateData = enum.map( whatLinksHere, function(x) return {templateName=x, invokeList=getInvokeCallList(x)} end )
    templateData = enum.filter( templateData, function(x)
        return enum.any( x.invokeList, function(y)
            return y.moduleName:lower() == moduleName:lower()
        end )
    end )

    local invokedByList = {}

    for _, template in ipairs( templateData ) do
        for _, invoke in ipairs( template.invokeList ) do
            table.insert( invokedByList, string.format( "a função '''%s''' é invocada por [[%s]]", invoke.funcName, template.templateName ) )
        end
    end

    table.sort( invokedByList)

    local res = {}

    if #invokedByList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' é invocada por %s''.</div>",
            moduleName,
            collapseList( invokedByList, 'invokedBy', 'predefinições' )[1]
        ) )
    else
        for _, item in ipairs( invokedByList ) do
            table.insert( res, string.format(
                "<div class='seealso'>'''''%s's''' %s''.</div>",
                moduleName,
                item
            ) )
        end
    end

    if #templateData > 0 then
        moduleIsUsed = true
        table.insert( res, (addCategories and '[[Categoria:Módulos invocados por predefinições]]' or '') )
    end

    return table.concat( res )
end

[email protected] moduleName string
[email protected] addCategories boolean
[email protected] whatLinksHere string      @A list generated by a dpl of pages in the Módulo namespace which link to moduleName.
[email protected] string
local function formatRequiredByList( moduleName, addCategories, whatLinksHere )
    local childModuleData = enum.map( whatLinksHere, function ( title )
        local requireList, loadDataList = getRequireList( title )
        return {name=title, requireList=requireList, loadDataList=loadDataList}
    end )

    local requiredByList = enum.map( childModuleData, function ( item )
        if enum.any( item.requireList, function(x) return x:lower()==moduleName:lower() end ) then
            if item.name:find( '%%' ) then
                return formatDynamicQueryLink( item.name )
            else
                return '[[' .. item.name .. ']]'
            end
        end
    end )

    local loadedByList = enum.map( childModuleData, function ( item )
        if enum.any( item.loadDataList, function(x) return x:lower()==moduleName:lower() end ) then
            if item.name:find( '%%' ) then
                return formatDynamicQueryLink( item.name )
            else
                return '[[' .. item.name .. ']]'
            end
        end
    end )

    if #requiredByList > 0 or #loadedByList > 0 then
        moduleIsUsed  = true
    end

    if #requiredByList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        requiredByList = collapseList( requiredByList, 'requiredBy', 'módulos' )
    end

    if #loadedByList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        loadedByList = collapseList( loadedByList, 'loadedBy', 'módulos' )
    end

    local res = {}

    for _, requiredByModuleName in ipairs( requiredByList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' é solicitado por %s''.</div>",
            moduleName,
            requiredByModuleName
        ) )
    end

    if #requiredByList > 0 then
        table.insert( res, (addCategories and '[[Categoria:Módulos necessários por módulos]]' or '') )
    end

    for _, loadedByModuleName in ipairs( loadedByList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' é carregado por %s''.</div>",
            moduleName,
            loadedByModuleName
        ) )
    end

    if #loadedByList > 0 then
        table.insert( res, (addCategories and '[[Categoria:Dados de módulo]]' or '') )
    end

    return table.concat( res )
end

local function formatRequireList( currentPageName, addCategories, requireList )
    local res = {}

    if #requireList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        requireList = collapseList( requireList, 'require', 'módulos' )
    end

    for _, requiredModuleName in ipairs( requireList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' requer %s.''</div>",
            currentPageName,
            requiredModuleName
        ) )
    end

    if #requireList > 0 then
        table.insert( res, (addCategories and '[[Categoria:Módulos necessários por módulos]]' or '') )
    end

    return table.concat( res )
end

local function formatLoadDataList( currentPageName, addCategories, loadDataList )
    local res = {}

    if #loadDataList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        loadDataList = collapseList( loadDataList, 'loadData', 'módulos' )
    end

    for _, loadedModuleName in ipairs( loadDataList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' carrega dados de %s.''</div>",
            currentPageName,
            loadedModuleName
        ) )
    end

    if #loadDataList > 0 then
        table.insert( res, (addCategories and '[[Categoria:Módulos usando dados]]' or '') )
    end

    return table.concat( res )
end

local function formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList )
    local res = {}

    if #usedTemplateList > COLLAPSE_LIST_LENGTH_THRESHOLD then
        usedTemplateList = collapseList( usedTemplateList, 'usedTemplates', 'predefinições' )
    end

    for _, templateName in ipairs( usedTemplateList ) do
        table.insert( res, string.format(
            "<div class='seealso'>'''''%s''' transclui %s usando <samp>frame:preprocess()</samp>.''</div>",
            currentPageName,
            templateName
        ) )
    end

    return table.concat( res )
end

function p.main( frame )
    local args = frame:getParent().args
    return p._main( args[1], args.category, args.isUsed )
end

[email protected] currentPageName string|nil
[email protected] addCategories boolean|string|nil
[email protected] string
function p._main( currentPageName, addCategories, isUsed )
    libraryUtil.checkType( 'Módulo:RequireList._main', 1, currentPageName, 'string', true )
    libraryUtil.checkTypeMulti( 'Módulo:RequireList._main', 2, addCategories, {'boolean', 'string', 'nil'} )
    libraryUtil.checkTypeMulti( 'Módulo:RequireList._main', 3, isUsed, {'boolean', 'string', 'nil'} )

    local title = mw.title.getCurrentTitle()

    -- Leave early if not in module, template or calculator namespace or if module is part of exchange, sandbox or data groups
    if param.is_empty( currentPageName ) and (
        ( not enum.contains( {'Módulo', 'Predefinição', 'Calculadora'}, title.nsText ) ) or
        ( title.nsText == 'Módulo' and ( enum.contains( {'Mercado', 'Exchange historical', 'Sandbox', 'Data'}, title.text:match( '^(.-)/' ) ) ) )
    ) then
        return ''
    end

    currentPageName = param.default_to( currentPageName, title.fullText )
    currentPageName = string.gsub( currentPageName, '/[Dd]oc$', '' )
    currentPageName = formatModuleName( currentPageName )
    addCategories = yn( param.default_to( addCategories, title.subpageText~='doc' ) )
    moduleIsUsed = yn( param.default_to( isUsed, false ) )

    if currentPageName:find( '^Predefinição:' ) or currentPageName:find( '^Calculadora:' ) then
        local invokeList = getInvokeCallList( currentPageName )
        return formatInvokeCallList( currentPageName, addCategories, invokeList )
    end

    local whatTemplatesLinkHere, whatModulesLinkHere = dpl.ask( {
        namespace = 'Predefinição|Calculadora',
        linksto = currentPageName,
        distinct = 'strict',
        ignorecase = true,
        ordermethod = 'title',
        allowcachedresults = true,
        cacheperiod = 604800 -- One week
    }, {
        namespace = 'Módulo',
        linksto = currentPageName,
        nottitlematch = '%/doc|%sandbox%|Mercado/%|Exchange historical/%|Data/%|' .. currentPageName:gsub( 'Módulo:', '' ),
        distinct = 'strict',
        ignorecase = true,
        ordermethod = 'title',
        allowcachedresults = true,
        cacheperiod = 604800 -- One week
    } )

    local requireList, loadDataList, usedTemplateList = getRequireList( currentPageName, true )

    requireList = enum.map( requireList, function ( moduleName )
        if moduleName:find( '%%' ) then
            return formatDynamicQueryLink( moduleName )
        else
            return '[[' .. moduleName .. ']]'
        end
    end )

    loadDataList = enum.map( loadDataList, function ( moduleName )
        if moduleName:find( '%%' ) then
            return formatDynamicQueryLink( moduleName )
        else
            return '[[' .. moduleName .. ']]'
        end
    end )

    usedTemplateList = enum.map( usedTemplateList, function( templateName )
        if mw.title.new( templateName ).exists then
            return '[['..templateName..']]'
        else
            return "'''&#123;&#123;"..templateName.."&#125;&#125;'''" -- Magic words don't have a page so make them bold instead
        end
    end )

    local res = {}

    table.insert( res, formatInvokedByList( currentPageName, addCategories, whatTemplatesLinkHere ) )
    table.insert( res, formatRequireList( currentPageName, addCategories, requireList ) )
    table.insert( res, formatLoadDataList( currentPageName, addCategories, loadDataList ) )
    table.insert( res, formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList ) )
    table.insert( res, formatRequiredByList( currentPageName, addCategories, whatModulesLinkHere ) )

    if not moduleIsUsed then
        table.insert( res, 1, messageBoxUnused( currentPageName:gsub( 'Módulo:', '' ), addCategories ) )
    end

    return table.concat( res )
end

return p
-- </nowiki>