How do I automatically reset the sandbox to default content?

Hello, tell me please how to automatically reset the sandbox to default content.
I realized that this can be done using the Scheduler Application,
but I don’t know what code to enter.

i found this code snippet

    {{velocity}}
    #set ($rollbackDoc = $xwiki.getDocument("Sandbox.WebHome"))
    ##-----------------------
    ## Find the last but one revision
    ##-----------------------
    #set ($criteria = $xwiki.criteriaService.revisionCriteriaFactory.createRevisionCriteria())
    #set ($range = $xwiki.criteriaService.rangeFactory.createTailRange(2))
    $criteria.setRange($range)
    $criteria.setIncludeMinorVersions(true)
    #set ($revision = $rollbackDoc.getRevisions($criteria).get(0))
    ##-----------------------
    ## Perform the rollback
    ##-----------------------
    $xwiki.getXWiki().rollback($rollbackDoc.document, $revision, $context.context)
    {{/velocity}}

but i need to convert this code to Groovy language (for paste into Scheduler).
Can anybody help me?

It’s quite easy to convert to Groovy but you could also keep it in velocity and from groovy just render the page that has the velocity in it using:

xwiki.getDocument('MySpace.MyPage').getRenderedContent()

Now that’s not perfect since viewing that page by someone will also trigger the reset.

Let me start the groovy conversion for you and maybe you can figure out the rest:

{{velocity}}
def rollbackDoc = xwiki.getDocument(“Sandbox.WebHome”)
//-----------------------
// Find the last but one revision
//-----------------------
def criteria = xwiki.criteriaService.revisionCriteriaFactory.createRevisionCriteria()
def range = xwiki.criteriaService.rangeFactory.createTailRange(2)
criteria.setRange(range)
...
1 Like

PS: The scheduler features would need a bit of love and we need to make it support any wiki syntax.

1 Like

Thank you.
I try to append your code example

def rollbackDoc = xwiki.getDocument("Sandbox.WebHome")
//-----------------------
// Find the last but one revision
//-----------------------
def criteria = xwiki.criteriaService.revisionCriteriaFactory.createRevisionCriteria()
def range = xwiki.criteriaService.rangeFactory.createTailRange(2)
criteria.setRange(range)
criteria.setIncludeMinorVersions(true)
xwiki.getXWiki().rollback(rollbackDoc.document, revision, context.context)

but it’s still not work i think the problem in “context.context”

Do you know how http://playground.xwiki.org/xwiki/bin/view/Main/WebHome have rollback their sandbox?

See step 3 of https://www.xwiki.org/xwiki/bin/view/XWiki/Maintenance/Migrations/#HUpgradethepagesonxwikiorg-node2.xwikisas.com

Thanks but unfortunately i haven’t access to this page image

Right this is internal :slight_smile:

In short, to reset the sandbox:

  • backup the MySQL SQL into a file once
  • remove the playground wiki
  • recreate it based on a wiki template
  • restore the MySQL SQL from the saved file
1 Like

Hello, can you help me please?
When i use this code snippet:

{{velocity}}
#set ($rollbackDoc = $xwiki.getDocument("Sandbox.WebHome"))
##-----------------------
## Find the last but one revision
##-----------------------
#set ($criteria = $xwiki.criteriaService.revisionCriteriaFactory.createRevisionCriteria())
#set ($range = $xwiki.criteriaService.rangeFactory.createTailRange(2))
$criteria.setRange($range)
$criteria.setIncludeMinorVersions(true)
#set ($revision = $rollbackDoc.getRevisions($criteria).get(0))
##-----------------------
## Perform the rollback
##-----------------------
$xwiki.getXWiki().rollback($rollbackDoc.document, $revision, $context.context)
{{/velocity}}

for any one page it is work

But then i use Sanbox.WebHome i see this error.
Do you any suggestions what it means?

Failed to execute the [velocity] macro. Cause: [A different object with the same identifier value was already associated with the session : [com.xpn.xwiki.doc.XWikiAttachment#-2096787253]]. Click on this message for details.

`