cat = n.portal_catalog

batch = 200
page_brains = cat(Type='Page')
i = 0
for page_brain in page_brains:
    try:
        page = page_brain.getObject()
    except AttributeError:
        # catalog ghost... might as well clear it out
        cat._catalog.uncatalogObject(page_brain.getPath())
        continue
    try:
        cache = IWikiHistory(page)
    except TypeError:
        # couldn't adapt, we don't cache history, skip it
        continue
    cache.resync_history_cache()
    logger.log(INFO, '%s history cache updated' % page_brain.getPath())
    i += 1
    if i == batch:
        msg = 'incremental page history sync commit: %d' % batch
        transaction.get().note(msg)
        transaction.commit()
        logger.log(INFO, msg)
        i = 0

transaction.get().note('final page history sync commit: %d' % i)
transaction.commit()
logger.log(INFO, 'FINISHED!')