cogDeletes revision from a given author in a given document
Developed by

Vincent Massol

Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1

Description

This snippet requires Programming Rights to execute.

This snippet can be useful if you have spam done by a given author in a page and you wish to remove all the revisions done by the author. Note that the code below only support contiguous revisions by the spam author.

{{velocity}}
#macro(deleteRevisions $firstRevision $lastRevision $targetDoc)
  #set ($docArchive = $targetDoc.getDocumentArchive())
  #set ($discard = $docArchive.removeVersions($lastRevision, $firstRevision, $xcontext.context))
  #set ($discard = $xwiki.getXWiki().getVersioningStore().saveXWikiDocArchive($docArchive, true, $xcontext.context))
  #set ($discard = $targetDoc.setDocumentArchive($docArchive))
  ## If we removed the last revision then remove the document too!
  ## If there are still versions we must rollback to the first undeleted version
  #if ("$!docArchive.getLatestVersion()" == '')
    ## Do not send to the trash
    #set ($discard = $xwiki.getXWiki().deleteAllDocuments($targetDoc.getDocument(), false, $xcontext.context))
  #else
    #set ($newDoc = $docArchive.loadDocument($docArchive.getLatestVersion(), $xcontext.context))
    ## Reset the document reference, since the one taken from the archive might be wrong (old name from before a rename)
    #set ($discard = $newDoc.setDocumentReference($targetDoc.getDocumentReference()))
    ## Make sure we don't create a new rev!
    #set ($discard = $newDoc.setMetaDataDirty(false))
    #set ($discard = $newDoc.addXObjectsToRemoveFromVersion($targetDoc))
    #set ($discard = $xwiki.getXWiki().getStore().saveXWikiDoc($newDoc, $xcontext.context))
    #set ($discard = $xcontext.context.setDoc($newDoc))
  #end
#end
#macro(deleteContiguousRevisionsFromLastAuthor $revisions $targetDoc)
  ## Revert the order of the $revisions array so that the last revisions comes first which makes
  ## it possible to loop over in Velocity...
  #set ($revisionList = $collectionstool.getLinkedList())
  #foreach ($revision in $revisions)
    #set ($discard = $revisionList.addFirst($revision))
  #end
  #if (!$revisionList.isEmpty())
    ## Navigate the revisions to find the first contiguous revision from author of the first revision
    #foreach ($revision in $revisionList)
      #set ($revisionInfo = $targetDoc.getRevisionInfo($revision))
      #set ($revisionAuthor = $revisionInfo.author)
      #if ($velocityCount == 1)
        ## Save the author
        #set ($lastRevisionAuthor = $revisionAuthor)
        ## Save the revision
        #set ($lastRevision = $revision)
      #elseif ($revisionAuthor != $lastRevisionAuthor)
        ## Stop the navigation!
        #break
      #end
      #set ($firstRevision = $revision)
    #end
    #deleteRevisions($firstRevision $lastRevision $targetDoc)
  #end
#end

## Set up some test conditions
#set ($targetDoc = $xwiki.getDocument('Main.TestPage'))
#set ($discard = $targetDoc.delete())
#set ($targetDoc = $xwiki.getDocument('Main.TestPage'))
#set ($currentUserReference = $xcontext.getUserReference())
#set ($discard = $xcontext.getContext().setUserReference($services.model.resolveDocument('Admin', 'user')))
#set ($discard = $targetDoc.setContent('content 1'))
#set ($discard = $targetDoc.save())
#set ($discard = $xcontext.getContext().setUserReference($services.model.resolveDocument('spam', 'user')))
#set ($discard = $targetDoc.setContent('content 2'))
#set ($discard = $targetDoc.save())
#set ($discard = $xcontext.getContext().setUserReference($services.model.resolveDocument('spam', 'user')))
#set ($discard = $targetDoc.setContent('content 3'))
#set ($discard = $targetDoc.save())
#set ($discard = $xcontext.getContext().setUserReference($currentUserReference))

#set ($revisions = $targetDoc.getRevisions())
#deleteContiguousRevisionsFromLastAuthor($revisions $targetDoc)

{{velocity}}

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Tags:
     

Get Connected