Jump to content

Module:User:SD0001/Code page header

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by SD0001 (talk | contribs) at 14:35, 20 January 2024 (wip). 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:User:SD0001/Gadgets')

local function arr_contains(array, val)
    for _, value in ipairs(array) do
        if value == val then
            return true
        end
    end
    return false
end

local p = {}

p.main = function()
	return p.makeHeader(mw.title.getCurrentTitle())
end

p._makeHeader = function(frame) 
	return p.makeHeader(mw.title.new(require('Module:Arguments').getArgs(frame)[1]))
end

p.makeHeader = function(title)
	local model = title.contentModel
	if model ~= 'javascript' and model ~= 'css' and model ~= 'json' then
		return ''
	end
	
	local msg = ''
	if title.namespace == 8 and title.text:find('^Gadget-') ~= nil then
		local repo = gadgets.parse()
		local shortName = title.text:gsub('^Gadget%-', '')
		for name, config in pairs(repo) do
			if arr_contains(config.pages, shortName) then
				msg = msg .. 
					'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 '..gadgets.get_usage(name)..' users ') .. '<br>'
			end
		end
	end
	
	if msg:len() > 0 then
		return messageBox.main('fmbox', {
			text = msg,
			type = 'notice',
			image = '[[File:Template-info.svg]]',
			style = '',
			class = 'code-page-header'
		})
	else 
		return ''
	end
end

return p