Export Selected Pages/Spaces

Last modified by Guido Kracke on 2023/06/15 13:57

cogSave selected top level spaces and user preferences to client or server machine.
Type
Category
Developed by

Guido Kracke

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

You have to adapt the variables exclude, include, backupFile and backupPath to your needs

{{groovy}}
import com.xpn.xwiki.api.Document
import com.xpn.xwiki.util.Util;
import com.xpn.xwiki.web.XWikiServletResponse
import org.xwiki.logging.LogLevel
import org.xwiki.job.Job
import java.text.DateFormat
import java.text.SimpleDateFormat
import org.xwiki.filter.instance.input.DocumentInstanceInputProperties
import org.xwiki.filter.type.FilterStreamType
import org.xwiki.model.reference.DocumentReference
import org.xwiki.model.reference.EntityReferenceSet
import org.xwiki.model.reference.SpaceReference
import org.xwiki.model.reference.WikiReference

DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss")
String now = df.format(new Date())

ArrayList<String> exclude = ["Help", "Menu", "XWiki", "Sandbox", "ScriptingDocumentation"]
ArrayList<String> include = ["XWiki.DefaultSkin"]
String backupFile = "Robo.${now}.xar"
String backupPath = "C:/Robo/backups/${backupFile}"

ArrayList<String> names = []
Document home = xwiki.getDocument("Main.WebHome")
for (name in home.getChildren()) {
    name = name.tokenize(".")[0]
   if (!exclude.contains(name)) {
        names << name
    }
}
names.sort().unique()
names += include
ArrayList<String> users = []
Document wiki = xwiki.getDocument("XWiki.XWikiUsers")
for (user in wiki.getChildren()) {
    users << user
}
users.sort()
names += users
String[] selectedNames = request.getParameterValues("pages")

StringBuffer out = new StringBuffer()
out << """
(
% class="row" %)
(((
(
% class="col-md-4" %)
(((
{{html}}
<form action="" id="backup" method="post">
  <div>
    <table>
      <tbody>
        <tr>
          <th>Pages</th>
          <td>
            <select name="pages" multiple size=${names.size()}>
"""

for (name in names) {
    out << "<option"
   if (selectedNames == null || selectedNames.contains(name)) {
        out << " selected"
    }
    out << ">" << name << "</option>\n"
}
out << """
            </select>
          </td>
        </tr>
        <tr>
          <th>Target</th>
          <td>
            <select name="target" size=1 onchange="setButtonText(this.value)">
"""

for (opt in ["Server", "Download"]) {
    out << "<option"
   if (request.target == opt) {
        out << " selected"
    }
    out << ">" << opt << "</option>\n"
}
out << """
            </select>
          </td>
        </tr>
        <tr>
          <th>History</th>
          <td>
            <select name="history" size=1>
"""

for (opt in ["Include", "Ignore"]) {
    out << "<option"
   if (request.history == opt) {
        out << " selected"
    }
    out << ">" << opt << "</option>\n"
}
out << """
            </select>
          </td>
        </tr>
      </tbody>
    </table>
    <span class="buttonwrapper"><input id="btn1" type="submit" value="Save Selected Pages" class="button"/></span>
  </div>
</form>
<script type="text/javascript">
function setButtonText(val) {
    if (val == "Server") {
        document.getElementById("btn1").value="Save Selected Pages"
    } else {
        document.getElementById("btn1").value="Download Selected Pages"
    }
}
</script>
{{/html}}

)))
(
% class="col-md-4" %)
(((

"""

String base = xcontext.URLFactory.getServerURL(xcontext.context)
for (name in names) {
    String url = "${base}/xwiki/bin/export/${name}/WebHome/?format=xar&name=${name}.${now}&pages=xwiki%3A${name}%25&history=true&backup=true"
    out << "\n[[Download ${name}>>${url}]]"
}
out << """
)))
)))


"""

if (selectedNames != null) {
    EntityReferenceSet entities = new EntityReferenceSet()
   for (name in selectedNames) {
       int index = name.lastIndexOf(".")
       if (index == -1) {
            entities.includes(new SpaceReference(name, new WikiReference("xwiki")))
        } else {
            String space = name.substring(0, index)
            String page = name.substring(index + 1)
            entities.includes(new DocumentReference("xwiki", space, page))
        }
    }
    DocumentInstanceInputProperties iProps = new DocumentInstanceInputProperties()
    iProps.setEntities(entities)
    iProps.setVerbose(true)
   if (request.history == "Include") {
        iProps.setWithJRCSRevisions(true)
        boolean attachmentJRCS = xwiki.getXWiki().ParamAsLong("xwiki.action.export.xar.attachment.jrcs", 1) != 0
        iProps.setWithWikiAttachmentsRevisions(!attachmentJRCS);
        iProps.setWithWikiAttachmentJRCSRevisions(attachmentJRCS);
    } else {
        iProps.setWithJRCSRevisions(false)
    }
    iProps.setWithRevisions(false)
    Map oProps = [
        packageBackupPack: Boolean.TRUE
    ]
   if (request.target == "Server") {
        oProps["target"] = new File(backupPath)
    } else {
        String filename = Util.encodeURI(backupFile, xcontext.getContext())
        XWikiServletResponse response = xcontext.getResponse()
        response.setContentType("application/zip")
        response.setHeader("Content-disposition", "attachment; filename=${filename}")
        oProps["target"] = response.getOutputStream()
    }
    Job job = services.filter.instance.startExport(FilterStreamType.XWIKI_XAR_CURRENT, oProps, iProps)
    job.join()
   for (event in job.getStatus().getLog().getLogsFrom(LogLevel.DEBUG)) {
        out << "{{{$event}}}" << "\n"
    }
}

print out.toString()
{{/groovy}}
Tags: export
     

Get Connected