Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / controller / DiffusionSymbolController.php
blobc4ce99fa878e9957fae239a4e7287ae907351f79
1 <?php
3 final class DiffusionSymbolController extends DiffusionController {
5 public function handleRequest(AphrontRequest $request) {
6 $viewer = $this->getViewer();
8 // See T13638 for discussion of escaping.
9 $name = $request->getURIData('name');
10 $name = phutil_unescape_uri_path_component($name);
12 $query = id(new DiffusionSymbolQuery())
13 ->setViewer($viewer)
14 ->setName($name);
16 if ($request->getStr('context')) {
17 $query->setContext($request->getStr('context'));
20 if ($request->getStr('type')) {
21 $query->setType($request->getStr('type'));
24 if ($request->getStr('lang')) {
25 $query->setLanguage($request->getStr('lang'));
28 $repos = array();
29 if ($request->getStr('repositories')) {
30 $phids = $request->getStr('repositories');
31 $phids = explode(',', $phids);
32 $phids = array_filter($phids);
34 if ($phids) {
35 $repos = id(new PhabricatorRepositoryQuery())
36 ->setViewer($request->getUser())
37 ->withPHIDs($phids)
38 ->execute();
40 $repo_phids = mpull($repos, 'getPHID');
41 if ($repo_phids) {
42 $query->withRepositoryPHIDs($repo_phids);
47 $query->needPaths(true);
48 $query->needRepositories(true);
50 $symbols = $query->execute();
52 $external_query = id(new DiffusionExternalSymbolQuery())
53 ->withNames(array($name));
55 if ($request->getStr('context')) {
56 $external_query->withContexts(array($request->getStr('context')));
59 if ($request->getStr('type')) {
60 $external_query->withTypes(array($request->getStr('type')));
63 if ($request->getStr('lang')) {
64 $external_query->withLanguages(array($request->getStr('lang')));
67 if ($request->getStr('path')) {
68 $external_query->withPaths(array($request->getStr('path')));
71 if ($request->getInt('line')) {
72 $external_query->withLines(array($request->getInt('line')));
75 if ($request->getInt('char')) {
76 $external_query->withCharacterPositions(
77 array(
78 $request->getInt('char'),
79 ));
82 if ($repos) {
83 $external_query->withRepositories($repos);
86 $external_sources = id(new PhutilClassMapQuery())
87 ->setAncestorClass('DiffusionExternalSymbolsSource')
88 ->execute();
90 $results = array($symbols);
91 foreach ($external_sources as $source) {
92 $source_results = $source->executeQuery($external_query);
94 if (!is_array($source_results)) {
95 throw new Exception(
96 pht(
97 'Expected a list of results from external symbol source "%s".',
98 get_class($source)));
101 try {
102 assert_instances_of($source_results, 'PhabricatorRepositorySymbol');
103 } catch (InvalidArgumentException $ex) {
104 throw new Exception(
105 pht(
106 'Expected a list of PhabricatorRepositorySymbol objects '.
107 'from external symbol source "%s".',
108 get_class($source)));
111 $results[] = $source_results;
113 $symbols = array_mergev($results);
115 if ($request->getBool('jump') && count($symbols) == 1) {
116 // If this is a clickthrough from Differential, just jump them
117 // straight to the target if we got a single hit.
118 $symbol = head($symbols);
119 return id(new AphrontRedirectResponse())
120 ->setIsExternal($symbol->isExternal())
121 ->setURI($symbol->getURI());
124 $rows = array();
125 foreach ($symbols as $symbol) {
126 $href = $symbol->getURI();
128 if ($symbol->isExternal()) {
129 $source = $symbol->getSource();
130 $location = $symbol->getLocation();
131 } else {
132 $repo = $symbol->getRepository();
133 $file = $symbol->getPath();
134 $line = $symbol->getLineNumber();
136 $source = $repo->getMonogram();
137 $location = $file.':'.$line;
139 $location = phutil_tag(
140 'a',
141 array(
142 'href' => $href,
144 $location);
146 $rows[] = array(
147 $symbol->getSymbolType(),
148 $symbol->getSymbolContext(),
149 $symbol->getSymbolName(),
150 $symbol->getSymbolLanguage(),
151 $source,
152 $location,
156 $table = new AphrontTableView($rows);
157 $table->setHeaders(
158 array(
159 pht('Type'),
160 pht('Context'),
161 pht('Name'),
162 pht('Language'),
163 pht('Source'),
164 pht('Location'),
166 $table->setColumnClasses(
167 array(
170 'pri',
175 $table->setNoDataString(
176 pht('No matching symbol could be found in any indexed repository.'));
178 $header = id(new PHUIHeaderView())
179 ->setHeader(pht('Similar Symbols'))
180 ->setHeaderIcon('fa-bullseye');
182 $crumbs = $this->buildApplicationCrumbs();
183 $crumbs->addTextCrumb(pht('Find Symbol'));
184 $crumbs->setBorder(true);
186 $view = id(new PHUITwoColumnView())
187 ->setHeader($header)
188 ->setFooter(array(
189 $table,
192 return $this->newPage()
193 ->setTitle(pht('Find Symbol'))
194 ->setCrumbs($crumbs)
195 ->appendChild($view);