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