Wiki source code of Migration
Last modified by Guillaume Fenollar on 2009/02/20 11:34
Show last authors
author | version | line-number | content |
---|---|---|---|
1 | 1.1 Import 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 | |||
12 | finalPageName = "" | ||
13 | exception = "" | ||
14 | |||
15 | def updateDoc(String newname, String xml, String type, XWikiContext context) { | ||
16 | try { | ||
17 | def fdoc = new XWikiDocument() | ||
18 | fdoc.fromXML(xml); | ||
19 | if (newname=="") | ||
20 | newname = fdoc.getFullName() | ||
21 | def prevDoc = context.getWiki().getDocument(newname, context); | ||
22 | def prevArchive = prevDoc.getDocumentArchive(); | ||
23 | if (prevDoc.isNew()||((type!="NOOVERVERSIONS")&&(type!="NOOVERNOVERSIONS"))) { | ||
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 | |||
31 | if (type=="ADDVERSION") { | ||
32 | fdoc.setDocumentArchive(prevArchive); | ||
33 | context.getWiki().saveDocument(fdoc, context); | ||
34 | if (prevDoc.isNew()) { | ||
35 | fdoc.resetArchive(context); | ||
36 | } | ||
37 | return 1; | ||
38 | } else { | ||
39 | def pack = new Package() | ||
40 | if ((type=="NOOVERVERSIONS")||(type=="OVERVERSIONS")) | ||
41 | pack.setWithVersions(true) | ||
42 | else | ||
43 | pack.setWithVersions(false) | ||
44 | pack.setBackupPack(true) | ||
45 | pack.add(fdoc, 0, context) | ||
46 | return pack.install(context) | ||
47 | } | ||
48 | } else { | ||
49 | return -1000; | ||
50 | } | ||
51 | } catch (Throwable e) { | ||
52 | exception = e.getMessage() | ||
53 | return -2000; | ||
54 | } | ||
55 | } | ||
56 | |||
57 | if (request.get("filename")!=null) { | ||
58 | %> | ||
59 | <script type="text/javascript" src="$xwiki.getSkinFile("js/xwiki/importer/import.js", true)"></script> | ||
60 | <form action="" method="post"> | ||
61 | <input type="hidden" name="filename2" value="${request.filename}" /> | ||
62 | <span id="selectDocsActions"> | ||
63 | <a href="javascript:void()" onclick="selectItems(’selCheckedDoc’, false); return false;" class="Exportlink">Unselect All</a>, | ||
64 | <a href="javascript:void()" onclick="selectItems(’selCheckedDoc’, true); return false;" class="Exportlink">Select All</a> | ||
65 | </span> | ||
66 | <table border="0" cellspacing="0" cellpadding="0"> | ||
67 | <% | ||
68 | def file = new File(request.get("filename")); | ||
69 | def zipfile = new ZipFile(file); | ||
70 | def counter = 0; | ||
71 | def time1 = new Date() | ||
72 | for(zipentry in zipfile.entries()) { | ||
73 | filename = zipentry.name | ||
74 | if (!filename.equals("package.xml")) { | ||
75 | counter++; | ||
76 | %> | ||
77 | <tr><td><input class="selCheckedDoc" type="checkbox" name="file_${counter}" value="${filename}" checked />${filename}</td></tr> | ||
78 | <% | ||
79 | } | ||
80 | } | ||
81 | |||
82 | def time2 = new Date() | ||
83 | |||
84 | %> | ||
85 | <td>Type:</td><td> | ||
86 | <input type="radio" name="type" value="NOOVERVERSIONS" checked /> Do Not overwrite with versions | ||
87 | <input type="radio" name="type" value="NOOVERNOVERSIONS" /> Do Not overwrite without versions | ||
88 | <br /> | ||
89 | <input type="radio" name="type" value="ADDVERSION" /> Add version | ||
90 | <input type="radio" name="type" value="OVERVERSIONS" /> Overwrite with version | ||
91 | <input type="radio" name="type" value="OVERNOVERSIONS" /> Overwrite without versions </td> | ||
92 | </tr> | ||
93 | <tr> | ||
94 | <td colspan="2" align="center"> | ||
95 | <input type="hidden" name="counter" value="${counter}" /> | ||
96 | <input type="submit" name="Preview" /> | ||
97 | </td> | ||
98 | </tr> | ||
99 | </table> | ||
100 | </form> | ||
101 | <% | ||
102 | |||
103 | def dur = time2.getTime() - time1.getTime() | ||
104 | println "* Duration: $dur" | ||
105 | } else if (request.get("filename2")!=null) { | ||
106 | |||
107 | def file = new File(request.get("filename2")); | ||
108 | def zipfile = new ZipFile(file); | ||
109 | def maxcounter = Integer.parseInt(request.counter) | ||
110 | def counter = 1; | ||
111 | def total = 0; | ||
112 | while (counter<=maxcounter) { | ||
113 | def filename = request.get("file_${counter}") | ||
114 | if (filename!=null) { | ||
115 | total++; | ||
116 | def zipentry = zipfile.getEntry(filename) | ||
117 | if (zipentry==null) { | ||
118 | println "* Error while getting file named ${filename}" | ||
119 | } else { | ||
120 | |||
121 | def is = zipfile.getInputStream(zipentry) | ||
122 | def xml = new String(Util.getFileContentAsBytes(is)) | ||
123 | def type = request.get("type"); | ||
124 | def ret = updateDoc("", xml, type, context.context); | ||
125 | |||
126 | if (ret < 0) { | ||
127 | if (ret==-1000) | ||
128 | println "* Document for filename ${filename} already exists and overwrite was not specified."; | ||
129 | else if (ret==-2000) | ||
130 | println "* Document for filename ${filename} could not be imported with exception (${exception})"; | ||
131 | else | ||
132 | println "* Document for filename ${filename} could not be imported with error ${ret}."; | ||
133 | } else { | ||
134 | println "* Document for filename ${filename} has been imported properly: [${finalPageName}]." | ||
135 | } | ||
136 | } | ||
137 | } | ||
138 | counter++; | ||
139 | } | ||
140 | println "* Finished importing ${total} documents" | ||
141 | } else { | ||
142 | %> | ||
143 | <form action="" method="post"> | ||
144 | <table border="0"> | ||
145 | <tr> | ||
146 | <td>File to read from:</td><td><input type="text" name="filename" size="60" /></td> | ||
147 | </tr> | ||
148 | <tr> | ||
149 | <td colspan="2" align="center"> | ||
150 | <input type="submit" name="Preview" /> | ||
151 | </td> | ||
152 | </tr> | ||
153 | </table> | ||
154 | </form> | ||
155 | <% | ||
156 | } | ||
157 | %> | ||
158 | |||
159 | |||
160 | |||
161 | ##<% | ||
162 | ##if (request.migrate == "1") { | ||
163 | ##def f = new File("/var/tmp/btsbatiment2/") | ||
164 | ##println "START" | ||
165 | ##println "" + xwiki.getPlugin("package") | ||
166 | ##pkg = xwiki.getPlugin("package").getPackage() | ||
167 | ##pkg.readFromDir(f, context.context) | ||
168 | ##pkg.install(context.context) | ||
169 | ##} | ||
170 | #%> |