Module:Fileinfo

From Japari Library, the Kemono Friends Wiki
Jump to navigation Jump to search

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

--Adapted from Module:Fileinfo on Megami Tensei Wiki by Mistiryshak (Also the author of this Module)
local getArgument = require('Module:Arguments').getArgument

local p = {}

function capitalFirst(str) -- Allows capitalization of the first letter of each word in a string; adapted from (https://stackoverflow.com/questions/20284515/capitalize-first-letter-of-every-word-in-lua)
    return (str:gsub("(%l)(%w*)", function(a,b) return string.upper(a)..b end))
end

function trim(s) -- Allows removing of whitespace from inputs; adapted from (https://www.lua.org/manual/2.4/node32.html)
	local l = 1
	while string.sub(s,l,l) == ' ' do
	l = l+1
	end
	local r = string.len(s)
	while string.sub(s,r,r) == ' ' do
	r = r-1
	end
	return string.sub(s,l,r)
end

function p.Main(frame)
	local args = frame.args
	local summary = args['summary']
	local filetype = trim(args['type']:lower())
	local source = args['source']
	local media = trim(args['media']:lower())
	local license = trim(args['licensing']:lower())
	local subject = args['subject']
	local trademark = args['trademark']
	local artists = args['artists']
	local suppressCat = args['suppresscat']
	local suppressCatType = args['suppresscattype']
	
	local filecat = {
		['artwork'] = {
			displaytext = "[[:Category:Artwork|Artwork]] [[Category:Artwork]]",
			cattext = " Artwork"
			},
		['illustration'] = {
			displaytext = "[[:Category:Illustrations|Illustration]] [[Category:Illustrations]]",
			cattext = " Illustrations"
			},
		['render'] = {
			displaytext = '[[:Category:Renders|Render]] [[Category:Renders]]',
			cattext = ' Renders'
			},
		['logo'] = {
			displaytext = '[[:Category:Logos|Logo]] [[Category:Logos]]',
			cattext = " Logos"
			},
		['cover'] = {
			displaytext = '[[:Category:Covers|Cover]] [[Category:Covers]]',
			cattext = ' Covers'
			},
		['map'] = {
			displaytext = "[[:Category:Maps|Map]] [[Category:Maps]]",
			cattext = ' Maps'
			},
		['model'] = {
			displaytext = "[[:Category:Models|Model]] [[Category:Models]]",
			cattext = " Models"
			},
		['graphic'] = {
			displaytext = "[[:Category:Graphics|Graphic]] [[Category:Graphics]]",
			cattext = " Graphics"
			},
		['sprite'] = {
			displaytext = "[[:Category:Sprites|Sprite]] [[Category:Sprites]]",
			cattext = " Sprites"
			},
		['screenshot'] = {
			displaytext = "[[:Category:Screenshots|Screenshot]] [[Category:Screenshots]]",
			cattext = " Screenshots"
			},
		['card'] = { 
			displaytext = '[[:Category:Cards|Card]] [[Category:Cards]]',
			cattext = " Cards"
			},
		['icon'] = {
			displaytext = '[[:Category:Icons|Icon]] [[Category:Icons]]',
			cattext = " Icons"
			},
		['crop'] = {
			displaytext = '[[:Category:Cropped Images|Cropped Image]] [[Category:Cropped Images]]',
			cattext = " Cropped Images"
			},
		['photo'] = {
			displaytext = "[[:Category:Photos|Photo]] [[Category:Photos]]",
			cattext = " Photos"
			},
		['merchandise'] = {
			displaytext = "[[:Category:Merchandise Images|Merchandise]] [[Category:Merchandise Images]]",
			cattext = " Merchandise Images"
			},
		['audio'] = {
			displaytext = "[[:Category:Audio|Audio]] [[Category:Audio]]",
			cattext = " Audio"
			},
		['video'] = {
			displaytext = "[[:Category:Videos|Video]] [[Category:Videos]]",
			cattext = " Videos"
			},
		['wiki'] = {
			displaytext = "[[:Category:Wiki Images|Wiki Image]] [[Category:Wiki Images]]",
			cattext = " Wiki Images"
			},
		['flag'] = {
			displaytext = "[[:Category:Flags|Flag]] [[Category:Flags]]",
			cattext = " Flags"
			},
		['advertisement'] = {
			displaytext = "[[:Category:Advertisements|Advertisement]] [[Category:Advertisements]]",
			cattext = " Advertisements"
			},
		['other'] = {
			displaytext = "[[:Category:Other Images|Other Image]] [[Category:Other Images]]",
			cattext = " Other Images"
			}
	}
	
	if summary ~= '' then
		summary = "\n!Summary \n|" .. summary .. "\n|-"
	else
		summary = ''
	end
	
	if filetype ~= '' then
		filetype = "\n!Type \n|" .. filecat[filetype].displaytext .. "\n|-"
	else
		filetype = "\n!Type \n|''This file is missing a type. Please assign the correct type to the file.'' [[Category:Files missing type]]\n|-"
		typemissing = true
	end
	
	if source ~= '' then
		source = "\n!Source \n|" .. source .. "\n|-"
	else
		source = "\n!Source \n|''This file does not have a source. Please add the source, contact the original uploader, or upload a new version of the file.'' [[Category:Unsourced Files]]" .. "\n|-"
	end
	
	local licenselist = {
		['copyright'] = "Copyright",
		['public domain'] = "Public Domain",
		['gfdl'] = "GFDL",
		['cc by-sa'] = "CC BY-SA",
		['cc by-nc-sa'] = "CC BY-NC-SA"
	}
	
	if license == '' then
		license = "\n!Licensing\n|''This file is missing a license tag. Please assign the correct license tag to the file.'' [[Category:Files missing license tag]]" .. "\n|-"
	else
		license = "\n!Licensing\n|\n" .. frame:expandTemplate { title = licenselist[license] }  .. "\n|-"
	end
	
	if subject ~= '' then
		subject = "\n!Subject\n|" .. subject .. "\n|-"
	else
		subject = ''
	end
	
	if trademark ~= '' then
		trademark = "\n!Trademark\n|" .. trademark .. "\n|-"
	else
		trademark = ''
	end
	
			if artists ~= '' and artists:lower() ~= 'unknown' and string.find(artists, ':') ~= 7 then
			local Init = 0
			local staffBreak = true
			local artLinks = ''
			while staffBreak == true do
				stringEnd = string.find(artists, ',', Init)
					if stringEnd ~= nil then
						artSubst = trim(string.sub(artists, Init, stringEnd - 1))
							if mw.title.makeTitle(0, artSubst).redirectTarget ~= false then
								pagetitle = tostring(mw.title.makeTitle(0, artSubst).redirectTarget)
								artNew = "[[" .. pagetitle .. "]][[Category:Works by " .. pagetitle .. "]], "
								artLinks = artLinks .. artNew
							elseif mw.title.makeTitle(0, artSubst).exists == true then
								artNew = "[[" .. artSubst .. "]][[Category:Works by " .. artSubst .. "]], "
								artLinks = artLinks .. artNew
							else
								artLinks = "''This file has incorrectly attributed an artist. Please add the proper attribution or correct spelling errors.''[[Category:Files with errors in artist name]]<br>" .. artLinks
								staffBreak = false
							end
						Init = stringEnd + 1
					else
						artSubst = trim(string.sub(artists, Init,-1))
							if mw.title.makeTitle(0, artSubst).redirectTarget ~= false then
									pagetitle = tostring(mw.title.makeTitle(0, artSubst).redirectTarget)
									artNew = "[[" .. pagetitle .. "]][[Category:Works by " .. pagetitle .. "]]" 
									artLinks = artLinks .. artNew
								elseif mw.title.makeTitle(0, artSubst).exists == true then
									artNew = "[[" .. artSubst .. "]][[Category:Works by " .. artSubst .. "]]"
									artLinks = artLinks .. artNew
								else
									artLinks = "''This file has incorrectly attributed an artist. Please add the proper attribution or correct spelling errors.''[[Category:Files with errors in artist name]]<br>" .. artLinks
							end
						staffBreak = false
					end
			end
			artOutput = "\n!Artists\n|" .. artLinks .. "\n|-"
		else
			if args['type']:lower() == 'artwork' or args['type']:lower() == 'illustration' then
				if artists:lower() == 'unknown' then
					artOutput = "\n!Artists\n|" .. "''The author of this artwork is unknown.''[[Category:Artwork with unknown authorship]]" .. "\n|-"
				elseif string.find(artists, ':', 0) == 7 then
					artOutput = "\n!Artists\n|" .. string.sub(artists, 9) .. "\n|-"
				else
					artOutput = "\n!Artists\n|" .. "''This file requires artist attribution. Please add the proper attribution.''[[Category:Files without artist attribution]]" .. "\n|-"
				end
			else
				artOutput = ''
			end
		end	
		
		--This: The current addition to artLinks for this loop. artNew
		--artLinks: The second column of fileinfo; includes links and categories. artLinks
		--artOutput: The final output sent to the return output line. Includes the new line and header cell and encapsulates artLinks. artOutput
	
	if media ~= '' and media ~= 'none' then
		local getLink = require('Module:Data/Media')
			if mw.title.makeTitle('Module', 'Data/Media' .. '/raw').exists == true then
				getLinkRaw = require('Module:Data/Media' .. '/raw')
			end
	
		local link = getLink.media[media]
		if getLinkRaw and getLinkRaw.media[media] then
			link = "\n!Origin\n|''[[" .. getLink.media[media] .. "|" .. getLinkRaw.media[media] .. "]]''" .. "\n|-"
		else
			if link:sub(-1) == ')' then
				link = "\n!Origin\n|[[" .. getLink.media[media] .. "|''" .. string.sub(link,1,string.find(link, '%(') - 2) .. "'' " .. string.sub(link,string.find(link, '%('),string.find(link, '%)')) .. "]]\n|-"
			else
				link = "\n!Origin\n|''[[" .. link .. "]]''\n|-"
			end
		end
		
		local categorytype = ''
		
		if typemissing == true then
			category = '[[Category:' .. getLink.media[media] .. " Files]]"
		else
			category = '[[Category:' .. getLink.media[media] .. " Files]]"
			categorytype = "[[Category:" .. getLink.media[media] .. filecat[trim(args['type']:lower())].cattext .. "]]"
		end
		
		if suppressCat ~= '' then
			category = ''
		end
		
		if suppressCatType ~= '' then
			categorytype = ''
		end
		
		local output = '{| class="wikitable"' .. summary .. filetype .. source  .. artOutput .. link .. subject .. trademark .. license .. "\n|}" .. category .. categorytype
		return output
	elseif media:lower() == 'none' then
		local output = '{| class="wikitable"' .. summary .. filetype .. source .. artOutput .. subject .. trademark .. license .. "\n|}"
		return output	
	else
		local output = '{| class="wikitable"' .. summary .. filetype .. source .. artOutput .. subject .. trademark .. license .. "\n|}[[Category:Files with no associated media]]"
		return output
		end
	end
return p