|
| 1 | +# These are patches not managed by collective.monkeypatcher |
| 2 | +from Products.CMFPlone.controlpanel.browser import redirects |
| 3 | +from plone.app.redirector.interfaces import IRedirectionStorage |
| 4 | +from Products.CMFCore.utils import getToolByName |
| 5 | +from urllib.parse import urlparse |
| 6 | +from zope.component import getUtility |
| 7 | +from zope.component.hooks import getSite |
| 8 | +from zope.i18nmessageid import MessageFactory |
| 9 | +from plone.app.content.browser.vocabulary import PERMISSIONS |
| 10 | +from plone.folder.nogopip import GopipIndex |
| 11 | +from Products.ZCatalog.Catalog import Catalog |
| 12 | +from redturtle.volto.catalogplan import Catalog_sorted_search_indexes |
| 13 | +from ZTUtils.Lazy import LazyCat |
| 14 | +from ZTUtils.Lazy import LazyMap |
| 15 | +from experimental.noacquisition import config |
| 16 | + |
| 17 | + |
| 18 | +import logging |
| 19 | + |
| 20 | +logger = logging.getLogger(__name__) |
| 21 | + |
| 22 | +_ = MessageFactory("plone") |
| 23 | + |
| 24 | + |
| 25 | +def absolutize_path_patched(path, is_source=True): |
| 26 | + """Create path including the path of the portal root. |
| 27 | +
|
| 28 | + The path must be absolute, so starting with a slash. |
| 29 | + Or it can be a full url. |
| 30 | +
|
| 31 | + If is_source is true, this is an alternative url |
| 32 | + that will point to a target (unknown here). |
| 33 | +
|
| 34 | + If is_source is true, path is the path of a target. |
| 35 | + An object must exist at this path, unless it is a full url. |
| 36 | +
|
| 37 | + Return a 2-tuple: (absolute redirection path, |
| 38 | + an error message if something goes wrong and otherwise ''). |
| 39 | + """ |
| 40 | + |
| 41 | + portal = getSite() |
| 42 | + err = None |
| 43 | + is_external_url = False |
| 44 | + if not path: |
| 45 | + if is_source: |
| 46 | + err = _("You have to enter an alternative url.") |
| 47 | + else: |
| 48 | + err = _("You have to enter a target.") |
| 49 | + elif not path.startswith("/"): |
| 50 | + if is_source: |
| 51 | + err = _("Alternative url path must start with a slash.") |
| 52 | + else: |
| 53 | + # For targets, we accept external urls. |
| 54 | + # Do basic check. |
| 55 | + parsed = urlparse(path) |
| 56 | + if parsed.scheme in ("https", "http") and parsed.netloc: |
| 57 | + is_external_url = True |
| 58 | + else: |
| 59 | + err = _("Target path must start with a slash.") |
| 60 | + elif "@@" in path: |
| 61 | + if is_source: |
| 62 | + err = _("Alternative url path must not be a view.") |
| 63 | + else: |
| 64 | + err = _("Target path must not be a view.") |
| 65 | + else: |
| 66 | + context_path = "/".join(portal.getPhysicalPath()) |
| 67 | + path = f"{context_path}{path}" |
| 68 | + if not err and not is_external_url: |
| 69 | + catalog = getToolByName(portal, "portal_catalog") |
| 70 | + if is_source: |
| 71 | + # Check whether already exists in storage |
| 72 | + storage = getUtility(IRedirectionStorage) |
| 73 | + if storage.get(path): |
| 74 | + err = _("The provided alternative url already exists!") |
| 75 | + else: |
| 76 | + # Check whether obj exists at source path. |
| 77 | + # A redirect would be useless then. |
| 78 | + |
| 79 | + ### THIS IS THE PATCH ### |
| 80 | + # unrestrictedTraverse returns the object with acquisition, so if |
| 81 | + # you have a content with path /Plone/aaa and try to call unrestrictedTraverse |
| 82 | + # with /Plone/aaa/aaa/aaa/aaa it will always return /Plone/aaa object |
| 83 | + # and this is not correct because we could want to create an alias for /Plone/aaa |
| 84 | + # that is /Plone/aaa/aaa |
| 85 | + # In Plone7 this will not be a problem anymore because of this: |
| 86 | + # https://github.com/plone/Products.CMFPlone/issues/3871 |
| 87 | + item = portal.unrestrictedTraverse(path, None) |
| 88 | + # if item is not None: this is the original check |
| 89 | + if item is not None and "/".join(item.getPhysicalPath()) == path: |
| 90 | + # if paths are different, it's an acquisition false positive, |
| 91 | + # so go on and let create the alias |
| 92 | + err = _("Cannot use a working path as alternative url.") |
| 93 | + ### END OF THE PATCH ### |
| 94 | + else: |
| 95 | + # Check whether obj exists at target path |
| 96 | + result = catalog.searchResults(path={"query": path}) |
| 97 | + if len(result) == 0: |
| 98 | + err = _("The provided target object does not exist.") |
| 99 | + |
| 100 | + return path, err |
| 101 | + |
| 102 | + |
| 103 | +MAX_SORTABLE = 5000 |
| 104 | + |
| 105 | + |
| 106 | +def Catalog_sortResults( |
| 107 | + self, |
| 108 | + rs, |
| 109 | + sort_index, |
| 110 | + reverse=False, |
| 111 | + limit=None, |
| 112 | + merge=True, |
| 113 | + actual_result_count=None, |
| 114 | + b_start=0, |
| 115 | + b_size=None, |
| 116 | +): |
| 117 | + if MAX_SORTABLE > 0: |
| 118 | + if actual_result_count is None: |
| 119 | + actual_result_count = len(rs) |
| 120 | + if actual_result_count >= MAX_SORTABLE and isinstance(sort_index, GopipIndex): |
| 121 | + logger.warning( |
| 122 | + "too many results %s disable GopipIndex sorting", actual_result_count |
| 123 | + ) |
| 124 | + switched_reverse = bool( |
| 125 | + b_size and b_start and b_start > actual_result_count / 2 |
| 126 | + ) |
| 127 | + if hasattr(rs, "keys"): |
| 128 | + sequence, slen = self._limit_sequence( |
| 129 | + rs.keys(), actual_result_count, b_start, b_size, switched_reverse |
| 130 | + ) |
| 131 | + return LazyMap( |
| 132 | + self.__getitem__, |
| 133 | + sequence, |
| 134 | + len(sequence), |
| 135 | + actual_result_count=actual_result_count, |
| 136 | + ) |
| 137 | + else: |
| 138 | + logger.error( |
| 139 | + "too many results %s disable GopipIndex sorting results %s has no key", |
| 140 | + actual_result_count, |
| 141 | + type(rs), |
| 142 | + ) |
| 143 | + return LazyCat([], 0, actual_result_count) |
| 144 | + return self._orig_sortResults( |
| 145 | + rs, sort_index, reverse, limit, merge, actual_result_count, b_start, b_size |
| 146 | + ) |
| 147 | + |
| 148 | + |
| 149 | +# apply patches |
| 150 | +logger.info( |
| 151 | + "install monkey patch for Products.ZCatalog.Catalog.Catalog._sorted_search_indexes #### WORK IN PROGRESS ####" |
| 152 | +) |
| 153 | +Catalog._orig_sorted_search_indexes = Catalog._sorted_search_indexes |
| 154 | +Catalog._sorted_search_indexes = Catalog_sorted_search_indexes |
| 155 | + |
| 156 | +logger.info("install monkey patch for Products.ZCatalog.Catalog.Catalog.sortResults") |
| 157 | +Catalog._orig_sortResults = Catalog.sortResults |
| 158 | +Catalog.sortResults = Catalog_sortResults |
| 159 | + |
| 160 | +logger.info("install monkey patch for plone.app.content.browser.vocabulary.PERMISSIONS") |
| 161 | +PERMISSIONS["plone.app.vocabularies.Keywords"] = "View" |
| 162 | + |
| 163 | +logger.info( |
| 164 | + "install monkey patch for from Products.CMFPlone.controlpanel.browser.redirects.absolutize_path" |
| 165 | +) |
| 166 | +redirects.absolutize_path = absolutize_path_patched |
| 167 | + |
| 168 | +logger.info("enable experimental.noacquisition") |
| 169 | +# config.DRYRUN = False |
0 commit comments