Back to index

plone3  3.1.7
cleanup.py
Go to the documentation of this file.
00001 """
00002 $Id: cleanup.py 10017 2006-06-09 20:53:36Z hannosch $
00003 """
00004 
00005 from sets import Set
00006 from Products.CMFPlone.utils import transaction
00007 
00008 def cleanUpCatalog(catalog, out):
00009     _cat = catalog._catalog
00010     print >> out, "calculating inconsistencies on metadata trees..."
00011     data_rids = Set(_cat.data.keys())
00012     path_rids = Set(_cat.paths.keys())
00013     uids_rids = Set(_cat.uids.values())
00014 
00015     good_rids = data_rids & path_rids & uids_rids
00016     bad_rids = (data_rids | path_rids | uids_rids) - good_rids
00017     print >> out, "%s inconsistent entries detected" % len(bad_rids)
00018     pf_rids = Set()
00019     bad_paths = Set()
00020     for rid, path in _cat.paths.items():
00021         if "portal_factory/" in path:
00022             pf_rids.add(rid)
00023             bad_paths.add(path)
00024     for path, rid in _cat.uids.items():
00025         if "portal_factory/" in path:
00026             pf_rids.add(rid)
00027             bad_paths.add(path)
00028         if rid in bad_rids:
00029             bad_paths.add(path)
00030     pathIndex = _cat.getIndex('path')
00031     p_args = dict(query='portal_factory', level=-1)
00032     pf_rids |= Set(pathIndex._apply_index(dict(path=p_args))[0].keys())
00033     print >> out, "%s portal_factory entries detected" % len(pf_rids)
00034     bad_rids |= pf_rids
00035     print >> out, "cleaning up uids..."
00036     for path in bad_paths:
00037         try:
00038             del _cat.uids[path]
00039         except KeyError:
00040             pass
00041     print >> out, "cleaning up data and paths and indexes..."
00042     indexes = [_cat.getIndex(name) for name in _cat.indexes.keys()]
00043     for rid in bad_rids:
00044         try:
00045             del _cat.paths[rid]
00046         except KeyError:
00047             pass
00048         try:
00049             del _cat.data[rid]
00050         except KeyError:
00051             pass
00052         for index in indexes:
00053             if hasattr(index, 'unindex_object'):
00054                 index.unindex_object(rid)
00055     print >> out, "Done"
00056 
00057 def removeUnreachable(catalog, out):
00058     _cat = catalog._catalog
00059     count = 0
00060     print >> out, "removing unreachable entries in the catalog..."
00061     for path in _cat.uids.keys():
00062         if not catalog.aq_parent.unrestrictedTraverse(path, None):
00063             _cat.uncatalogObject(path)
00064             count +=1
00065     print >> out, "%s entries removed" % count
00066 
00067 if __name__ == "__main__":
00068     # run this under "zopectl run misc.py /path/to/Plone"
00069     import sys
00070     remove_unreachable = sys.argv[2:]
00071     catalog = app.unrestrictedTraverse(sys.argv[1])
00072     cleanUpCatalog(catalog, sys.stdout)
00073     if remove_unreachable:
00074         removeUnreachable(catalog, sys.stdout)
00075     transaction.commit()