use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultDocumentRestorerFromAttachedXAR method restoreDocumentFromAttachedXAR.
@Override
public void restoreDocumentFromAttachedXAR(DocumentReference docReference, String attachmentName, List<DocumentReference> documentsToRestore) throws XWikiException {
XWikiContext xcontext = xcontextProvider.get();
XWiki xwiki = xcontext.getWiki();
File tempZipFile = null;
try {
tempZipFile = getTemporaryZipFile(docReference, attachmentName);
if (tempZipFile == null) {
return;
}
ZipFile zipFile = new ZipFile(tempZipFile);
// We look for each document to restore if there is a corresponding zipEntry.
Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
while (itDocumentsToRestore.hasNext()) {
DocumentReference docRef = itDocumentsToRestore.next();
// Compute what should be the filename of the document to restore
String fileNameToRestore = String.format("%s/%s.xml", docRef.getLastSpaceReference().getName(), docRef.getName());
// Get the corresponding zip Entry
ZipArchiveEntry zipEntry = zipFile.getEntry(fileNameToRestore);
if (zipEntry != null) {
// Restore the document
XWikiDocument docToRestore = xwiki.getDocument(docRef, xcontext);
docToRestore.fromXML(zipFile.getInputStream(zipEntry));
xwiki.saveDocument(docToRestore, xcontext);
// We have restored this document
itDocumentsToRestore.remove();
}
}
zipFile.close();
} catch (IOException e) {
logger.error("Error during the decompression of [{}].", attachmentName, e);
} finally {
// Delete the temporary zip file
if (tempZipFile != null) {
tempZipFile.delete();
}
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultSearchSuggestCustomConfigDeleter method deleteSearchSuggestCustomConfig.
@Override
public void deleteSearchSuggestCustomConfig(String wikiId) throws XWikiException {
XWikiContext xcontext = xcontextProvider.get();
XWiki xwiki = xcontext.getWiki();
DocumentReference searchConfigDocRef = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "SearchSuggestConfig");
DocumentReference searchConfigClass = new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "SearchSuggestSourceClass");
XWikiDocument searchConfigDoc = xwiki.getDocument(searchConfigDocRef, xcontext);
// Get the config objects
List<BaseObject> objects = searchConfigDoc.getXObjects(searchConfigClass);
if (objects != null) {
boolean found = false;
// Find the object to remove
for (BaseObject object : objects) {
if (object == null) {
continue;
}
// Look if the object is to remove
String name = object.getStringValue("name");
if (name.equals("platform.workspace.searchSuggestSourceWorkspaces")) {
String query = object.getStringValue("query");
String engine = object.getStringValue("engine");
String url = object.getStringValue("url");
if (isSolrObject(query, engine, url) || isLuceneObject(query, engine, url)) {
searchConfigDoc.removeXObject(object);
found = true;
}
}
}
if (found) {
xwiki.saveDocument(searchConfigDoc, "Remove object previously introduced by WorkspaceManager.Install", xcontext);
}
}
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class WorkspacesMigration method isWorkspace.
private boolean isWorkspace(String wikiId) throws DataMigrationException, XWikiException {
// The main wiki is not a workspace
if (wikiId.equals(wikiDescriptorManager.getMainWikiId())) {
return false;
}
// Context, XWiki
XWikiContext context = getXWikiContext();
XWiki xwiki = context.getWiki();
// Get the old wiki descriptor
DocumentReference oldWikiDescriptorReference = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, String.format("XWikiServer%s", StringUtils.capitalize(wikiId)));
XWikiDocument oldWikiDescriptor = xwiki.getDocument(oldWikiDescriptorReference, context);
// Try to get the old workspace object
DocumentReference oldClassDocument = new DocumentReference(wikiDescriptorManager.getMainWikiId(), WORKSPACE_CLASS_SPACE, WORKSPACE_CLASS_PAGE);
BaseObject oldObject = oldWikiDescriptor.getXObject(oldClassDocument);
return (oldObject != null) || isWorkspaceTemplate(wikiId);
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultDocumentRestorerFromAttachedXARTest method restoreDocumentFromAttachedXAR.
@Test
public void restoreDocumentFromAttachedXAR() throws Exception {
// Mocks
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
when(xwiki.getDocument(eq(new DocumentReference("mainWiki", "WorkspaceManager", "Install")), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
XWikiAttachment xeXar = mock(XWikiAttachment.class);
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(getClass().getResourceAsStream("/test-restore-documents.xar"));
// Run
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
// Verify the document to restore has been restored from the xar
verify(docToRestore1).fromXML(any(InputStream.class));
verify(xwiki, times(1)).saveDocument(docToRestore1, xcontext);
verify(docToRestore2).fromXML(any(InputStream.class));
verify(xwiki, times(1)).saveDocument(docToRestore2, xcontext);
}
use of com.xpn.xwiki.doc.XWikiDocument in project xwiki-platform by xwiki.
the class DefaultDocumentRestorerFromAttachedXARTest method errorZipInvalid.
@Test
public void errorZipInvalid() throws Exception {
// Mocks
XWikiDocument workspaceInstallDoc = mock(XWikiDocument.class);
DocumentReference workspaceInstallDocRef = new DocumentReference("mainWiki", "WorkspaceManager", "Install");
when(xwiki.getDocument(eq(workspaceInstallDocRef), any(XWikiContext.class))).thenReturn(workspaceInstallDoc);
XWikiAttachment xeXar = mock(XWikiAttachment.class);
when(workspaceInstallDoc.getAttachment("workspace-template.xar")).thenReturn(xeXar);
when(xeXar.getContentInputStream(any(XWikiContext.class))).thenReturn(getClass().getResourceAsStream("/invalid-xar.xar"));
// Run
mocker.getComponentUnderTest().restoreDocumentFromAttachedXAR(new DocumentReference("mainWiki", "WorkspaceManager", "Install"), "workspace-template.xar", docsToRestore);
// Verify
verify(mocker.getMockedLogger()).error(eq("Error during the decompression of [{}]."), eq("workspace-template.xar"), any(IOException.class));
}
Aggregations