My blog entries were identified not by SnipSnap’s normal “magic names”, but via custom labels with datestamps.
XWiki-embedded Groovy script to import from attached snipsnap.xml file:
import org.codehaus.groovy.sandbox.util.XmlSlurper;
live = true
spaceName = "SnipSnap"
authorName = "XWiki.Hans"
xmlFile = new String(doc.getAttachment("snipsnap.xml").content)
def node = new XmlSlurper().parseText(xmlFile)
assert node != null
snips = node.snip
println "~~Found ${snips.size()} snips~~\n\n"
for (s in snips) {
commentParent = s.commentSnip
if (commentParent.text() == "") {
newTitle = s.name.text()
// need to zap a few gremlin characters...
newTitle = newTitle.replaceAll("/","*")
newTitle = newTitle.replaceAll("\\.","-")
newTitle = newTitle.replaceAll(":","-")
newTitle = newTitle.replaceAll("\\?","")
newContent = s.content.text()
createdTime = new Date(Long.parseLong(s.cTime.text()))
modifiedTime = new Date(Long.parseLong(s.mTime.text()))
// if I've got a label, we're assuming it's a DateLabel and updating our creation date
labelArray = s.labels.text().split(":")
if (labelArray.length > 2) {
labelText = labelArray[2]
labelLong = Long.parseLong(labelText.split(",")[0])
createdTime = new Date(labelLong)
}
if (live) {
newDoc = xwiki.getDocument(spaceName, newTitle)
newDoc.doc.setContent(newContent)
newDoc.doc.setAuthor(authorName)
newDoc.doc.setCreationDate(createdTime)
newDoc.doc.setDate(modifiedTime)
newDoc.saveDocument()
println("* saved [${newTitle}]\n")
}
else {
println("* ${newTitle} created ${createdTime}\n")
}
}
else { // commentParent.text() != ""
parentTitle = commentParent.text()
parentTitle = parentTitle.replaceAll("/","*")
parentTitle = parentTitle.replaceAll("\\.","-")
parentTitle = parentTitle.replaceAll(":",".")
parentTitle = parentTitle.replaceAll("\\?","")
newContent = s.content.text()
createdTime = new Date(Long.parseLong(s.cTime.text()))
if (live) {
parent = xwiki.getDocument(spaceName, parentTitle)
num = parent.doc.createNewObject("XWiki.XWikiComments", context.context)
newObj = parent.doc.getObject("XWiki.XWikiComments", num)
newObj.setStringValue("author", authorName)
newObj.setDateValue("date", createdTime)
newObj.setStringValue("comment", newContent)
parent.saveDocument()
println "* commented [${parentTitle}]\n"
}
else {
println "* comment for ${parentTitle}\n"
}
}
}
…and script for migrating imported docs.
fromSpace = "SnipSnap";
namePrefix = "Journal*";
toSpace = "Main";
toParent = "Main.Journal";
toTags = "";
docSql = "doc.web = '${fromSpace}' and doc.name like '${namePrefix}%' ";
list = xwiki.searchDocuments(docSql);
bigXWiki = xwiki.getXWiki();
for (item in list) {
fromDoc = xwiki.getDocument(item);
fromName = fromDoc.name;
toName = fromName.replaceFirst("\\Q"+namePrefix+"\\E", ""); // ignore special characters
toDoc = xwiki.getDocument(toSpace, toName);
toDoc.doc.setParent(toParent);
toDoc.doc.setContent(fromDoc.content);
toDoc.doc.setAuthor(fromDoc.author);
toDoc.doc.setCreationDate(fromDoc.creationDate);
objNum = toDoc.doc.createNewObject("XWiki.TopicTags", context.context);
tagObj = toDoc.doc.getObject("XWiki.TopicTags", objNum);
tagObj.setStringValue("tags", toTags);
toDoc.saveDocument();
bigXWiki.deleteDocument(fromDoc.doc, context.context);
println "* [${fromSpace}.${fromName}] -> [${toSpace}.${toName}]";
}