Jump to content

Module:Mock title

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Mr. Stradivarius (talk | contribs) at 03:17, 21 February 2021 (make a start on a general module for mocking and patching mw.title objects). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)

local checkType = require('libraryUtil').checkType

local p = {}
local mockTitleRegistry = {}

--[[
-- Patch an existing title object with the given options.
--]]
function p.patchTitleObject(title, options)
	-- Set protection levels
	local levels = {
		edit = {},
		move = {},
		autoreview = {}
	}
	for _, action in ipairs{'edit', 'move', 'autoreview'} do
		local level = options[action]
		if level then
			levels[action][1] = level
		end
	end
	rawset(title, 'protectionLevels', levels)

	-- Set content model
	rawset(title, 'contentModel', options.contentModel or 'wikitext')

	return title
end

--[[
-- Construct a new mock title.
--]]
function p.MockTitle(options)
	local title = mw.title.new(options.page)
	return p.patchTitleObject(title, options)
end

--[[
-- Normalize data for a mock title
--]]
local function createMockTitle(titleOrOptions)
	if type(titleOrOptions) == "string" then
		return p.MockTitle{page = titleOrOptions}
	elseif type(titleOrOptions.getContent) == "function" then
		return titleOrOptions
	else
		return p.MockTitle(titleOrOptions)
	end
end

--[[
-- Register a mock title.
-- This can be a string, a mock title object or a table of options for
-- MockTitle.
--]]
function p.registerMockTitle(titleOrOptions)
	checkType("registerMockTitle", 1, titleOrOptions, "table")
	title = normalizeMockTitle(titleOrOptions)
	mockTitleRegistry[title.prefixedText] = title
end

--[[
-- Remove a title from the mock title registry.
--]]
function p.deregisterMockTitle(titleOrOptions)
	checkType("deregisterMockTitle", 1, title, "table")
	title = normalizeMockTitle(titleOrOptions)
	mockTitleRegistry[title.prefixedText] = nil
end

--[[
-- Register multiple mock titles.
--]]
function p.registerMockTitles(...)
	checkType("registerMockTitles", 1, titleData, "table")
	for _, titleOrOptions in ipairs{...} do
		p.registerMockTitle(titleOrOptions)
	end
end

--[[
-- Deregister multiple mock titles.
--]]
function p.deregisterMockTitles(...)
	checkType("deregisterMockTitles", 1, titleData, "table")
	for _, titleOrOptions in ipairs{...} do
		p.deregisterMockTitle(titleOrOptions)
	end
end

--[[
-- Clear the mock title registry.
--]]
function p.clearMockTitleRegistry()
	mockTitleRegistry = {}
end

function p.patchTitleNew(func, ...)
	local oldTitleNew = mw.title.new
	mw.title.new = function(text, namespace)
		local title = oldTitleNew(text, namespace)
		local mockTitle = mockTitleRegistry[title.prefixedText]
		if mockTitle then
			return mockTitle
		else
			return title
		end
	end
	func(...)
	mw.title.new = oldTitleNew
end

function p.patchMakeTitle()
end

function p.patchTitleConstructors()
end

return p