OLD | NEW |
(Empty) | |
| 1 # Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 # Use of this source code is govered by a BSD-style |
| 3 # license that can be found in the LICENSE file or at |
| 4 # https://developers.google.com/open-source/licenses/bsd |
| 5 |
| 6 """Helper functions and classes used in issue search and sorting.""" |
| 7 |
| 8 import logging |
| 9 import re |
| 10 |
| 11 from features import savedqueries_helpers |
| 12 from search import query2ast |
| 13 from services import tracker_fulltext |
| 14 from services import fulltext_helpers |
| 15 from tracker import tracker_helpers |
| 16 |
| 17 |
| 18 # Users can use "is:starred" in queries to limit |
| 19 # search results to issues starred by that user. |
| 20 IS_STARRED_RE = re.compile(r'\b(?![-@.:])is:starred\b(?![-@.:])', re.I) |
| 21 |
| 22 # Users can use "me" in other fields to refer to the logged in user name. |
| 23 ME_RE = re.compile(r'(?<=[=:])me\b(?![-@.:=])', re.I) |
| 24 |
| 25 |
| 26 def _AccumulateIssueProjectsAndConfigs( |
| 27 cnxn, project_dict, config_dict, services, issues): |
| 28 """Fetch any projects and configs that we need but haven't already loaded. |
| 29 |
| 30 Args: |
| 31 cnxn: connection to SQL database. |
| 32 project_dict: dict {project_id: project} of projects that we have |
| 33 already retrieved. |
| 34 config_dict: dict {project_id: project} of configs that we have |
| 35 already retrieved. |
| 36 services: connections to backends. |
| 37 issues: list of issues, which may be parts of different projects. |
| 38 |
| 39 Returns: |
| 40 Nothing, but projects_dict will be updated to include all the projects that |
| 41 contain the given issues, and config_dicts will be updated to incude all |
| 42 the corresponding configs. |
| 43 """ |
| 44 new_ids = {issue.project_id for issue in issues} |
| 45 new_ids.difference_update(project_dict.iterkeys()) |
| 46 new_projects_dict = services.project.GetProjects(cnxn, new_ids) |
| 47 project_dict.update(new_projects_dict) |
| 48 new_configs_dict = services.config.GetProjectConfigs(cnxn, new_ids) |
| 49 config_dict.update(new_configs_dict) |
| 50 |
| 51 |
| 52 def ReplaceKeywordsWithUserID(me_user_id, query): |
| 53 """Substitutes User ID in terms such as is:starred and me. |
| 54 |
| 55 This is done on the query string before it is parsed because the query string |
| 56 is used as a key for cached search results in memcache. A search for by one |
| 57 user for owner:me should not retrieve results stored for some other user. |
| 58 |
| 59 Args: |
| 60 me_user_id: Null when no user is logged in, or user ID of the logged in |
| 61 user when doing an interactive search, or the viewed user ID when |
| 62 viewing someone else's dashboard, or the subscribing user's ID when |
| 63 evaluating subscriptions. |
| 64 query: The query string. |
| 65 |
| 66 Returns: |
| 67 A string with "me" and "is:starred" removed or replaced by new terms that |
| 68 use the numeric user ID provided. |
| 69 """ |
| 70 if me_user_id: |
| 71 star_term = 'starredby:%d' % me_user_id |
| 72 query = IS_STARRED_RE.sub(star_term, query) |
| 73 query = ME_RE.sub(str(me_user_id), query) |
| 74 else: |
| 75 query = IS_STARRED_RE.sub('', query) |
| 76 query = ME_RE.sub('', query) |
| 77 |
| 78 return query |
| 79 |
| 80 |
| 81 def ParseQuery(mr, config, services): |
| 82 """Parse the user's query. |
| 83 |
| 84 Args: |
| 85 mr: commonly used info parsed from the request. |
| 86 config: The ProjectConfig PB for the project. |
| 87 services: connections to backends. |
| 88 |
| 89 Returns: |
| 90 A pair (ast, is_fulltext) with the parsed query abstract syntax tree |
| 91 and a boolean that is True if the query included any fulltext terms. |
| 92 """ |
| 93 canned_query = savedqueries_helpers.SavedQueryIDToCond( |
| 94 mr.cnxn, services.features, mr.can) |
| 95 query_ast = query2ast.ParseUserQuery( |
| 96 mr.query, canned_query, query2ast.BUILTIN_ISSUE_FIELDS, config) |
| 97 |
| 98 is_fulltext_query = bool( |
| 99 query_ast.conjunctions and |
| 100 fulltext_helpers.BuildFTSQuery( |
| 101 query_ast.conjunctions[0], tracker_fulltext.ISSUE_FULLTEXT_FIELDS)) |
| 102 |
| 103 return query_ast, is_fulltext_query |
| 104 |
OLD | NEW |