Marvel Database
Marvel Database

Documentation for this module may be created at Module:Disambiguation/doc

local p = {}
local list_of_realities = mw.loadData( 'Module:Disambiguation/Realities' )
local h = require("Module:HF")
local pagetype = require("Module:PageType")
local design = require("Module:Design")
local module_reality = require("Module:Reality")
local standard = require("Module:StandardizedName")
local getArgs = require('Dev:Arguments').getArgs 
local g_pagename = mw.title.getCurrentTitle().text
local g_noimage
local g_main_name
local g_main_title

-----------------------------------------------------------------------------------------
function p.main(frame)
	local args = getArgs (frame)
	local g_type
	local g_main_version = args['main']
	local g_alternative_names = {}
	local g_include = {}
	local g_exclude = {}
	local g_includeComic = {}
	local g_includeLiveAction = {}
	local g_includeAnimated = {}
	local g_includeGame = {}
	local g_includeOther = {}
	local main_articles = {}
	local main_descriptions = {}
	local list = ''
	local all_versions = {}
	local comics = {}
	local live_action = {}
	local animated = {}
	local games = {}
	local other_media = {}
	local subpagename = string.match(g_pagename, '(.+)%(.+%)') or g_pagename
	local headers1 = { 'Comic Variants', 'Live-Action Variants', 'Animated Variants', 'Game Variants', 'Other Media Variants' }
	local headers2 = { 'Others', subpagename..' Related', 'Teams' }
	local tables = {}
	local i
	local s = ''
	local s1 = ''
	local s2 = ''
	local see_also = {}
	local output = {}

	g_noimage = args['noimage'] or 'No Image Available.png'
	g_main_name = args['main_name']
	g_main_title = args['main_title'] or g_main_name
	table.insert(output, p.lua_add_logo(args))

	-- if 'main_name' parameter is not empty, then use DPL to create a list of alternate versions
	if not h.isempty(g_main_version)
		then
			table.insert(main_articles, h.Link(g_main_version, g_main_name)..'; '..args['main_image'])
			table.insert(main_descriptions, args['description'])
			for i = 2, 9 do
				table.insert(main_articles, args['main_article'..i])
				table.insert(main_descriptions, args['description'..i])
			end
			table.insert(output, p.lua_add_main_version(main_articles, main_descriptions))
			table.insert( output, p.lua_add_toc() )
			g_type = args['type'] or pagetype.get_page_type(g_main_version)
			for i = 1,200 do
				table.insert(g_alternative_names, args['alternative'..i])
				table.insert(g_include, args['include'..i])
				table.insert(g_includeComic, args['includeComic'..i])
				table.insert(g_includeLiveAction, args['includeLiveAction'..i])
				table.insert(g_includeAnimated, args['includeAnimated'..i])
				table.insert(g_includeGame, args['includeGame'..i])
				table.insert(g_includeOther, args['includeOther'..i])
				table.insert(g_exclude, args['exclude'..i])
			end
			
			if g_type == 'Team' or g_type == 'Organization'
				then 
					list = p.lua_dpl_list(g_main_name, g_main_version, 'Team', g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, 'Team', g_alternative_names, g_exclude, 500, 500)
					list = list..p.lua_dpl_list(g_main_name, g_main_version, 'Organization', g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, 'Organization', g_alternative_names, g_exclude, 500, 500)
					list = frame:preprocess(list)
				else 
					list = frame:preprocess( p.lua_dpl_list(g_main_name, g_main_version, g_type, g_alternative_names, g_exclude, 500)..p.lua_dpl_list(g_main_name, g_main_version, g_type, g_alternative_names, g_exclude, 500, 500) )
			end
			list = string.gsub(list, ' ', '')
			list = h.explode('@', list)
			
			for i = 1, #list do
				s = mw.text.split(list[i], '~') --h.explode('~', list[i])
				s1, s2 = module_reality.lua_get_name_and_reality(s[1])
				table.insert( all_versions, { ['page'] = s[1], ['image'] = s[2], ['title'] = s[3], ['name'] = s1, ['reality'] = s2 } )
			end
			all_versions = h.join_tables(all_versions, p.lua_transform_from_old_format(g_include, true))

			for i = 1, #all_versions do
				s = all_versions[i].reality.type
				if s == 'Game'
					then table.insert(games, all_versions[i])
					elseif s == "Animated" 
						then table.insert(animated, all_versions[i])
					elseif s == "Live Action" 
						then table.insert(live_action, all_versions[i])
					elseif s == "Other" 
						then table.insert(other_media, all_versions[i])		
					else table.insert(comics, all_versions[i])
				end
			end
			
			comics = h.join_tables(comics, p.lua_transform_from_old_format(g_includeComic, false))
			live_action = h.join_tables(live_action, p.lua_transform_from_old_format(g_includeLiveAction, false))
			animated =  h.join_tables(animated, p.lua_transform_from_old_format(g_includeAnimated, false))
			games = h.join_tables(games, p.lua_transform_from_old_format(g_includeGame, false))
			other_media = h.join_tables(other_media, p.lua_transform_from_old_format(g_includeOther, false))

			tables = { comics, live_action, animated, games, other_media }
			for i = 1,#headers1 do
				 table.insert(output, p.lua_add_section(tables[i], headers1[i], false) )
			end
		elseif not h.isempty(args['main_article1'])
			then
				table.insert(main_articles, args['main_article1'])
				table.insert(main_descriptions, args['description'])
				for i = 2, 9 do
					table.insert(main_articles, args['main_article'..i])
					table.insert(main_descriptions, args['description'..i])
				end
				table.insert(output, p.lua_add_main_version(main_articles, main_descriptions))
				table.insert( output, p.lua_add_toc() )
		else
			table.insert( output, p.lua_add_toc() )
	end

	for i = 1,9 do
		table.insert(headers2, args['group'..i..'_header'] or '')
	end
	table.insert(headers2, 'Media')
	tables = {}
	table.insert(tables, p.lua_transform_from_old_format(args.others, false) )
	table.insert(tables, p.lua_transform_from_old_format(args.related, false) )
	table.insert(tables, p.lua_transform_from_old_format(args.teams, false) )
	for i = 1,9 do
		table.insert(tables, p.lua_transform_from_old_format(args['group'..i], false) )
	end
	table.insert(tables, p.lua_transform_from_old_format(args.media, false) )

	for i = 1,#tables do
		table.insert(output, p.lua_add_section(tables[i], headers2[i], true) )
	end
	
	-- see also
	if not h.isempty(args.see_also)
		then table.insert(see_also, '\n'..args.see_also)
	end
	if #see_also > 0
		then table.insert(output, design.add_section('See Also', table.concat(see_also), 2, 'center'))
	end

	return frame:preprocess( table.concat(output) )..p.lua_get_outdated_fields(args)
end


-----------------------------------------------------------------------------------------
function p.lua_sort_by_reality(a, b)
	local reality1 = a.reality.number
	local reality2 = b.reality.number
	local dimension1 = h.isempty(a.dimension)
	local dimension2 = h.isempty(b.dimension)
	local earth33 = {'33 1/3', '33⅓'}
	local output
	
	if h.isempty(reality1) or h.isempty(reality2)
		then output = false
		else
			if h.in_list(earth33, reality1)
				then reality1 = 33.3
			end
			if h.in_list(earth33, reality2)
				then reality2 = 33.3
			end
			if reality1 == reality2 and (dimension1 == false or dimension2 == false)
				then
					if dimension1 == false and dimension2 == true
						then output = false
						elseif dimension1 == true and dimension2 == false
							then output = true
						else
							output = a.dimension.name < b.dimension.name
					end
				else
					if tonumber(reality1) ~= nil
						then
							if tonumber(reality2) ~= nil
								then output = tonumber(reality1)<tonumber(reality2)
								else output = true
							end
						elseif tonumber(reality2) ~= nil
							then output = false
							else output = a.reality.padded_number < b.reality.padded_number
					end	
			end
	end

	return output
end


-----------------------------------------------------------------------------------------
function p.lua_dpl_list(main_name, main_version, main_version_type, alternative_names, exclude, count, offset)
	local template = '{Marvel Database:'..main_version_type..' Template}'
	local image = template..':Image, '
	local title = template..':CurrentAlias'
	local s = ''
	local output = {}
	
	if not h.isempty(alternative_names)
		then alternative_names = mw.text.listToText(alternative_names, '%¦', '%¦')..'%'
		else alternative_names = ''
	end
	if not h.isempty(exclude)
		then exclude = mw.text.listToText(exclude, '%¦', '%¦')..'%'
		else exclude = ''
	end

	main_version_type = string.lower(main_version_type)

	table.insert(output, '{{#dpl:')
	table.insert(output, '|category = '..main_version_type..'s')
	table.insert(output, '|namespace = ')
	table.insert(output, '|titlematch = '..main_name..' (%¦'..alternative_names)
	table.insert(output, '|nottitlematch = '..main_version..'¦'..exclude)
	table.insert(output, '|include = '..image..title)
	table.insert(output, '|mode = userformat')
	table.insert(output, '|secseparators = %PAGE%~,~,,@')
	table.insert(output, '|noresultsheader = &#x20;')
	--table.insert(output, '|eliminate = templates')
	table.insert(output, '|allowcachedresults = true')
	if not h.isempty(count)
		then table.insert(output, '|count ='..count)
	end
	if not h.isempty(offset)
		then table.insert(output, '|offset ='..offset)
	end
	table.insert(output, '}}')
	
	return table.concat(output)
end


-----------------------------------------------------------------------------------------
-- transform string in 'page; image' format into table
function p.lua_transform_from_old_format(list, add_red_links)
	local i
	local s_page = ''
	local s_title = ''
	local s_image = ''
	local s_reality = ''
	local s_name = ''
	local value = ''
	local output = {}

	if not h.isempty(list)
		then
			if type(list) == 'string'
				then list = mw.text.split(list, '\n')
			end
			for i = 1, #list do
				value = list[i]
				if not h.isempty(value)
					then
						value = mw.text.split(h.trim(value), ';', true)
						s_page = h.break_link(value[1], 1)
						s_title = h.break_link(value[1], 2)
						s_image = h.trim(value[2] or '')
						s_name, s_reality = module_reality.lua_get_name_and_reality(s_page)
						if not h.isempty(s_reality) and s_title == s_page
							then s_title = s_name
						end
						if add_red_links == true
							then s_title = ' '..s_title
						end

						table.insert( output, { 
												['page'] = s_page, 
												['image'] = s_image, 
												['title'] = s_title, 
												['name'] = s_name, 
												['reality'] = s_reality } 
									)
				end
			end
	end
	
	return output
end


-----------------------------------------------------------------------------------------
-- adds image with link to page (if needed) and link to reality (if stated)
function p.lua_add_article(list, old_format, media_section)
	local image = ''
	local name = ''
	local reality = ''
	local output = ''

	if not h.isempty(list)
		then
			image = list.image
			if h.isempty(image)
				then image = g_noimage
			end 
			name = p.lua_add_link(list.page, list.name, list.title, old_format, list.reality, media_section)
			reality = p.lua_add_reality(list.reality)
			output = '\n'..image..'|link='..list.page..'|'..name..reality
	end
	
	return output
end


-----------------------------------------------------------------------------------------
function p.lua_add_link(page, name, title, old_format, reality, media_section)
	local i = ''
	local p_type
	local subname = ''
	local output = ''

	if media_section -- for Media section use check and formatiing for comics, volumes, episodes, etc.
		then
			p_type = pagetype.get_page_type(page)
			output = standard.lua_standardized_link(page, p_type)
			p_type = mw.html.create('span'):addClass('messagebox__help'):wikitext('('..p_type..')'):done()
			output = output..'<br>'..tostring(p_type)
		elseif old_format -- check for old disambiguation pages
			then 
				output = h.Link(page, title)
		else
			i, subname = string.match(name, '(.+)%((.+)%)')
			if not h.isempty(subname) and reality.dimension ~= nil and reality.dimension.name == subname
				then
					name = h.trim(i)
					subname = ""
			end

			if not h.isempty(title)
				then
					title = h.break_link(title, 2)
					if not h.in_list({g_main_title, g_main_name, g_pagename}, title)
						then
							title = mw.text.trim(title) -- placed here to not remove additional space in front of red links
							if not h.in_list({g_main_title, g_main_name, title, g_pagename}, name)
								then 
									if not h.isempty(subname) 
										then name = '<br />('..h.trim(i)..')<br />('..subname..')'
										else name = '<br />('..name..')'
									end
									output = h.Link(page, title..name)
								else output = h.Link(page, title)
							end
						elseif not h.in_list({g_main_title, g_main_name}, name)
							then 
								if not h.isempty(subname) 
									then name = h.trim(i)..'<br />('..subname..')'
								end
								output = h.Link(page, name)  
					end
				elseif not h.in_list({g_main_title, g_main_name}, name)
					then 
						if not h.isempty(subname) 
							then name = h.trim(i)..'<br />('..subname..')'
						end
						output = h.Link(page, name)
			end
	end

	if not h.isempty(output)
		then output = tostring(mw.html.create('div'):addClass('disambiguation_link'):wikitext(output))
	end

	return output
end
	

-----------------------------------------------------------------------------------------
function p.lua_add_reality(reality)
	local div = mw.html.create('div'):addClass('disambiguation_reality')
	local output = ''

	if not h.isempty(reality)
		then
			if not h.isempty(reality.dimension)
				then 
					output = h.Link(reality.dimension.link, reality.dimension.name..'<br>('..reality.name..' '..reality.dimension.type..')')
				else
					if reality.name ~= reality.description
						then output = h.Link(reality.name, h.break_link(reality.description, 2)..'<br>('..reality.name..')')
						else output = h.Link(reality.name, '('..reality.name..')')
					end
			end
	end
	
	return tostring(div:wikitext(output))
end


-----------------------------------------------------------------------------------------
function p.lua_add_main_version(articles, descriptions)
	local description
	local gallery_class = 'disambiguation_main_version'
	local image
	local pagename
	local text
	local link
	local reality
	local article
	local i
	local j
	local output = {}
	
	if #articles <= 2
		then gallery_class = gallery_class..'_'..#articles
	end

	for i = 1, #articles do
		link, image = string.match(articles[i], '(.+);(.+)')
		pagename = h.break_link(link, 1)
		text = h.break_link(link, 2)
		j = string.find(text, ' (', 1, true)
		if j ~= nil
			then 
				text = string.sub(text, 1, j-1)
				link = h.Link(pagename, text)
		end
		_, reality = module_reality.lua_get_name_and_reality(pagename)
		reality = p.lua_add_reality(reality)

		if h.isempty(descriptions[i])
			then description = ''
			else description = tostring(mw.html.create('div'):addClass('disambiguation_main_version_description'):wikitext(descriptions[i]) )
		end
		
		image = '[[File:'..image..'|250px|link='..pagename..']]'
		image = tostring(mw.html.create('div'):addClass('disambiguation_main_version_image'):wikitext(image) )
		link = tostring(mw.html.create('div'):addClass('disambiguation_link'):wikitext(link))
		article = tostring(mw.html.create('div'):addClass('disambiguation_main_version_article'):wikitext(image..link..reality) )
		if description ~= ''
			then article = tostring(mw.html.create('div'):addClass('disambiguation_main_version_article_and_description'):wikitext(article..description) )
			else article = tostring(mw.html.create('div'):addClass('disambiguation_main_version_article_no_description'):wikitext(article) )
		end
		table.insert(output, article)
	end
	output = table.concat(output)
	return tostring(mw.html.create('div'):addClass(gallery_class):wikitext(output) )
end


-----------------------------------------------------------------------------------------
function p.lua_add_section(list, header, old_format)
	local div = mw.html.create('div'):attr('class', 'navfaces')
	local media_section
	local output = ''
	
	if header ~= 'Media'
		then media_section = false
		else media_section = true
	end

	if not h.isempty(list) 
		then
			if not old_format
				then table.sort(list, p.lua_sort_by_reality)
			end
			
			output = {}
			if not h.isempty(header)
				then header = design.add_header(header, 2, 'center')
				else header = ''
			end
			for i = 1, #list do
				table.insert(output, p.lua_add_article(list[i], old_format, media_section) )
			end
			if not media_section
				then
					output = '<gallery widths="150" position="center" captionalign="center" captionsize="medium" orientation="square" navigation="true">'..table.concat(output)..'</gallery>'
					output = tostring( div:wikitext(output) )
				else
					output = '<gallery widths="150" position="center" captionalign="center" captionsize="medium" navigation="true">'..table.concat(output)..'</gallery>'
			end
			output = header..output
	end
	
	return output
end


-----------------------------------------------------------------------------------------
function p.lua_add_logo(args)
	local logo = args.logo
	local invert = args.invert_logo
	local output = ''
	
	if not h.isempty(logo)
		then
			output = mw.html.create('div'):addClass('disambiguation_logo')
			if not h.isempty(invert) and string.lower(invert) == 'yes'
				then output:addClass('logo-colors-invert')
			end
			output = tostring(output:wikitext(logo))
	end
	
	return output
end


-----------------------------------------------------------------------------------------
function p.lua_add_toc()
	return tostring(mw.html.create('div'):addClass('disambiguation_toc'):wikitext('__TOC__'))
end


--------------------------------------------------------------------------------------------------
function p.lua_get_outdated_fields(args)
	local i
	local j
	local output = ''

	if not h.isempty(args['Main Character']) or not h.isempty(args['Main Image Size'])
		then output = 'Outdated Fields/Disambiguation'
	end

	for i = 1,9 do
		if not h.isempty(args['New Header'..i]) or not h.isempty(args['Header'..i])
			then output = 'Outdated Fields/Disambiguation'
		end
	end
	
	for i = 1,200 do
		if not h.isempty(args['Teams'..i]) or not h.isempty(args['Others'..i])	or not h.isempty(args['Related'..i])
			then output = 'Outdated Fields/Disambiguation'
		end
		for j = 1,9 do
			if not h.isempty(args['New Header'..j..'_'..i]) or not h.isempty(args['Box'..j..'_'..i])
				then output = 'Outdated Fields/Disambiguation'
			end
		end
	end
	
	if not h.isempty(output)
		then output = '[[Category:'..output..']]'
	end

	return output	
end

return p