Skip to content

Instantly share code, notes, and snippets.

@molaschi
Last active April 8, 2018 02:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save molaschi/9e2654f42ece23829d17 to your computer and use it in GitHub Desktop.
Save molaschi/9e2654f42ece23829d17 to your computer and use it in GitHub Desktop.
export templates
import net.sourceforge.openutils.mgnlcriteria.jcr.query.JCRCriteriaFactory
import net.sourceforge.openutils.mgnlcriteria.jcr.query.criterion.Restrictions
import info.magnolia.cms.core.*
import info.magnolia.repository.*
import info.magnolia.jcr.util.*
class TemplateReport {
static def mgnl45 = true
String basePath
def templates = [:]
def templatesPerBasePath = [:]
def urls = [:]
public report() {
//println "processing ${basePath}"
templatesPerBasePath[basePath] = [:]
def res = JCRCriteriaFactory.createCriteria().setWorkspace("website").setBasePath("/jcr:root${basePath}//element(*, mgnl:${mgnl45 ? 'page' : 'content'})").execute()
res.items.each { node ->
//println "processing node ${node.path}"
def template = getTemplate(node)
if (!templates[template]) templates[template] = [activeCount: 0, inactiveCount: 0, components: [:]]
if (!templatesPerBasePath[basePath][template]) templatesPerBasePath[basePath][template] = [activeCount: 0, inactiveCount: 0]
def active = isActive(node)
templates[template][active ? 'activeCount' : 'inactiveCount']++
templatesPerBasePath[basePath][template][active ? 'activeCount' : 'inactiveCount']++
/*try {
def components = getComponents(node)
components.each { component ->
templates[template].components[component.key] = (templates[template].components[component.key] ?: 0) + component.value
}
} catch(e) {
e.printStackTrace()
println "ERROR: processing ${node.handle}"
}*/
def url = "${mgnl45 ? node.path : node.handle}.html"
urls[template] = urls[template] ?: url
}
}
def getTemplate(content) {
if (mgnl45) {
def property = "template"
if (content.hasNode(MetaData.DEFAULT_META_NODE)) {
def node = content.getNode(MetaData.DEFAULT_META_NODE);
if (node.hasProperty(property)) {
return PropertyUtil.getProperty(node, property).getString();
}
else if (node.hasProperty(RepositoryConstants.NAMESPACE_PREFIX + ":" + property))
{
return PropertyUtil.getProperty(node, RepositoryConstants.NAMESPACE_PREFIX + ":" + property).getString();
}
}
} else {
content.metaData.template
}
}
def isActive(content) {
if (mgnl45) {
def property = "activated"
if (content.hasNode(MetaData.DEFAULT_META_NODE)) {
def node = content.getNode(MetaData.DEFAULT_META_NODE);
if (node.hasProperty(property)) {
return PropertyUtil.getProperty(node, property).getBoolean();
}
else if (node.hasProperty(RepositoryConstants.NAMESPACE_PREFIX + ":" + property))
{
return PropertyUtil.getProperty(node, RepositoryConstants.NAMESPACE_PREFIX + ":" + property).getBoolean();
}
}
} else {
content.metaData.activated != 0
}
}
def getComponents(content) {
def areas = JCRCriteriaFactory.createCriteria().setWorkspace("website").setBasePath("/jcr:root${content.path}/element(*, mgnl:${mgnl45 ? 'area' : 'contentNode'})").execute()
def components = [:]
areas.items.each { area ->
def componentsRes = JCRCriteriaFactory.createCriteria().setWorkspace("website").setBasePath("/jcr:root${area.path}//element(*, mgnl:${mgnl45 ? 'component' : 'contentNode'})").execute()
componentsRes.items.each { component ->
def template = getTemplate(component)
if (template) {
components[template] = (components[template] ?: 0) + 1
}
}
}
components
}
}
//println "ciao"
//TemplateReport.println = println
TemplateReport.mgnl45 = true
report = new TemplateReport()
countries = JCRCriteriaFactory.createCriteria().setWorkspace("website").setBasePath("/jcr:root/maserati/element(*, mgnl:${TemplateReport.mgnl45 ? 'page' : 'content'})/element(*, mgnl:${TemplateReport.mgnl45 ? 'page' : 'content'})").execute()
countries.items.each { country ->
report.basePath = country.path
report.report()
}
println "----------------"
println "TEMPLATES INUTILIZZATI"
def registry = info.magnolia.objectfactory.Components.getComponent(info.magnolia.rendering.template.registry.TemplateDefinitionRegistry.class)
registry.templateDefinitions.sort({ a, b -> a.id <=> b.id }).each {
if (it.id.startsWith("maserati:pages") && !report.templates[it.id])
println it.id
}
println ""
println ""
println "UTILIZZO TEMPLATES GLOBALE: /maserati"
println "template, n pages active, n pages inactive, component, n components per template, url example"
report.templates.sort({ a, b -> b.value.activeCount <=> a.value.activeCount }).each {
if (it.key && !"null".equals(it.key)) {
println "${it.key},${it.value.activeCount},${it.value.inactiveCount},,,${report.urls[it.key]}"
it.value.components.sort({ a, b -> b.value <=> a.value }).each { component ->
println ",,,${component.key},${component.value},"
}
}
}
report.templatesPerBasePath.sort({a, b -> a.key <=> b.key}).each {
println ""
println ""
println "UTILIZZO TEMPLATES PER IL RAMO: ${it.key}"
println "template, n pages active, n pages inactive"
it.value.sort({ a, b -> b.value.activeCount <=> a.value.activeCount }).each {
if (it.key && !"null".equals(it.key))
println "${it.key},${it.value.activeCount},${it.value.inactiveCount}"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment