Module:Forumlink: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
No edit summary
(added NS forum/phpbb, TSP/mybb, ability to link to post)
Line 4: Line 4:
--Keys must be lowercase
--Keys must be lowercase
local presets = {
local presets = {
npo = { domain = 'npowned.net', type = 'ips' }
npo = { domain = 'npowned.net', type = 'ips' },
ns = { domain = 'forum.nationstates.net', type='phpbb' },
tsp = { domain = 'tspforums.xyz', type='mybb' },
tsparchive = { domain = 'archive.tspforums.xyz', type='mybbold' },
}
}


--Keys must be lowercase
--Keys must be lowercase
--If parameters are numerical only, set sep to ''
local types = {
local types = {
ips = {
ips = {
Line 16: Line 20:
profile = 'profile/%s/',
profile = 'profile/%s/',
topic = 'forums/topic/%s/',
topic = 'forums/topic/%s/',
default = 'forums/'
post = '?do=findComment&comment=%s',
}
default = 'forums/',
postRequiresTopic = true
},
mybbold = {
sep = '',
forum = 'forumdisplay.php?fid=%s',
profile = 'member.php?action=profile&uid=%s',
topic = 'showthread.php?tid=%s',
post = '&pid=%s#pid%s',
default = 'index.php',
postRequiresTopic = true
},
mybb = {
sep = '',
forum = 'forum-%s.html',
profile = 'user-%s.html',
topic = 'thread-%s.html',
post = '-post-%s.html#pid%s',
default = 'index.php',
postRequiresTopic = true
},
phpbb = {
sep = '',
forum = 'viewforum.php?f=%s',
profile = 'memberlist.php?mode=viewprofile&u=%s',
topic = 'viewtopic.php?t=%s',
post = 'viewtopic.php?p=%s#p%s',
default = 'index.php',
postRequiresTopic = false
},
}
}


Line 28: Line 61:
end
end


local function geturl(url, arg, ftype)
local function geturl(url, arg1, arg2, ftype, typename)
if arg == 'default' then
if arg1 == 'default' then
return url .. ftype[arg]
return url .. ftype[arg1]
end
end
local text = mw.ustring.lower(largs[arg])
local text1, text2 = '', ''
text1 = mw.ustring.lower(largs[arg1])
if arg2 ~= '' then
text2 = mw.ustring.lower(largs[arg2])
end
if ftype.sep ~= ' ' then
if ftype.sep ~= ' ' then
text = mw.ustring.gsub(text, '%s', ftype.sep)
--character class: %s is any space character
text1 = mw.ustring.gsub(text1, '%s', ftype.sep)
text2 = mw.ustring.gsub(text2, '%s', ftype.sep)
end
--printf: %s is a placeholder for a string
--param passed twice for formats that need it (e.g. phpbb post);
--  if different params are needed the script will need a rewrite
--  (Lua doesn't support specifying arg num)
url = url .. mw.ustring.format(ftype[arg1], text1, text1)
if text2 ~= '' then
url = url .. mw.ustring.format(ftype[arg2], text2, text2)
end
end
return url .. mw.ustring.format(ftype[arg], text)
if typename == 'mybb' and arg2 == 'post' then
url = mw.ustring.gsub(url, '.html', '', 1)
end
return url
end
end


Line 45: Line 99:
function p.call(args)
function p.call(args)
largs = args-- lazy shortcut for local args
largs = args-- lazy shortcut for local args
local ftype, url, domain, display
local ftype, typename, url, domain, display
if argDefault('http') then
if argDefault('http') then
url = 'http://'
url = 'http://'
Line 65: Line 119:
return '<span class="error">missing forum type</span>'
return '<span class="error">missing forum type</span>'
end
end
typename = ftype
ftype = types[mw.ustring.lower(ftype)]
ftype = types[mw.ustring.lower(ftype)]
if not ftype then
if not ftype then
Line 71: Line 126:
url = url .. domain .. '/'
url = url .. domain .. '/'
local params = {'topic', 'profile', 'forum', 'news', 'announcement'}
--post has to come first, the rest are just in case multiple params are used accidentally
local component = 'default'
local params = {'post', 'topic', 'profile', 'forum', 'news', 'announcement'}
local component1 = 'default'
local component2 = ''
for i, para in ipairs(params) do
for i, para in ipairs(params) do
if argDefault(para) then
if argDefault(para) then
component = para
component1 = para
if para == 'post' and ftype.postRequiresTopic then
component2 = 'topic'
end
if component2 ~= '' and not argDefault(component2) then
return '<span class="error">' .. component2 .. ' is required to link to ' .. component1 .. '</span>'
end
if component1 == 'post' and component2 == 'topic' then
component1 = 'topic'
component2 = 'post'
end
break
break
end
end
end
end
url = geturl(url, component, ftype)
url = geturl(url, component1, component2, ftype, typename)
if argDefault('numbered') then
if argDefault('numbered') then
Line 87: Line 154:
display = argDefault(2)
display = argDefault(2)
if not display then
if not display then
if component == 'default' then
if component1 == 'default' then
display = args[1] .. ' forums'
display = args[1] .. ' forums'
else
else
display = args[component]
display = args[component1]
end
end
end
end

Revision as of 22:44, 15 July 2020

Documentation [edit]

This page is a stub - it only covers the very basics of its subject. More information should be added to make the page informative and useful. If no more information is available, the page should be considered for a merge, redirect, or deletion.

Details: documentation to be written

If you can correct the issue, please edit the page to do so, then remove this notice.
Please see Template:Forumlink for usage information. Use the template rather than invoking the module.

The above documentation is transcluded from Module:Forumlink/doc.

local p = {}
local largs

--Keys must be lowercase
local presets = {
	npo = { domain = 'npowned.net', type = 'ips' },
	ns = { domain = 'forum.nationstates.net', type='phpbb' },
	tsp = { domain = 'tspforums.xyz', type='mybb' },
	tsparchive = { domain = 'archive.tspforums.xyz', type='mybbold' },
}

--Keys must be lowercase
--If parameters are numerical only, set sep to ''
local types = {
	ips = {
		sep = '-',
		announcement = 'announcement/%s/',
		forum = 'forums/forum/%s/',
		news = 'news/community/%s/',
		profile = 'profile/%s/',
		topic = 'forums/topic/%s/',
		post = '?do=findComment&comment=%s',
		default = 'forums/',
		postRequiresTopic = true
	},
	mybbold = {
		sep = '',
		forum = 'forumdisplay.php?fid=%s',
		profile = 'member.php?action=profile&uid=%s',
		topic = 'showthread.php?tid=%s',
		post = '&pid=%s#pid%s',
		default = 'index.php',
		postRequiresTopic = true
	},
	mybb = {
		sep = '',
		forum = 'forum-%s.html',
		profile = 'user-%s.html',
		topic = 'thread-%s.html',
		post = '-post-%s.html#pid%s',
		default = 'index.php',
		postRequiresTopic = true
	},
	phpbb = {
		sep = '',
		forum = 'viewforum.php?f=%s',
		profile = 'memberlist.php?mode=viewprofile&u=%s',
		topic = 'viewtopic.php?t=%s',
		post = 'viewtopic.php?p=%s#p%s',
		default = 'index.php',
		postRequiresTopic = false
	},
}

local function argDefault(arg, default)
	if mw.text.trim(largs[arg] or '') == '' then
		return default
	else
		return largs[arg]
	end
end

local function geturl(url, arg1, arg2, ftype, typename)
	if arg1 == 'default' then
		return url .. ftype[arg1]
	end
	local text1, text2 = '', ''
	text1 = mw.ustring.lower(largs[arg1])
	if arg2 ~= '' then
		text2 = mw.ustring.lower(largs[arg2])
	end
	
	if ftype.sep ~= ' ' then
		--character class: %s is any space character
		text1 = mw.ustring.gsub(text1, '%s', ftype.sep)
		text2 = mw.ustring.gsub(text2, '%s', ftype.sep)
	end
	
	--printf: %s is a placeholder for a string
	--param passed twice for formats that need it (e.g. phpbb post);
	--  if different params are needed the script will need a rewrite
	--  (Lua doesn't support specifying arg num)
	url = url .. mw.ustring.format(ftype[arg1], text1, text1)
	if text2 ~= '' then
		url = url .. mw.ustring.format(ftype[arg2], text2, text2)
	end
	
	if typename == 'mybb' and arg2 == 'post' then
		url = mw.ustring.gsub(url, '.html', '', 1)
	end
	
	return url
end

function p.main(frame)
	return p.call(frame:getParent().args)
end

function p.call(args)
	largs = args-- lazy shortcut for local args
	local ftype, typename, url, domain, display
	if argDefault('http') then
		url = 'http://'
	else
		url = 'https://'
	end
	local preset = presets[mw.ustring.lower(argDefault(1, ''))]
	if not preset then
		domain = argDefault(1)
		ftype = argDefault('type')
	else
		domain = preset.domain
		ftype = preset.type
	end
	if not domain then
		return '<span class="error">missing site address</span>'
	end
	if not ftype then
		return '<span class="error">missing forum type</span>'
	end
	typename = ftype
	ftype = types[mw.ustring.lower(ftype)]
	if not ftype then
		return '<span class="error">type is invalid or unrecognized</span>'
	end
	url = url .. domain .. '/'
	
	--post has to come first, the rest are just in case multiple params are used accidentally
	local params = {'post', 'topic', 'profile', 'forum', 'news', 'announcement'}
	local component1 = 'default'
	local component2 = ''
	for i, para in ipairs(params) do
		if argDefault(para) then
			component1 = para
			if para == 'post' and ftype.postRequiresTopic then
				component2 = 'topic'
			end
			if component2 ~= '' and not argDefault(component2) then
				return '<span class="error">' .. component2 .. ' is required to link to ' .. component1 .. '</span>'
			end
			if component1 == 'post' and component2 == 'topic' then
				component1 = 'topic'
				component2 = 'post'
			end
			break
		end
	end
	url = geturl(url, component1, component2, ftype, typename)
	
	if argDefault('numbered') then
		return '[' .. url .. ']'
	end
	
	display = argDefault(2)
	if not display then
		if component1 == 'default' then
			display = args[1] .. ' forums'
		else
			display = args[component1]
		end
	end
	return '[' .. url .. ' ' .. display .. ']'
end

return p