diff --git a/src/applications/almanac/query/AlmanacServiceQuery.php b/src/applications/almanac/query/AlmanacServiceQuery.php --- a/src/applications/almanac/query/AlmanacServiceQuery.php +++ b/src/applications/almanac/query/AlmanacServiceQuery.php @@ -192,4 +192,33 @@ 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 --- a/src/applications/almanac/query/AlmanacServiceSearchEngine.php +++ b/src/applications/almanac/query/AlmanacServiceSearchEngine.php @@ -14,18 +14,28 @@ 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 --- a/src/applications/almanac/typeahead/AlmanacServiceDatasource.php +++ b/src/applications/almanac/typeahead/AlmanacServiceDatasource.php @@ -16,10 +16,10 @@ $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 --- a/src/applications/repository/query/PhabricatorRepositorySearchEngine.php +++ b/src/applications/repository/query/PhabricatorRepositorySearchEngine.php @@ -27,7 +27,6 @@ public function buildQueryFromSavedQuery(PhabricatorSavedQuery $saved) { $query = id(new PhabricatorRepositoryQuery()) - ->setDefaultBuiltinOrder() ->needProjectPHIDs(true) ->needCommitCounts(true) ->needMostRecentCommits(true); @@ -43,10 +42,7 @@ $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 --- a/src/applications/search/engine/PhabricatorApplicationSearchEngine.php +++ b/src/applications/search/engine/PhabricatorApplicationSearchEngine.php @@ -580,6 +580,47 @@ /* -( 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 --- a/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php +++ b/src/infrastructure/query/policy/PhabricatorCursorPagedPolicyAwareQuery.php @@ -480,26 +480,6 @@ /** - * 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. * * In application UIs, we want to be able to present users with a small