Search in sources :

Example 1 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class DeleteVersionsAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    DeleteVersionsForm form = (DeleteVersionsForm) context.getForm();
    if (!form.isConfirmed() || !csrfTokenCheck(context)) {
        return true;
    }
    XWikiDocument doc = context.getDoc();
    String language = form.getLanguage();
    XWikiDocument tdoc = doc.getTranslatedDocument(language, context);
    XWikiDocumentArchive archive = tdoc.getDocumentArchive(context);
    // Get the versions
    Version[] versions = getVersionsFromForm(form, archive);
    Version v1 = versions[0];
    Version v2 = versions[1];
    if (v1 != null && v2 != null) {
        // Remove the versions
        archive.removeVersions(v1, v2, context);
        context.getWiki().getVersioningStore().saveXWikiDocArchive(archive, true, context);
        tdoc.setDocumentArchive(archive);
        // Is this the last remaining version? If so, then recycle the document.
        if (archive.getLatestVersion() == null) {
            // Wrap the work as a batch operation.
            BatchOperationExecutor batchOperationExecutor = Utils.getComponent(BatchOperationExecutor.class);
            batchOperationExecutor.execute(() -> {
                if (StringUtils.isEmpty(language) || language.equals(doc.getDefaultLanguage())) {
                    context.getWiki().deleteAllDocuments(doc, context);
                } else {
                    // Only delete the translation
                    context.getWiki().deleteDocument(tdoc, context);
                }
            });
        } else {
            // If we delete the most recent (current) version, then rollback to latest undeleted version.
            if (!tdoc.getRCSVersion().equals(archive.getLatestVersion())) {
                XWikiDocument newdoc = archive.loadDocument(archive.getLatestVersion(), context);
                // Reset the document reference, since the one taken from the archive might be wrong (old name from
                // before a rename)
                newdoc.setDocumentReference(tdoc.getDocumentReference());
                // Get rid of objects that don't exist in new version
                newdoc.addXObjectsToRemoveFromVersion(tdoc);
                // Make sure we don't create a new rev!
                newdoc.setMetaDataDirty(false);
                newdoc.setContentDirty(false);
                // Make sure the previous current document is seen as original document of
                // the new current document for comparisons
                newdoc.setOriginalDocument(tdoc.getOriginalDocument());
                // Update the database with what is now the current document
                context.getWiki().saveDocument(newdoc, newdoc.getComment(), context);
                context.setDoc(newdoc);
            }
        }
    }
    sendRedirect(context);
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) Version(org.suigeneris.jrcs.rcs.Version) BatchOperationExecutor(org.xwiki.refactoring.batch.BatchOperationExecutor)

Example 2 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWikiHibernateVersioningStore method getXWikiDocVersions.

@Override
public Version[] getXWikiDocVersions(XWikiDocument doc, XWikiContext context) throws XWikiException {
    try {
        XWikiDocumentArchive archive = getXWikiDocumentArchive(doc, context);
        if (archive == null) {
            return new Version[0];
        }
        Collection<XWikiRCSNodeInfo> nodes = archive.getNodes();
        Version[] versions = new Version[nodes.size()];
        Iterator<XWikiRCSNodeInfo> it = nodes.iterator();
        for (int i = 0; i < versions.length; i++) {
            XWikiRCSNodeInfo node = it.next();
            versions[versions.length - 1 - i] = node.getId().getVersion();
        }
        return versions;
    } catch (Exception e) {
        Object[] args = { doc.getFullName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_READING_REVISIONS, "Exception while reading document {0} revisions", e, args);
    }
}
Also used : XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) Version(org.suigeneris.jrcs.rcs.Version) XWikiRCSNodeInfo(com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo) XWikiException(com.xpn.xwiki.XWikiException) HibernateException(org.hibernate.HibernateException) XWikiException(com.xpn.xwiki.XWikiException)

Example 3 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWikiHibernateVersioningStore method loadXWikiDoc.

@Override
public XWikiDocument loadXWikiDoc(XWikiDocument basedoc, String sversion, XWikiContext inputxcontext) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        XWikiDocumentArchive archive = getXWikiDocumentArchive(basedoc, context);
        Version version = new Version(sversion);
        XWikiDocument doc = archive.loadDocument(version, context);
        if (doc == null) {
            Object[] args = { basedoc.getDocumentReferenceWithLocale(), version.toString() };
            throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_UNEXISTANT_VERSION, "Version {1} does not exist while reading document {0}", null, args);
        }
        // Make sure the document has the same name
        // as the new document (in case there was a name change
        // FIXME: is this really needed ?
        doc.setDocumentReference(basedoc.getDocumentReference());
        doc.setStore(basedoc.getStore());
        return doc;
    } finally {
        restoreExecutionXContext();
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) Version(org.suigeneris.jrcs.rcs.Version) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException)

Example 4 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class XWikiHibernateVersioningStore method updateXWikiDocArchive.

@Override
public void updateXWikiDocArchive(XWikiDocument doc, boolean bTransaction, XWikiContext inputxcontext) throws XWikiException {
    XWikiContext context = getExecutionXContext(inputxcontext, true);
    try {
        XWikiDocumentArchive archiveDoc = getXWikiDocumentArchive(doc, context);
        archiveDoc.updateArchive(doc, doc.getAuthor(), doc.getDate(), doc.getComment(), doc.getRCSVersion(), context);
        doc.setRCSVersion(archiveDoc.getLatestVersion());
        saveXWikiDocArchive(archiveDoc, bTransaction, context);
    } catch (Exception e) {
        Object[] args = { doc.getFullName() };
        throw new XWikiException(XWikiException.MODULE_XWIKI_STORE, XWikiException.ERROR_XWIKI_STORE_HIBERNATE_SAVING_OBJECT, "Exception while updating archive {0}", e, args);
    } finally {
        restoreExecutionXContext();
    }
}
Also used : XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiException(com.xpn.xwiki.XWikiException) HibernateException(org.hibernate.HibernateException) XWikiException(com.xpn.xwiki.XWikiException)

Example 5 with XWikiDocumentArchive

use of com.xpn.xwiki.doc.XWikiDocumentArchive in project xwiki-platform by xwiki.

the class RollbackAction method action.

@Override
public boolean action(XWikiContext context) throws XWikiException {
    // CSRF prevention
    if (!csrfTokenCheck(context)) {
        return false;
    }
    RollbackForm form = (RollbackForm) context.getForm();
    if (!"1".equals(form.getConfirm())) {
        return true;
    }
    XWiki xwiki = context.getWiki();
    XWikiResponse response = context.getResponse();
    XWikiDocument doc = context.getDoc();
    String rev = form.getRev();
    String language = form.getLanguage();
    // We don't clone the document here because the rollback method does it before making modifications.
    XWikiDocument tdoc = getTranslatedDocument(doc, language, context);
    // Support for the "previous" pseudoversions.
    if ("previous".equals(rev)) {
        XWikiDocumentArchive archive = tdoc.loadDocumentArchive();
        // Note: Using Object to try to avoid to use jrcs objects.
        Object previousVersion = archive.getPrevVersion(archive.getLatestVersion());
        if (previousVersion != null) {
            rev = previousVersion.toString();
        } else {
            // Some inexistent version, since we have found no previous version in the archive.
            rev = "-1";
        }
    }
    // Perform the rollback.
    xwiki.rollback(tdoc, rev, context);
    // Forward to view.
    String redirect = Utils.getRedirect("view", context);
    sendRedirect(response, redirect);
    return false;
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiDocumentArchive(com.xpn.xwiki.doc.XWikiDocumentArchive) XWiki(com.xpn.xwiki.XWiki)

Aggregations

XWikiDocumentArchive (com.xpn.xwiki.doc.XWikiDocumentArchive)8 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 XWikiException (com.xpn.xwiki.XWikiException)3 Version (org.suigeneris.jrcs.rcs.Version)3 HibernateException (org.hibernate.HibernateException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiConfig (com.xpn.xwiki.XWikiConfig)1 XWikiRCSNodeInfo (com.xpn.xwiki.doc.rcs.XWikiRCSNodeInfo)1 ParseGroovyFromString (com.xpn.xwiki.internal.render.groovy.ParseGroovyFromString)1 XWikiHibernateStore (com.xpn.xwiki.store.XWikiHibernateStore)1 XWikiHibernateVersioningStore (com.xpn.xwiki.store.XWikiHibernateVersioningStore)1 XWikiStoreInterface (com.xpn.xwiki.store.XWikiStoreInterface)1 XWikiRightServiceImpl (com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl)1 IncludeServletAsString (com.xpn.xwiki.web.includeservletasstring.IncludeServletAsString)1 Date (java.util.Date)1 Invocation (org.jmock.core.Invocation)1 CustomStub (org.jmock.core.stub.CustomStub)1 DocumentReference (org.xwiki.model.reference.DocumentReference)1 BatchOperationExecutor (org.xwiki.refactoring.batch.BatchOperationExecutor)1