Search in sources :

Example 76 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 77 with XWikiContext

use of com.xpn.xwiki.XWikiContext 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 78 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class WorkspacesMigration method restoreDeletedDocuments.

/**
 * The WorkspaceManager.Install script has removed some pages, that we need to restore.
 * - XWiki.AdminRegistrationSheet
 * - XWiki.RegistrationConfig
 * - XWiki.RegistrationHelp
 * - XWiki.AdminUsersSheet
 *
 * @param wikiId id of the wiki to upgrade
 */
private void restoreDeletedDocuments(String wikiId) {
    XWikiContext xcontext = getXWikiContext();
    XWiki xwiki = xcontext.getWiki();
    // Create the list of documents to restore
    List<DocumentReference> documentsToRestore = new LinkedList<DocumentReference>();
    documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminRegistrationSheet"));
    documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationConfig"));
    documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "RegistrationHelp"));
    documentsToRestore.add(new DocumentReference(wikiId, XWiki.SYSTEM_SPACE, "AdminUsersSheet"));
    // Remove from the list the document that already exists (so we don't need to restore them)
    Iterator<DocumentReference> itDocumentsToRestore = documentsToRestore.iterator();
    while (itDocumentsToRestore.hasNext()) {
        DocumentReference docRef = itDocumentsToRestore.next();
        if (xwiki.exists(docRef, xcontext)) {
            itDocumentsToRestore.remove();
        }
    }
    // If the list is empty, there is nothing to do
    if (documentsToRestore.isEmpty()) {
        return;
    }
    // Try to restore from the workspace-template.xar
    restoreDocumentsFromWorkspaceXar(documentsToRestore);
    // If the list is empty, the job is done
    if (documentsToRestore.isEmpty()) {
        return;
    }
    // Try to copy these documents from the main wiki
    restoreDocumentFromMainWiki(documentsToRestore);
    // If the list is empty, the job is done
    if (!documentsToRestore.isEmpty()) {
        String documentsToRestoreAsString = new String();
        int counter = 0;
        for (DocumentReference d : documentsToRestore) {
            if (counter++ > 0) {
                documentsToRestoreAsString += ", ";
            }
            documentsToRestoreAsString += d;
        }
        logger.warn("Failed to restore some documents: [{}]. You should import manually " + "(1) xwiki-platform-administration-ui.xar and then (2) xwiki-platform-wiki-ui-wiki.xar into your" + " wiki, to restore these documents.", documentsToRestoreAsString);
    }
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DocumentReference(org.xwiki.model.reference.DocumentReference) LinkedList(java.util.LinkedList)

Example 79 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class WikiManagerScriptService method checkProgrammingRights.

// TODO: move to new API a soon as a proper helper is provided
private void checkProgrammingRights() throws AuthorizationException {
    XWikiContext xcontext = this.xcontextProvider.get();
    authorizationManager.checkAccess(Right.PROGRAM, xcontext.getDoc().getAuthorReference(), xcontext.getDoc().getDocumentReference());
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext)

Example 80 with XWikiContext

use of com.xpn.xwiki.XWikiContext in project xwiki-platform by xwiki.

the class WikiManagerScriptService method deleteWiki.

/**
 * Delete the specified wiki.
 *
 * @param wikiId unique identifier of the wiki to delete
 * @return true if the wiki has been successfully deleted
 */
public boolean deleteWiki(String wikiId) {
    // Test if the script has the programming right
    XWikiContext context = xcontextProvider.get();
    try {
        // Check if the current script has the programming rights
        checkProgrammingRights();
        // Test right
        if (!canDeleteWiki(entityReferenceSerializer.serialize(context.getUserReference()), wikiId)) {
            throw new AuthorizationException("You don't have the right to delete the wiki");
        }
        // Delete the wiki
        wikiManager.delete(wikiId);
        // Return success
        return true;
    } catch (Exception e) {
        error(String.format("Failed to delete wiki [%s]", wikiId), e);
    }
    return false;
}
Also used : AuthorizationException(org.xwiki.security.authorization.AuthorizationException) XWikiContext(com.xpn.xwiki.XWikiContext) AuthorizationException(org.xwiki.security.authorization.AuthorizationException) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) AccessDeniedException(org.xwiki.security.authorization.AccessDeniedException)

Aggregations

XWikiContext (com.xpn.xwiki.XWikiContext)564 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)203 XWikiException (com.xpn.xwiki.XWikiException)195 DocumentReference (org.xwiki.model.reference.DocumentReference)150 XWiki (com.xpn.xwiki.XWiki)106 BaseObject (com.xpn.xwiki.objects.BaseObject)104 Test (org.junit.Test)64 ArrayList (java.util.ArrayList)55 ComponentLookupException (org.xwiki.component.manager.ComponentLookupException)43 ExecutionContext (org.xwiki.context.ExecutionContext)43 Session (org.hibernate.Session)37 InitializationException (org.xwiki.component.phase.InitializationException)36 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)34 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)33 WikiReference (org.xwiki.model.reference.WikiReference)31 Before (org.junit.Before)29 EntityReference (org.xwiki.model.reference.EntityReference)28 QueryException (org.xwiki.query.QueryException)28 Execution (org.xwiki.context.Execution)27 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)24