def reindex_neutral(obj, event):
    # we need to look for the parent that is already indexed
    if IPloneSiteRoot.providedBy(obj) \
       or (not is_shared(obj) and not is_shared_original(obj)):
        return
    parent = aq_parent(obj)
    if ILanguageRootFolder.providedBy(parent):
        # If it's parent is language root folder no need to reindex
        return
    site = getSite()
    language_tool = getToolByName(site, 'portal_languages')
    language_infos = language_tool.supported_langs
    if IPloneSiteRoot.providedBy(parent):
        # It's plone site root we need to look at LRF
        for language_info in language_infos:
            lrf_to_reindex = getattr(parent, language_info, None)
            to_reindex = getattr(lrf_to_reindex, obj.id, None)
            if to_reindex is not None:
                to_reindex.reindexObject()
    else:
        content_id = IUUID(parent).split('-')[0]
        pc = getToolByName(site, 'portal_catalog')
        for language_info in language_infos:
            brains = pc.unrestrictedSearchResults(
                UID=content_id + '-' + language_info)
            if len(brains):
                obj.unrestrictedTraverse(
                    brains[0].getPath() + '/' + obj.id).reindexObject()
    return
def remove_ghosts(obj, event):
    """We are going to remove a object: we need to check if its neutral
       and remove their indexes also.
    """
    if not IObjectWillBeAddedEvent.providedBy(event) \
       and (IObjectWillBeMovedEvent.providedBy(event)
            or IObjectWillBeRemovedEvent.providedBy(event)):
        if not is_shared_original(obj):
            return

        content_id = IUUID(obj).split('-')[0]
        site = getSite()
        try:
            pc = getToolByName(site, 'portal_catalog')
        except AttributeError:
            # In case we are removing the site there is no portal_catalog
            return
        language_tool = getToolByName(site, 'portal_languages')
        language_infos = language_tool.supported_langs

        for language_info in language_infos:
            brains = pc.unrestrictedSearchResults(
                UID=content_id + '-' + language_info)
            for brain in brains:
                obj.unrestrictedTraverse(brain.getPath()).unindexObject()
            brains = pc.unrestrictedSearchResults(
                UID=content_id)
            for brain in brains:
                obj.unrestrictedTraverse(brain.getPath()).unindexObject()
    if IActionSucceededEvent.providedBy(event):
        reindex_neutral(obj, event)
def reindex_neutral(obj, event):
    """Neutral
    On shared elements, the uuid is different so we need to take care of
    them on catalog in case we modify any shared element
    """
    # is the given object Neutral?
    if IPloneSiteRoot.providedBy(obj) \
       or obj.getId() in BLACK_LIST_IDS \
       or (not is_shared(obj) and not is_shared_original(obj)):
        return

    parent = aq_parent(obj)
    if ILanguageRootFolder.providedBy(parent):
        # If it's parent is language root folder there is no need to reindex
        return

    site = getSite()
    language_tool = getToolByName(site, 'portal_languages')
    language_infos = language_tool.supported_langs
    if IPloneSiteRoot.providedBy(parent):
        # It's plone site root we need to look at LRF
        _reindex_site_root(obj, parent, language_infos)
        return

    # ok, we're very neutral
    content_id = IUUID(parent).split('-')[0]
    pc = getToolByName(site, 'portal_catalog')
    for language_info in language_infos:
        brains = pc.unrestrictedSearchResults(
            UID=content_id + '-' + language_info
        )
        if len(brains):
            # we have results, so parent was indexed before.
            brain = brains[0]
            obj.unrestrictedTraverse(
                brain.getPath() + '/' + obj.id).reindexObject()