Skip to content

Instantly share code, notes, and snippets.

@molaschi
Last active April 8, 2018 02:21
Show Gist options
  • Save molaschi/ad4274d35baaa006c908 to your computer and use it in GitHub Desktop.
Save molaschi/ad4274d35baaa006c908 to your computer and use it in GitHub Desktop.
Magnolia export pages and used 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.*
// if you don't know which is magnolia version
mgnl45 = JCRCriteriaFactory.createCriteria().setWorkspace("website").setBasePath("/jcr:root/*").execute().items.next() instanceof javax.jcr.Node
// otherwise set it directly to true or false
// e.g.
// mgnl45 = true
def basePath = "/path/to/the/node" // "" to export all
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 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
}
templates = [:]
urls = [:]
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] = [count: 0, components: [:]]
templates[template].count++
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}"
}
url = "${mgnl45 ? node.path : node.handle}.html"
urls[template] = urls[template] ?: url
}
println "----------------"
println "template, n pages, component, n components per template, url example"
templates.sort({ a, b -> b.value.count <=> a.value.count }).each {
println "${it.key},${it.value.count},,,${urls[it.key]}"
it.value.components.sort({ a, b -> b.value <=> a.value }).each { component ->
println ",,${component.key},${component.value},"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment