Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.19 KB | None | 0 0
  1. 1.1 Import by Page
  2.  
  3. <%
  4.  
  5. import com.xpn.xwiki.*;
  6. import com.xpn.xwiki.doc.*;
  7. import com.xpn.xwiki.plugin.packaging.*;
  8. import java.util.zip.*;
  9. import com.xpn.xwiki.util.Util;
  10.  
  11. finalPageName = ""
  12. exception = ""
  13.  
  14. def updateDoc(String newname, InputStream xml, String type, XWikiContext context)
  15. {
  16. try {
  17. def fdoc = new XWikiDocument()
  18.  
  19. if ((type == "NOOVERVERSIONS") || (type == "OVERVERSIONS")) {
  20. fdoc.fromXML(xml, true);
  21. } else {
  22. fdoc.fromXML(xml, false);
  23. }
  24.  
  25. int nbAttachments = fdoc.getAttachmentList().size();
  26. System.out.println("|____ Document: " + fdoc.getFullName() + "-" + fdoc.getLanguage() + "-" + fdoc.getDefaultLanguage() + " - " + fdoc.getVersion() + " - ${nbAttachments} attachments");
  27.  
  28. if (newname == "")
  29. newname = fdoc.getFullName()
  30.  
  31. def prevDoc = context.getWiki().getDocument(newname, context);
  32. def prevArchive = prevDoc.getDocumentArchive();
  33.  
  34. if (prevDoc.isNew() || ((type != "NOOVERVERSIONS") && (type != "NOOVERNOVERSIONS"))) {
  35. // We need to verify the document has the right name
  36. // Otherwise we might override something we don't expect
  37. if ((newname != "") && (fdoc.fullName != newname)) {
  38. fdoc = fdoc.copyDocument(newname, context);
  39. }
  40.  
  41. finalPageName = fdoc.getFullName()
  42.  
  43. if (type == "ADDVERSION") {
  44. fdoc.setDocumentArchive(prevArchive);
  45. context.getWiki().saveDocument(fdoc, context);
  46. if (prevDoc.isNew()) {
  47. fdoc.resetArchive(context);
  48. }
  49.  
  50. return 1;
  51. } else {
  52. def pack = new Package()
  53. if ((type == "NOOVERVERSIONS") || (type == "OVERVERSIONS"))
  54. pack.setWithVersions(true)
  55. else
  56. pack.setWithVersions(false)
  57.  
  58. pack.setBackupPack(true)
  59. pack.add(fdoc, 0, context)
  60.  
  61. return pack.install(context)
  62. }
  63. } else {
  64. return -1000;
  65. }
  66. } catch (Throwable e) {
  67. exception = e.getMessage()
  68.  
  69. return -2000;
  70. }
  71. }
  72.  
  73. def updateDoc(ZipFile zipfile, ZipEntry zipentry, String newname, String type, XWikiContext context)
  74. {
  75. def is = zipfile.getInputStream(zipentry)
  76. def ret = updateDoc("", is, type, context);
  77.  
  78. if (ret < 0) {
  79. if (ret == -1000) {
  80. System.err.println("* Document for entry ${zipentry} already exists and overwrite was not specified.")
  81. println "* Document for entry ${zipentry} already exists and overwrite was not specified.";
  82. } else if (ret == -2000) {
  83. System.err.println("* Document for entry ${zipentry} could not be imported with exception (${exception})")
  84. println "* Document for entry ${zipentry} could not be imported with exception (${exception})";
  85. } else {
  86. System.err.println("* Document for entry ${zipentry} could not be imported with error ${ret}.")
  87. println "* Document for entry ${zipentry} could not be imported with error ${ret}.";
  88. }
  89. } else {
  90. System.out.println("__ Document for entry ${zipentry} has been imported properly: [${finalPageName}].")
  91. println "* Document for entry ${zipentry} has been imported properly: [${finalPageName}]."
  92. }
  93. }
  94.  
  95. def file = null
  96. if (request.get("filename") != null) {
  97. def filename = request.get("filename")
  98. file = new File(filename);
  99. if (!file.exists()) {
  100. %>
  101. #error("File $file does not exists")
  102. <%
  103. file = null
  104. }
  105. }
  106.  
  107. if (file != null && request.get("confirm") == null) {
  108. %>
  109. {pre}
  110. <script type="text/javascript">
  111. function selectItems(classId, selected)
  112. {
  113. var docs = document.getElementsByClassName(classId);
  114. var i;
  115. for (i = 0; i < docs.length; i++)
  116. {
  117. var doc = docs[i];
  118. doc.checked = selected;
  119. }
  120. }
  121. </script>
  122. {/pre}
  123. <form action="" method="post">
  124. <input type="hidden" name="filename" value="${request.filename}" />
  125. <input type="hidden" name="confirm" value="1" />
  126. <%
  127. def counter = 0;
  128. def time1 = new Date()
  129. if (request.get("importall") == null) {
  130. def time2 = new Date()
  131. %>
  132. <span id="selectDocsActions">
  133. <a href="javascript:void()" onclick="selectItems('selCheckedDoc', false); return false;" class="Exportlink">Unselect All</a>,
  134. <a href="javascript:void()" onclick="selectItems('selCheckedDoc', true); return false;" class="Exportlink">Select All</a>
  135. </span>
  136. <table border="0" cellspacing="0" cellpadding="0">
  137. <%
  138. def zipfile = new ZipFile(file);
  139. for (zipentry in zipfile.entries()) {
  140. filename = zipentry.name
  141. if (!filename.endsWith("package.xml") && !zipentry.isDirectory()) {
  142. counter++;
  143. %>
  144. {pre}<tr><td><input class="selCheckedDoc" type="checkbox" name="file_${counter}" value="${filename}" checked />${filename}</td></tr>{/pre}
  145. <%
  146. }
  147. }
  148. %>
  149. </table>
  150. <%
  151. def dur = time2.getTime() - time1.getTime()
  152.  
  153. println "* Duration: $dur"
  154. } else {
  155. %>
  156. Import all
  157. <input type="hidden" name="importall" value="1" />
  158. <%
  159. }
  160. %>
  161. <table border="0" cellspacing="0" cellpadding="0">
  162. <tr>
  163. <td>Type:</td>
  164. <td>
  165. <input type="radio" name="type" value="NOOVERVERSIONS" /> Do Not overwrite with versions
  166. <input type="radio" name="type" value="NOOVERNOVERSIONS" /> Do Not overwrite without versions
  167. <br />
  168. <input type="radio" name="type" value="ADDVERSION" /> Add version
  169. <input type="radio" name="type" value="OVERVERSIONS" checked /> Overwrite with version
  170. <input type="radio" name="type" value="OVERNOVERSIONS" /> Overwrite without versions
  171. </td>
  172. </tr>
  173. <tr>
  174. <td colspan="2" align="center">
  175. <input type="hidden" name="counter" value="${counter}" />
  176. <input type="submit" name="Preview" />
  177. </td>
  178. </tr>
  179. </table>
  180. </form>
  181. <%
  182. } else if (file != null && request.get("confirm") != null) {
  183. def zipfile = new ZipFile(file);
  184.  
  185. def type = request.get("type");
  186.  
  187. def counter = 1;
  188. if (request.get("importall") == null) {
  189. def maxcounter = Integer.parseInt(request.counter)
  190.  
  191. while (counter <= maxcounter) {
  192. def filename = request.get("file_${counter}")
  193.  
  194. if (filename != null) {
  195. def zipentry = zipfile.getEntry(filename)
  196. if (zipentry != null) {
  197. System.out.println("Import ${counter}/${maxcounter}: " + filename);
  198. updateDoc(zipfile, zipentry, "", type, context.context)
  199. }
  200. }
  201.  
  202. counter++;
  203. }
  204. } else {
  205. def maxcounter = zipfile.size()
  206.  
  207. for (zipentry in zipfile.entries()) {
  208. def filename = zipentry.name
  209.  
  210. if (!filename.endsWith("package.xml") && !zipentry.isDirectory()) {
  211. System.out.println("Import ${counter}/${maxcounter}: " + filename);
  212.  
  213. updateDoc(zipfile, zipentry, "", type, context.context)
  214. }
  215.  
  216. counter++;
  217. }
  218. }
  219.  
  220. counter--;
  221. System.out.println("Finished importing ${counter} documents");
  222. println "* Finished importing ${counter} documents"
  223. } else {
  224. %>
  225. <form action="" method="post">
  226. <table border="0">
  227. <tr>
  228. <td>File to read from:</td><td><input type="text" name="filename" size="60" /></td>
  229. </tr>
  230. <tr>
  231. <td colspan="2" align="center">
  232. <input type="submit" name="Preview" />
  233. <input type="checkbox" name="importall" value="1" /> Import all
  234. </td>
  235. </tr>
  236. </table>
  237. </form>
  238. <%
  239. }
  240. %>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement