From 65b891b095907357fd6443c046bef32eb7acc17a Mon Sep 17 00:00:00 2001 From: epriestley Date: Fri, 5 Jun 2015 11:21:22 -0700 Subject: [PATCH] Move some value massaging into SearchFields Summary: Ref T8441. Ref T7715. Let SearchFields do some value massaging before buiding queries so we don't have to work as hard in each SearchEngine. Particularly, they can handle evaluateTokens() from Tokenizers. Test Plan: Paste automatically gained access to `viewer()`, etc. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T7715, T8441 Differential Revision: https://secure.phabricator.com/D13170 --- .../query/PhabricatorPasteSearchEngine.php | 25 +++++++++++-------- .../PhabricatorApplicationSearchEngine.php | 20 +++++++++++++-- .../field/PhabricatorSearchDateField.php | 4 +++ .../search/field/PhabricatorSearchField.php | 4 +++ .../field/PhabricatorSearchTokenizerField.php | 7 ++++++ .../field/PhabricatorSearchUsersField.php | 4 +-- 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/applications/paste/query/PhabricatorPasteSearchEngine.php b/src/applications/paste/query/PhabricatorPasteSearchEngine.php index 4b4b621895..bbc01164b7 100644 --- a/src/applications/paste/query/PhabricatorPasteSearchEngine.php +++ b/src/applications/paste/query/PhabricatorPasteSearchEngine.php @@ -11,21 +11,24 @@ final class PhabricatorPasteSearchEngine return 'PhabricatorPasteApplication'; } - public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { + public function buildQueryFromParameters(array $map) { $query = id(new PhabricatorPasteQuery()) - ->needContent(true) - ->withAuthorPHIDs($saved->getParameter('authorPHIDs', array())) - ->withLanguages($saved->getParameter('languages', array())); + ->needContent(true); - $start = $this->parseDateTime($saved->getParameter('createdStart')); - $end = $this->parseDateTime($saved->getParameter('createdEnd')); - - if ($start) { - $query->withDateCreatedAfter($start); + if ($map['authorPHIDs']) { + $query->withAuthorPHIDs($map['authorPHIDs']); } - if ($end) { - $query->withDateCreatedBefore($end); + if ($map['languages']) { + $query->withLanguages($map['languages']); + } + + if ($map['createdStart']) { + $query->withDateCreatedAfter($map['createdStart']); + } + + if ($map['createdEnd']) { + $query->withDateCreatedBefore($map['createdEnd']); } return $query; diff --git a/src/applications/search/engine/PhabricatorApplicationSearchEngine.php b/src/applications/search/engine/PhabricatorApplicationSearchEngine.php index 7891ef5174..1f2c7a3b45 100644 --- a/src/applications/search/engine/PhabricatorApplicationSearchEngine.php +++ b/src/applications/search/engine/PhabricatorApplicationSearchEngine.php @@ -90,8 +90,24 @@ abstract class PhabricatorApplicationSearchEngine extends Phobject { * @param PhabricatorSavedQuery The saved query to operate on. * @return The result of the query. */ - abstract public function buildQueryFromSavedQuery( - PhabricatorSavedQuery $saved); + public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { + $fields = $this->buildSearchFields(); + $viewer = $this->requireViewer(); + + $parameters = array(); + foreach ($fields as $field) { + $field->setViewer($viewer); + $field->readValueFromSavedQuery($saved); + $value = $field->getValueForQuery($field->getValue()); + $parameters[$field->getKey()] = $value; + } + + return $this->buildQueryFromParameters($parameters); + } + + protected function buildQueryFromParameters(array $parameters) { + throw new PhutilMethodNotImplementedException(); + } /** * Builds the search form using the request. diff --git a/src/applications/search/field/PhabricatorSearchDateField.php b/src/applications/search/field/PhabricatorSearchDateField.php index e7568f7c01..c67c7178cc 100644 --- a/src/applications/search/field/PhabricatorSearchDateField.php +++ b/src/applications/search/field/PhabricatorSearchDateField.php @@ -11,6 +11,10 @@ final class PhabricatorSearchDateField return $request->getStr($key); } + public function getValueForQuery($value) { + return $this->parseDateTime($value); + } + protected function validateControlValue($value) { if (!strlen($value)) { return; diff --git a/src/applications/search/field/PhabricatorSearchField.php b/src/applications/search/field/PhabricatorSearchField.php index 75a7453b4a..6944cf31fe 100644 --- a/src/applications/search/field/PhabricatorSearchField.php +++ b/src/applications/search/field/PhabricatorSearchField.php @@ -201,6 +201,10 @@ abstract class PhabricatorSearchField extends Phobject { return null; } + public function getValueForQuery($value) { + return $value; + } + /* -( Rendering Controls )------------------------------------------------- */ diff --git a/src/applications/search/field/PhabricatorSearchTokenizerField.php b/src/applications/search/field/PhabricatorSearchTokenizerField.php index 1feb23f8a4..b2699bb846 100644 --- a/src/applications/search/field/PhabricatorSearchTokenizerField.php +++ b/src/applications/search/field/PhabricatorSearchTokenizerField.php @@ -11,11 +11,18 @@ abstract class PhabricatorSearchTokenizerField return $this->getListFromRequest($request, $key); } + public function getValueForQuery($value) { + return $this->newDatasource() + ->setViewer($this->getViewer()) + ->evaluateTokens($value); + } + protected function newControl() { return id(new AphrontFormTokenizerControl()) ->setDatasource($this->newDatasource()); } + abstract protected function newDatasource(); } diff --git a/src/applications/search/field/PhabricatorSearchUsersField.php b/src/applications/search/field/PhabricatorSearchUsersField.php index 789ffb0a83..2c9620ac3c 100644 --- a/src/applications/search/field/PhabricatorSearchUsersField.php +++ b/src/applications/search/field/PhabricatorSearchUsersField.php @@ -8,9 +8,7 @@ final class PhabricatorSearchUsersField } protected function newDatasource() { - // TODO: Make this use PhabricatorPeopleUserFunctionDatasource once field - // support is a little more powerful. - return new PhabricatorPeopleDatasource(); + return new PhabricatorPeopleUserFunctionDatasource(); } protected function getValueFromRequest(AphrontRequest $request, $key) {