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