Difference between revisions of "Module:Sandbox/Chao"

From BlazBlue Wiki
Jump to: navigation, search
m (test lua and template expansion)
m
Line 5: Line 5:
 
=p.GallerySection ( myFrame )
 
=p.GallerySection ( myFrame )
 
=p.testExpandTemplate( myFrame )
 
=p.testExpandTemplate( myFrame )
 +
=p.render(myFrame)
 +
 +
local itemData = mw.loadData('Module:ItemData')
 
--]]
 
--]]
  
 
local p = {}
 
local p = {}
 
local cargo = mw.ext.cargo
 
local cargo = mw.ext.cargo
 +
 +
frame = { character = 'Noel Vermillion '}
 +
 +
local sections = {
 +
['Arcade'] = {
 +
{
 +
template = 'Gallery Section (Cargo)',
 +
source = 'BlazBlue: Calamity Trigger',
 +
section = 'Arcade Mode',
 +
limit = '',
 +
widths = '',
 +
header = 'h3',
 +
title = 'BlazBlue: Calamity Trigger',
 +
sort = '',
 +
see_all = '',
 +
see_cameos = '',
 +
},{
 +
template = 'Gallery Section (Cargo)',
 +
source = 'BlazBlue: Continuum Shift',
 +
section = 'Arcade Mode',
 +
header = 'h3'
 +
},{
 +
template = 'Gallery Section (Cargo)',
 +
source = 'BlazBlue: Chrono Phantasma',
 +
section = 'Arcade Mode',
 +
header = 'h3'
 +
}
 +
}
 +
}
 +
 +
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
 +
v2.character = _char
 +
 +
if v2.template == 'Gallery Section (Cargo)' then
 +
v2.count_all = p.countImagesCargo(v2) or 0
 +
 +
if v2.count_all > 0 then
 +
table.insert(t, dump(v))
 +
table.insert(text, frame:expandTemplate{ v })
 +
else
 +
mw.log("count returned 0: " .. v2.count_all)
 +
end
 +
end
 +
end
 +
 +
templates = concatvalues(t,"")
 +
if not templates == "" then
 +
-- insert the section, prepended by its header, to the final text
 +
table.insert(text, k .. templates)
 +
end
 +
 +
mw.log(dump(text))
 +
return text
 +
end
 +
 +
gallery = concatvalues(text,"")
 +
mw.log(dump(gallery))
 +
return gallery
 +
end
 +
 +
function p.countImagesCargo(v)
 +
tables = 'Files'
 +
fields = 'COUNT(Files._pageName)'
 +
 +
character = v.character or ''
 +
source = v.source or ''
 +
section = v.section or ''
 +
 +
where_clause_parts = {}
 +
table.insert(where_clause_parts, "Files.Characters HOLDS '" .. character .. "'")
 +
if not source == '' then
 +
table.insert(where_clause_parts, "Files.Source HOLDS '" .. source .. "'")
 +
end
 +
if not section == '' then
 +
table.insert(where_clause_parts, "Files.Gallery_Sections = '" .. section .. "'")
 +
end
 +
where_clause = concatvalues(where_clause_parts, ",")
 +
 +
local args = {
 +
where = where_clause
 +
}
 +
 +
result = cargo.query( tables, fields, args )
 +
count = result[1][fields] or '0'
 +
 +
return tonumber(count)
 +
end
  
 
function p.testExpandTemplate(frame)
 
function p.testExpandTemplate(frame)
 
 
text = p.GallerySection(frame, 'BlazBlue: Central Fiction', '', 8, 'title', 'h3', 'y', 'y', '7')
+
text = p._testExpandTemplate(frame, 'BlazBlue: Central Fiction', '', 8, 'title', 'h3', 'y', 'y', '7')
 
 
 
return text
 
return text
 
end
 
end
  
function p.GallerySection(frame, source, section, limit, title, header, see_all, see_cameo, count_all)
+
function p._testExpandTemplate(frame, source, section, limit, title, header, see_all, see_cameo, count_all)
 
_character = frame.args.character or ''
 
_character = frame.args.character or ''
 
_source = frame.args.source or source or ''
 
_source = frame.args.source or source or ''
Line 48: Line 147:
 
     for k,v in ipairs(s) do
 
     for k,v in ipairs(s) do
 
         t[#t+1] = tostring(v)
 
         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
 
     end
 
     return table.concat(t,delimiter)
 
     return table.concat(t,delimiter)

Revision as of 02:10, 13 September 2019

Test test test ;w; Lua error in mw.lua at line 308: frame:expandTemplate: a title is required.


--[[
{{#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 itemData = mw.loadData('Module:ItemData')
--]]

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

frame = { character = 'Noel Vermillion '}

local sections = {
	['Arcade'] = {
		{
			template = 'Gallery Section (Cargo)',
			source = 'BlazBlue: Calamity Trigger',
			section = 'Arcade Mode',
			limit = '',
			widths = '',
			header = 'h3',
			title = 'BlazBlue: Calamity Trigger',
			sort = '',
			see_all = '',
			see_cameos = '',
		},{
			template = 'Gallery Section (Cargo)',
			source = 'BlazBlue: Continuum Shift',
			section = 'Arcade Mode',
			header = 'h3'
		},{
			template = 'Gallery Section (Cargo)',
			source = 'BlazBlue: Chrono Phantasma',
			section = 'Arcade Mode',
			header = 'h3'
		}
	}
}

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
			v2.character = _char

			if v2.template == 'Gallery Section (Cargo)' then
				v2.count_all = p.countImagesCargo(v2) or 0
				
				if v2.count_all > 0 then
					table.insert(t, dump(v))
					table.insert(text, frame:expandTemplate{ v })
				else
					mw.log("count returned 0: " .. v2.count_all)
				end
			end
		end
		
		templates = concatvalues(t,"")
		if not templates == "" then
			-- insert the section, prepended by its header, to the final text
			table.insert(text, k .. templates)
		end
		
		mw.log(dump(text))
		return text
	end
	
	gallery = concatvalues(text,"")
	mw.log(dump(gallery))
	return gallery
end

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

	character = v.character or ''
	source = v.source or ''
	section = v.section or ''

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

	local args = {
		where = where_clause
	}

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

	return tonumber(count)
end

function p.testExpandTemplate(frame)
	
	text = p._testExpandTemplate(frame, 'BlazBlue: Central Fiction', '', 8, 'title', 'h3', 'y', 'y', '7')
	
	return text
end

function p._testExpandTemplate(frame, source, section, limit, title, header, see_all, see_cameo, count_all)
	_character = frame.args.character or ''
	_source = frame.args.source or source or ''
	_section = section or ''
	_limit = tonumber(limit) or 4
	_title = title or ''
	_header = header or ''
	_see_all = see_all or ''
	_see_cameo = see_cameo or ''
	_count_all = 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_cameo = _see_cameo
		 ,count_all = _count_all
	} }

	return gallery
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 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