Исходный код вики Import Page
Редактировал(а) 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 | |||
| 6 | import java.util.zip.*; | ||
| 7 | |||
| 8 | finalPageName = ''; | ||
| 9 | finalPageURL = ''; | ||
| 10 | |||
| 11 | /** | ||
| 12 | * Import a document described in XML to a specified location. | ||
| 13 | * @param newname the target of the document | ||
| 14 | * @param xml the XML description of the document | ||
| 15 | * @param type the type of change on document's version | ||
| 16 | * @param context the XWiki context | ||
| 17 | */ | ||
| 18 | def updateDoc(String newname, String xml, String type, XWikiContext context) { | ||
| 19 | def prevDoc = context.getWiki().getDocument(newname, context); | ||
| 20 | def prevArchive = prevDoc.getDocumentArchive(); | ||
| 21 | if (prevDoc.isNew() || ((type != 'NOOVERVERSIONS') && (type != 'NOOVERNOVERSIONS'))) { | ||
| 22 | def fdoc = new XWikiDocument(); | ||
| 23 | fdoc.fromXML(xml); | ||
| 24 | // We need to verify the document has the right name | ||
| 25 | // Otherwise we might override something we don't expect | ||
| 26 | if ((newname != '') && (fdoc.fullName != newname)) { | ||
| 27 | fdoc = fdoc.copyDocument(newname, context); | ||
| 28 | } | ||
| 29 | finalPageName = fdoc.getFullName(); | ||
| 30 | finalPageURL = fdoc.getURL('view', context); | ||
| 31 | |||
| 32 | if (type == 'ADDVERSION') { | ||
| 33 | fdoc.setDocumentArchive(prevArchive); | ||
| 34 | context.getWiki().saveDocument(fdoc, context); | ||
| 35 | if (prevDoc.isNew()) { | ||
| 36 | fdoc.resetArchive(context); | ||
| 37 | } | ||
| 38 | return 1; | ||
| 39 | } else { | ||
| 40 | def pack = new Package(); | ||
| 41 | if ((type == 'NOOVERVERSIONS') || (type == 'OVERVERSIONS')) { | ||
| 42 | pack.setWithVersions(true); | ||
| 43 | } | ||
| 44 | else { | ||
| 45 | pack.setWithVersions(false); | ||
| 46 | } | ||
| 47 | pack.setBackupPack(true); | ||
| 48 | pack.add(fdoc, 0, context); | ||
| 49 | return pack.install(context); | ||
| 50 | } | ||
| 51 | } else { | ||
| 52 | return -1000; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | def importDoc() { | ||
| 57 | def fupl = xwiki.get('fileupload'); | ||
| 58 | def docName = fupl.getFileItem('name'); | ||
| 59 | def type = fupl.getFileItem('type'); | ||
| 60 | def data = fupl.getFileItemData("file"); | ||
| 61 | def xmlData = new StringBuilder(); | ||
| 62 | |||
| 63 | // generate the xml data from byte data | ||
| 64 | if ((data != null) && (data.length > 0)) { | ||
| 65 | def zipStream = new ZipInputStream(new ByteArrayInputStream(data)); | ||
| 66 | def buffer = new byte[1024]; | ||
| 67 | while ((entry = zipStream.getNextEntry()) != null) { | ||
| 68 | if (entry.getName() != 'package.xml') { | ||
| 69 | while ((read = zipStream.read(buffer, 0, 1024)) >= 0) { | ||
| 70 | xmlData.append(new String(buffer, 0, read)); | ||
| 71 | } | ||
| 72 | // If the xar contains more than one document, after package.xml is ignored, only first document will be imported. | ||
| 73 | break; | ||
| 74 | } | ||
| 75 | } | ||
| 76 | } | ||
| 77 | def ret = updateDoc(docName, xmlData.toString(), type, xcontext.context); | ||
| 78 | if (ret < 0) { | ||
| 79 | if (ret==-1000) | ||
| 80 | println 'Document already exists and overwrite was not specified.'; | ||
| 81 | else | ||
| 82 | println "Document could not be imported with error ${ret}."; | ||
| 83 | } else { | ||
| 84 | println "Document has been imported properly: [[${finalPageName}>>path:${finalPageURL}]]."; | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | def displayForm() { | ||
| 89 | println """ | ||
| 90 | {{html clean='false'}} | ||
| 91 | <form action="" enctype="multipart/form-data" method="post"> | ||
| 92 | <table border="0"> | ||
| 93 | <tr> | ||
| 94 | <td> | ||
| 95 | Page Name to save to:</td><td><input type="text" name="name" size="60" /> | ||
| 96 | </td> | ||
| 97 | </tr> | ||
| 98 | <tr> | ||
| 99 | <td> | ||
| 100 | Type: | ||
| 101 | </td> | ||
| 102 | <td> | ||
| 103 | <input type="radio" name="type" value="NOOVERVERSIONS" checked /> Do Not overwrite with versions | ||
| 104 | <input type="radio" name="type" value="NOOVERNOVERSIONS" /> Do Not overwrite without versions | ||
| 105 | <br /> | ||
| 106 | <input type="radio" name="type" value="ADDVERSION" /> Add version | ||
| 107 | <input type="radio" name="type" value="OVERVERSIONS" /> Overwrite with version | ||
| 108 | <input type="radio" name="type" value="OVERNOVERSIONS" /> Overwrite without versions | ||
| 109 | </td> | ||
| 110 | </tr> | ||
| 111 | <tr> | ||
| 112 | <td> | ||
| 113 | File: | ||
| 114 | </td> | ||
| 115 | <td> | ||
| 116 | <input type="file" name="file" size="60" /> | ||
| 117 | </td> | ||
| 118 | </tr> | ||
| 119 | <tr> | ||
| 120 | <td colspan="2" align="center"> | ||
| 121 | <input type="submit" name="Import" /> | ||
| 122 | </td> | ||
| 123 | </tr> | ||
| 124 | </table> | ||
| 125 | </form> | ||
| 126 | {{/html}} | ||
| 127 | """; | ||
| 128 | } | ||
| 129 | |||
| 130 | println 'This document allows you to import a single page XAR, with the possibility to change the name and the version of the new page.'; | ||
| 131 | |||
| 132 | if (request.get('name') != null) { | ||
| 133 | importDoc(); | ||
| 134 | } else { | ||
| 135 | displayForm(); | ||
| 136 | } | ||
| 137 | {{/groovy}} |