Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:Sandbox/Chao

From BlazBlue Wiki
< Module:Sandbox
Revision as of 19:30, 20 October 2019 by Chao (talk | contribs) (bad argument #1 to 'insert' (table expected, got string))

Script error: The function "main" does not exist.


--[[
{{#invoke:Sandbox/Chao|GallerySection|character=Noel Vermillion|section=Portraits}}
simulate passing a frame to the function in the console with:
myFrame = { args = { character='Noel Vermillion', section='Portraits' } }
=p.GallerySection ( myFrame )
=p.testExpandTemplate( myFrame )
=p.render(myFrame)

--]]

local p = {}
local cargo = mw.ext.cargo

local galleryData = mw.loadData('Module:Sandbox/Chao/data')
local ordering = galleryData.ordering
local sections = galleryData.sections

function p.render(frame)
	text = {}
	_char = frame.args['character'] or ''
	
	for i,v in ipairs (ordering) do
		category = v
		params = sections[v] or ''

		if params == '' then
		else
			section = {}
			galleries = {}
			images_in_section = 0
			current_subheader = ''
			
			-- add a wrapper div if there's a class to apply to the section
			class = params.class or ''
			if class ~= '' then
				table.insert(section, '<div class="' .. class .. '">')
			end
	
			-- v is a table of templates in this section
			for k2,v2 in pairs (params) do
				
				template = v2.template or ''
				params = {}
				
				--fill in any missing template arguments
				--Gallery Section (Cargo)
				params.character = _char
				params.source = v2.source or ''
				params.section = v2.section or ''
				params.limit = v2.limit or ''
				params.widths = v2.widths or ''
				params.header = v2.header or ''
				params.title = v2.title or ''
				params.sort = v2.sort or ''
				params.see_all = v2.see_all or ''
				params.see_cameos = v2.see_cameos or ''
				--Gallery Section (BBRadio)
				params.season = v2.season or ''
				params.episode = v2.episode or ''
				params.widths = v2.widths or ''
				--Gallery Section
				params.cat = v2.category or ''
				params.cat2 = v2.category2 or ''
	
				if template == 'Gallery Section (Cargo)' then
					params.character = cargo_escape(params.character)

					params.count_all = p._countImages_GallerySectionCargo(params, false) or 0
					params.count_cameos = p._countImages_GallerySectionCargo(params, true) or 0
					total_images = params.count_all + params.count_cameos
					
					if params.count_all > 0 then
						params.see_all = 'y'
					end
					
					if params.count_cameos > 0 then
						params.see_cameos = 'y'
					end
					
					if total_images > 0 then
						expanded_template = p._expandTemplate_GallerySectionCargo(frame, params)
						table.insert(galleries, expanded_template)
						images_in_section = images_in_section + total_images
					end
				elseif template == 'Gallery Section (BBRadio)' then
					params.character = cargo_escape(params.character)
					params.count_all = p._countImages_GallerySectionBBRadio(params) or 0
					expanded_template = p._expandTemplate_GallerySectionBBRadio(frame, params)
					table.insert(galleries, expanded_template)
					images_in_section = images_in_section + params.count_all
				elseif template == 'Gallery Section' then
					params.count_all = p._countImages_GallerySection(frame, params) or 0
					expanded_template = p._expandTemplate_GallerySection(frame, params)
					table.insert(galleries, expanded_template)
					images_in_section = images_in_section + params.count_all
				elseif template == 'Header' then
					-- if this is not the first subheader in this section
					if current_subheader ~= '' then
						-- check if the previous subheader's galleries had images in them
						if images_in_section > 0 then
							-- insert the previous subheader into the section
							table.insert(section, current_subheader)
							-- insert the galleries after the table
							table.insert(section, galleries)
							-- reset galleries and images for the next subsection
							galleries = {}
							images_in_section = 0
						end
					end
					-- save the new subheader and wait to see if the following galleries have any images in them
					current_subheader = '<' .. params.header .. '>' .. params.title .. '</' .. params.header .. ">\n"
				end
			end
			
			-- if there is a subheader to insert, then insert it
			if current_subheader ~= '' and images_in_section > 0 then
				table.insert(section, current_subheader)
			end
			
			-- if there are galleries to insert, then insert them
			table.insert(section, galleries)

			-- close the wrapping div if one was added
			if class ~= '' then
				table.insert(section, '</div>')
			end
			
			--templates = concatvalues(t,"")
			templates = table.concat(section)
			if templates ~= "" then
				-- insert the section, prepended by its header, to the final text
				table.insert(text, '<h2>' .. category .. '</h2>\n' .. templates)
			end
		end
	end
	
	gallery = table.concat(text)
	return gallery
end

-- counts the number of images with this character (or cameo) for sections using Template:Gallery Section (Cargo)
-- countCameos: boolean
function p._countImages_GallerySectionCargo(params, countCameos)
	tables = 'Files'
	fields = 'COUNT(Files._pageName)'

	character = params.character or ''
	source = params.source or ''
	section = params.section or ''

	where_clause_parts = {}
	if countCameos then
		table.insert(where_clause_parts, "Files.Cameos HOLDS '" .. character .. "'")
	else
		table.insert(where_clause_parts, "Files.Characters HOLDS '" .. character .. "'")
	end
	
	if source == '' then else table.insert(where_clause_parts, "Files.Source HOLDS '" .. source .. "'")	end
	if section == '' then else table.insert(where_clause_parts, "Files.Gallery_Sections HOLDS '" .. section .. "'")	end
	where_clause = concatvalues(where_clause_parts, " AND ")

	local args = {
		where = where_clause
	}

	result = cargo.query( tables, fields, args )
	count = result[1][fields] or '0'

	return tonumber(count)
end

function p._countImages_GallerySectionBBRadio(params)
	tables = 'Files,BBRadio_Cuts'
	fields = 'COUNT(Files._pageName)'
	join_on = 'Files._pageName=BBRadio_Cuts._pageName'

	_character = params.character or ''
	_season = params.season or ''
	
	mw.log(_character)
	mw.log(_season)

	where_clause = 'Files.Characters HOLDS "' .. _character .. '" AND BBRadio_Cuts.Season="' .. _season .. '"'

	local args = {
		where = where_clause,
		join = join_on
	}

	result = cargo.query( tables, fields, args )
	count = result[1][fields] or '0'

	return tonumber(count)
end

function p._countImages_GallerySection(frame, params)
	
	cat1 = params.category1 or ''
	cat2 = params.category2 or ''
	cat3 = params.category3 or ''

	cat = {}
	if cat1 == '' then else table.insert(cat, '[[Category:' .. cat1 .. ']]') end
	if cat2 == '' then else table.insert(cat, '[[Category:' .. cat2 .. ']]') end
	if cat3 == '' then else table.insert(cat, '[[Category:' .. cat3 .. ']]') end
	_categories = table.concat(cat)

	frame:callParserFunction{
		name = '#ask', 
		args = { _categories, format = _format }
	}
end

function p._expandTemplate_GallerySectionCargo(frame, params)
	_character = params.character or ''
	_source = params.source or ''
	_section = params.section or ''
	_limit = tonumber(params.limit) or 4
	_title = params.title or ''
	_header = params.header or ''
	_see_all = params.see_all or ''
	_see_cameos = params.see_cameos or ''
	_count_all = params.count_all or ''
	_count_cameos = params.count_cameos or ''
	
	gallery = frame:expandTemplate{ title = 'Gallery Section (Cargo)', args = { 
		character = _character,
		source = _source,
		section = _section,
		limit = _limit,
		title = _title,
		header = _header,
		see_all = _see_all,
		see_cameos = _see_cameos,
		count_all = _count_all,
		count_cameos = _count_cameos
	}}

	return gallery
end

function p._expandTemplate_GallerySectionBBRadio(frame, params)
	_character = params.character or ''
	_season = params.season or ''
	_episode = params.episode or ''
	_header = params.header or ''
	_title = params.title or ''
	_limit = tonumber(params.limit) or 4
	_widths = tonumber(params.widths) or 250
	_count_all = params.count_all or ''

	gallery = frame:expandTemplate { title = 'Gallery Section (BBRadio)', args = {
		_character,
		_season,
		_episode,
		_header,
		_title,
		widths = _widths,
		limit = _limit,
		count_all = _count_all
	}}

	return gallery
end

function p._expandTemplate_GallerySection(frame, params)
	_character = params.character or ''
	_cat = params.cat or ''
	_cat2 = params.cat2 or ''
	_header = params.header or ''
	_title = params.title or ''
	_limit = tonumber(params.limit) or 4
	_widths = tonumber(params.widths) or 250
	_count_all = params.count_all or 0

	gallery = frame:expandTemplate { title = 'Gallery Section', args = {
		_character,
		_cat,
		_cat2,
		_header,
		_title,
		widths = _widths,
		limit = _limit,
		count_all = _count_all,
	}}

	return gallery
end

function p.testExpandTemplate_1(frame)
	
	v = {
		character = 'Noel Vermillion',
		source = 'BlazBlue: Central Fiction', 
		section = '', 
		limit = 8, 
		title = 'title', 
		header = 'h3', 
		see_all = 'y', 
		see_cameos = 'y', 
		count_all = '7'
	}
	text = p._expandTemplate_GallerySectionCargo(frame, v)
	
	return text
end

function p.testExpandTemplate_2(frame)
	
	v = {
		character = 'Noel Vermillion',
		season = 'NEO', 
		limit = 8, 
		title = 'title', 
		header = 'h3', 
		see_all = 'y', 
		count_all = '7',
	}
	text = p._expandTemplate_GallerySectionBBRadio(frame, v)
	
	return text
end

function p.testOrdering()
	for i,v in ipairs (ordering) do
		section = v
		k = sections[v] or ''

		mw.log('\n' .. i .. ': ' .. section)
		if k == '' then
			mw.log('no parameters found')
		else
			mw.log(k.class)
			for k2,v2 in pairs (k) do
				mw.log(k2)
			end
		end
		
	end
end

-- concat all the strings in table s together with the given delimiter
function concatvalues(s,delimiter)
    local t = { }
    for k,v in ipairs(s) do
        t[#t+1] = tostring(v)
    end
    return table.concat(t,delimiter)
end

-- concat all the strings in table s together with the given delimiter
-- skips blank entries (where value v = "")
function concatvaluesonly(s,delimiter)
    local t = { }
    for k,v in ipairs(s) do
    	if not (v == '') then
    		t[#t+1] = tostring(v)
    	end
    end
    return table.concat(t,delimiter)
end

function cargo_escape(s)
	return (string.gsub(s, "[']", {
		["'"] = "",
		['"'] = "",
	}))
end

function quote_escape(s)
	return (string.gsub(s, "[\"']", {
		["\'"] = "\\'",
		['\"'] = '\\"'
	}))
end

function html_escape(s)
    return (string.gsub(s, "[<>\"'/ ,]", {
        ["<"] = "%3C",
        [">"] = "%3E",
        ['"'] = "%22",
        ["'"] = "%27",
        ["/"] = "%2F",
        [" "] = "+",
        [","] = "%2C"
    }))
end

-- helpful for printing tables
function dump(o)
   if type(o) == 'table' then
      local s = '{ '
      for k,v in pairs(o) do
         if type(k) ~= 'number' then k = '"'..k..'"' end
         s = s .. '['..k..'] = ' .. dump(v) .. ','
      end
      return s .. '} '
   else
      return tostring(o)
   end
end

return p