Skip to content

Instantly share code, notes, and snippets.

@jechlin
Created May 21, 2012 13:58
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 jechlin/2762463 to your computer and use it in GitHub Desktop.
Save jechlin/2762463 to your computer and use it in GitHub Desktop.
spit and sawdust confluence calendar upgrade
@Test
public void getAllCalendars() {
def sql = Sql.newInstance("jdbc:oracle:thin:@localhost:1521:XE", "confluence", "confluence")
def getCalsSql = """
SELECT CONTENT.SPACEID, SPACES.spacekey, SPACES.spacename, CONTENT.CONTENTID, CONTENT.title, CONTENT.LASTMODDATE
FROM CONTENT, BODYCONTENT, SPACES
WHERE PREVVER IS NULL AND
(CONTENTTYPE = 'PAGE' OR CONTENTTYPE = 'BLOGPOST')
AND CONTENT.SPACEID = :spaceId
AND CONTENT.CONTENTID = BODYCONTENT.CONTENTID
AND SPACES.spaceid=CONTENT.spaceid
AND BODYCONTENT.BODY like '%{calendar%'
ORDER BY CONTENT.LASTMODDATE DESC
"""
sql.eachRow("select * from spaces") {spaceRow ->
println spaceRow.SPACENAME
sql.rows(getCalsSql, [spaceId: spaceRow.SPACEID]).each {cal ->
println cal
getCalsForPage(cal.CONTENTID as Integer)
}
}
}
private getCalsForPage(int pageId) {
def saveDir = new File("c:/tmp/calendars")
assert saveDir.exists()
def baseUrl = "http://confluence.host.here/confluence"
def credential = "&os_username=adminId&os_password=adminPassword"
driver.get("$baseUrl/pages/viewpage.action?pageId=${pageId}${credential}")
def pattern = "\\('([^']*?)'\\).viewCalendar\\('(.*?)'\\)\">(.*?)<"
def regex = Pattern.compile(pattern, Pattern.MULTILINE | Pattern.DOTALL);
def matcher = (driver.pageSource =~ regex)
def String spaceKey
try {
spaceKey = (driver as JavascriptExecutor).executeScript('return AJS.$("#confluence-space-key").attr("content")')
} catch (WebDriverException wde) {
println "WARN: No AJS, probably general page failure"
return
}
spaceKey = spaceKey.replace("~", "PERSONAL-SPACE-")
def spaceSaveDir = new File(saveDir, spaceKey)
spaceSaveDir.mkdir()
def title = (driver as JavascriptExecutor).executeScript('return AJS.$("#title-text a").html()')
if (! matcher.count) {
println "WARN: No calendars found for $pageId ($spaceKey)"
}
else {
(0..matcher.count - 1).each {i ->
def cid = matcher[i][1]
def scid = matcher[i][2]
def String calName = matcher[i][3]
calName = calName.replaceAll("/", "-")
calName = calName.replaceAll("\\/", "-")
def saveTo = "$spaceKey-$title-$cid-${calName}.ics"
def download = "$baseUrl/plugins/calendar/ical/dc.action?pageId=$pageId&cid=$cid&scid=$scid$credential"
println "Download $download to $saveTo"
try {
new File(spaceSaveDir, saveTo).withWriter {w ->
w.write(new URL(download).text)
}
} catch (IOException e) {
// a couple of calendars give 412s - don't know why they need to done manually
println ("ERROR: Failed to save cal at $download from page ${driver.currentUrl}: ${e.message}")
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment