2011-03-08 02:25:47 +01:00
|
|
|
<?php
|
|
|
|
|
2012-03-10 00:46:25 +01:00
|
|
|
final class DiffusionBrowseController extends DiffusionController {
|
2011-03-08 02:25:47 +01:00
|
|
|
|
|
|
|
public function processRequest() {
|
2011-03-08 23:29:02 +01:00
|
|
|
$drequest = $this->diffusionRequest;
|
2013-04-23 20:12:02 +02:00
|
|
|
$is_file = false;
|
2011-03-08 02:25:47 +01:00
|
|
|
|
2012-12-14 23:07:39 +01:00
|
|
|
if ($this->getRequest()->getStr('before')) {
|
|
|
|
$is_file = true;
|
2013-04-23 20:12:02 +02:00
|
|
|
} else if ($this->getRequest()->getStr('grep') == '') {
|
2012-12-14 23:07:39 +01:00
|
|
|
$browse_query = DiffusionBrowseQuery::newFromDiffusionRequest($drequest);
|
2013-03-01 02:15:09 +01:00
|
|
|
$browse_query->setViewer($this->getRequest()->getUser());
|
2012-12-14 23:07:39 +01:00
|
|
|
$results = $browse_query->loadPaths();
|
|
|
|
$reason = $browse_query->getReasonForEmptyResultSet();
|
|
|
|
$is_file = ($reason == DiffusionBrowseQuery::REASON_IS_FILE);
|
|
|
|
}
|
2011-03-08 02:25:47 +01:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
if ($is_file) {
|
|
|
|
$controller = new DiffusionBrowseFileController($this->getRequest());
|
|
|
|
$controller->setDiffusionRequest($drequest);
|
|
|
|
$controller->setCurrentApplication($this->getCurrentApplication());
|
|
|
|
return $this->delegateToController($controller);
|
|
|
|
}
|
|
|
|
|
2011-03-08 03:21:43 +01:00
|
|
|
$content = array();
|
|
|
|
|
2012-04-24 03:36:25 +02:00
|
|
|
if ($drequest->getTagContent()) {
|
|
|
|
$title = 'Tag: '.$drequest->getSymbolicCommit();
|
|
|
|
|
|
|
|
$tag_view = new AphrontPanelView();
|
2013-02-13 23:50:15 +01:00
|
|
|
$tag_view->setHeader($title);
|
2012-04-24 03:36:25 +02:00
|
|
|
$tag_view->appendChild(
|
|
|
|
$this->markupText($drequest->getTagContent()));
|
|
|
|
|
|
|
|
$content[] = $tag_view;
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$content[] = $this->renderSearchForm();
|
2011-03-08 03:21:43 +01:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
if ($this->getRequest()->getStr('grep') != '') {
|
|
|
|
$content[] = $this->renderSearchResults();
|
2011-03-08 03:21:43 +01:00
|
|
|
|
2011-03-08 02:25:47 +01:00
|
|
|
} else {
|
2013-04-23 20:12:02 +02:00
|
|
|
if (!$results) {
|
|
|
|
$empty_result = new DiffusionEmptyResultView();
|
|
|
|
$empty_result->setDiffusionRequest($drequest);
|
|
|
|
$empty_result->setBrowseQuery($browse_query);
|
|
|
|
$empty_result->setView($this->getRequest()->getStr('view'));
|
|
|
|
$content[] = $empty_result;
|
|
|
|
|
|
|
|
} else {
|
2011-04-03 01:44:19 +02:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$phids = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
$data = $result->getLastCommitData();
|
|
|
|
if ($data) {
|
|
|
|
if ($data->getCommitDetail('authorPHID')) {
|
|
|
|
$phids[$data->getCommitDetail('authorPHID')] = true;
|
|
|
|
}
|
2011-04-03 01:44:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$phids = array_keys($phids);
|
|
|
|
$handles = $this->loadViewerHandles($phids);
|
2011-04-03 01:44:19 +02:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$browse_table = new DiffusionBrowseTableView();
|
|
|
|
$browse_table->setDiffusionRequest($drequest);
|
|
|
|
$browse_table->setHandles($handles);
|
|
|
|
$browse_table->setPaths($results);
|
|
|
|
$browse_table->setUser($this->getRequest()->getUser());
|
2011-03-08 02:25:47 +01:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$browse_panel = new AphrontPanelView();
|
|
|
|
$browse_panel->appendChild($browse_table);
|
|
|
|
$browse_panel->setNoBackground();
|
2011-03-08 02:25:47 +01:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$content[] = $browse_panel;
|
|
|
|
}
|
2011-03-08 02:25:47 +01:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$content[] = $this->buildOpenRevisions();
|
2011-10-02 21:52:54 +02:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$readme_content = $browse_query->renderReadme($results);
|
|
|
|
if ($readme_content) {
|
|
|
|
$readme_panel = new AphrontPanelView();
|
|
|
|
$readme_panel->setHeader('README');
|
|
|
|
$readme_panel->appendChild($readme_content);
|
2012-04-30 16:47:41 +02:00
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
$content[] = $readme_panel;
|
|
|
|
}
|
2012-04-30 16:47:41 +02:00
|
|
|
}
|
|
|
|
|
2011-03-13 01:17:34 +01:00
|
|
|
$nav = $this->buildSideNav('browse', false);
|
|
|
|
$nav->appendChild($content);
|
2011-03-08 02:25:47 +01:00
|
|
|
|
upgrade diffusion to use modern header UI and fix a few quirks
Summary:
upgrades are CrumbsView, HeaderView, PropertyListView, and ActionListView. I had to modify CrumbsView stuff a bit to handle the "advanced" diffusion crumbs.
Quirks fixed include making file tree view show up in diffusion, the page not have extra space when the file tree is hidden, links no longer breaking once you visit files (since without the change the files always got "/" appended and thus 404'd), and a differential quirk where it read "next step:" and that colon is a no no,
Test Plan: played around in diffusion and differential
Reviewers: epriestley
Reviewed By: epriestley
CC: aran, Korvin, chad
Maniphest Tasks: T2048, T2178
Differential Revision: https://secure.phabricator.com/D4169
2012-12-13 02:50:42 +01:00
|
|
|
$crumbs = $this->buildCrumbs(
|
|
|
|
array(
|
|
|
|
'branch' => true,
|
|
|
|
'path' => true,
|
|
|
|
'view' => 'browse',
|
|
|
|
));
|
|
|
|
$nav->setCrumbs($crumbs);
|
|
|
|
|
|
|
|
return $this->buildApplicationPage(
|
2011-03-13 01:17:34 +01:00
|
|
|
$nav,
|
2011-03-08 02:25:47 +01:00
|
|
|
array(
|
2012-04-24 03:36:25 +02:00
|
|
|
'title' => array(
|
|
|
|
nonempty(basename($drequest->getPath()), '/'),
|
|
|
|
$drequest->getRepository()->getCallsign().' Repository',
|
|
|
|
),
|
2011-03-08 02:25:47 +01:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2013-04-23 20:12:02 +02:00
|
|
|
|
|
|
|
private function renderSearchForm() {
|
|
|
|
$drequest = $this->getDiffusionRequest();
|
|
|
|
$form = id(new AphrontFormView())
|
|
|
|
->setUser($this->getRequest()->getUser())
|
|
|
|
->setMethod('GET');
|
|
|
|
|
|
|
|
switch ($drequest->getRepository()->getVersionControlSystem()) {
|
|
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_SVN:
|
|
|
|
$form->appendChild(pht('Search is not available in Subversion.'));
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$form
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormTextControl())
|
|
|
|
->setLabel(pht('Search Here'))
|
|
|
|
->setName('grep')
|
|
|
|
->setValue($this->getRequest()->getStr('grep'))
|
|
|
|
->setCaption(pht('Regular expression')))
|
|
|
|
->appendChild(
|
|
|
|
id(new AphrontFormSubmitControl())
|
|
|
|
->setValue(pht('Grep')));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function renderSearchResults() {
|
|
|
|
$drequest = $this->getDiffusionRequest();
|
|
|
|
$repository = $drequest->getRepository();
|
|
|
|
$results = array();
|
|
|
|
$no_data = pht('No results found.');
|
|
|
|
|
|
|
|
$limit = 100;
|
|
|
|
$page = $this->getRequest()->getInt('page', 0);
|
|
|
|
$pager = new AphrontPagerView();
|
|
|
|
$pager->setPageSize($limit);
|
|
|
|
$pager->setOffset($page);
|
|
|
|
$pager->setURI($this->getRequest()->getRequestURI(), 'page');
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
switch ($repository->getVersionControlSystem()) {
|
|
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_GIT:
|
|
|
|
$future = $repository->getLocalCommandFuture(
|
|
|
|
// NOTE: --perl-regexp is available only with libpcre compiled in.
|
|
|
|
'grep --extended-regexp --null -n --no-color -e %s %s -- %s',
|
|
|
|
$this->getRequest()->getStr('grep'),
|
|
|
|
$drequest->getStableCommitName(),
|
|
|
|
$drequest->getPath());
|
|
|
|
|
|
|
|
$binary_pattern = '/Binary file [^:]*:(.+) matches/';
|
|
|
|
$lines = new LinesOfALargeExecFuture($future);
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$result = null;
|
|
|
|
if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) {
|
|
|
|
$results[] = array_slice($result, 1);
|
|
|
|
} else if (preg_match($binary_pattern, $line, $result)) {
|
|
|
|
list(, $path) = $result;
|
|
|
|
$results[] = array($path, null, pht('Binary file'));
|
|
|
|
} else {
|
|
|
|
$results[] = array(null, null, $line);
|
|
|
|
}
|
|
|
|
if (count($results) > $page + $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($lines);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PhabricatorRepositoryType::REPOSITORY_TYPE_MERCURIAL:
|
|
|
|
$future = $repository->getLocalCommandFuture(
|
|
|
|
'grep --rev %s --print0 --line-number %s %s',
|
|
|
|
hgsprintf('ancestors(%s)', $drequest->getStableCommitName()),
|
|
|
|
$this->getRequest()->getStr('grep'),
|
|
|
|
$drequest->getPath());
|
|
|
|
|
|
|
|
$lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");
|
|
|
|
$parts = array();
|
|
|
|
foreach ($lines as $line) {
|
|
|
|
$parts[] = $line;
|
|
|
|
if (count($parts) == 4) {
|
|
|
|
list($path, $offset, $line, $string) = $parts;
|
|
|
|
$results[] = array($path, $line, $string);
|
|
|
|
if (count($results) > $page + $limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
$parts = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
unset($lines);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (CommandException $ex) {
|
2013-04-25 02:10:39 +02:00
|
|
|
$stderr = $ex->getStderr();
|
|
|
|
if ($stderr != '') {
|
|
|
|
return id(new AphrontErrorView())
|
|
|
|
->setTitle(pht('Search Error'))
|
|
|
|
->appendChild($stderr);
|
|
|
|
}
|
2013-04-23 20:12:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$results = array_slice($results, $page);
|
|
|
|
$results = $pager->sliceResults($results);
|
|
|
|
|
|
|
|
require_celerity_resource('syntax-highlighting-css');
|
|
|
|
|
|
|
|
// NOTE: This can be wrong because we may find the string inside the
|
|
|
|
// comment. But it's correct in most cases and highlighting the whole file
|
|
|
|
// would be too expensive.
|
|
|
|
$futures = array();
|
|
|
|
$engine = PhabricatorSyntaxHighlighter::newEngine();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
list($path, $line, $string) = $result;
|
|
|
|
$futures["{$path}:{$line}"] = $engine->getHighlightFuture(
|
|
|
|
$engine->getLanguageFromFilename($path),
|
|
|
|
ltrim($string));
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
Futures($futures)->limit(8)->resolveAll();
|
|
|
|
} catch (PhutilSyntaxHighlighterException $ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$rows = array();
|
|
|
|
foreach ($results as $result) {
|
|
|
|
list($path, $line, $string) = $result;
|
|
|
|
|
|
|
|
$href = $drequest->generateURI(array(
|
|
|
|
'action' => 'browse',
|
|
|
|
'path' => $path,
|
|
|
|
'line' => $line,
|
|
|
|
));
|
|
|
|
|
|
|
|
try {
|
|
|
|
$string = $futures["{$path}:{$line}"]->resolve();
|
|
|
|
} catch (PhutilSyntaxHighlighterException $ex) {
|
|
|
|
}
|
|
|
|
|
|
|
|
$string = phutil_tag(
|
|
|
|
'pre',
|
|
|
|
array('class' => 'PhabricatorMonospaced'),
|
|
|
|
$string);
|
|
|
|
|
|
|
|
$path = Filesystem::readablePath($path, $drequest->getPath());
|
|
|
|
|
|
|
|
$rows[] = array(
|
|
|
|
phutil_tag('a', array('href' => $href), $path),
|
|
|
|
$line,
|
|
|
|
$string,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$table = id(new AphrontTableView($rows))
|
|
|
|
->setClassName('remarkup-code')
|
|
|
|
->setHeaders(array(pht('Path'), pht('Line'), pht('String')))
|
|
|
|
->setColumnClasses(array('', 'n', 'wide'))
|
|
|
|
->setNoDataString($no_data);
|
|
|
|
|
|
|
|
return id(new AphrontPanelView())
|
|
|
|
->setHeader(pht('Search Results'))
|
|
|
|
->appendChild($table)
|
|
|
|
->appendChild($pager);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-24 03:36:25 +02:00
|
|
|
private function markupText($text) {
|
|
|
|
$engine = PhabricatorMarkupEngine::newDiffusionMarkupEngine();
|
2013-03-04 21:33:05 +01:00
|
|
|
$engine->setConfig('viewer', $this->getRequest()->getUser());
|
2013-02-13 23:50:15 +01:00
|
|
|
$text = $engine->markupText($text);
|
2012-04-24 03:36:25 +02:00
|
|
|
|
2013-01-18 09:32:58 +01:00
|
|
|
$text = phutil_tag(
|
2012-04-24 03:36:25 +02:00
|
|
|
'div',
|
|
|
|
array(
|
|
|
|
'class' => 'phabricator-remarkup',
|
|
|
|
),
|
|
|
|
$text);
|
|
|
|
|
|
|
|
return $text;
|
|
|
|
}
|
|
|
|
|
2011-03-08 02:25:47 +01:00
|
|
|
}
|