Search in sources :

Example 91 with XWikiDocument

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();
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) ZipArchiveEntry(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) IOException(java.io.IOException) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 92 with XWikiDocument

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);
        }
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 93 with XWikiDocument

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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 94 with XWikiDocument

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);
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) InputStream(java.io.InputStream) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Example 95 with XWikiDocument

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));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) DocumentReference(org.xwiki.model.reference.DocumentReference) Test(org.junit.Test)

Aggregations

XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)869 DocumentReference (org.xwiki.model.reference.DocumentReference)469 BaseObject (com.xpn.xwiki.objects.BaseObject)318 Test (org.junit.Test)284 XWikiContext (com.xpn.xwiki.XWikiContext)232 XWikiException (com.xpn.xwiki.XWikiException)178 ArrayList (java.util.ArrayList)99 XWiki (com.xpn.xwiki.XWiki)97 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)86 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)71 Document (com.xpn.xwiki.api.Document)48 EntityReference (org.xwiki.model.reference.EntityReference)48 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)41 Date (java.util.Date)41 IOException (java.io.IOException)40 HashMap (java.util.HashMap)33 QueryException (org.xwiki.query.QueryException)27 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)25 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)23 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)23