More actions
m (add function clear_table(t)) |
No edit summary |
||
Line 1: | Line 1: | ||
local cargo = mw.ext.cargo | local cargo = mw.ext.cargo | ||
local | ------------------------------------- | ||
local | -- Gallery Section Class | ||
local | ------------------------------------- | ||
local GalleryTemplate = {} | |||
GalleryTemplate.__index = GalleryTemplate | |||
GalleryTemplate.fields = { | |||
character = true, | |||
source = true, | |||
section = true, | |||
limit = true, | |||
widths = true, | |||
header = true, | |||
title = true, | |||
sort = true, | |||
see_all = true, | |||
see_cameos = true, | |||
--Gallery Section (BBRadio) | |||
season = true, | |||
episode = true, | |||
widths = true, | |||
--Gallery Section | |||
cat = true, | |||
cat2 = true, | |||
--Misc/Shared | |||
count_all = true, | |||
count_cameos = true, | |||
images_in_section = true, | |||
} | |||
GalleryTemplate.templateMethods = { | |||
['Gallery Section (Cargo)'] = 'makeGallerySectionCargo', | |||
['Gallery Section (BBRadio)'] = 'makeGallerySectionBBRadio', | |||
['Gallery Section'] = 'makeGallerySection', | |||
} | |||
function GalleryTemplate.new(frame, templateType, params) | |||
local self = setmetatable({}, GalleryTemplate) | |||
self.templateType= templateType | |||
self.frame = frame | |||
for field in pairs(GalleryTemplate.fields) do | |||
self[field] = params[field] or '' | |||
end | |||
self.images_in_section = 0 | |||
self.cat = params.category or '' | |||
if (params.character == nil) | |||
then self.character = frame.args.character or '' | |||
else self.character = params.character or '' | |||
end | |||
return self | |||
end | |||
function GalleryTemplate:__tostring() | |||
local method = GalleryTemplate.templateMethods[self.templateType] | |||
if method then | |||
return self[method](self) | |||
end | |||
return '' | |||
end | |||
function | function GalleryTemplate.makeGallerySectionCargo(self) | ||
self.character = cargo_escape(self.character) | |||
self.count_all = GalleryTemplate._countImages_GallerySectionCargo(self,false) or 0 | |||
self.count_cameos = GalleryTemplate._countImages_GallerySectionCargo(self,true) or 0 | |||
local total_images = self.count_all + self.count_cameos | |||
if self.count_all > 0 then | |||
self.see_all = 'y' | |||
end | |||
if self.count_cameos > 0 then | |||
self.see_cameos = 'y' | |||
end | |||
if total_images > 0 then | |||
expanded_template = GalleryTemplate._expandTemplate_GallerySectionCargo(self, self.frame) | |||
--table.insert(t_galleries, expanded_template) | |||
self.images_in_section = self.images_in_section + total_images | |||
end | |||
return expanded_template | |||
end | |||
function GalleryTemplate.makeGallerySectionBBRadio(self) | |||
self.character = cargo_escape(self.character) | |||
self.count_all = GalleryTemplate._countImages_GallerySectionBBRadio(self) or 0 | |||
expanded_template = GalleryTemplate._expandTemplate_GallerySectionBBRadio(self,self.frame) | |||
--table.insert(t_galleries, expanded_template) | |||
self.images_in_section = self.images_in_section + self.count_all | |||
return expanded_template | |||
end | |||
function GalleryTemplate.makeGallerySection(self) | |||
self.count_all = GalleryTemplate._countImages_GallerySection(self) or 0 | |||
expanded_template = GalleryTemplate._expandTemplate_GallerySection(self,self.frame) | |||
--table.insert(t_galleries, expanded_template) | |||
self.images_in_section = self.images_in_section + self.count_all | |||
return expanded_template | |||
return | |||
end | end | ||
-- counts the number of images with this character (or cameo) for sections using Template:Gallery Section (Cargo) | -- counts the number of images with this character (or cameo) for sections using Template:Gallery Section (Cargo) | ||
-- countCameos: boolean | -- countCameos: boolean | ||
function | function GalleryTemplate._countImages_GallerySectionCargo(self, countCameos) | ||
tables = 'Files' | tables = 'Files' | ||
fields = 'COUNT(Files._pageName)' | fields = 'COUNT(Files._pageName)' | ||
character = | character = self.character or '' | ||
source = | source = self.source or '' | ||
section = | section = self.section or '' | ||
where_clause_parts = {} | where_clause_parts = {} | ||
Line 172: | Line 135: | ||
end | end | ||
function | function GalleryTemplate._countImages_GallerySectionBBRadio(self) | ||
tables = 'Files,BBRadio_Cuts' | tables = 'Files,BBRadio_Cuts' | ||
fields = 'COUNT(Files._pageName)' | fields = 'COUNT(Files._pageName)' | ||
join_on = 'Files._pageName=BBRadio_Cuts._pageName' | join_on = 'Files._pageName=BBRadio_Cuts._pageName' | ||
mw.log( | mw.log("Count images for " .. self.character .. ' season ' .. self.season) | ||
where_clause = 'Files.Characters HOLDS "' .. | where_clause = 'Files.Characters HOLDS "' .. self.character .. '" AND BBRadio_Cuts.Season="' .. self.season .. '"' | ||
local args = { | local args = { | ||
Line 196: | Line 155: | ||
end | end | ||
function | function GalleryTemplate._countImages_GallerySection(self) | ||
cat1 = self.character or '' | |||
cat1 = | cat2 = self.cat or '' | ||
cat2 = | cat3 = self.cat2 or '' | ||
cat3 = | |||
local query = {} | local query = {} | ||
Line 211: | Line 169: | ||
return count | return count | ||
end | end | ||
function | function GalleryTemplate._expandTemplate_GallerySectionCargo(self) | ||
_limit = tonumber(self.limit) or 4 | |||
if self.frame.expandTemplate == nil then return '' end | |||
_limit = tonumber( | gallery = self.frame:expandTemplate{ title = 'Gallery Section (Cargo)', args = { | ||
character = self.character, | |||
source = self.source, | |||
section = self.section, | |||
character = | |||
source = | |||
section = | |||
limit = _limit, | limit = _limit, | ||
title = | title = self.title, | ||
header = | header = self.header, | ||
see_all = | see_all = self.see_all, | ||
see_cameos = | see_cameos = self.see_cameos, | ||
count_all = | count_all = self.count_all, | ||
count_cameos = | count_cameos = self.count_cameos | ||
}} | }} | ||
Line 242: | Line 191: | ||
end | end | ||
function | function GalleryTemplate._expandTemplate_GallerySectionBBRadio(self) | ||
_limit = tonumber(self.limit) or 4 | |||
_widths = tonumber(self.widths) or 250 | |||
_limit = tonumber( | |||
_widths = tonumber( | |||
gallery = frame:expandTemplate { title = 'Gallery Section (BBRadio)', args = { | if self.frame.expandTemplate == nil then return '' end | ||
gallery = self.frame:expandTemplate { title = 'Gallery Section (BBRadio)', args = { | |||
self.character, | |||
self.season, | |||
self.episode, | |||
self.header, | |||
self.title, | |||
widths = _widths, | widths = _widths, | ||
limit = _limit, | limit = _limit, | ||
count_all = | count_all = self.count_all | ||
}} | }} | ||
Line 266: | Line 210: | ||
end | end | ||
function | function GalleryTemplate._expandTemplate_GallerySection(self) | ||
_limit = tonumber(self.limit) or 4 | |||
_widths = tonumber(self.widths) or 250 | |||
_count_all = self.count_all or 0 | |||
_title = self.title or '' | |||
_limit = tonumber( | |||
_widths = tonumber( | |||
_count_all = | |||
gallery = frame:expandTemplate { title = 'Gallery Section', args = { | if self.frame.expandTemplate == nil then return '' end | ||
gallery = self.frame:expandTemplate { title = 'Gallery Section', args = { | |||
self.character, | |||
self.cat, | |||
self.cat2, | |||
self.header, | |||
self.title, | |||
widths = _widths, | widths = _widths, | ||
limit = _limit, | limit = _limit, | ||
Line 289: | Line 230: | ||
return gallery | return gallery | ||
end | end | ||
function GalleryTemplate:getNumberOfImages(self) | |||
return self.images_in_section | |||
end | |||
------------------------------------- | |||
-- Gallery Class | |||
------------------------------------- | |||
local Gallery = {} | |||
Gallery.__index = Gallery | |||
function Gallery.new(data, frame) | |||
local self = setmetatable({}, Gallery) | |||
self.galleryData = mw.loadData(data) | |||
self.ordering = self.galleryData.ordering | |||
self.sections = self.galleryData.sections | |||
self.frame = frame | |||
return self | |||
end | |||
function Gallery:__tostring() | |||
local frame = self.frame | |||
text = {} | |||
_char = frame.args['character'] or '' | |||
for i,v in ipairs (self.ordering) do | |||
category = v | |||
local params = self.sections[v] or '' | |||
if params == '' then | |||
else | |||
local t_section = {} | |||
local t_galleries = {} | |||
local images_in_section = 0 | |||
local 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(t_section, '<div class="' .. class .. '">') | |||
end | |||
-- each v2 represents one template call | |||
for k2,v2 in pairs(params) do | |||
template = v2.template or '' | |||
local title = v2.title or '' | |||
mw.log(title .. ": " .. template) | |||
if 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 | |||
table.insert(t_section, current_subheader) | |||
g = table.concat(t_galleries) | |||
if g ~= "" then | |||
table.insert(t_section, g) | |||
end | |||
-- reset galleries and images for the next subsection | |||
clear_table(t_galleries) | |||
images_in_section = 0 | |||
end | |||
end | |||
local header = v2.header or '' | |||
-- save the new subheader and wait to see if the following galleries have any images in them | |||
current_subheader = '<' .. header .. '>' .. title .. '</' .. header .. ">\n" | |||
elseif template ~= '' then | |||
images_in_section = images_in_section | |||
temp = GalleryTemplate.new(frame, template, v2) | |||
table.insert(t_galleries, tostring(temp)) | |||
mw.log("TEMP: " .. dump(temp)) | |||
images_in_section = tonumber(temp:getNumberOfImages(temp)) or 0 | |||
end | |||
end | |||
-- check the number of images in the section | |||
mw.log(category .. " images_in_section = " .. images_in_section) | |||
if images_in_section > 0 then | |||
-- if there is a subheader to insert, then insert it | |||
if current_subheader ~= "" then | |||
table.insert(t_section, current_subheader) | |||
end | |||
-- if there are galleries to insert, then insert them | |||
g = table.concat(t_galleries) | |||
table.insert(t_section, g) | |||
-- close the wrapping div if one was added | |||
if class ~= "" then | |||
table.insert(t_section, "</div>") | |||
end | |||
-- insert the section, prepended by the section header, to the final text | |||
s = table.concat(t_section) | |||
if s ~= "" then | |||
table.insert(text, '<h2>' .. category .. '</h2>\n' .. s) | |||
end | |||
else | |||
mw.log(images_in_section .. " images total in this section") | |||
end | |||
end | |||
end | |||
gallery = table.concat(text) | |||
return gallery | |||
end | |||
------------------------------------- | |||
-- Main | |||
------------------------------------- | |||
local p = {} | |||
function p.main(frame) | |||
data = frame.args.data or 'Module:Sandbox/Chao/data' | |||
gallery = Gallery.new(data, frame) | |||
--mw.log(dump(tostring(gallery))) | |||
return tostring(gallery) | |||
end | |||
------------------------------------- | |||
-- Tests | |||
------------------------------------- | |||
function p.testExpandTemplate_1(frame) | function p.testExpandTemplate_1(frame) | ||
Line 341: | Line 408: | ||
end | end | ||
end | end | ||
------------------------------------- | |||
-- Utility Functions | |||
------------------------------------- | |||
-- clear a table | -- clear a table | ||
Line 358: | Line 429: | ||
end | end | ||
function cargo_escape(s) | function cargo_escape(s) | ||
return (string.gsub(s, "[' | return (string.gsub(s, "[']", { | ||
["'"] = "", | ["'"] = "", | ||
['"'] = "", | ['"'] = "", |
Revision as of 01:51, 21 April 2020
This page is part of an automated Gallery system. For more details, please refer to Help:Manual of Style/Character Pages/Gallery. See all pages included in this system |
This module creates a character gallery.
Please see Module:Character Gallery/data for the format of the character gallery and how to change it.
Usage
{{#invoke:Character Gallery|render|character=CHARACTER NAME}}
Lua Testing
frame={args={character="Es"}} p.main(frame)
local cargo = mw.ext.cargo
-------------------------------------
-- Gallery Section Class
-------------------------------------
local GalleryTemplate = {}
GalleryTemplate.__index = GalleryTemplate
GalleryTemplate.fields = {
character = true,
source = true,
section = true,
limit = true,
widths = true,
header = true,
title = true,
sort = true,
see_all = true,
see_cameos = true,
--Gallery Section (BBRadio)
season = true,
episode = true,
widths = true,
--Gallery Section
cat = true,
cat2 = true,
--Misc/Shared
count_all = true,
count_cameos = true,
images_in_section = true,
}
GalleryTemplate.templateMethods = {
['Gallery Section (Cargo)'] = 'makeGallerySectionCargo',
['Gallery Section (BBRadio)'] = 'makeGallerySectionBBRadio',
['Gallery Section'] = 'makeGallerySection',
}
function GalleryTemplate.new(frame, templateType, params)
local self = setmetatable({}, GalleryTemplate)
self.templateType= templateType
self.frame = frame
for field in pairs(GalleryTemplate.fields) do
self[field] = params[field] or ''
end
self.images_in_section = 0
self.cat = params.category or ''
if (params.character == nil)
then self.character = frame.args.character or ''
else self.character = params.character or ''
end
return self
end
function GalleryTemplate:__tostring()
local method = GalleryTemplate.templateMethods[self.templateType]
if method then
return self[method](self)
end
return ''
end
function GalleryTemplate.makeGallerySectionCargo(self)
self.character = cargo_escape(self.character)
self.count_all = GalleryTemplate._countImages_GallerySectionCargo(self,false) or 0
self.count_cameos = GalleryTemplate._countImages_GallerySectionCargo(self,true) or 0
local total_images = self.count_all + self.count_cameos
if self.count_all > 0 then
self.see_all = 'y'
end
if self.count_cameos > 0 then
self.see_cameos = 'y'
end
if total_images > 0 then
expanded_template = GalleryTemplate._expandTemplate_GallerySectionCargo(self, self.frame)
--table.insert(t_galleries, expanded_template)
self.images_in_section = self.images_in_section + total_images
end
return expanded_template
end
function GalleryTemplate.makeGallerySectionBBRadio(self)
self.character = cargo_escape(self.character)
self.count_all = GalleryTemplate._countImages_GallerySectionBBRadio(self) or 0
expanded_template = GalleryTemplate._expandTemplate_GallerySectionBBRadio(self,self.frame)
--table.insert(t_galleries, expanded_template)
self.images_in_section = self.images_in_section + self.count_all
return expanded_template
end
function GalleryTemplate.makeGallerySection(self)
self.count_all = GalleryTemplate._countImages_GallerySection(self) or 0
expanded_template = GalleryTemplate._expandTemplate_GallerySection(self,self.frame)
--table.insert(t_galleries, expanded_template)
self.images_in_section = self.images_in_section + self.count_all
return expanded_template
end
-- counts the number of images with this character (or cameo) for sections using Template:Gallery Section (Cargo)
-- countCameos: boolean
function GalleryTemplate._countImages_GallerySectionCargo(self, countCameos)
tables = 'Files'
fields = 'COUNT(Files._pageName)'
character = self.character or ''
source = self.source or ''
section = self.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 GalleryTemplate._countImages_GallerySectionBBRadio(self)
tables = 'Files,BBRadio_Cuts'
fields = 'COUNT(Files._pageName)'
join_on = 'Files._pageName=BBRadio_Cuts._pageName'
mw.log("Count images for " .. self.character .. ' season ' .. self.season)
where_clause = 'Files.Characters HOLDS "' .. self.character .. '" AND BBRadio_Cuts.Season="' .. self.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 GalleryTemplate._countImages_GallerySection(self)
cat1 = self.character or ''
cat2 = self.cat or ''
cat3 = self.cat2 or ''
local query = {}
if cat1 ~= '' then table.insert(query, '[[Category:' .. cat1 .. ']]') end
if cat2 ~= '' then table.insert(query, '[[Category:' .. cat2 .. ']]') end
if cat3 ~= '' then table.insert(query, '[[Category:' .. cat3 .. ']]') end
local result = mw.smw.getQueryResult( query )
count = result.meta.count or 0
return count
end
function GalleryTemplate._expandTemplate_GallerySectionCargo(self)
_limit = tonumber(self.limit) or 4
if self.frame.expandTemplate == nil then return '' end
gallery = self.frame:expandTemplate{ title = 'Gallery Section (Cargo)', args = {
character = self.character,
source = self.source,
section = self.section,
limit = _limit,
title = self.title,
header = self.header,
see_all = self.see_all,
see_cameos = self.see_cameos,
count_all = self.count_all,
count_cameos = self.count_cameos
}}
return gallery
end
function GalleryTemplate._expandTemplate_GallerySectionBBRadio(self)
_limit = tonumber(self.limit) or 4
_widths = tonumber(self.widths) or 250
if self.frame.expandTemplate == nil then return '' end
gallery = self.frame:expandTemplate { title = 'Gallery Section (BBRadio)', args = {
self.character,
self.season,
self.episode,
self.header,
self.title,
widths = _widths,
limit = _limit,
count_all = self.count_all
}}
return gallery
end
function GalleryTemplate._expandTemplate_GallerySection(self)
_limit = tonumber(self.limit) or 4
_widths = tonumber(self.widths) or 250
_count_all = self.count_all or 0
_title = self.title or ''
if self.frame.expandTemplate == nil then return '' end
gallery = self.frame:expandTemplate { title = 'Gallery Section', args = {
self.character,
self.cat,
self.cat2,
self.header,
self.title,
widths = _widths,
limit = _limit,
count_all = _count_all,
}}
return gallery
end
function GalleryTemplate:getNumberOfImages(self)
return self.images_in_section
end
-------------------------------------
-- Gallery Class
-------------------------------------
local Gallery = {}
Gallery.__index = Gallery
function Gallery.new(data, frame)
local self = setmetatable({}, Gallery)
self.galleryData = mw.loadData(data)
self.ordering = self.galleryData.ordering
self.sections = self.galleryData.sections
self.frame = frame
return self
end
function Gallery:__tostring()
local frame = self.frame
text = {}
_char = frame.args['character'] or ''
for i,v in ipairs (self.ordering) do
category = v
local params = self.sections[v] or ''
if params == '' then
else
local t_section = {}
local t_galleries = {}
local images_in_section = 0
local 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(t_section, '<div class="' .. class .. '">')
end
-- each v2 represents one template call
for k2,v2 in pairs(params) do
template = v2.template or ''
local title = v2.title or ''
mw.log(title .. ": " .. template)
if 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
table.insert(t_section, current_subheader)
g = table.concat(t_galleries)
if g ~= "" then
table.insert(t_section, g)
end
-- reset galleries and images for the next subsection
clear_table(t_galleries)
images_in_section = 0
end
end
local header = v2.header or ''
-- save the new subheader and wait to see if the following galleries have any images in them
current_subheader = '<' .. header .. '>' .. title .. '</' .. header .. ">\n"
elseif template ~= '' then
images_in_section = images_in_section
temp = GalleryTemplate.new(frame, template, v2)
table.insert(t_galleries, tostring(temp))
mw.log("TEMP: " .. dump(temp))
images_in_section = tonumber(temp:getNumberOfImages(temp)) or 0
end
end
-- check the number of images in the section
mw.log(category .. " images_in_section = " .. images_in_section)
if images_in_section > 0 then
-- if there is a subheader to insert, then insert it
if current_subheader ~= "" then
table.insert(t_section, current_subheader)
end
-- if there are galleries to insert, then insert them
g = table.concat(t_galleries)
table.insert(t_section, g)
-- close the wrapping div if one was added
if class ~= "" then
table.insert(t_section, "</div>")
end
-- insert the section, prepended by the section header, to the final text
s = table.concat(t_section)
if s ~= "" then
table.insert(text, '<h2>' .. category .. '</h2>\n' .. s)
end
else
mw.log(images_in_section .. " images total in this section")
end
end
end
gallery = table.concat(text)
return gallery
end
-------------------------------------
-- Main
-------------------------------------
local p = {}
function p.main(frame)
data = frame.args.data or 'Module:Sandbox/Chao/data'
gallery = Gallery.new(data, frame)
--mw.log(dump(tostring(gallery)))
return tostring(gallery)
end
-------------------------------------
-- Tests
-------------------------------------
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
-------------------------------------
-- Utility Functions
-------------------------------------
-- clear a table
function clear_table(t)
for k in pairs (t) do
t [k] = nil
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
function cargo_escape(s)
return (string.gsub(s, "[']", {
["'"] = "",
['"'] = "",
}))
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