Export a list of wiki pages as a XAR on the file system
Last modified by Mohamed Boussaa on 2026/06/02 17:54
| A groovy script which allows to export in a XAR file a list of wiki pages on the file system. |
| Type | Snippet |
| Category | Other |
| Developed by | |
| Rating | |
| License | GNU Lesser General Public License 2.1 |
Table of contents
Description
In a wiki page, copy-paste the following snippet.
Make sure to adapt what should be adapted (Pages to be exported, target location in the file system, in the snippet the XAR file is created in the XWiki temporary directory).
Note that this code is an adaptation of the code of the com.xpn.xwiki.web.ExportAction#exportXAR() method used by the standard xar export feature.
{{groovy}}
import java.io.File;
import java.lang.reflect.Type;
import org.xwiki.environment.Environment;
import org.xwiki.filter.input.InputFilterStream;
import org.xwiki.filter.input.InputFilterStreamFactory;
import org.xwiki.filter.instance.input.DocumentInstanceInputProperties;
import org.xwiki.filter.output.BeanOutputFilterStreamFactory;
import org.xwiki.filter.output.DefaultOutputStreamOutputTarget;
import org.xwiki.filter.output.OutputFilterStream;
import org.xwiki.filter.output.OutputFilterStreamFactory;
import org.xwiki.filter.type.FilterStreamType;
import org.xwiki.filter.xar.output.XAROutputProperties;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.EntityReferenceSet;
import com.xpn.xwiki.web.Utils;
// List of pages to be exported
List<String> pages = ['Sandbox.TestPage1', 'Sandbox.TestPage2', 'Sandbox.TestPage3'];
List<DocumentReference> pageReferences = [];
for (def page : pages) {
pageReferences.add(services.model.resolveDocument(page));
}
// Export configuration
boolean history = true;// Export pages with history
// Create input wiki stream
DocumentInstanceInputProperties inputProperties = new DocumentInstanceInputProperties();
inputProperties.setVerbose(false);// We don't want to log the details
inputProperties.setWithJRCSRevisions(history);
inputProperties.setWithRevisions(false);
EntityReferenceSet entities = new EntityReferenceSet();
for (DocumentReference documentReference : pageReferences) {
entities.includes(documentReference);
}
inputProperties.setEntities(entities);
InputFilterStreamFactory inputFilterStreamFactory =
Utils.getComponent(InputFilterStreamFactory.class, FilterStreamType.XWIKI_INSTANCE.serialize());
InputFilterStream inputFilterStream = inputFilterStreamFactory.createInputFilterStream(inputProperties);
// Create output wiki stream
XAROutputProperties xarProperties = new XAROutputProperties();
xarProperties.setVerbose(false);// We don't want to log the details
// Prepare the xar file on the file system
String fileName = "pages-export.xar";
File parentFolder = services.component.getInstance((Type) Environment.class).getTemporaryDirectory();// XWiki temporary directory
File file = new File(parentFolder, fileName);
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fop = new FileOutputStream(file);
xarProperties.setTarget(new DefaultOutputStreamOutputTarget(fop));
xarProperties.setPreserveVersion(history);
BeanOutputFilterStreamFactory<XAROutputProperties> xarFilterStreamFactory = Utils.getComponent((Type) OutputFilterStreamFactory.class, FilterStreamType.XWIKI_XAR_CURRENT.serialize());
OutputFilterStream outputFilterStream = xarFilterStreamFactory.createOutputFilterStream(xarProperties);
// Export
inputFilterStream.read(outputFilterStream.getFilter());
inputFilterStream.close();
outputFilterStream.close();
println("The pages have been exported as XAR file on the file system :");
println(file);
{{/groovy}}Prerequisites & Installation Instructions
Requires programming rights.