Module:Script doc auto
Appearance
![]() | This Lua module is used in system messages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
![]() | This module can only be edited by administrators because it is transcluded onto one or more cascade-protected pages. |
![]() | This module depends on the following other modules: |
![]() | This module uses TemplateStyles: |
This module is for use by {{Script doc auto}}.
Usage
{{#invoke:Script doc auto|main}}
No arguments are needed. For testing what the output would be when used on a specific page, you use |page=
param.
local messageBox = require('Module:Message box')
local gadgets = require('Module:Gadgets')
local getArgs = require('Module:Arguments').getArgs
local p = {}
p.main = function(frame)
local args = getArgs(frame)
return p.core(args.page or mw.title.getCurrentTitle().fullText)
end
p.core = function(page)
local len = page:len()
if len < 4 then
-- Too short page name, do nothing
return ''
end
if page:sub(-4, -1) == '.css' then
local basepage = page:sub(0, -5)
local sisterpage = basepage..'.js'
return p.makeMessage('css', mw.title.new(basepage), mw.title.new(sisterpage), 'js')
end
if page:sub(-3, -1) == '.js' then
local basepage = page:sub(0, -4)
local sisterpage = basepage..'.css'
return p.makeMessage('js', mw.title.new(basepage), mw.title.new(sisterpage), 'css')
end
end
local skins = {
['common'] = true,
['vector-2022'] = true,
['vector'] = true,
['timeless'] = true,
['minerva'] = true,
['monobook'] = true,
['modern'] = true,
['cologneblue'] = true
}
local function arr_contains(array, val)
for _, value in ipairs(array) do
if value == val then
return true
end
end
return false
end
p.gadget_text = function(name, repo)
local lang = mw.getContentLanguage()
local options = repo[name].options
local dependents = {}
-- [WIP]
-- if options.hidden ~= nil then
-- -- Find dependents
-- for n, c in pairs(repo) do
-- if (c.options.dependencies and arr_contains(c.options.dependencies, name)) or
-- (c.options.peers and arr_contains(c.options.peers, name)) then
-- table.insert(dependents, '[[Special:Gadgets#gadget-'..n..'|'..n..']]')
-- end
-- end
-- end
return 'This page is loaded as a part of ' ..
'[[Special:Gadgets#gadget-'..name..'|'..name..']] gadget' ..
(options.hidden ~= nil and ', a hidden gadget'..
(#dependents > 0 and ' used by '..mw.text.listToText(dependents) or '') or
(options.default ~= nil and ' <b>enabled by default</b>.' or
(', used by '..lang:formatNum(tonumber(gadgets.get_usage(name)))..' users. '))) ..
'<br>'
end
p.makeMessage = function(pagetype, basepage, sisterpage, sistertype)
local text = ''
if basepage.namespace == 2 then
if skins[basepage.subpageText] ~= nil then
-- We are on a user skin file
text = 'The accompanying .'..sistertype..' page for this skin '..
(sisterpage.exists and 'is' or 'can be added')..' at [['..sisterpage.fullText..']].'
else
-- We are on some script page, not a user skin file
local docpageExists = basepage.exists
local sisterpageExists = sisterpage.exists
if docpageExists and sisterpageExists then
text = 'This [[Wikipedia:User scripts|user script]] seems to have a documentation page at [['..basepage.fullText..']] and an accompanying .'..sistertype..' page at [['..sisterpage.fullText..']].'
elseif docpageExists and not sisterpageExists then
text = 'This [[Wikipedia:User scripts|user script]] seems to have a documentation page at [['..basepage.fullText..']].'
elseif sisterpageExists then
text = 'Documentation for this [[Wikipedia:User scripts|user script]] can be added at [['..basepage.fullText..']]. This user script seems to have an accompanying .'..sistertype..' page at [['..sisterpage.fullText..']]. '
else
text = 'Documentation for this [[Wikipedia:User scripts|user script]] can be added at [['..basepage.fullText..']].'
end
end
elseif basepage.namespace == 8 then
if basepage.text:find('^Gadget-') ~= nil then
local gadgetRepo = gadgets.parse()
local shortName = basepage.text:gsub('^Gadget%-', '') .. '.' .. pagetype
for name, config in pairs(gadgetRepo) do
if arr_contains(config.pages, shortName) then
text = text .. p.gadget_text(name, gadgetRepo)
end
end
end
end
if text ~= '' then
return messageBox.main('fmbox', {
id = 'mw-script-doc',
type = 'system',
image = '[[File:Template-info.svg|43x40px]]',
style = 'background: #ecfcf4;',
text = text
})
end
end
return p