Something went wrong. Try again.
@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
Something went wrong. Try again.
16 kB · 615 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616<?php
abstract class DiffusionController extends PhabricatorController {
private $diffusionRequest;
/** * @return DiffusionRequest */ protected function getDiffusionRequest() { if (!$this->diffusionRequest) { throw new PhutilInvalidStateException('loadDiffusionContext'); } return $this->diffusionRequest; }
protected function hasDiffusionRequest() { return (bool)$this->diffusionRequest; }
public function willBeginExecution() { $request = $this->getRequest();
// Check if this is a VCS request, e.g. from "git clone", "hg clone", or // "svn checkout". If it is, we jump off into repository serving code to // process the request.
$serve_controller = new DiffusionServeController(); if ($serve_controller->isVCSRequest($request)) { return $this->delegateToController($serve_controller); }
return parent::willBeginExecution(); }
protected function loadDiffusionContextForEdit() { return $this->loadContext( array( 'edit' => true, )); }
protected function loadDiffusionContext() { return $this->loadContext(array()); }
private function loadContext(array $options) { $request = $this->getRequest(); $viewer = $this->getViewer(); require_celerity_resource('diffusion-repository-css');
$identifier = $this->getRepositoryIdentifierFromRequest($request);
$params = $options + array( 'repository' => $identifier, 'user' => $viewer, 'blob' => $this->getDiffusionBlobFromRequest($request), 'commit' => $request->getURIData('commit'), 'path' => $request->getURIData('path'), 'line' => $request->getURIData('line'), 'branch' => $request->getURIData('branch'), 'lint' => $request->getStr('lint'), );
$drequest = DiffusionRequest::newFromDictionary($params);
if (!$drequest) { return new Aphront404Response(); }
// If the client is making a request like "/diffusion/1/...", but the // repository has a different canonical path like "/diffusion/XYZ/...", // redirect them to the canonical path.
// Skip this redirect if the request is an AJAX request, like the requests // that Owners makes to complete and validate paths.
if (!$request->isAjax()) { $request_path = $request->getPath(); $repository = $drequest->getRepository();
$canonical_path = $repository->getCanonicalPath($request_path); if ($canonical_path !== null) { if ($canonical_path != $request_path) { return id(new AphrontRedirectResponse())->setURI($canonical_path); } } }
$this->diffusionRequest = $drequest;
return null; }
protected function getDiffusionBlobFromRequest(AphrontRequest $request) { return $request->getURIData('dblob'); }
protected function getRepositoryIdentifierFromRequest( AphrontRequest $request) {
$short_name = $request->getURIData('repositoryShortName'); if (phutil_nonempty_string($short_name)) { // If the short name ends in ".git", ignore it. $short_name = preg_replace('/\\.git\z/', '', $short_name); return $short_name; }
$identifier = $request->getURIData('repositoryCallsign'); if (phutil_nonempty_string($identifier)) { return $identifier; }
$id = $request->getURIData('repositoryID'); if (phutil_nonempty_string($id)) { return (int)$id; }
return null; }
public function buildCrumbs(array $spec = array()) { $crumbs = $this->buildApplicationCrumbs(); $crumb_list = $this->buildCrumbList($spec); foreach ($crumb_list as $crumb) { $crumbs->addCrumb($crumb); } return $crumbs; }
private function buildCrumbList(array $spec = array()) {
$spec = $spec + array( 'commit' => null, 'tags' => null, 'branches' => null, 'view' => null, );
$crumb_list = array();
// On the home page, we don't have a DiffusionRequest. if ($this->hasDiffusionRequest()) { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); } else { $drequest = null; $repository = null; }
if (!$repository) { return $crumb_list; }
$repository_name = $repository->getName();
if (!$spec['commit'] && !$spec['tags'] && !$spec['branches']) { $branch_name = $drequest->getBranch(); if (phutil_nonempty_string($branch_name)) { $repository_name .= ' ('.$branch_name.')'; } }
$crumb = id(new PHUICrumbView()) ->setName($repository_name); if (!$spec['view'] && !$spec['commit'] && !$spec['tags'] && !$spec['branches']) { $crumb_list[] = $crumb; return $crumb_list; } $crumb->setHref( $drequest->generateURI( array( 'action' => 'branch', 'path' => '/', ))); $crumb_list[] = $crumb;
$stable_commit = $drequest->getStableCommit(); $commit_name = $repository->formatCommitName($stable_commit, $local = true); $commit_uri = $repository->getCommitURI($stable_commit);
if ($spec['tags']) { $crumb = new PHUICrumbView(); if ($spec['commit']) { $crumb->setName(pht('Tags for %s', $commit_name)); $crumb->setHref($commit_uri); } else { $crumb->setName(pht('Tags')); } $crumb_list[] = $crumb; return $crumb_list; }
if ($spec['branches']) { $crumb = id(new PHUICrumbView()) ->setName(pht('Branches')); $crumb_list[] = $crumb; return $crumb_list; }
if ($spec['commit']) { $crumb = id(new PHUICrumbView()) ->setName($commit_name); $crumb_list[] = $crumb; return $crumb_list; }
$crumb = new PHUICrumbView(); $view = $spec['view'];
switch ($view) { case 'history': $view_name = pht('History'); break; case 'browse': $view_name = pht('Code'); break; case 'lint': $view_name = pht('Lint'); break; case 'change': $view_name = pht('Change'); break; case 'compare': $view_name = pht('Compare'); break; }
$crumb = id(new PHUICrumbView()) ->setName($view_name);
$crumb_list[] = $crumb; return $crumb_list; }
protected function callConduitWithDiffusionRequest( $method, array $params = array()) {
$user = $this->getRequest()->getUser(); $drequest = $this->getDiffusionRequest();
return DiffusionQuery::callConduitWithDiffusionRequest( $user, $drequest, $method, $params); }
protected function callConduitMethod($method, array $params = array()) { $user = $this->getViewer(); $drequest = $this->getDiffusionRequest();
return DiffusionQuery::callConduitWithDiffusionRequest( $user, $drequest, $method, $params, true); }
protected function getRepositoryControllerURI( PhabricatorRepository $repository, $path) { return $repository->getPathURI($path); }
protected function renderPathLinks(DiffusionRequest $drequest, $action) { $path = $drequest->getPath(); $path_parts = array_filter(explode('/', trim($path, '/')));
$divider = phutil_tag( 'span', array( 'class' => 'phui-header-divider', ), '/');
$links = array(); if ($path_parts) { $links[] = phutil_tag( 'a', array( 'href' => $drequest->generateURI( array( 'action' => $action, 'path' => '', )), ), $drequest->getRepository()->getDisplayName()); $links[] = $divider; $accum = ''; $last_key = last_key($path_parts); foreach ($path_parts as $key => $part) { $accum .= '/'.$part; if ($key === $last_key) { $links[] = $part; } else { $links[] = phutil_tag( 'a', array( 'href' => $drequest->generateURI( array( 'action' => $action, 'path' => $accum.'/', )), ), $part); $links[] = $divider; } } } else { $links[] = $drequest->getRepository()->getDisplayName(); $links[] = $divider; }
return $links; }
protected function renderStatusMessage($title, $body) { return id(new PHUIInfoView()) ->setSeverity(PHUIInfoView::SEVERITY_NOTICE) ->setTitle($title) ->setFlush(true) ->appendChild($body); }
protected function renderCommitHashTag(DiffusionRequest $drequest) { $stable_commit = $drequest->getStableCommit(); $commit = phutil_tag( 'a', array( 'href' => $drequest->generateURI( array( 'action' => 'commit', 'commit' => $stable_commit, )), ), $drequest->getRepository()->formatCommitName($stable_commit, true));
$tag = id(new PHUITagView()) ->setName($commit) ->setColor(PHUITagView::COLOR_INDIGO) ->setBorder(PHUITagView::BORDER_NONE) ->setType(PHUITagView::TYPE_SHADE);
return $tag; }
protected function renderBranchTag(DiffusionRequest $drequest) { $branch = $drequest->getBranch(); $branch = id(new PhutilUTF8StringTruncator()) ->setMaximumGlyphs(24) ->truncateString($branch);
$tag = id(new PHUITagView()) ->setName($branch) ->setColor(PHUITagView::COLOR_INDIGO) ->setBorder(PHUITagView::BORDER_NONE) ->setType(PHUITagView::TYPE_OUTLINE) ->addClass('diffusion-header-branch-tag');
return $tag; }
protected function renderSymbolicCommit(DiffusionRequest $drequest) { $symbolic_tag = $drequest->getSymbolicCommit(); $symbolic_tag = id(new PhutilUTF8StringTruncator()) ->setMaximumGlyphs(24) ->truncateString($symbolic_tag);
$tag = id(new PHUITagView()) ->setName($symbolic_tag) ->setIcon('fa-tag') ->setColor(PHUITagView::COLOR_INDIGO) ->setBorder(PHUITagView::BORDER_NONE) ->setType(PHUITagView::TYPE_SHADE);
return $tag; }
protected function renderDirectoryReadme(DiffusionBrowseResultSet $browse) { $readme_path = $browse->getReadmePath(); if ($readme_path === null) { return null; }
$drequest = $this->getDiffusionRequest(); $viewer = $this->getViewer(); $repository = $drequest->getRepository(); $repository_phid = $repository->getPHID(); $stable_commit = $drequest->getStableCommit();
$stable_commit_hash = PhabricatorHash::digestForIndex($stable_commit); $readme_path_hash = PhabricatorHash::digestForIndex($readme_path);
$cache = PhabricatorCaches::getMutableStructureCache(); $cache_key = "diffusion". ".repository({$repository_phid})". ".commit({$stable_commit_hash})". ".readme({$readme_path_hash})";
$readme_cache = $cache->getKey($cache_key); if (!$readme_cache) { try { $result = $this->callConduitWithDiffusionRequest( 'diffusion.filecontentquery', array( 'path' => $readme_path, 'commit' => $drequest->getStableCommit(), )); } catch (Exception $ex) { return null; }
$file_phid = $result['filePHID']; if (!$file_phid) { return null; }
$file = id(new PhabricatorFileQuery()) ->setViewer($viewer) ->withPHIDs(array($file_phid)) ->executeOne(); if (!$file) { return null; }
$corpus = $file->loadFileData();
$readme_cache = array( 'corpus' => $corpus, );
$cache->setKey($cache_key, $readme_cache); }
$readme_corpus = $readme_cache['corpus']; if (!strlen($readme_corpus)) { return null; }
return id(new DiffusionReadmeView()) ->setUser($this->getViewer()) ->setPath($readme_path) ->setContent($readme_corpus); }
protected function renderSearchForm($path = '/') { $drequest = $this->getDiffusionRequest(); $viewer = $this->getViewer(); switch ($drequest->getRepository()->getVersionControlSystem()) { case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN: return null; }
$search_term = $this->getRequest()->getStr('grep'); require_celerity_resource('diffusion-icons-css'); require_celerity_resource('diffusion-css');
$href = $drequest->generateURI(array( 'action' => 'browse', 'path' => $path, ));
$bar = javelin_tag( 'input', array( 'type' => 'text', 'id' => 'diffusion-search-input', 'name' => 'grep', 'class' => 'diffusion-search-input', 'sigil' => 'diffusion-search-input', 'placeholder' => pht('Pattern Search'), 'value' => $search_term, ));
$form = phabricator_form( $viewer, array( 'method' => 'GET', 'action' => $href, 'sigil' => 'diffusion-search-form', 'class' => 'diffusion-search-form', 'id' => 'diffusion-search-form', ), array( $bar, ));
$form_view = phutil_tag( 'div', array( 'class' => 'diffusion-search-form-view', ), $form);
return $form_view; }
protected function buildTabsView($key) { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository();
$view = new PHUIListView();
$view->addMenuItem( id(new PHUIListItemView()) ->setKey('home') ->setName(pht('Home')) ->setIcon('fa-home') ->setHref($drequest->generateURI( array( 'action' => 'branch', 'path' => '', ))) ->setSelected($key == 'home'));
$view->addMenuItem( id(new PHUIListItemView()) ->setKey('code') ->setName(pht('Code')) ->setIcon('fa-code') ->setHref($drequest->generateURI( array( 'action' => 'browse', ))) ->setSelected($key == 'code'));
if (!$repository->isSVN()) { $view->addMenuItem( id(new PHUIListItemView()) ->setKey('branch') ->setName(pht('Branches')) ->setIcon('fa-code-fork') ->setHref($drequest->generateURI( array( 'action' => 'branches', ))) ->setSelected($key == 'branch')); }
if (!$repository->isSVN()) { $view->addMenuItem( id(new PHUIListItemView()) ->setKey('tags') ->setName(pht('Tags')) ->setIcon('fa-tags') ->setHref($drequest->generateURI( array( 'action' => 'tags', ))) ->setSelected($key == 'tags')); }
$view->addMenuItem( id(new PHUIListItemView()) ->setKey('history') ->setName(pht('History')) ->setIcon('fa-history') ->setHref($drequest->generateURI( array( 'action' => 'history', ))) ->setSelected($key == 'history'));
return $view;
}
/** * @return PHUIBoxView|null */ protected function buildLocateFile() { $request = $this->getRequest(); $viewer = $request->getUser(); $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository();
$form_box = null; if ($repository->canUsePathTree()) { Javelin::initBehavior( 'diffusion-locate-file', array( 'controlID' => 'locate-control', 'inputID' => 'locate-input', 'symbolicCommit' => $drequest->getSymbolicCommit(), 'browseBaseURI' => (string)$drequest->generateURI( array( 'action' => 'browse', 'commit' => '', 'path' => '', )), 'uri' => (string)$drequest->generateURI( array( 'action' => 'pathtree', )), ));
$form = id(new AphrontFormView()) ->setUser($viewer) ->appendChild( id(new AphrontFormTypeaheadControl()) ->setHardpointID('locate-control') ->setID('locate-input') ->setPlaceholder(pht('Locate File'))); $form_box = id(new PHUIBoxView()) ->appendChild($form->buildLayoutView()) ->addClass('diffusion-profile-locate'); } return $form_box; }
}