Module:Sandbox/Chao

From BlazBlue Wiki
< Module:Sandbox
Revision as of 21:14, 15 September 2019 by Chao (talk | contribs)

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 sections = mw.loadData('Module:Sandbox/Chao/data').sections

function p.render(frame)
	text = {}
	_char = frame.args['character'] or ''
	
	for k,v in pairs (sections) do
		section = k

		-- v is a table of templates in this section
		t = {}
		for k2,v2 in pairs (v) do
			
			params = {}
			
			--fill in any missing template arguments
			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 v2.section or ''
			params.sort = v2.sort or ''
			params.see_all = v2.see_all or ''
			params.see_cameos = v2.see_cameos or ''

			if v2.template == 'Gallery Section (Cargo)' then
				params.count_all = p.countImagesCargo(params, false) or 0
				params.count_cameos = p.countImagesCargo(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.count_cameos = 'y'
				end
				
				if total_images > 0 then
					expanded_template = p._expandTemplate(frame, params)
					table.insert(t, expanded_template)
				end
			end
		end
		
		--templates = concatvalues(t,"")
		templates = table.concat(t)
		if templates == "" then
		else
			-- insert the section, prepended by its header, to the final text
			table.insert(text, '<h2>' .. k .. '</h2>\n' .. templates)
		end
	end
	
	gallery = table.concat(text)
	mw.log(dump(gallery))
	return gallery
end

function p.countImagesCargo(v, countCameos)
	tables = 'Files'
	fields = 'COUNT(Files._pageName)'

	character = v.character or ''
	source = v.source or ''
	section = v.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._expandTemplate(frame, v)
	_character = v.character or ''
	_source = v.source or ''
	_section = v.section or ''
	_limit = tonumber(v.limit) or 4
	_title = v.title or ''
	_header = v.header or ''
	_see_all = v.see_all or ''
	_see_cameos = v.see_cameos or ''
	_count_all = v.count_all 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.testExpandTemplate(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(frame, v)
	
	return text
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 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