317
редагувань
starcitizen>Alistair3149 Немає опису редагування |
м (56 ревізій: Template:Infobox_Species from StarCitizenTools) |
||
| (Не показано 24 проміжні версії 2 користувачів) | |||
| Рядок 1: | Рядок 1: | ||
require("strict"); | |||
local p = {} | local p = {} | ||
local libraryUtil = require( 'libraryUtil' ) | local libraryUtil = require( 'libraryUtil' ) | ||
| Рядок 6: | Рядок 7: | ||
local param = require( 'Module:Paramtest' ) | local param = require( 'Module:Paramtest' ) | ||
local dpl = require( 'Module:DPLlua' ) | local dpl = require( 'Module:DPLlua' ) | ||
local userError = require("Module:User error") | |||
local mHatnote = require('Module:Hatnote') | local mHatnote = require('Module:Hatnote') | ||
local mHatlist = require('Module:Hatnote list') | |||
local moduleIsUsed = false | local moduleIsUsed = false | ||
local COLLAPSE_LIST_LENGTH_THRESHOLD = 1 | local COLLAPSE_LIST_LENGTH_THRESHOLD = 1 | ||
local MAX_DYNAMIC_REQUIRE_LIST_LENGTH = 30 | local MAX_DYNAMIC_REQUIRE_LIST_LENGTH = 30 | ||
local dynamicRequireListQueryCache = {} | local dynamicRequireListQueryCache = {} | ||
local builtins = { | |||
--[[ | |||
["libraryUtil"] = { | |||
link = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#libraryUtil", | |||
categories = {}, | |||
} | |||
]] | |||
["strict"] = { | |||
link = "mw:Special:MyLanguage/Extension:Scribunto/Lua reference manual#strict", | |||
categories = { "Strict mode modules" }, | |||
}, | |||
}; | |||
--- 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'. | ||
| Рядок 54: | Рядок 70: | ||
---@param str string | ---@param str string | ||
---@return string | ---@return string | ||
local function formatModuleName( str ) | local function formatModuleName( str, allowBuiltins ) | ||
if allowBuiltins then | |||
local name = mw.text.trim(str) | |||
-- Only remove quotes at start and end of string if both are the same type | |||
:gsub([[^(['"])(.-)%1$]], function(_, x) return x end); | |||
local builtin = builtins[name]; | |||
if builtin then | |||
return builtin.link .. "|" .. name, builtin; | |||
end | |||
end | |||
local module = formatPageName( str ) | local module = formatPageName( str ) | ||
| Рядок 77: | Рядок 104: | ||
---@return string[] Sequence of strings | ---@return string[] Sequence of strings | ||
local function getDynamicRequireList( query ) | local function getDynamicRequireList( query ) | ||
local isDynamic = true; | |||
if query:find( '%.%.' ) then | if query:find( '%.%.' ) then | ||
query = mw.text.split( query, '..', true ) | query = mw.text.split( query, '..', true ) | ||
| Рядок 83: | Рядок 112: | ||
query = table.concat( query ) | query = table.concat( query ) | ||
else | else | ||
_, query = query:match( '(["\'])(.-)%1' ) | local _; _, query = query:match( '(["\'])(.-)%1' ) | ||
query = query:gsub( '%%%a', '%%' ) | |||
if query == nil then | |||
return {}, isDynamic | |||
end | |||
local replacements; | |||
query, replacements = query:gsub( '%%%a', '%%' ) | |||
if replacements == 0 then | |||
isDynamic = false; | |||
end | |||
end | end | ||
query = query:gsub( '^[Mm]odule:', '' ) | query = query:gsub( '^[Mm]odule:', '' ) | ||
if query:find( '^[Dd]ata/' ) then | |||
return { 'Module:' .. query }, isDynamic; -- This format will later be used by formatDynamicQueryLink() | |||
end | |||
if dynamicRequireListQueryCache[ query ] then | if dynamicRequireListQueryCache[ query ] then | ||
return dynamicRequireListQueryCache[ query ] | return dynamicRequireListQueryCache[ query ], isDynamic; | ||
end | end | ||
| Рядок 111: | Рядок 153: | ||
dynamicRequireListQueryCache[ query ] = list | dynamicRequireListQueryCache[ query ] = list | ||
return list | return list, isDynamic; | ||
end | end | ||
| Рядок 117: | Рядок 159: | ||
---@param moduleName string | ---@param moduleName string | ||
---@param searchForUsedTemplates boolean | ---@param searchForUsedTemplates boolean | ||
---@return string[], string[], string[] | ---@return string[], string[], string[], string[] | ||
local function getRequireList( moduleName, searchForUsedTemplates ) | local function getRequireList( moduleName, searchForUsedTemplates ) | ||
local content = mw.title.new( moduleName ):getContent() | local content = mw.title.new( moduleName ):getContent() | ||
| Рядок 125: | Рядок 167: | ||
local dynamicRequirelist = arr{} | local dynamicRequirelist = arr{} | ||
local dynamicLoadDataList = arr{} | local dynamicLoadDataList = arr{} | ||
local extraCategories = arr{} | |||
assert( content ~= nil, string.format( '%s does not exist', moduleName ) ) | assert( content ~= nil, string.format( '%s does not exist', moduleName ) ) | ||
| Рядок 139: | Рядок 182: | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = formatModuleName( match ) | local builtin; | ||
match, builtin = formatModuleName( match, true ) | |||
table.insert( requireList, match ) | table.insert( requireList, match ) | ||
if builtin then | |||
local builtinCategories = builtin.categories; | |||
if type(builtinCategories) == "table" then | |||
for _, x in ipairs(builtinCategories) do | |||
table.insert(extraCategories, x); | |||
end | |||
end | |||
end | |||
end | end | ||
end | end | ||
| Рядок 153: | Рядок 206: | ||
end | end | ||
elseif match ~= '' then | elseif match ~= '' then | ||
match = formatModuleName( match ) | match = formatModuleName( match, true ) | ||
table.insert( loadDataList, match ) | |||
end | |||
end | |||
for match in dualGmatch( content, 'mw%.loadJsonData%s*(%b())', 'mw%.loadJsonData%s*((["\'])%s*[Mm]odule:.-%2)' ) do | |||
match = mw.text.trim( match ) | |||
match = extractModuleName( match, content ) | |||
if match:find( '%.%.' ) or match:find( '%%%a' ) then | |||
for _, x in ipairs( getDynamicRequireList( match ) ) do | |||
table.insert( dynamicLoadDataList, x ) | |||
end | |||
elseif match ~= '' then | |||
match = formatModuleName( match, true ) | |||
table.insert( loadDataList, match ) | table.insert( loadDataList, match ) | ||
end | end | ||
| Рядок 162: | Рядок 229: | ||
match = mw.text.trim( match ) | match = mw.text.trim( match ) | ||
local dynList, isDynamic; | |||
if func == 'require' then | if func == 'require' then | ||
dynList, isDynamic = getDynamicRequireList(match); | |||
if (isDynamic == false and #dynList == 1) then | |||
table.insert(requireList, dynList[1]); | |||
else for _, x in ipairs(dynList) do | |||
table.insert( dynamicRequirelist, x ) | table.insert( dynamicRequirelist, x ) | ||
end end | |||
elseif func == 'mw.loadData' then | elseif func == 'mw.loadData' then | ||
dynList, isDynamic = getDynamicRequireList(match); | |||
if (isDynamic == false and #dynList == 1) then | |||
table.insert(loadDataList, dynList[1]); | |||
else for _, x in ipairs(dynList) do | |||
table.insert( dynamicLoadDataList, x ) | table.insert( dynamicLoadDataList, x ) | ||
end end | |||
end | end | ||
end | end | ||
| Рядок 219: | Рядок 293: | ||
loadDataList = loadDataList:unique() | loadDataList = loadDataList:unique() | ||
usedTemplateList = usedTemplateList:unique() | usedTemplateList = usedTemplateList:unique() | ||
extraCategories = extraCategories:unique() | |||
table.sort( requireList ) | table.sort( requireList ) | ||
table.sort( loadDataList ) | table.sort( loadDataList ) | ||
table.sort( usedTemplateList ) | table.sort( usedTemplateList ) | ||
table.sort( extraCategories ) | |||
return requireList, loadDataList, usedTemplateList | return requireList, loadDataList, usedTemplateList, extraCategories | ||
end | end | ||
| Рядок 248: | Рядок 324: | ||
return invokeList | return invokeList | ||
end | end | ||
| Рядок 281: | Рядок 330: | ||
---@return string | ---@return string | ||
local function messageBoxUnused( pageName, addCategories ) | local function messageBoxUnused( pageName, addCategories ) | ||
local mbox = require( 'Module:Mbox' )._mbox | |||
local category = addCategories and '[[Category:Unused modules]]' or '' | |||
return mbox( | |||
'This module is unused.', | |||
string.format( 'This module is neither invoked by a template nor required/loaded by another module. If this is in error, make sure to add <code>{{[[Template:Documentation|Documentation]]}}</code>/<code>{{[[Template:No documentation|No documentation]]}}</code> to the calling template\'s or parent\'s module documentation.', | |||
pageName | |||
), | |||
{ icon = 'WikimediaUI-Alert.svg' } | |||
) .. category | |||
end | end | ||
local function collapseList( list, id, listType ) | local function collapseList( list, id, listType ) | ||
local text = string.format( '%d %s', #list, listType ) | local text = string.format( '%d %s', #list, listType ) | ||
local button = '<span>' .. text .. ':</span>' | local button = '<span>' .. text .. ':</span> ' | ||
local content = mHatlist.andList( list, false ) | |||
local content = | |||
return { tostring( button ) .. tostring( content ) } | return { tostring( button ) .. tostring( content ) } | ||
| Рядок 526: | Рядок 563: | ||
for _, loadedModuleName in ipairs( loadDataList ) do | for _, loadedModuleName in ipairs( loadDataList ) do | ||
local msg = string.format( | local msg = string.format( | ||
"''%s''' loads data from %s.", | "'''%s''' loads data from %s.", | ||
currentPageName, | currentPageName, | ||
loadedModuleName | loadedModuleName | ||
| Рядок 554: | Рядок 591: | ||
) | ) | ||
table.insert( res, mHatnote._hatnote( msg, { icon='WikimediaUI-Code.svg' } ) ) | table.insert( res, mHatnote._hatnote( msg, { icon='WikimediaUI-Code.svg' } ) ) | ||
end | end | ||
| Рядок 613: | Рядок 626: | ||
moduleIsUsed = true -- Don't show sandbox modules as unused | moduleIsUsed = true -- Don't show sandbox modules as unused | ||
end | end | ||
if currentPageName:find( '^Template:' ) then | if currentPageName:find( '^Template:' ) then | ||
local invokeList = getInvokeCallList | local ok, invokeList = pcall( getInvokeCallList, currentPageName ) | ||
if ok then | |||
return formatInvokeCallList( currentPageName, addCategories, invokeList ) | |||
else | |||
return userError(invokeList) | |||
end | |||
end | end | ||
| Рядок 640: | Рядок 655: | ||
} ) | } ) | ||
local requireList, loadDataList, usedTemplateList = getRequireList | local requireList, loadDataList, usedTemplateList, extraCategories; | ||
do | |||
local ok; | |||
ok, requireList, loadDataList, usedTemplateList, extraCategories = pcall(getRequireList, currentPageName, true); | |||
if not ok then | |||
return userError(requireList); | |||
end | |||
end | |||
requireList = arr.map( requireList, function ( moduleName ) | requireList = arr.map( requireList, function ( moduleName ) | ||
| Рядок 673: | Рядок 695: | ||
table.insert( res, formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList ) ) | table.insert( res, formatUsedTemplatesList( currentPageName, addCategories, usedTemplateList ) ) | ||
table.insert( res, formatRequiredByList( currentPageName, addCategories, whatModulesLinkHere ) ) | table.insert( res, formatRequiredByList( currentPageName, addCategories, whatModulesLinkHere ) ) | ||
if addCategories then | |||
extraCategories = arr.map(extraCategories, function(categoryName) | |||
return "[[Category:" .. categoryName .. "]]"; | |||
end); | |||
table.insert(res, table.concat(extraCategories)); | |||
end | |||
if not moduleIsUsed then | if not moduleIsUsed then | ||