diff --git a/src/applications/almanac/query/AlmanacServiceQuery.php b/src/applications/almanac/query/AlmanacServiceQuery.php index 002f8e71b1..d9aca2650e 100644 --- a/src/applications/almanac/query/AlmanacServiceQuery.php +++ b/src/applications/almanac/query/AlmanacServiceQuery.php @@ -192,4 +192,33 @@ final class AlmanacServiceQuery return parent::didFilterPage($services); } + public function getOrderableColumns() { + return parent::getOrderableColumns() + array( + 'name' => array( + 'table' => 'service', + 'column' => 'name', + 'type' => 'string', + 'unique' => true, + 'reverse' => true, + ), + ); + } + + protected function getValueMap($cursor, array $keys) { + $service = $this->loadCursorObject($cursor); + return array( + 'id' => $service->getID(), + 'name' => $service->getServiceName(), + ); + } + + public function getBuiltinOrders() { + return array( + 'name' => array( + 'vector' => array('name'), + 'name' => pht('Service Name'), + ), + ) + parent::getBuiltinOrders(); + } + } diff --git a/src/applications/almanac/query/AlmanacServiceSearchEngine.php b/src/applications/almanac/query/AlmanacServiceSearchEngine.php index fe51fd09f2..8610f86572 100644 --- a/src/applications/almanac/query/AlmanacServiceSearchEngine.php +++ b/src/applications/almanac/query/AlmanacServiceSearchEngine.php @@ -14,18 +14,28 @@ final class AlmanacServiceSearchEngine public function buildSavedQueryFromRequest(AphrontRequest $request) { $saved = new PhabricatorSavedQuery(); + $this->saveQueryOrder($saved, $request); + return $saved; } public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { $query = id(new AlmanacServiceQuery()); + $this->setQueryOrder($query, $saved); + return $query; } public function buildSearchForm( AphrontFormView $form, - PhabricatorSavedQuery $saved_query) {} + PhabricatorSavedQuery $saved) { + + $this->appendOrderFieldsToForm( + $form, + $saved, + new AlmanacServiceQuery()); + } protected function getURI($path) { return '/almanac/service/'.$path; diff --git a/src/applications/almanac/typeahead/AlmanacServiceDatasource.php b/src/applications/almanac/typeahead/AlmanacServiceDatasource.php index 00da7d4fd6..4cc0d1376a 100644 --- a/src/applications/almanac/typeahead/AlmanacServiceDatasource.php +++ b/src/applications/almanac/typeahead/AlmanacServiceDatasource.php @@ -16,10 +16,10 @@ final class AlmanacServiceDatasource $raw_query = $this->getRawQuery(); $services = id(new AlmanacServiceQuery()) - ->setViewer($viewer) ->withNamePrefix($raw_query) - ->setLimit($this->getLimit()) - ->execute(); + ->setOrder('name'); + + $services = $this->executeQuery($services); if ($services) { $handles = id(new PhabricatorHandleQuery()) diff --git a/src/applications/repository/query/PhabricatorRepositorySearchEngine.php b/src/applications/repository/query/PhabricatorRepositorySearchEngine.php index 1b8009fec5..8b1902e0fa 100644 --- a/src/applications/repository/query/PhabricatorRepositorySearchEngine.php +++ b/src/applications/repository/query/PhabricatorRepositorySearchEngine.php @@ -27,7 +27,6 @@ final class PhabricatorRepositorySearchEngine public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { $query = id(new PhabricatorRepositoryQuery()) - ->setDefaultBuiltinOrder() ->needProjectPHIDs(true) ->needCommitCounts(true) ->needMostRecentCommits(true); @@ -43,10 +42,7 @@ final class PhabricatorRepositorySearchEngine $query->withStatus($status); } - $order = $saved->getParameter('order'); - if ($order) { - $query->setOrder($order); - } + $this->setQueryOrder($query, $saved); $hosted = $saved->getParameter('hosted'); $hosted = idx($this->getHostedValues(), $hosted); diff --git a/src/applications/search/engine/PhabricatorApplicationSearchEngine.php b/src/applications/search/engine/PhabricatorApplicationSearchEngine.php index dec842f1f8..9705c64c89 100644 --- a/src/applications/search/engine/PhabricatorApplicationSearchEngine.php +++ b/src/applications/search/engine/PhabricatorApplicationSearchEngine.php @@ -580,6 +580,47 @@ abstract class PhabricatorApplicationSearchEngine { /* -( Result Ordering )---------------------------------------------------- */ + + /** + * Save order selection to a @{class:PhabricatorSavedQuery}. + */ + protected function saveQueryOrder( + PhabricatorSavedQuery $saved, + AphrontRequest $request) { + + $saved->setParameter('order', $request->getStr('order')); + + return $this; + } + + + /** + * Set query ordering from a saved value. + */ + protected function setQueryOrder( + PhabricatorCursorPagedPolicyAwareQuery $query, + PhabricatorSavedQuery $saved) { + + $order = $saved->getParameter('order'); + if (strlen($order)) { + $builtin = $query->getBuiltinOrders(); + if (isset($builtin[$order])) { + $query->setOrder($order); + } else { + // If the order is invalid or not available, we choose the first + // builtin order. This isn't always the default order for the query, + // but is the first value in the "Order" dropdown, and makes the query + // behavior more consistent with the UI. In queries where the two + // orders differ, this order is the preferred order for humans. + $query->setOrder(head_key($builtin)); + } + } + + return $this; + } + + + protected function appendOrderFieldsToForm( AphrontFormView $form, PhabricatorSavedQuery $saved, diff --git a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php index f0cff78e7c..7e7bfc2d35 100644 --- a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php +++ b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php @@ -479,26 +479,6 @@ abstract class PhabricatorCursorPagedPolicyAwareQuery } - /** - * Select the default builtin result ordering. - * - * This sets the result order to the default order among the builtin result - * orders (see @{method:getBuiltinOrders}). This is often the same as the - * query's builtin default order vector, but some objects have different - * default vectors (which are internally-facing) and builtin orders (which - * are user-facing). - * - * For example, repositories sort by ID internally (which is efficient and - * consistent), but sort by most recent commit as a default builtin (which - * better aligns with user expectations). - * - * @return this - */ - public function setDefaultBuiltinOrder() { - return $this->setOrder(head_key($this->getBuiltinOrders())); - } - - /** * Get builtin orders for this class. *