Misplaced Pages

Module:Gadgets: Difference between revisions

Article snapshot taken from[REDACTED] with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
Browse history interactively← Previous editNext edit →Content deleted Content added
Revision as of 19:59, 26 January 2024 view sourcePppery (talk | contribs)Interface administrators, Administrators101,525 edits Per request on talk← Previous edit Revision as of 20:24, 7 May 2024 view source Xaosflux (talk | contribs)Edit filter managers, Autopatrolled, Bureaucrats, Importers, Interface administrators, Oversighters, Administrators83,989 edits usage get tweak requested on talkNext edit →
Line 67: Line 67:
p.get_usage = function(name) p.get_usage = function(name)
-- escape name for use in pattern -- escape name for use in pattern
name = name:gsub("%(%)%$%^%%%?%*]", "%%%1") name = name:gsub("%(%)%$%^%%%?%*]", "%%%1"):gsub("_", " ")


-- rely on ] until ] is implemented -- rely on ] until ] is implemented

Revision as of 20:24, 7 May 2024

Module documentation[view] [edit] [history] [purge]
WarningThis Lua module is used in system messages.
Changes to it can cause immediate changes to the Misplaced Pages 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.

Module to parse gadget definitions from MediaWiki:Gadgets-definition.

Usage

Intended for use from other modules only.

local gadgets = require('Module:Gadgets')
local gadgetRegistry = gadgets.parse()
The above documentation is transcluded from Module:Gadgets/doc. (edit | history)
Editors can experiment in this module's sandbox (edit | diff) and testcases (create) pages.
Subpages of this module.

local p = {}

p.parse = function()
	local text = mw.title.new('MediaWiki:Gadgets-definition'):getContent()
	local lines = mw.text.split(text, '\n', false)
	
	local repo = {}
	for _, line in ipairs(lines) do
		if line:sub(1, 1) == '*' then
			local name, options, pages = p.parse_line(line)
			if name and #pages ~= 0 then
				repo = { options = options, pages = pages }
			end
		end
	end
	return repo
end

p.parse_line = function(def) 
	local pattern = "^%*%s*(.+)%s*(%b)%s*(.-)$"
	local name, opts, pageList = string.match(def, pattern)
	
	name = mw.text.trim(name)

	-- Process options string into a Lua table
    local options = {}	
	if opts then
	    -- Extracting the options without square brackets and trimming spaces
	    opts = opts:sub(2, -2):gsub("%s+", "")
	    
	    for pair in opts:gmatch("%s*(+)%s*|?") do
		    local key, value = pair:match("%s*(+)%s*=%s*(+)%s*")
		    if key and value then
		        options = value:match("^%s*(.-)%s*$")
		    else
		        key = pair:match("%s*(.-)%s*$")
		        options = true
		    end
		end
	end

	-- Process page list into an array
	local pages = {}
	if pageList then
	    for page in pageList:gmatch("+") do
	        table.insert(pages, mw.text.trim(page))
	    end
    end
    return name, options, pages
end

p.get_type = function(def) 
	if def.options.type == 'general' or def.options.type == 'styles' then
		return def.options.type
	end
	if def.options.dependencies ~= nil then
		return 'general'
	end
	for _, page in ipairs(def.pages) do
		if not string.match(page, '%.css$') then
			return 'general'
		end
	end
	return 'styles'
end

p.get_usage = function(name)
	-- escape name for use in pattern
	name = name:gsub("%(%)%$%^%%%?%*]", "%%%1"):gsub("_", " ")

	-- rely on ] until ] is implemented
	local _, _, count = mw.title.new('Misplaced Pages:GUS2Wiki'):getContent():find('\n'..name..',(%S+)\n')
	return tonumber(count) or -1
end

return p
Module:Gadgets: Difference between revisions Add topic