Ejemplo n.º 1
0
    def index_object(self, documentId, obj, treshold=None):
        """Index the object"""
        if not ITranslatable.providedBy(obj):
            if IIndexableObjectWrapper.providedBy(obj):
                # wrapped object in `plone.indexer`
                wrapped = getattr(obj, '_IndexableObjectWrapper__object', None)
                # XXX: the rest can probably go now...
                # Wrapper doesn't proxy __implements__
                if wrapped is None:
                    wrapped = getattr(obj, '_IndexableObjectWrapper__ob', None)
                # Older CMFPlone
                if wrapped is None:
                    wrapped = getattr(obj, '_obj', None)
                if wrapped is None:
                    return 0
                obj = wrapped

        try:
            language = obj.Language
            if callable(language):
                language = language()
        except AttributeError:
            return 0

        if ITranslatable.providedBy(obj):
            canonical = obj.getCanonical()
            # Gracefully deal with broken references
            if canonical is None:
                return 0
            cid = canonical.UID()
        else:
            # Also index non-translatable content, otherwise
            # LinguaPlone only shows translatable content.
            # This assumes a catalog documentId will never
            # be equal to a UID.
            cid = documentId

        if documentId not in self._unindex:
            self._length.change(1)
        else:
            self._remove(self._unindex[documentId])

        main, sub = splitLanguage(language)
        entry = IndexEntry(documentId, main, sub, cid)
        self._insert(entry)
        self._unindex[documentId] = entry
        self._sortindex[documentId] = str(entry)

        return 1
Ejemplo n.º 2
0
    def _search(self, language, fallback=True):
        main, sub = splitLanguage(language)

        if main not in self._index:
            return None

        if fallback:
            # Search in sorted order, specific sub tag first, None second
            subs = list(self._index[main].keys())
            subs.sort()
            if sub in subs:
                subs.remove(sub)
                subs.insert(0, sub)
        else:
            subs = [sub]

        result = OOSet()

        for sublanguage in subs:
            result = oo_union(result, self._index[main][sublanguage])

        return IISet(entry.docid for entry in result)