Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.1 Import by Page
- <%
- import com.xpn.xwiki.*;
- import com.xpn.xwiki.doc.*;
- import com.xpn.xwiki.plugin.packaging.*;
- import java.util.zip.*;
- import com.xpn.xwiki.util.Util;
- finalPageName = ""
- exception = ""
- def updateDoc(String newname, InputStream xml, String type, XWikiContext context)
- {
- try {
- def fdoc = new XWikiDocument()
- if ((type == "NOOVERVERSIONS") || (type == "OVERVERSIONS")) {
- fdoc.fromXML(xml, true);
- } else {
- fdoc.fromXML(xml, false);
- }
- int nbAttachments = fdoc.getAttachmentList().size();
- System.out.println("|____ Document: " + fdoc.getFullName() + "-" + fdoc.getLanguage() + "-" + fdoc.getDefaultLanguage() + " - " + fdoc.getVersion() + " - ${nbAttachments} attachments");
- if (newname == "")
- newname = fdoc.getFullName()
- def prevDoc = context.getWiki().getDocument(newname, context);
- def prevArchive = prevDoc.getDocumentArchive();
- if (prevDoc.isNew() || ((type != "NOOVERVERSIONS") && (type != "NOOVERNOVERSIONS"))) {
- // We need to verify the document has the right name
- // Otherwise we might override something we don't expect
- if ((newname != "") && (fdoc.fullName != newname)) {
- fdoc = fdoc.copyDocument(newname, context);
- }
- finalPageName = fdoc.getFullName()
- if (type == "ADDVERSION") {
- fdoc.setDocumentArchive(prevArchive);
- context.getWiki().saveDocument(fdoc, context);
- if (prevDoc.isNew()) {
- fdoc.resetArchive(context);
- }
- return 1;
- } else {
- def pack = new Package()
- if ((type == "NOOVERVERSIONS") || (type == "OVERVERSIONS"))
- pack.setWithVersions(true)
- else
- pack.setWithVersions(false)
- pack.setBackupPack(true)
- pack.add(fdoc, 0, context)
- return pack.install(context)
- }
- } else {
- return -1000;
- }
- } catch (Throwable e) {
- exception = e.getMessage()
- return -2000;
- }
- }
- def updateDoc(ZipFile zipfile, ZipEntry zipentry, String newname, String type, XWikiContext context)
- {
- def is = zipfile.getInputStream(zipentry)
- def ret = updateDoc("", is, type, context);
- if (ret < 0) {
- if (ret == -1000) {
- System.err.println("* Document for entry ${zipentry} already exists and overwrite was not specified.")
- println "* Document for entry ${zipentry} already exists and overwrite was not specified.";
- } else if (ret == -2000) {
- System.err.println("* Document for entry ${zipentry} could not be imported with exception (${exception})")
- println "* Document for entry ${zipentry} could not be imported with exception (${exception})";
- } else {
- System.err.println("* Document for entry ${zipentry} could not be imported with error ${ret}.")
- println "* Document for entry ${zipentry} could not be imported with error ${ret}.";
- }
- } else {
- System.out.println("__ Document for entry ${zipentry} has been imported properly: [${finalPageName}].")
- println "* Document for entry ${zipentry} has been imported properly: [${finalPageName}]."
- }
- }
- def file = null
- if (request.get("filename") != null) {
- def filename = request.get("filename")
- file = new File(filename);
- if (!file.exists()) {
- %>
- #error("File $file does not exists")
- <%
- file = null
- }
- }
- if (file != null && request.get("confirm") == null) {
- %>
- {pre}
- <script type="text/javascript">
- function selectItems(classId, selected)
- {
- var docs = document.getElementsByClassName(classId);
- var i;
- for (i = 0; i < docs.length; i++)
- {
- var doc = docs[i];
- doc.checked = selected;
- }
- }
- </script>
- {/pre}
- <form action="" method="post">
- <input type="hidden" name="filename" value="${request.filename}" />
- <input type="hidden" name="confirm" value="1" />
- <%
- def counter = 0;
- def time1 = new Date()
- if (request.get("importall") == null) {
- def time2 = new Date()
- %>
- <span id="selectDocsActions">
- <a href="javascript:void()" onclick="selectItems('selCheckedDoc', false); return false;" class="Exportlink">Unselect All</a>,
- <a href="javascript:void()" onclick="selectItems('selCheckedDoc', true); return false;" class="Exportlink">Select All</a>
- </span>
- <table border="0" cellspacing="0" cellpadding="0">
- <%
- def zipfile = new ZipFile(file);
- for (zipentry in zipfile.entries()) {
- filename = zipentry.name
- if (!filename.endsWith("package.xml") && !zipentry.isDirectory()) {
- counter++;
- %>
- {pre}<tr><td><input class="selCheckedDoc" type="checkbox" name="file_${counter}" value="${filename}" checked />${filename}</td></tr>{/pre}
- <%
- }
- }
- %>
- </table>
- <%
- def dur = time2.getTime() - time1.getTime()
- println "* Duration: $dur"
- } else {
- %>
- Import all
- <input type="hidden" name="importall" value="1" />
- <%
- }
- %>
- <table border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td>Type:</td>
- <td>
- <input type="radio" name="type" value="NOOVERVERSIONS" /> Do Not overwrite with versions
- <input type="radio" name="type" value="NOOVERNOVERSIONS" /> Do Not overwrite without versions
- <br />
- <input type="radio" name="type" value="ADDVERSION" /> Add version
- <input type="radio" name="type" value="OVERVERSIONS" checked /> Overwrite with version
- <input type="radio" name="type" value="OVERNOVERSIONS" /> Overwrite without versions
- </td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <input type="hidden" name="counter" value="${counter}" />
- <input type="submit" name="Preview" />
- </td>
- </tr>
- </table>
- </form>
- <%
- } else if (file != null && request.get("confirm") != null) {
- def zipfile = new ZipFile(file);
- def type = request.get("type");
- def counter = 1;
- if (request.get("importall") == null) {
- def maxcounter = Integer.parseInt(request.counter)
- while (counter <= maxcounter) {
- def filename = request.get("file_${counter}")
- if (filename != null) {
- def zipentry = zipfile.getEntry(filename)
- if (zipentry != null) {
- System.out.println("Import ${counter}/${maxcounter}: " + filename);
- updateDoc(zipfile, zipentry, "", type, context.context)
- }
- }
- counter++;
- }
- } else {
- def maxcounter = zipfile.size()
- for (zipentry in zipfile.entries()) {
- def filename = zipentry.name
- if (!filename.endsWith("package.xml") && !zipentry.isDirectory()) {
- System.out.println("Import ${counter}/${maxcounter}: " + filename);
- updateDoc(zipfile, zipentry, "", type, context.context)
- }
- counter++;
- }
- }
- counter--;
- System.out.println("Finished importing ${counter} documents");
- println "* Finished importing ${counter} documents"
- } else {
- %>
- <form action="" method="post">
- <table border="0">
- <tr>
- <td>File to read from:</td><td><input type="text" name="filename" size="60" /></td>
- </tr>
- <tr>
- <td colspan="2" align="center">
- <input type="submit" name="Preview" />
- <input type="checkbox" name="importall" value="1" /> Import all
- </td>
- </tr>
- </table>
- </form>
- <%
- }
- %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement