From 0a3a3eae00e7b67c89a236562bd573e5f2ec9920 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 17 Jul 2014 15:48:36 -0700 Subject: [PATCH] Modernize global search typeahead datasource Summary: Ref T4420. Bring the global search up to date. Test Plan: Typed various things into global search. Reviewers: btrahan Reviewed By: btrahan Subscribers: epriestley Maniphest Tasks: T4420 Differential Revision: https://secure.phabricator.com/D9889 --- src/__phutil_library_map__.php | 6 ++ .../typeahead/DiffusionSymbolDatasource.php | 47 +++++++++++ .../PhabricatorApplicationDatasource.php | 43 ++++++++++ .../typeahead/PhabricatorPeopleDatasource.php | 31 ++++--- .../typeahead/PhabricatorSearchDatasource.php | 24 ++++++ ...torTypeaheadCommonDatasourceController.php | 82 +------------------ .../menu/PhabricatorMainMenuSearchView.php | 4 +- 7 files changed, 142 insertions(+), 95 deletions(-) create mode 100644 src/applications/diffusion/typeahead/DiffusionSymbolDatasource.php create mode 100644 src/applications/meta/typeahead/PhabricatorApplicationDatasource.php create mode 100644 src/applications/search/typeahead/PhabricatorSearchDatasource.php diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index c1997e3206..1010f952da 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -592,6 +592,7 @@ phutil_register_library_map(array( 'DiffusionSvnRawDiffQuery' => 'applications/diffusion/query/rawdiff/DiffusionSvnRawDiffQuery.php', 'DiffusionSvnRequest' => 'applications/diffusion/request/DiffusionSvnRequest.php', 'DiffusionSymbolController' => 'applications/diffusion/controller/DiffusionSymbolController.php', + 'DiffusionSymbolDatasource' => 'applications/diffusion/typeahead/DiffusionSymbolDatasource.php', 'DiffusionSymbolQuery' => 'applications/diffusion/query/DiffusionSymbolQuery.php', 'DiffusionTagListController' => 'applications/diffusion/controller/DiffusionTagListController.php', 'DiffusionTagListView' => 'applications/diffusion/view/DiffusionTagListView.php', @@ -1150,6 +1151,7 @@ phutil_register_library_map(array( 'PhabricatorApplicationCountdown' => 'applications/countdown/application/PhabricatorApplicationCountdown.php', 'PhabricatorApplicationDaemons' => 'applications/daemon/application/PhabricatorApplicationDaemons.php', 'PhabricatorApplicationDashboard' => 'applications/dashboard/application/PhabricatorApplicationDashboard.php', + 'PhabricatorApplicationDatasource' => 'applications/meta/typeahead/PhabricatorApplicationDatasource.php', 'PhabricatorApplicationDetailViewController' => 'applications/meta/controller/PhabricatorApplicationDetailViewController.php', 'PhabricatorApplicationDifferential' => 'applications/differential/application/PhabricatorApplicationDifferential.php', 'PhabricatorApplicationDiffusion' => 'applications/diffusion/application/PhabricatorApplicationDiffusion.php', @@ -2157,6 +2159,7 @@ phutil_register_library_map(array( 'PhabricatorSearchConfigOptions' => 'applications/search/config/PhabricatorSearchConfigOptions.php', 'PhabricatorSearchController' => 'applications/search/controller/PhabricatorSearchController.php', 'PhabricatorSearchDAO' => 'applications/search/storage/PhabricatorSearchDAO.php', + 'PhabricatorSearchDatasource' => 'applications/search/typeahead/PhabricatorSearchDatasource.php', 'PhabricatorSearchDeleteController' => 'applications/search/controller/PhabricatorSearchDeleteController.php', 'PhabricatorSearchDocument' => 'applications/search/storage/document/PhabricatorSearchDocument.php', 'PhabricatorSearchDocumentField' => 'applications/search/storage/document/PhabricatorSearchDocumentField.php', @@ -3301,6 +3304,7 @@ phutil_register_library_map(array( 'DiffusionSvnRawDiffQuery' => 'DiffusionRawDiffQuery', 'DiffusionSvnRequest' => 'DiffusionRequest', 'DiffusionSymbolController' => 'DiffusionController', + 'DiffusionSymbolDatasource' => 'PhabricatorTypeaheadDatasource', 'DiffusionSymbolQuery' => 'PhabricatorOffsetPagedQuery', 'DiffusionTagListController' => 'DiffusionController', 'DiffusionTagListView' => 'DiffusionView', @@ -3924,6 +3928,7 @@ phutil_register_library_map(array( 'PhabricatorApplicationCountdown' => 'PhabricatorApplication', 'PhabricatorApplicationDaemons' => 'PhabricatorApplication', 'PhabricatorApplicationDashboard' => 'PhabricatorApplication', + 'PhabricatorApplicationDatasource' => 'PhabricatorTypeaheadDatasource', 'PhabricatorApplicationDetailViewController' => 'PhabricatorApplicationsController', 'PhabricatorApplicationDifferential' => 'PhabricatorApplication', 'PhabricatorApplicationDiffusion' => 'PhabricatorApplication', @@ -5004,6 +5009,7 @@ phutil_register_library_map(array( 'PhabricatorSearchConfigOptions' => 'PhabricatorApplicationConfigOptions', 'PhabricatorSearchController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchDAO' => 'PhabricatorLiskDAO', + 'PhabricatorSearchDatasource' => 'PhabricatorTypeaheadCompositeDatasource', 'PhabricatorSearchDeleteController' => 'PhabricatorSearchBaseController', 'PhabricatorSearchDocument' => 'PhabricatorSearchDAO', 'PhabricatorSearchDocumentField' => 'PhabricatorSearchDAO', diff --git a/src/applications/diffusion/typeahead/DiffusionSymbolDatasource.php b/src/applications/diffusion/typeahead/DiffusionSymbolDatasource.php new file mode 100644 index 0000000000..bfc34806f9 --- /dev/null +++ b/src/applications/diffusion/typeahead/DiffusionSymbolDatasource.php @@ -0,0 +1,47 @@ +getViewer(); + $raw_query = $this->getRawQuery(); + + $results = array(); + + if (strlen($raw_query)) { + $symbols = id(new DiffusionSymbolQuery()) + ->setNamePrefix($raw_query) + ->setLimit(15) + ->needArcanistProjects(true) + ->needRepositories(true) + ->needPaths(true) + ->execute(); + foreach ($symbols as $symbol) { + $lang = $symbol->getSymbolLanguage(); + $name = $symbol->getSymbolName(); + $type = $symbol->getSymbolType(); + $proj = $symbol->getArcanistProject()->getName(); + + $results[] = id(new PhabricatorTypeaheadResult()) + ->setName($name) + ->setURI($symbol->getURI()) + ->setPHID(md5($symbol->getURI())) // Just needs to be unique. + ->setDisplayName($name) + ->setDisplayType(strtoupper($lang).' '.ucwords($type).' ('.$proj.')') + ->setPriorityType('symb'); + } + } + + return $results; + } + +} diff --git a/src/applications/meta/typeahead/PhabricatorApplicationDatasource.php b/src/applications/meta/typeahead/PhabricatorApplicationDatasource.php new file mode 100644 index 0000000000..f092b2a81d --- /dev/null +++ b/src/applications/meta/typeahead/PhabricatorApplicationDatasource.php @@ -0,0 +1,43 @@ +getViewer(); + $raw_query = $this->getRawQuery(); + + $results = array(); + + $applications = PhabricatorApplication::getAllInstalledApplications(); + foreach ($applications as $application) { + $uri = $application->getTypeaheadURI(); + if (!$uri) { + continue; + } + $name = $application->getName().' '.$application->getShortDescription(); + $img = 'apps-'.$application->getIconName().'-dark-large'; + $results[] = id(new PhabricatorTypeaheadResult()) + ->setName($name) + ->setURI($uri) + ->setPHID($application->getPHID()) + ->setPriorityString($application->getName()) + ->setDisplayName($application->getName()) + ->setDisplayType($application->getShortDescription()) + ->setImageuRI($application->getIconURI()) + ->setPriorityType('apps') + ->setImageSprite('phabricator-search-icon sprite-apps-large '.$img); + } + + return $results; + } + +} diff --git a/src/applications/people/typeahead/PhabricatorPeopleDatasource.php b/src/applications/people/typeahead/PhabricatorPeopleDatasource.php index 26e7cbe9f2..5b1a91d953 100644 --- a/src/applications/people/typeahead/PhabricatorPeopleDatasource.php +++ b/src/applications/people/typeahead/PhabricatorPeopleDatasource.php @@ -3,6 +3,18 @@ final class PhabricatorPeopleDatasource extends PhabricatorTypeaheadDatasource { + private $enrichResults; + + /** + * Controls enriched rendering, for global search. This is a bit hacky and + * should probably be handled in a more general way, but is fairly reasonable + * for now. + */ + public function setEnrichResults($enrich) { + $this->enrichResults = $enrich; + return $this; + } + public function getPlaceholderText() { return pht('Type a username...'); } @@ -71,16 +83,14 @@ final class PhabricatorPeopleDatasource } } - // TODO: Restore this when mainsearch moves here. - /* - - if ($need_rich_data) { + if ($this->enrichResults && $users) { $phids = mpull($users, 'getPHID'); - $handles = $this->loadViewerHandles($phids); + $handles = id(new PhabricatorHandleQuery()) + ->setViewer($viewer) + ->withPHIDs($phids) + ->execute(); } - */ - foreach ($users as $user) { $closed = null; if ($user->getIsDisabled()) { @@ -98,10 +108,7 @@ final class PhabricatorPeopleDatasource ->setPriorityType('user') ->setClosed($closed); - // TODO: Restore this too. - /* - - if ($need_rich_data) { + if ($this->enrichResults) { $display_type = 'User'; if ($user->getIsAdmin()) { $display_type = 'Administrator'; @@ -110,8 +117,6 @@ final class PhabricatorPeopleDatasource $result->setImageURI($handles[$user->getPHID()]->getImageURI()); } - */ - $results[] = $result; } diff --git a/src/applications/search/typeahead/PhabricatorSearchDatasource.php b/src/applications/search/typeahead/PhabricatorSearchDatasource.php new file mode 100644 index 0000000000..9cbd8d477a --- /dev/null +++ b/src/applications/search/typeahead/PhabricatorSearchDatasource.php @@ -0,0 +1,24 @@ +setEnrichResults(true), + new PhabricatorProjectDatasource(), + new PhabricatorApplicationDatasource(), + new PhabricatorTypeaheadMonogramDatasource(), + new DiffusionSymbolDatasource(), + ); + } + +} diff --git a/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php b/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php index 300d265a2c..a29d461c3b 100644 --- a/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php +++ b/src/applications/typeahead/controller/PhabricatorTypeaheadCommonDatasourceController.php @@ -19,24 +19,12 @@ final class PhabricatorTypeaheadCommonDatasourceController $query = $request->getStr('q'); $raw_query = $request->getStr('raw'); - $need_rich_data = false; - $need_users = false; - $need_applications = false; $need_projs = false; $need_upforgrabs = false; $need_noproject = false; - $need_symbols = false; - $need_jump_objects = false; + $need_rich_data = false; switch ($this->type) { - case 'mainsearch': - $need_users = true; - $need_applications = true; - $need_rich_data = true; - $need_symbols = true; - $need_projs = true; - $need_jump_objects = true; - break; case 'searchowner': $need_users = true; $need_upforgrabs = true; @@ -186,74 +174,6 @@ final class PhabricatorTypeaheadCommonDatasourceController } } - if ($need_applications) { - $applications = PhabricatorApplication::getAllInstalledApplications(); - foreach ($applications as $application) { - $uri = $application->getTypeaheadURI(); - if (!$uri) { - continue; - } - $name = $application->getName().' '.$application->getShortDescription(); - $img = 'apps-'.$application->getIconName().'-dark-large'; - $results[] = id(new PhabricatorTypeaheadResult()) - ->setName($name) - ->setURI($uri) - ->setPHID($application->getPHID()) - ->setPriorityString($application->getName()) - ->setDisplayName($application->getName()) - ->setDisplayType($application->getShortDescription()) - ->setImageuRI($application->getIconURI()) - ->setPriorityType('apps') - ->setImageSprite('phabricator-search-icon sprite-apps-large '.$img); - } - } - - if ($need_symbols) { - $symbols = id(new DiffusionSymbolQuery()) - ->setNamePrefix($query) - ->setLimit(15) - ->needArcanistProjects(true) - ->needRepositories(true) - ->needPaths(true) - ->execute(); - foreach ($symbols as $symbol) { - $lang = $symbol->getSymbolLanguage(); - $name = $symbol->getSymbolName(); - $type = $symbol->getSymbolType(); - $proj = $symbol->getArcanistProject()->getName(); - - $results[] = id(new PhabricatorTypeaheadResult()) - ->setName($name) - ->setURI($symbol->getURI()) - ->setPHID(md5($symbol->getURI())) // Just needs to be unique. - ->setDisplayName($name) - ->setDisplayType(strtoupper($lang).' '.ucwords($type).' ('.$proj.')') - ->setPriorityType('symb'); - } - } - - if ($need_jump_objects) { - $objects = id(new PhabricatorObjectQuery()) - ->setViewer($viewer) - ->withNames(array($raw_query)) - ->execute(); - if ($objects) { - $handles = id(new PhabricatorHandleQuery()) - ->setViewer($viewer) - ->withPHIDs(mpull($objects, 'getPHID')) - ->execute(); - $handle = head($handles); - if ($handle) { - $results[] = id(new PhabricatorTypeaheadResult()) - ->setName($handle->getFullName()) - ->setDisplayType($handle->getTypeName()) - ->setURI($handle->getURI()) - ->setPHID($handle->getPHID()) - ->setPriorityType('jump'); - } - } - } - $content = mpull($results, 'getWireFormat'); if ($request->isAjax()) { diff --git a/src/view/page/menu/PhabricatorMainMenuSearchView.php b/src/view/page/menu/PhabricatorMainMenuSearchView.php index 3c4b677c9f..9c8058c81e 100644 --- a/src/view/page/menu/PhabricatorMainMenuSearchView.php +++ b/src/view/page/menu/PhabricatorMainMenuSearchView.php @@ -35,13 +35,15 @@ final class PhabricatorMainMenuSearchView extends AphrontView { ), ''); + $search_datasource = new PhabricatorSearchDatasource(); + Javelin::initBehavior( 'phabricator-search-typeahead', array( 'id' => $target_id, 'input' => $search_id, 'button' => $button_id, - 'src' => '/typeahead/common/mainsearch/', + 'src' => $search_datasource->getDatasourceURI(), 'limit' => 10, 'placeholder' => pht('Search'), ));