Skip to content

Instantly share code, notes, and snippets.

@jone
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jone/00ab50abcb0213ec7621 to your computer and use it in GitHub Desktop.
Save jone/00ab50abcb0213ec7621 to your computer and use it in GitHub Desktop.
Plone: Remove versions of deleted objects
from AccessControl.SecurityManagement import newSecurityManager
from pprint import pprint
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.interfaces import IPloneSiteRoot
from Testing.makerequest import makerequest
from zope.app.component.hooks import setSite
app = globals()['app']
user = None
while not user:
user = app.acl_users.getUser(raw_input('Admin user ID (Zope level): ').strip())
user = user.__of__(app.acl_users)
newSecurityManager(app, user)
app = makerequest(app)
overview_view = app.restrictedTraverse('plone-overview')
print 'SITES:'
for site in overview_view.sites():
print '-', '/'.join(site.getPhysicalPath())
while True:
site_path = raw_input('Path to Plone site to update: ').strip().strip('/')
try:
site = app.restrictedTraverse(site_path)
except Exception, exc:
print 'ERROR:', exc
else:
print 'FOUND:', site
if not IPloneSiteRoot.providedBy(site):
print 'ERROR: not a Plone site:', site
else:
break
setSite(site)
historiesstorage = getToolByName(site, 'portal_historiesstorage')
hidhandler = getToolByName(site, "portal_historyidhandler")
shadowstorage = historiesstorage._getShadowStorage()._storage
versions_repo = historiesstorage._getZVCRepo()
print 'Gathering statistics..'
statistics = historiesstorage.zmi_getStorageStatistics()
pprint(statistics['summaries'])
print ''
print 'Deleting', statistics['summaries']['deletedHistories'], 'histories', \
'and', statistics['summaries']['deletedVersions'], 'versions.'
for historyinfo in statistics['deleted']:
history_id = historyinfo['history_id']
history = historiesstorage._getShadowHistory(history_id)
for zvc_key in set([
historiesstorage._getZVCAccessInfo(history_id, selector, True)[0]
for selector in history._available]):
if zvc_key in versions_repo._histories:
del versions_repo._histories[zvc_key]
shadowstorage.pop(history_id, None)
import transaction
transaction.commit()
print 'All done.'
@buchi
Copy link

buchi commented Apr 19, 2015

I would remove the complete VersionHistory object instead of wiping it's _versions attribute (lines 39-41):

if zvc_key in versions_repo._histories:
    del versions_repo._histories[zvc_key]

Deleting the version_id attribute from the object should not be necessary as the object shouldn't exist anymore (lines 45-47). I would expect obj is always None in this case.

@jone
Copy link
Author

jone commented Apr 21, 2015

@buchi thanks, updated 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment