нема опису редагування
starcitizen>Alistair3149 м (1 revision imported) |
starcitizen>Alistair3149 Немає опису редагування |
||
| Рядок 6: | Рядок 6: | ||
local param = require( 'Module:Paramtest' ) | local param = require( 'Module:Paramtest' ) | ||
local dpl = require( 'Module:DPLlua' ) | local dpl = require( 'Module:DPLlua' ) | ||
--local tooltip = require( 'Module:Tooltip' ) | |||
local moduleIsUsed = false | local moduleIsUsed = false | ||
--local COLLAPSE_LIST_LENGTH_THRESHOLD = 5 | |||
local MAX_DYNAMIC_REQUIRE_LIST_LENGTH = 30 | local MAX_DYNAMIC_REQUIRE_LIST_LENGTH = 30 | ||
local dynamicRequireListQueryCache = {} | local dynamicRequireListQueryCache = {} | ||
--- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'. | --- Used in case 'require( varName )' is found. Attempts to find a string value stored in 'varName'. | ||
---@param content string | ---@param content string The content of the module to search in | ||
---@param varName string | ---@param varName string | ||
---@return string | ---@return string | ||
local function substVarValue( content, varName ) | local function substVarValue( content, varName ) | ||
local res = content:match( varName .. '%s*=%s*(%b""%s-%.*)' ) or content:match( varName .. "%s*=%s*(%b''%s-%.*)" ) or '' | |||
return | if res:find( '^(["\'])[Mm]odule:[%S]+%1' ) and not res:find( '%.%.' ) and not res:find( '%%%a' ) then | ||
return mw.text.trim( res ) | |||
else | else | ||
local | return '' | ||
end | |||
end | |||
---@param capture string | |||
---@param content string The content of the module to search in | |||
---@return string | |||
local function extractModuleName( capture, content ) | |||
capture = capture:gsub( '^%(%s*(.-)%s*%)$', '%1' ) | |||
if capture:find( '^(["\']).-%1$' ) then -- Check if it is already a pure string | |||
return capture | |||
elseif capture:find( '^[%a_][%w_]*$' ) then -- Check if if is a single variable | |||
return substVarValue( content, capture ) | |||
end | end | ||
return capture | |||
end | |||
---@param str string | |||
---@return 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 | end | ||
| Рядок 30: | Рядок 52: | ||
--- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value. | --- Will return a list of pages which satisfy this pattern where 'isTheBest' can take any value. | ||
---@param query string | ---@param query string | ||
---@return string[] | ---@return string[] Sequence of strings | ||
local function getDynamicRequireList( query ) | local function getDynamicRequireList( query ) | ||
query = mw.text.split( query, '..', true ) | if query:find( '%.%.' ) then | ||
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 ) | |||
else | |||
_, query = query:match( '(["\'])(.-)%1' ) | |||
query = query:gsub( '%%%a', '%%' ) | |||
end | |||
query = query:gsub( '^[Mm]odule:', '' ) | query = query:gsub( '^[Mm]odule:', '' ) | ||
if query:find( '^[Ee]xchange/' ) or query:find( '^[Dd]ata/' ) then | |||
return { 'Module:' .. query } -- This format will later be used by formatDynamicQueryLink() | |||
end | |||
if dynamicRequireListQueryCache[ query ] then | if dynamicRequireListQueryCache[ query ] then | ||
| Рядок 45: | Рядок 76: | ||
namespace = 'Module', | namespace = 'Module', | ||
titlematch = query, | titlematch = query, | ||
nottitlematch = '%/doc | nottitlematch = '%/doc|'..query..'/%', | ||
distinct = 'strict', | distinct = 'strict', | ||
ignorecase = true, | ignorecase = true, | ||
| Рядок 66: | Рядок 97: | ||
--- Returns a list of modules loaded and required by module 'moduleName'. | --- Returns a list of modules loaded and required by module 'moduleName'. | ||
---@param moduleName string | ---@param moduleName string | ||
---@return string[], string[] | ---@param searchForUsedTemplates boolean | ||
local function getRequireList( moduleName ) | ---@return string[], string[], string[] | ||
local function getRequireList( moduleName, searchForUsedTemplates ) | |||
local content = mw.title.new( moduleName ):getContent() | local content = mw.title.new( moduleName ):getContent() | ||
local requireList = {} | local requireList = {} | ||
| Рядок 87: | Рядок 119: | ||
end | end | ||
for match in dualGmatch( content, 'require%s*%( | for match in dualGmatch( content, 'require%s*(%b())', 'require%s*((["\'])%s*[Mm]odule:.-%2)' ) do | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
match = | match = extractModuleName( match, content ) | ||
if match:find( '%.%.' ) then | if match:find( '%.%.' ) or match:find( '%%%a' ) then | ||
for _, x in ipairs( getDynamicRequireList( match ) ) do | for _, x in ipairs( getDynamicRequireList( match ) ) do | ||
table.insert( dynamicRequirelist, x ) | table.insert( dynamicRequirelist, x ) | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = match | match = formatModuleName( match ) | ||
if match == ' | if match == 'LibraryUtil' then | ||
match = 'Module:LibraryUtil' | match = 'Module:LibraryUtil' | ||
end | end | ||
| Рядок 106: | Рядок 138: | ||
end | end | ||
for match in dualGmatch( content, 'mw%.loadData%s*%( | for match in dualGmatch( content, 'mw%.loadData%s*(%b())', 'mw%.loadData%s*((["\'])%s*[Mm]odule:.-%2)' ) do | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
match = | match = extractModuleName( match, content ) | ||
if match:find( '%.%.' ) then | if match:find( '%.%.' ) or match:find( '%%%a' ) then | ||
for _, x in ipairs( getDynamicRequireList( match ) ) do | for _, x in ipairs( getDynamicRequireList( match ) ) do | ||
table.insert( dynamicLoadDataList, x ) | table.insert( dynamicLoadDataList, x ) | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = match | match = formatModuleName( match ) | ||
table.insert( loadDataList, match ) | table.insert( loadDataList, match ) | ||
end | end | ||
| Рядок 135: | Рядок 167: | ||
end | end | ||
for preprocess in string.gmatch( content, ':preprocess%s*%( | if searchForUsedTemplates then | ||
for preprocess in string.gmatch( content, ':preprocess%s*(%b())' ) do | |||
local function recursiveGMatch( str, pat ) | |||
local list = {} | |||
table.insert( usedTemplateList, | 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( {'', 'template', 'calculator', 'user'}, 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, 'Template:'..name ) | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Рядок 159: | Рядок 220: | ||
return requireList, loadDataList, usedTemplateList | return requireList, loadDataList, usedTemplateList | ||
end | end | ||
| Рядок 178: | Рядок 232: | ||
for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | for moduleName, funcName in string.gmatch( content, '{{[{|safeubt:}]-#[Ii]nvoke:([^|]+)|([^}|]+)[^}]*}}' ) do | ||
moduleName = string.format( 'Module:%s', mw.text.trim( moduleName ) ) | |||
moduleName = string.format( 'Module:%s', moduleName ) | moduleName = formatModuleName( moduleName ) | ||
moduleName = moduleName | funcName = mw.text.trim( funcName ) | ||
table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | table.insert( invokeList, {moduleName=moduleName, funcName=funcName} ) | ||
end | end | ||
| Рядок 215: | Рядок 269: | ||
return tostring( html ) | return tostring( html ) | ||
end | 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. | --- Creates a link to [[Special:Search]] showing all pages found by getDynamicRequireList() in case it found more than MAX_DYNAMIC_REQUIRE_LIST_LENGTH pages. | ||
| Рядок 272: | Рядок 335: | ||
---@param moduleName string | ---@param moduleName string | ||
---@param addCategories boolean | ---@param addCategories boolean | ||
---@param whatLinksHere string @A list generated by a dpl of pages in the Template namespace which link to moduleName. | ---@param whatLinksHere string @A list generated by a dpl of pages in the Template or Calculator namespace which link to moduleName. | ||
---@return string | ---@return string | ||
local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | local function formatInvokedByList( moduleName, addCategories, whatLinksHere ) | ||
local templateData = enum.map( whatLinksHere, function(x) return {templateName=x, invokeList=getInvokeCallList(x)} end ) | 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==moduleName end ) 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( "function '''%s''' is invoked by [[%s]]", invoke.funcName, template.templateName ) ) | |||
end | end | ||
end | |||
table.sort( invokedByList) | |||
local res = {} | |||
--if #invokedByList > COLLAPSE_LIST_LENGTH_THRESHOLD then | |||
table.insert( res, | -- table.insert( res, string.format( | ||
-- "<div class='seealso'>'''''%s''' is invoked by %s''.</div>", | |||
-- moduleName, | |||
-- collapseList( invokedByList, 'invokedBy', 'templates' )[1] | |||
-- ) ) | |||
--else | |||
for _, item in ipairs( invokedByList ) do | |||
table.insert( res, string.format( | |||
"<div role='note' class='hatnote'><div class=hatnote-container navigation-not-searchable'><span class='hatnote-icon metadata'>[[File:WikimediaUI-Code.svg|14px|link=]]</span>'''%s''' is invoked by %s.</div></div>", | |||
moduleName, | |||
item | |||
) ) | |||
end | |||
--end | |||
if #templateData > 0 then | |||
moduleIsUsed = true | |||
table.insert( res, (addCategories and '[[Category:Template invoked modules]]' or '') ) | |||
end | end | ||
return | return table.concat( res ) | ||
end | end | ||
| Рядок 314: | Рядок 392: | ||
local requiredByList = enum.map( childModuleData, function ( item ) | local requiredByList = enum.map( childModuleData, function ( item ) | ||
if enum.any( item.requireList, function(x) return x==moduleName end ) then | if enum.any( item.requireList, function(x) return x:lower()==moduleName:lower() end ) then | ||
if item.name:find( '%%' ) then | if item.name:find( '%%' ) then | ||
return formatDynamicQueryLink( item.name ) | return formatDynamicQueryLink( item.name ) | ||
| Рядок 324: | Рядок 402: | ||
local loadedByList = enum.map( childModuleData, function ( item ) | local loadedByList = enum.map( childModuleData, function ( item ) | ||
if enum.any( item.loadDataList, function(x) return x==moduleName end ) then | if enum.any( item.loadDataList, function(x) return x:lower()==moduleName:lower() end ) then | ||
if item.name:find( '%%' ) then | if item.name:find( '%%' ) then | ||
return formatDynamicQueryLink( item.name ) | return formatDynamicQueryLink( item.name ) | ||
| Рядок 336: | Рядок 414: | ||
moduleIsUsed = true | moduleIsUsed = true | ||
end | end | ||
--if #requiredByList > COLLAPSE_LIST_LENGTH_THRESHOLD then | |||
-- requiredByList = collapseList( requiredByList, 'requiredBy', 'modules' ) | |||
--end | |||
--if #loadedByList > COLLAPSE_LIST_LENGTH_THRESHOLD then | |||
-- loadedByList = collapseList( loadedByList, 'loadedBy', 'modules' ) | |||
--end | |||
local res = {} | local res = {} | ||
| Рядок 361: | Рядок 447: | ||
if #loadedByList > 0 then | if #loadedByList > 0 then | ||
table.insert( res, (addCategories and '[[Category:Module data]]' or '') ) | table.insert( res, (addCategories and '[[Category:Module data]]' 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', 'modules' ) | |||
--end | |||
for _, requiredModuleName in ipairs( requireList ) do | |||
table.insert( res, string.format( | |||
"<div role='note' class='hatnote'><div class=hatnote-container navigation-not-searchable'><span class='hatnote-icon metadata'>[[File:WikimediaUI-Code.svg|14px|link=]]</span>'''%s''' requires %s.</div></div>", | |||
currentPageName, | |||
requiredModuleName | |||
) ) | |||
end | |||
if #requireList > 0 then | |||
table.insert( res, (addCategories and '[[Category:Modules requiring modules]]' 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', 'modules' ) | |||
--end | |||
for _, loadedModuleName in ipairs( loadDataList ) do | |||
table.insert( res, string.format( | |||
"<div role='note' class='hatnote'><div class=hatnote-container navigation-not-searchable'><span class='hatnote-icon metadata'>[[File:WikimediaUI-Code.svg|14px|link=]]</span>'''%s''' loads data from %s.</div></div>", | |||
currentPageName, | |||
loadedModuleName | |||
) ) | |||
end | |||
if #loadDataList > 0 then | |||
table.insert( res, (addCategories and '[[Category:Modules using data]]' 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', 'templates' ) | |||
--end | |||
for _, templateName in ipairs( usedTemplateList ) do | |||
table.insert( res, string.format( | |||
"<div role='note' class='hatnote'><div class=hatnote-container navigation-not-searchable'><span class='hatnote-icon metadata'>[[File:WikimediaUI-Code.svg|14px|link=]]</span>'''%s''' transcludes [[%s]] using <samp>frame:preprocess()</samp>.</div></div>", | |||
currentPageName, | |||
templateName | |||
) ) | |||
end | end | ||
| Рядок 381: | Рядок 529: | ||
local title = mw.title.getCurrentTitle() | local title = mw.title.getCurrentTitle() | ||
-- Leave early if not in module, template or calculator namespace or if module is part of exchange or data groups | |||
if param.is_empty( currentPageName ) and ( | if param.is_empty( currentPageName ) and ( | ||
( not enum.contains( {'Module', 'Template', 'Calculator'}, title.nsText ) ) or | |||
( title.nsText == 'Module' and ( enum.contains( {'Exchange', 'Exchange historical', 'Data'}, title.text:match( '^(.-)/' ) ) ) ) | |||
) then | |||
return '' | return '' | ||
end | end | ||
currentPageName = param.default_to( currentPageName, title.fullText ) | currentPageName = param.default_to( currentPageName, title.fullText ) | ||
currentPageName = string.gsub( currentPageName, '/[Dd]oc$', '' ) | currentPageName = string.gsub( currentPageName, '/[Dd]oc$', '' ) | ||
currentPageName = | currentPageName = formatModuleName( currentPageName ) | ||
addCategories = yn( param.default_to( addCategories, title.subpageText~='doc' ) ) | addCategories = yn( param.default_to( addCategories, title.subpageText~='doc' ) ) | ||
moduleIsUsed = yn( param.default_to( isUsed, false ) ) | moduleIsUsed = yn( param.default_to( isUsed, false ) ) | ||
if title.text:match( '^(.-)/' ) == 'Sandbox' then | |||
moduleIsUsed = true -- Don't show sandbox modules as unused | |||
end | |||
if currentPageName:find( '^Template:' ) then | if currentPageName:find( '^Template:' ) or currentPageName:find( '^Calculator:' ) then | ||
local invokeList = getInvokeCallList( currentPageName ) | local invokeList = getInvokeCallList( currentPageName ) | ||
return formatInvokeCallList( currentPageName, addCategories, invokeList ) | return formatInvokeCallList( currentPageName, addCategories, invokeList ) | ||
end | end | ||
local | local whatTemplatesLinkHere, whatModulesLinkHere = dpl.ask( { | ||
namespace = 'Template', | namespace = 'Template', | ||
linksto = currentPageName, | linksto = currentPageName, | ||
| Рядок 410: | Рядок 563: | ||
namespace = 'Module', | namespace = 'Module', | ||
linksto = currentPageName, | linksto = currentPageName, | ||
nottitlematch = '%/doc|% | nottitlematch = '%/doc|Exchange/%|Data/%|' .. currentPageName:gsub( 'Module:', '' ), | ||
distinct = 'strict', | distinct = 'strict', | ||
ignorecase = true, | ignorecase = true, | ||
| Рядок 418: | Рядок 571: | ||
} ) | } ) | ||
local requireList, loadDataList, usedTemplateList = getRequireList( currentPageName, true ) | |||
local requireList, loadDataList, usedTemplateList = getRequireList( currentPageName ) | |||
requireList = enum.map( requireList, function ( moduleName ) | requireList = enum.map( requireList, function ( moduleName ) | ||
| Рядок 436: | Рядок 586: | ||
else | else | ||
return '[[' .. moduleName .. ']]' | return '[[' .. moduleName .. ']]' | ||
end | |||
end ) | |||
usedTemplateList = enum.map( usedTemplateList, function( templateName ) | |||
if string.find( templateName, ':' ) then -- Real templates are prefixed by a namespace, magic words are not | |||
return '[['..templateName..']]' | |||
else | |||
return "'''{{"..templateName.."}}'''" -- Magic words don't have a page so make them bold instead | |||
end | end | ||
end ) | end ) | ||
local res = {} | 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 | if not moduleIsUsed then | ||
table.insert( res, messageBoxUnused( currentPageName:gsub( 'Module:', '' ), addCategories ) ) | table.insert( res, 1, messageBoxUnused( currentPageName:gsub( 'Module:', '' ), addCategories ) ) | ||
end | end | ||
return table.concat( res ) | return table.concat( res ) | ||