|
4 | 4 | from plone.app.querystring.interfaces import IParsedQueryIndexModifier |
5 | 5 | from plone.app.querystring.interfaces import IQueryModifier |
6 | 6 | from plone.app.querystring.interfaces import IQuerystringRegistryReader |
| 7 | +from plone.base.utils import munge_search_term as _munge_search_term |
7 | 8 | from plone.batching import Batch |
8 | 9 | from plone.registry.interfaces import IRegistry |
9 | 10 | from Products.CMFCore.utils import getToolByName |
10 | 11 | from zope.component import getMultiAdapter |
11 | 12 | from zope.component import getUtilitiesFor |
12 | 13 | from zope.component import getUtility |
| 14 | +from zope.deferredimport import deprecated |
13 | 15 | from zope.i18n import translate |
14 | 16 | from zope.i18nmessageid import MessageFactory |
15 | 17 | from zope.publisher.browser import BrowserView |
16 | 18 |
|
17 | 19 | import json |
18 | 20 | import logging |
19 | | -import re |
20 | 21 |
|
21 | 22 | logger = logging.getLogger("plone.app.querystring") |
22 | 23 | _ = MessageFactory("plone") |
23 | 24 |
|
24 | | -# We should accept both a simple space, unicode u'\u0020 but also a |
25 | | -# multi-space, so called 'waji-kankaku', unicode u'\u3000' |
26 | | -_MULTISPACE = "\u3000" |
27 | | -_BAD_CHARS = ("?", "-", "+", "*", _MULTISPACE) |
28 | | - |
29 | | - |
30 | | -def _quote_chars(s): |
31 | | - # We need to quote parentheses when searching text indices |
32 | | - if "(" in s: |
33 | | - s = s.replace("(", '"("') |
34 | | - if ")" in s: |
35 | | - s = s.replace(")", '")"') |
36 | | - if _MULTISPACE in s: |
37 | | - s = s.replace(_MULTISPACE, " ") |
38 | | - return s |
39 | | - |
40 | | - |
41 | | -def _quote(term): |
42 | | - # The terms and, or and not must be wrapped in quotes to avoid |
43 | | - # being parsed as logical query atoms. |
44 | | - if term.lower() in ("and", "or", "not"): |
45 | | - term = '"%s"' % term |
46 | | - return _quote_chars(term) |
47 | | - |
48 | | - |
49 | | -def munge_search_term(query): |
50 | | - original_query = query |
51 | | - for char in _BAD_CHARS: |
52 | | - query = query.replace(char, " ") |
53 | | - |
54 | | - # extract quoted phrases first |
55 | | - quoted_phrases = re.findall(r'"([^"]*)"', query) |
56 | | - r = [] |
57 | | - for qp in quoted_phrases: |
58 | | - # remove from original query |
59 | | - query = query.replace(f'"{qp}"', "") |
60 | | - # replace with cleaned leading/trailing whitespaces |
61 | | - # and skip empty phrases |
62 | | - clean_qp = qp.strip() |
63 | | - if not clean_qp: |
64 | | - continue |
65 | | - r.append(f'"{clean_qp}"') |
66 | | - |
67 | | - r += map(_quote, query.strip().split()) |
68 | | - r = " AND ".join(r) |
69 | | - r = r + ("*" if r and not original_query.endswith('"') else "") |
70 | | - return r |
| 25 | +deprecated( |
| 26 | + "Moved to plone.base.utils. Import from there instead " |
| 27 | + "(will be removed in Plone 7).", |
| 28 | + munge_search_term="plone.base.utils:munge_search_term", |
| 29 | +) |
71 | 30 |
|
72 | 31 |
|
73 | 32 | class ContentListingView(BrowserView): |
@@ -267,7 +226,7 @@ def filter_query(self, query): |
267 | 226 | return query |
268 | 227 |
|
269 | 228 | def munge_search_term(self, q): |
270 | | - return munge_search_term(q) |
| 229 | + return _munge_search_term(q) |
271 | 230 |
|
272 | 231 |
|
273 | 232 | class RegistryConfiguration(BrowserView): |
|
0 commit comments