Skip to content

Instantly share code, notes, and snippets.

@jdorrance
Created January 2, 2014 02:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jdorrance/8214126 to your computer and use it in GitHub Desktop.
Save jdorrance/8214126 to your computer and use it in GitHub Desktop.
import groovy.json.JsonSlurper
def nodesToProcess = getNodes("admin" , "admin" , "localhost:4502" , "geometrixx")
def sites = getSites("https://mavendc-utils.s3.amazonaws.com/test/data.json")
processSite("geometrixx" , nodesToProcess, sites , false)
def getSites(json){
new JsonSlurper().parseText(new URL(json).getText("UTF-8"))
}
def processSite(key , nodesToProcess, sites, dryRun = true){
nodesToProcess.get(key).each{
processNode(getNode(it.path) , sites , dryRun)
}.grep()
}
def getNodes(usr, pwd, host, key){
def url = """http://HOSTTOREPLACE/crx/de/query.jsp?_dc=1388628075229&_charset_=utf-8&type=xpath&stmt=%2Fjcr%3Aroot%2Fcontent%2FSITETOREPLACE%2F%2F*%5Bjcr%3Acontains(%40text%2C'what')%5D&showResults=true"""
url = url.replace("HOSTTOREPLACE", host).replace("SITETOREPLACE",key)
def authString = "${usr}:${pwd}".getBytes().encodeBase64().toString()
def conn = url.toURL().openConnection()
conn.setRequestProperty("Authorization" , "Basic ${authString}")
new JsonSlurper().parseText(conn.content.getText("UTF-8")).results.groupBy{
def matched = it.path =~/(\/content\/)([a-zA-Z]+)/
matched[0][2]
}
}
def processNode(node , mappy, dryRun){
def wasProcessed = false
['text'].each{ prop ->
if(processProperty(node , prop, mappy)) wasProcessed = true
}
if(!dryRun){
println "SAVING"
save()
}
wasProcessed
}
def processProperty(node , prop, mappy){
if(!node.hasProperty(prop)) return false
def text = node.get(prop)
def wasProcessed = false
mappy.each{rewrite ->
if(text.contains(rewrite.fromurl)){
println "Rewrite match found!"
println node.path
println "Rewriting ${rewrite.fromurl} to ${rewrite.tourl}\n\n"
node.set(prop, text.replaceAll(rewrite.fromurl , rewrite.tourl))
wasProcessed = true
}
wasProcessed
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment