Module:Forumlink: Difference between revisions

From NPOWiki
Jump to navigation Jump to search
(added NS forum/phpbb, TSP/mybb, ability to link to post)
(wow, I can't read. Also added CN and override system to support npowned. More robust default text coming later)
Line 3: Line 3:


--Keys must be lowercase
--Keys must be lowercase
--Can override using same key as in types
local presets = {
local presets = {
npo = { domain = 'npowned.net', type = 'ips' },
npo = { domain = 'npowned.net', type = 'invision',
forum = 'forums/forum/%s/', topic = 'forums/topic/%s/', default = 'forums/' },
cn = { domain = 'forum.cybernations.net', type='invision' },
ns = { domain = 'forum.nationstates.net', type='phpbb' },
ns = { domain = 'forum.nationstates.net', type='phpbb' },
tsp = { domain = 'tspforums.xyz', type='mybb' },
tsp = { domain = 'tspforums.xyz', type='mybb' },
Line 13: Line 16:
--If parameters are numerical only, set sep to ''
--If parameters are numerical only, set sep to ''
local types = {
local types = {
ips = {
invision = {
sep = '-',
sep = '-',
announcement = 'announcement/%s/',
announcement = 'announcement/%s/',
forum = 'forums/forum/%s/',
forum = 'forum/%s/',
news = 'news/community/%s/',
news = 'news/community/%s/',
profile = 'profile/%s/',
profile = 'profile/%s/',
topic = 'forums/topic/%s/',
topic = 'topic/%s/',
post = '?do=findComment&comment=%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',
default = 'index.php',
postRequiresTopic = true
postRequiresTopic = true
Line 39: Line 33:
topic = 'thread-%s.html',
topic = 'thread-%s.html',
post = '-post-%s.html#pid%s',
post = '-post-%s.html#pid%s',
default = 'index.php',
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',
default = 'index.php',
postRequiresTopic = true
postRequiresTopic = true
Line 61: Line 64:
end
end


local function geturl(url, arg1, arg2, ftype, typename)
local function geturl(url, arg1, arg2, preset, ftype, typename)
--Calculate overrides
local part1, part2, sep
if not preset then preset = {} end
if preset[arg1] then
part1 = preset[arg1]
else
part1 = ftype[arg1]
end
if arg2 ~= '' then
if preset[arg2] then
part2 = preset[arg2]
else
part2 = ftype[arg2]
end
end
if preset.sep then
sep = preset.sep
else
sep = ftype.sep
end
if arg1 == 'default' then
if arg1 == 'default' then
return url .. ftype[arg1]
return url .. part1
end
end
local text1, text2 = '', ''
local text1, text2 = '', ''
text1 = mw.ustring.lower(largs[arg1])
text1 = mw.ustring.lower(largs[arg1])
Line 71: Line 96:
end
end
if ftype.sep ~= ' ' then
if sep ~= ' ' then
--character class: %s is any space character
--character class: %s is any space character
text1 = mw.ustring.gsub(text1, '%s', ftype.sep)
text1 = mw.ustring.gsub(text1, '%s', sep)
text2 = mw.ustring.gsub(text2, '%s', ftype.sep)
text2 = mw.ustring.gsub(text2, '%s', sep)
end
end
Line 81: Line 106:
--  if different params are needed the script will need a rewrite
--  if different params are needed the script will need a rewrite
--  (Lua doesn't support specifying arg num)
--  (Lua doesn't support specifying arg num)
url = url .. mw.ustring.format(ftype[arg1], text1, text1)
url = url .. mw.ustring.format(part1, text1, text1)
if text2 ~= '' then
if text2 ~= '' then
url = url .. mw.ustring.format(ftype[arg2], text2, text2)
url = url .. mw.ustring.format(part2, text2, text2)
end
end
Line 119: Line 144:
return '<span class="error">missing forum type</span>'
return '<span class="error">missing forum type</span>'
end
end
ftype = mw.ustring.lower(ftype)
typename = ftype
typename = ftype
ftype = types[mw.ustring.lower(ftype)]
ftype = types[ftype]
if not ftype then
if not ftype then
return '<span class="error">type is invalid or unrecognized</span>'
return '<span class="error">type is invalid or unrecognized</span>'
Line 146: Line 172:
end
end
end
end
url = geturl(url, component1, component2, ftype, typename)
url = geturl(url, component1, component2, preset, ftype, typename)
if argDefault('numbered') then
if argDefault('numbered') then

Revision as of 02:58, 16 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
--Can override using same key as in types
local presets = {
	npo = { domain = 'npowned.net', type = 'invision',
		forum = 'forums/forum/%s/', topic = 'forums/topic/%s/', default = 'forums/' },
	cn = { domain = 'forum.cybernations.net', type='invision' },
	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 = {
	invision = {
		sep = '-',
		announcement = 'announcement/%s/',
		forum = 'forum/%s/',
		news = 'news/community/%s/',
		profile = 'profile/%s/',
		topic = 'topic/%s/',
		post = '?do=findComment&comment=%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
	},
	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
	},
	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, preset, ftype, typename)
	--Calculate overrides
	local part1, part2, sep
	if not preset then preset = {} end
	if preset[arg1] then
		part1 = preset[arg1]
	else 
		part1 = ftype[arg1]
	end
	if arg2 ~= '' then
		if preset[arg2] then
			part2 = preset[arg2]
		else
			part2 = ftype[arg2]
		end
	end
	if preset.sep then
		sep = preset.sep
	else
		sep = ftype.sep
	end
	
	if arg1 == 'default' then
		return url .. part1
	end
	
	local text1, text2 = '', ''
	text1 = mw.ustring.lower(largs[arg1])
	if arg2 ~= '' then
		text2 = mw.ustring.lower(largs[arg2])
	end
	
	if sep ~= ' ' then
		--character class: %s is any space character
		text1 = mw.ustring.gsub(text1, '%s', sep)
		text2 = mw.ustring.gsub(text2, '%s', 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(part1, text1, text1)
	if text2 ~= '' then
		url = url .. mw.ustring.format(part2, 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
	ftype = mw.ustring.lower(ftype)
	typename = ftype
	ftype = types[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, preset, 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