如何使用 rt.bulkmodify 在 Plone 中搜索和替换灵巧内容

How to search and replace dexterity content in Plone using rt.bulkmodify

我们正在从现有的旧静态站点迁移到 Plone 4.3。我们从旧站点导入了几个 HTML 页面,现在面临 8000 多个硬编码链接,需要在我们的 Plone 系统中更新这些链接以符合我们新的 url 标准。这些页面是使用自定义灵巧类型构建的。我们不想手动编辑这些。

我们想在 Plone 中使用批量修改工具。我们正试图用它来替换我们所有使用正则表达式的链接。不幸的是,无论我们使用这个工具在 Plone 中搜索什么,它都找不到一个结果。

我觉得我们漏掉了一步或者走错了路。

是否有我们遗漏的程序或是否有更好的方法来搜索和替换我们灵巧类型内容中的硬编码链接?我们认为我们可能需要以某种方式为灵巧内容编制索引,以便可以对其进行搜索。

如果这是真的,我们似乎找不到相关文档。

以下是我们用来尝试实现此功能的参考资料:

Plone.org - rt.bulkmodify

Python - rt.bulkmodify

Plone.org - catalog-indexing-strategies

抱歉,rt.bulkmodify 目前不支持敏捷。您必须提供合适的 IBulkModifyContentChanger 适配器。

这里是我们正在为 plone.app.contenttypes/Plone 5 兼容性开发的未发布版本(由于现在无法在 Plone 5 上真正运行,因此没有真正测试过)。应该也适用于 Plone 4 上的纯灵巧自定义类型:

from ..interfaces import IBulkModifyContentChanger
from zope.interface import implements


class TextContentAdapters(object):
    """This is OK for every know plone.app.contenttypes dexterity item"""

    implements(IBulkModifyContentChanger)

    def __init__(self, context):
        self.context = context

    def _get_text(self):
        return self._get_utext().encode('utf-8')

    def _set_text(self, text):
        raise NotImplementedError("%s doesn't implements setter for 8-bit string" % self.context.portal_type)

    def _get_utext(self):
        text_data = getattr(self.context, 'text', None)
        if text_data:
            return text_data.raw

    def _set_utext(self, text):
        self.context.text = text
        self.context.reindexObject(idxs=['SearchableText'])

    text = property(_get_text, _set_text)
    utext = property(_get_utext, _set_utext)