Jump to content

Module:Script doc auto

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 21:27, 21 January 2024 (show if page is used in a gadget, along with the number of gadget users). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

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 basename = page:sub(0, -5)
		local docpage = basename
		local sisterpage = basename..'.js'
		return p.makeMessage('css', mw.title.new(docpage), mw.title.new(sisterpage), 'js')
	end
	
	if page:sub(-3, -1) == '.js' then
		local basename = page:sub(0, -4)
		local docpage = basename
		local sisterpage = basename..'.css'
		return p.makeMessage('js', mw.title.new(docpage), 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.makeMessage = function(pagetype, docpage, sisterpage, sistertype)
	local text = ''
	if docpage.namespace == 2 then
		if skins[docpage.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 = docpage.exists
			local sisterpageExists = sisterpage.exists
			
			if docpageExists and sisterpageExists then
				text = 'This [[Wikipedia:User scripts|user script]] seems to have a documentation page at [['..docpage.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 [['..docpage.fullText..']].'
			elseif sisterpageExists then
				text = 'Documentation for this [[Wikipedia:User scripts|user script]] can be added at [['..docpage.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 [['..docpage.fullText..']].'
			end
		end
	
	elseif docpage.namespace == 8 then
		if docpage.text:find('^Gadget-') ~= nil then
			local gadgetRepo = gadgets.parse()
			local lang = mw.getContentLanguage()
			local shortName = docpage.text:gsub('^Gadget%-', '') .. '.' .. pagetype
			for name, config in pairs(gadgetRepo) do
				if arr_contains(config.pages, shortName) then
					text = text .. 
						'This page is loaded as a part of ' ..
						'[[Special:Gadgets#gadget-'..name..'|'..name..']] gadget ' ..
						(config.options.default ~= nil and '<b>enabled by default</b>.' or
						'used by '..lang:formatNum(tonumber(gadgets.get_usage(name)))..' users. ') .. '<br>'
				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