Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / diffusion / controller / DiffusionExternalController.php
blob07a131c826a85eedee9603607dc050621c15e940
1 <?php
3 final class DiffusionExternalController extends DiffusionController {
5 public function shouldAllowPublic() {
6 return true;
9 public function handleRequest(AphrontRequest $request) {
10 $uri = $request->getStr('uri');
11 $id = $request->getStr('id');
13 $repositories = id(new PhabricatorRepositoryQuery())
14 ->setViewer($request->getUser())
15 ->execute();
17 if ($uri) {
18 $uri_path = id(new PhutilURI($uri))->getPath();
19 $matches = array();
21 // Try to figure out which tracked repository this external lives in by
22 // comparing repository metadata. We look for an exact match, but accept
23 // a partial match.
25 foreach ($repositories as $key => $repository) {
26 $remote_uri = new PhutilURI($repository->getRemoteURI());
27 if ($remote_uri->getPath() == $uri_path) {
28 $matches[$key] = 1;
30 if ($repository->getPublicCloneURI() == $uri) {
31 $matches[$key] = 2;
33 if ($repository->getRemoteURI() == $uri) {
34 $matches[$key] = 3;
38 arsort($matches);
39 $best_match = head_key($matches);
41 if ($best_match) {
42 $repository = $repositories[$best_match];
43 $redirect = $repository->generateURI(
44 array(
45 'action' => 'browse',
46 'branch' => $repository->getDefaultBranch(),
47 'commit' => $id,
48 ));
50 return id(new AphrontRedirectResponse())->setURI($redirect);
54 // TODO: This is a rare query but does a table scan, add a key?
56 $commits = id(new PhabricatorRepositoryCommit())->loadAllWhere(
57 'commitIdentifier = %s',
58 $id);
60 if (empty($commits)) {
61 $desc = null;
62 if (strlen($uri)) {
63 $desc = pht('"%s", at "%s"', $uri, $id);
64 } else {
65 $desc = pht('"%s"', $id);
68 $content = id(new PHUIInfoView())
69 ->setTitle(pht('Unknown External'))
70 ->setSeverity(PHUIInfoView::SEVERITY_WARNING)
71 ->appendChild(phutil_tag(
72 'p',
73 array(),
74 pht(
75 'This external (%s) does not appear in any tracked '.
76 'repository. It may exist in an untracked repository that '.
77 'Diffusion does not know about.',
78 $desc)));
79 } else if (count($commits) == 1) {
80 $commit = head($commits);
81 $repo = $repositories[$commit->getRepositoryID()];
82 $redirect = $repo->generateURI(
83 array(
84 'action' => 'browse',
85 'branch' => $repo->getDefaultBranch(),
86 'commit' => $commit->getCommitIdentifier(),
87 ));
88 return id(new AphrontRedirectResponse())->setURI($redirect);
89 } else {
91 $rows = array();
92 foreach ($commits as $commit) {
93 $repo = $repositories[$commit->getRepositoryID()];
94 $href = $repo->generateURI(
95 array(
96 'action' => 'browse',
97 'branch' => $repo->getDefaultBranch(),
98 'commit' => $commit->getCommitIdentifier(),
99 ));
100 $rows[] = array(
101 phutil_tag(
102 'a',
103 array(
104 'href' => $href,
106 $commit->getURI()),
107 $commit->loadCommitData()->getSummary(),
111 $table = new AphrontTableView($rows);
112 $table->setHeaders(
113 array(
114 pht('Commit'),
115 pht('Description'),
117 $table->setColumnClasses(
118 array(
119 'pri',
120 'wide',
123 $caption = id(new PHUIInfoView())
124 ->setSeverity(PHUIInfoView::SEVERITY_NOTICE)
125 ->appendChild(
126 pht('This external reference matches multiple known commits.'));
128 $content = new PHUIObjectBoxView();
129 $content->setHeaderText(pht('Multiple Matching Commits'));
130 $content->setInfoView($caption);
131 $content->setTable($table);
134 $crumbs = $this->buildApplicationCrumbs();
135 $crumbs->addTextCrumb(pht('External'));
137 return $this->newPage()
138 ->setTitle(pht('Unresolvable External'))
139 ->setCrumbs($crumbs)
140 ->appendChild($content);