Jump to content

Module:Unsigned/sandbox: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
scope
test
Line 1: Line 1:
local p = {}
local p = {}
local ips = require('Module:IPAddress')
-- alternative module title: HouseBlaster attempts to get around Lua patterns being awful
-- alternative module title: HouseBlaster attempts to get around Lua patterns being awful
-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.
-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.
Line 29: Line 28:
--if nothing is found, returns the empty string
--if nothing is found, returns the empty string
--we search for time and date seperately to allow for a time-less addition of date
--we search for time and date seperately to allow for a time-less addition of date
local toReturn
local toReturn
--match on a nil is a bad thing
toReturn = mw.ustring.match( s, '%d%d:%d%d,?', 0 ) or ''
local sOrBlank = s or ''
toReturn = mw.ustring.match( sOrBlank, '%d%d:%d%d,?', 0 ) or ''
if toReturn then
if toReturn then
toReturn = toReturn .. ' '
toReturn = toReturn .. ' '
end
end
toReturn = toReturn .. (mw.ustring.match( s, '%d%d? %u%l+ %d%d%d%d', 0 ) or '')
toReturn = toReturn .. (mw.ustring.match( sOrBlank, '%d%d? %u%l+ %d%d%d%d', 0 ) or '')
toReturn = toReturn .. (mw.ustring.match( s, ' %(UTC%)', 0 ) or '')
toReturn = toReturn .. (mw.ustring.match( sOrBlank, ' %(UTC%)', 0 ) or '')
return toReturn
return toReturn
end
end
Line 46: Line 47:
--if you try putting random garbage in, you **will** get garbage out
--if you try putting random garbage in, you **will** get garbage out
--------YOU HAVE BEEN WARNED--------
--------YOU HAVE BEEN WARNED--------
local toReturn
local toReturn = s or ''
--remove the date
--remove the date
toReturn = mw.ustring.gsub( s, '%d%d? %u%l+ %d%d%d%d', '', 1 ) or ''
toReturn = mw.ustring.gsub( toReturn, '%d%d? %u%l+ %d%d%d%d', '', 1 ) or ''
--remove the time
--remove the time
toReturn = mw.ustring.gsub( toReturn, '%d%d:%d%d,?', '', 1) or ''
toReturn = mw.ustring.gsub( toReturn, '%d%d:%d%d,?', '', 1) or ''
Line 79: Line 80:
timestamp = p.getTimestamp(args[1])
timestamp = p.getTimestamp(args[1])
end
end
isIP = ips.isIP(username)
--isIp = require('Module:IPAddress').isIP(username)
return frame:expandTemplate{ title = 'unsigned/core', args = { username , timestamp or '' } }
return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/core', args = { username , timestamp or '' } }
end
end



Revision as of 00:59, 25 May 2025

local p = {}
-- alternative module title: HouseBlaster attempts to get around Lua patterns being awful
-- There's probably a way to use strptime or some other more sophisticated way, but you're not supposed to be using a non-timestamp as input anyway.

local function endswith(String,End)
	return End == '' or string.sub(String,-string.len(End)) == End
end

local function trim(s)
	return s:gsub("^%s+", ""):gsub("%s+$", ""):gsub("\226\128\142", "")
end

local function addUtcToStringIfItDoesNotEndWithUtc(s)
	if s == "" or endswith(s, "~~~~") then return s end
	if not endswith(s, "(UTC)") then
		return s .. " (UTC)"
	end
	return s
end

local function _main(args)
	local hopefullyTimestamp = args[1] or os.date('%H:%M, %d %B %Y (%Z)')
	return addUtcToStringIfItDoesNotEndWithUtc(trim(hopefullyTimestamp))
end

function p.getTimestamp(s)
	--gets the timestamp in the input
	--if nothing is found, returns the empty string
	--we search for time and date seperately to allow for a time-less addition of date
	local toReturn
	--match on a nil is a bad thing
	local sOrBlank = s or ''
	toReturn = mw.ustring.match( sOrBlank, '%d%d:%d%d,?', 0 ) or ''
	if toReturn then
		toReturn = toReturn .. ' '
	end
	toReturn = toReturn .. (mw.ustring.match( sOrBlank, '%d%d? %u%l+ %d%d%d%d', 0 ) or '')
	toReturn = toReturn .. (mw.ustring.match( sOrBlank, ' %(UTC%)', 0 ) or '')
	return toReturn
end

function p.getUsername(s)
	--gets the username in the input
	--------WARNING--------
	--this method assumes that *everything* besides the timestamp is, in fact, part of the username
	--it does no further username validation
	--if you try putting random garbage in, you **will** get garbage out
	--------YOU HAVE BEEN WARNED--------
	local toReturn = s or ''
	--remove the date
	toReturn = mw.ustring.gsub( toReturn, '%d%d? %u%l+ %d%d%d%d', '', 1 ) or ''
	--remove the time
	toReturn = mw.ustring.gsub( toReturn, '%d%d:%d%d,?', '', 1) or ''
	--remove (UTC)
	toReturn = mw.ustring.gsub( toReturn, '%(UTC%)', '', 1) or ''
	--concat the empty string to ensure we only return the string and not the number of matches
	return trim(toReturn) .. ''
end

local function makeUnsigned(args)
	local isIP
	local username
	local timestamp
	local toReturn
	
	if args[2] then
		-- we have multiple parameters
		-- so we can be less janky
		timestamp = p.getTimestamp(args[2])
		if timestamp then
			username = args[1]
		else
			username = args[2]
			timestamp = args[1]
		end
	else
		-- be very janky; this has a high probability of GIGO-ing
		-- due to the way getUsername is implemented
		username = p.getUsername(args[1])
		timestamp = p.getTimestamp(args[1])
	end
	--isIp = require('Module:IPAddress').isIP(username)
	
	return mw.getCurrentFrame():expandTemplate{ title = 'unsigned/core', args = { username , timestamp or '' } } 
end

function p.main(frame)
	local args
	if type(frame.args) == 'table' then
		-- We're being called via #invoke. The args are passed through to the module
		-- from the template page, so use the args that were passed into the template.
		args = require('Module:Arguments').getArgs(frame)
	else
		-- We're being called from another module or from the debug console, so assume
		-- the args are passed in directly.
		args = frame
	end
	return makeUnsigned(args)
end

return p