Search in sources :

Example 81 with XWikiContext

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

the class TemplateWikiProvisioningJob method runInternal.

@Override
protected void runInternal() throws Exception {
    WikiProvisioningJobRequest request = getRequest();
    if (!(request.getProvisioningJobParameter() instanceof String)) {
        throw new Exception("The provisioning parameter is not a valid String.");
    }
    XWikiContext xcontext = xcontextProvider.get();
    String wikiId = request.getWikiId();
    String templateId = (String) request.getProvisioningJobParameter();
    // Set the user actually doing the action in the context
    xcontext.setUserReference(request.getProvisioningUser());
    try {
        observationManager.notify(new WikiProvisioningEvent(wikiId), wikiId, xcontext);
        wikiCopier.copyDocuments(templateId, wikiId, false);
        observationManager.notify(new WikiProvisionedEvent(wikiId), wikiId, xcontext);
        observationManager.notify(new WikiCopiedEvent(templateId, wikiId), templateId, xcontext);
    } catch (WikiManagerException e) {
        logger.error("Failed to provision wiki [{}] from template [{}].", wikiId, templateId, e);
        observationManager.notify(new WikiProvisioningFailedEvent(wikiId), wikiId, xcontext);
    }
}
Also used : WikiProvisioningFailedEvent(org.xwiki.bridge.event.WikiProvisioningFailedEvent) WikiProvisionedEvent(org.xwiki.bridge.event.WikiProvisionedEvent) WikiCopiedEvent(org.xwiki.bridge.event.WikiCopiedEvent) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) WikiProvisioningEvent(org.xwiki.bridge.event.WikiProvisioningEvent) WikiProvisioningJobRequest(org.xwiki.wiki.provisioning.WikiProvisioningJobRequest) XWikiContext(com.xpn.xwiki.XWikiContext) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException)

Example 82 with XWikiContext

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

the class WikiTemplatePropertyGroupProvider method save.

@Override
public void save(WikiPropertyGroup group, String wikiId) throws WikiPropertyGroupException {
    XWikiContext context = xcontextProvider.get();
    XWiki xwiki = context.getWiki();
    WikiTemplatePropertyGroup templateGroup = (WikiTemplatePropertyGroup) group;
    try {
        XWikiDocument descriptorDocument = wikiDescriptorDocumentHelper.getDocumentFromWikiId(wikiId);
        BaseObject object = descriptorDocument.getXObject(WikiTemplateClassDocumentInitializer.SERVER_CLASS, true, context);
        object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, templateGroup.isTemplate() ? 1 : 0);
        // The document must have a creator
        if (descriptorDocument.getCreatorReference() == null) {
            descriptorDocument.setCreatorReference(context.getUserReference());
        }
        // The document must have an author
        if (descriptorDocument.getAuthorReference() == null) {
            descriptorDocument.setAuthorReference(context.getUserReference());
        }
        xwiki.saveDocument(descriptorDocument, String.format("Changed property group [%s].", GROUP_NAME), context);
    } catch (WikiManagerException e) {
        throw new WikiPropertyGroupException(String.format(ERROR_MESSAGE_NO_DESCRIPTOR_DOCUMENT, wikiId), e);
    } catch (XWikiException e) {
        throw new WikiPropertyGroupException("Unable to save descriptor document.", e);
    }
}
Also used : WikiTemplatePropertyGroup(org.xwiki.wiki.template.WikiTemplatePropertyGroup) WikiPropertyGroupException(org.xwiki.wiki.properties.WikiPropertyGroupException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) WikiManagerException(org.xwiki.wiki.manager.WikiManagerException) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 83 with XWikiContext

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

the class WikiTemplateMigration method hibernateMigrate.

@Override
protected void hibernateMigrate() throws DataMigrationException, XWikiException {
    // XWiki objects
    XWikiContext context = getXWikiContext();
    XWiki xwiki = context.getWiki();
    // WikiManager.WikiTemplateClass reference
    DocumentReference templateClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), WikiTemplateClassDocumentInitializer.DOCUMENT_SPACE, WikiTemplateClassDocumentInitializer.DOCUMENT_NAME);
    // XWiki.XWikiServerClass reference
    DocumentReference descriptorClassReference = new DocumentReference(wikiDescriptorManager.getCurrentWikiId(), XWiki.SYSTEM_SPACE, "XWikiServerClass");
    // Superadmin reference
    DocumentReference superAdmin = new DocumentReference(wikiDescriptorManager.getMainWikiId(), XWiki.SYSTEM_SPACE, "superadmin");
    try {
        // Get all the descriptor documents
        String statement = "select distinct doc.fullName " + "from Document doc, doc.object(XWiki.XWikiServerClass) as obj";
        Query query = queryManager.createQuery(statement, Query.XWQL);
        List<String> results = query.execute();
        for (String wikiPage : results) {
            XWikiDocument document = xwiki.getDocument(documentReferenceResolver.resolve(wikiPage), context);
            // Get the "iswikitemplate" value
            BaseObject descriptorObject = document.getXObject(descriptorClassReference);
            int isTemplate = descriptorObject.getIntValue(OLD_TEMPLATE_PROPERTY, 0);
            // We remove the deprecated property from the descriptor
            descriptorObject.removeField(OLD_TEMPLATE_PROPERTY);
            // Add the new WikiManager.WikiTemplateClass object
            BaseObject object = document.getXObject(templateClassReference, true, context);
            // The new object might already exists and have a template property already set
            isTemplate = object.getIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // Set the (new) value
            object.setIntValue(WikiTemplateClassDocumentInitializer.FIELD_ISWIKITEMPLATE, isTemplate);
            // The document must have an author
            document.setAuthorReference(superAdmin);
            // Save the document
            xwiki.saveDocument(document, "[UPGRADE] Upgrade the template section.", context);
        }
    } catch (QueryException e) {
        throw new DataMigrationException("Failed to get the list of all existing descriptors.", e);
    } catch (XWikiException e) {
        throw new DataMigrationException("Failed to upgrade a wiki descriptor.", e);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) QueryException(org.xwiki.query.QueryException) Query(org.xwiki.query.Query) XWikiContext(com.xpn.xwiki.XWikiContext) XWiki(com.xpn.xwiki.XWiki) DataMigrationException(com.xpn.xwiki.store.migration.DataMigrationException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 84 with XWikiContext

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

the class WysiwygEditorScriptService method render.

/**
 * Produces the input for the editor by rendering the specified content template as a full HTML page, making sure
 * the skin extension hooks are resolved. The template is rendered in the context of the current document and the
 * Velocity context is not isolated so you can put the data needed by the template in the Velocity context before
 * calling this method. The advantage of using this method to obtain the editor input is that the editor doesn't
 * have to make an additional HTTP request for the content template.
 *
 * @param templateReference specifies the document that serves as the template for the editor content
 * @return the result of rendering the specified content template
 */
public String render(DocumentReference templateReference) {
    if (!this.authorization.hasAccess(Right.VIEW, templateReference)) {
        return null;
    }
    XWikiContext xcontext = this.xcontextProvider.get();
    try {
        XWikiDocument template = xcontext.getWiki().getDocument(templateReference, xcontext);
        String templateSyntax = template.getSyntax().toIdString();
        String output = xcontext.getDoc().getRenderedContent(template.getContent(), templateSyntax, xcontext);
        // Make sure the skin extension hooks are properly replaced with style sheets includes.
        return xcontext.getWiki().getPluginManager().endParsing(output, xcontext);
    } catch (XWikiException e) {
        this.logger.debug("Failed to render [{}].", templateReference, e);
        return null;
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 85 with XWikiContext

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

the class WysiwygEditorScriptService method createSecurityDocument.

/**
 * When the user switches to the Source tab he'll be able to make modifications and when he switches back to the
 * WYSIWYG tab his changes will be rendered. If the document had PR, then we need to be sure that if the user
 * doesn't have PR he won't be able to execute the code. We do this by setting as security document a clone of the
 * current document that has the current user as content author (because the content author is used to check PR).
 */
private XWikiDocument createSecurityDocument() {
    XWikiContext xwikiContext = this.xcontextProvider.get();
    // We clone the document in order to not impact the environment (the document cache for example).
    XWikiDocument clonedDocument = xwikiContext.getDoc().clone();
    clonedDocument.setContentAuthorReference(xwikiContext.getUserReference());
    return clonedDocument;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiContext(com.xpn.xwiki.XWikiContext)

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