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.
5.9 kB · 209 lines
PHP
at commit dcd7a316
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210<?php
final class DiffusionBrowseSearchController extends DiffusionBrowseController {
public function processRequest() { $drequest = $this->diffusionRequest;
$actions = $this->buildActionView($drequest); $properties = $this->buildPropertyView($drequest, $actions);
$object_box = id(new PHUIObjectBoxView()) ->setHeader($this->buildHeaderView($drequest)) ->addPropertyList($properties);
$content = array();
$content[] = $object_box; $content[] = $this->renderSearchForm($collapsed = false); $content[] = $this->renderSearchResults();
$crumbs = $this->buildCrumbs( array( 'branch' => true, 'path' => true, 'view' => 'browse', ));
return $this->buildApplicationPage( array( $crumbs, $content, ), array( 'device' => true, 'title' => array( nonempty(basename($drequest->getPath()), '/'), $drequest->getRepository()->getCallsign().' Repository', ), )); }
private function renderSearchResults() { $drequest = $this->getDiffusionRequest(); $repository = $drequest->getRepository(); $results = array();
$limit = 100; $page = $this->getRequest()->getInt('page', 0); $pager = new AphrontPagerView(); $pager->setPageSize($limit); $pager->setOffset($page); $pager->setURI($this->getRequest()->getRequestURI(), 'page');
$search_mode = null;
try { if (strlen($this->getRequest()->getStr('grep'))) { $search_mode = 'grep'; $query_string = $this->getRequest()->getStr('grep'); $results = $this->callConduitWithDiffusionRequest( 'diffusion.searchquery', array( 'grep' => $query_string, 'stableCommitName' => $drequest->getStableCommitName(), 'path' => $drequest->getPath(), 'limit' => $limit + 1, 'offset' => $page, )); } else { // Filename search. $search_mode = 'find'; $query_string = $this->getRequest()->getStr('find'); $results = $this->callConduitWithDiffusionRequest( 'diffusion.querypaths', array( 'pattern' => $query_string, 'commit' => $drequest->getStableCommitName(), 'path' => $drequest->getPath(), 'limit' => $limit + 1, 'offset' => $page, )); } } catch (ConduitException $ex) { $err = $ex->getErrorDescription(); if ($err != '') { return id(new AphrontErrorView()) ->setTitle(pht('Search Error')) ->appendChild($err); } }
$results = $pager->sliceResults($results);
if ($search_mode == 'grep') { $table = $this->renderGrepResults($results); $header = pht( 'File content matching "%s" under "%s"', $query_string, nonempty($drequest->getPath(), '/')); } else { $table = $this->renderFindResults($results); $header = pht( 'Paths matching "%s" under "%s"', $query_string, nonempty($drequest->getPath(), '/')); }
$box = id(new PHUIObjectBoxView()) ->setHeaderText($header) ->appendChild($table);
$pager_box = id(new PHUIBoxView()) ->addMargin(PHUI::MARGIN_LARGE) ->appendChild($pager);
return array($box, $pager_box); }
private function renderGrepResults(array $results) { $drequest = $this->getDiffusionRequest();
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( pht( 'The pattern you searched for was not found in the content of any '. 'files.'));
return $table; }
private function renderFindResults(array $results) { $drequest = $this->getDiffusionRequest();
$rows = array(); foreach ($results as $result) { $href = $drequest->generateURI(array( 'action' => 'browse', 'path' => $result, ));
$readable = Filesystem::readablePath($result, $drequest->getPath());
$rows[] = array( phutil_tag('a', array('href' => $href), $readable), ); }
$table = id(new AphrontTableView($rows)) ->setHeaders(array(pht('Path'))) ->setColumnClasses(array('wide')) ->setNoDataString( pht( 'The pattern you searched for did not match the names of any '. 'files.'));
return $table; }
}