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);
}
}
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);
}
}
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);
}
}
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;
}
}
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;
}
Aggregations