Silva altepeter.net Not logged in.

Family

Videos

Technology Stuff

Articles

Fun

Silva Products

Web Design

VIVO Javascript Framework

Photo Galleries

Contact Us

Blog

Silva: Remove Closed Versions

 This article describes how to use the VersionedContentCleanupAdapter to remove old ('closed') versions of VersionedContent objects (e.g. Silva Documents), as well as provides a sample PythonScript that uses this adapter.

Over time Silva's VersionedContent objects (e.g. SilvaDocuments, SilvaNews Articles) can get bloated with old versions.  Which of course also bloats your ZODB.  Usually, after a version is closed, it is never used again.

Silva 1.5 (maybe starting in 1.4) provides a nice adapter than can remove closed versions of VersionedContent objects.  This adapter can do two types of cleanup:

  1. Cleanup all versions marked as 'closed'.  This keeps the last closed version, which has a status of 'last_closed'.  The method you use is: cleanup().
  2. Cleanup all versions, including the 'last_closed' version (unless that is the only version.) The method used is cleanup_all_old().

This document describes how to use the cleanup method, but the code supplied can easily be modified to use the latter method by replace the call to cleanup with cleanup_all_old.

One drawback of this adapter is that it does not recursivly cleanup your entire Silva tree.  So the script you will shortly uses the ZopeFindAndApply functionality to locate all Silva Folders and Silva Publications.

Some Silva adapters are not callable from ZMI code (e.g. PythonScripts).  This adapter is, however.  Here is the script I used to cleanup my Silva tree.  This cleanup can take a long time, causing your browser to timeout, and then you never know what happened.  Because of this it writes directly to REQUEST.RESPONSE, so your browser is kept up-to-date with it's progress.

You'll notice that cont is the starting container to do the cleanup in.  You can change this to be your silva root:

cont = container.get_root()

And now, without further ado, here is the script:

## Script (Python) "cleanup_versions"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
from Products.Silva.adapters.cleanup import getCleanupVersionsAdapter

def print_stats():
    resp.write("Stats:\n")
    stats = req.get('mystats')
    if not stats:
        resp.write('no stats')
        return
    for k in stats.keys():
        resp.write("  %s: %s\n"%(k,str(stats[k])))
req = context.REQUEST
resp = context.REQUEST.RESPONSE
resp.write('Content-Type: text/plain\n\n')
resp.write("cleaning up\n")

req.set('mystats',None)
def myapply(ob,path):
    resp.write('checking container: ' + path + '\n')
    cva = getCleanupVersionsAdapter(ob)
    req.set('mystats',cva.cleanup(req.get('mystats')))

#cont = context.aq_parent.test
cont = context.bethelnet
cont.ZopeFindAndApply(cont,obj_metatypes=['Silva Publication','Silva Folder'],
		      search_sub=1,
		      apply_func=myapply)
print_stats()
resp.write('\n')
resp.write("done")