Module:Utils: Difference between revisions

From Halopedia, the Halo wiki

No edit summary
No edit summary
Line 16: Line 16:
return nil
return nil
end
end
end
function generateErrorWikitext( message, type, category, template )
if template == nil then
-- Find the first frame whose title doesn't begin with 'Template:'
local prevFrame = mw.getCurrentFrame()
local frame = prevFrame:getParent()
while frame ~= nil and frame:getTitle() ~= nil and frame:getTitle():startsWith('Template:') do
prevFrame = frame
frame = prevFrame:getParent()
end
-- Use the frame immediately before that as the template that is giving
-- the error
if prevFrame:getTitle():startsWith('Template:') then
-- Strip the 'Template:' part out
template = mw.ustring.sub( prevFrame:getTitle(), 9 )
end
end
local wikitext = [[''']]
.. string.upper( type )
.. [[:''' ]]
.. message
if template ~= nil then
wikitext = wikitext .. '(Template: [[Template:'
.. template
.. '|'
.. template
.. ']])'
end
local categorylink = '[[Category:' .. category .. ']]'
return wikitext, categorylink
end
end


Line 25: Line 59:
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
-- Style data  
-- Generate  data  
etype = string.upper( etype )
local wikitext, categorylink = generateErrorWikitext( message, etype .. ' error', category, template )
template = tostring( template )
-- Add custom styling
wikitext = [[<span class="error halopedia-template-error" style="color: red;">]] .. wikitext .. [[</span>]]
-- Add a warning to the edit preview
-- Add a warning to the edit preview
if template == nil then
mw.addWarning( wikitext )
mw.addWarning( "'''" .. etype .. " ERROR:''' " .. message )
else
mw.addWarning( "'''" .. etype .. " ERROR:''' " .. message .. ' (Template: [[Template:' .. template .. '|' .. template .. ']])' )
end
-- Create and return the text to display on the page itself
-- Create and return the text to display on the page itself
return [[<span class="error halopedia-template-error" style="color: red;">''']]
return wikitext .. ' ' .. categorylink
.. etype
.. [[ ERROR:''' ]]
.. message
.. '(Template: [[Template:'
.. template
.. '|'
.. template
.. ']]</span> [[Category:'
.. category
.. ']]'
end
end


function utils.warning( ... )
function utils.warning( ... )
-- Collect necessary information to display warning
-- Collect necessary information to display error
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'This template issued a warning. Are the parameters all correct?' ) )
local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'This template issued a warning. Are the parameters all correct?' ) )
local wtype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
local wtype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
Line 57: Line 79:
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
local template = utils.defaultIfEmpty( select( 4, ... ), nil )
-- Style data  
-- Generate  data  
wtype = string.upper( wtype )
local wikitext, categorylink = generateErrorWikitext( message, wtype .. ' warning', category, template )
-- Add a warning to the edit preview
-- Add custom styling
if template == nil then
wikitext = [[<span class="warning halopedia-template-warning" style="color: orange;">]] .. wikitext .. [[</span>]]
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message )
else
-- Add the warning to the edit preview
mw.addWarning( "'''" .. wtype .. " WARNING:''' " .. message .. ' (Template: [[Template:' .. template .. '|' .. template .. ']])' )
mw.addWarning( wikitext )
end
-- Create and return the text to add to the page. In this case, we only add
-- Create and return the text to add to the page. In this case, we only add
-- the category - no message, as warnings should be silent to the reader
-- the category - no message, as warnings should be silent to the reader
return '[[Category:' .. category .. ']]'
return categorylink
end
end


return utils
return utils

Revision as of 17:16, April 24, 2021

Documentation for this module may be created at ModuleDoc:Utils

local utils = {}

function utils.defaultIfEmpty( val, default )
	if val == nil or val == '' then
		return default
	else
		return val
	end
end

function utils.loadDataIfExists( name )
	local success, data = pcall( mw.loadData, name )
	if success then
		return data
	else
		return nil
	end
end

function generateErrorWikitext( message, type, category, template )
	if template == nil then
		-- Find the first frame whose title doesn't begin with 'Template:'
		local prevFrame = mw.getCurrentFrame()
		local frame = prevFrame:getParent()
		while frame ~= nil and frame:getTitle() ~= nil and frame:getTitle():startsWith('Template:') do
			prevFrame = frame
			frame = prevFrame:getParent()
		end
		-- Use the frame immediately before that as the template that is giving
		-- the error
		if prevFrame:getTitle():startsWith('Template:') then
			-- Strip the 'Template:' part out
			template = mw.ustring.sub( prevFrame:getTitle(), 9 )
		end
	end
	
	local wikitext = [[''']]
		.. string.upper( type )
		.. [[:''' ]]
		.. message
	if template ~= nil then
		wikitext = wikitext .. '(Template: [[Template:'
			.. template
			.. '|'
			.. template
			.. ']])'
	end

	local categorylink = '[[Category:' .. category .. ']]'
	
	return wikitext, categorylink
end

function utils.error( ... )
	-- Collect necessary information to display error
	local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'There was an error with a template. Are the parameters all correct?' ) )
	local etype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
	local category = tostring( utils.defaultIfEmpty( select( 3, ... ), 'Pages containing template errors' ) )
	local template = utils.defaultIfEmpty( select( 4, ... ), nil )
	
	-- Generate  data 
	local wikitext, categorylink = generateErrorWikitext( message, etype .. ' error', category, template )
	
	-- Add custom styling
	wikitext = [[<span class="error halopedia-template-error" style="color: red;">]] .. wikitext .. [[</span>]]
	
	-- Add a warning to the edit preview
	mw.addWarning( wikitext )
	
	-- Create and return the text to display on the page itself
	return wikitext .. ' ' .. categorylink
end

function utils.warning( ... )
	-- Collect necessary information to display error
	local message = tostring( utils.defaultIfEmpty( select( 1, ... ), 'This template issued a warning. Are the parameters all correct?' ) )
	local wtype = tostring( utils.defaultIfEmpty( select( 2, ... ), 'Template' ) )
	local category = tostring( utils.defaultIfEmpty( select( 3, ... ), 'Pages containing template warnings' ) )
	local template = utils.defaultIfEmpty( select( 4, ... ), nil )
	
	-- Generate  data 
	local wikitext, categorylink = generateErrorWikitext( message, wtype .. ' warning', category, template )
	
	-- Add custom styling
	wikitext = [[<span class="warning halopedia-template-warning" style="color: orange;">]] .. wikitext .. [[</span>]]
	
	-- Add the warning to the edit preview
	mw.addWarning( wikitext )
	
	-- Create and return the text to add to the page. In this case, we only add
	-- the category - no message, as warnings should be silent to the reader
	return categorylink
end

return utils