[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/controller/ -> DiffusionBrowseSearchController.php (source)

   1  <?php
   2  
   3  final class DiffusionBrowseSearchController extends DiffusionBrowseController {
   4  
   5    public function processRequest() {
   6      $drequest = $this->diffusionRequest;
   7  
   8      $actions = $this->buildActionView($drequest);
   9      $properties = $this->buildPropertyView($drequest, $actions);
  10  
  11      $object_box = id(new PHUIObjectBoxView())
  12        ->setHeader($this->buildHeaderView($drequest))
  13        ->addPropertyList($properties);
  14  
  15      $content = array();
  16  
  17      $content[] = $object_box;
  18      $content[] = $this->renderSearchForm($collapsed = false);
  19      $content[] = $this->renderSearchResults();
  20  
  21      $crumbs = $this->buildCrumbs(
  22        array(
  23          'branch' => true,
  24          'path'   => true,
  25          'view'   => 'browse',
  26        ));
  27  
  28      return $this->buildApplicationPage(
  29        array(
  30          $crumbs,
  31          $content,
  32        ),
  33        array(
  34          'title' => array(
  35            nonempty(basename($drequest->getPath()), '/'),
  36            $drequest->getRepository()->getCallsign().' Repository',
  37          ),
  38        ));
  39    }
  40  
  41    private function renderSearchResults() {
  42      $drequest = $this->getDiffusionRequest();
  43      $repository = $drequest->getRepository();
  44      $results = array();
  45  
  46      $limit = 100;
  47      $page = $this->getRequest()->getInt('page', 0);
  48      $pager = new AphrontPagerView();
  49      $pager->setPageSize($limit);
  50      $pager->setOffset($page);
  51      $pager->setURI($this->getRequest()->getRequestURI(), 'page');
  52  
  53      $search_mode = null;
  54  
  55      try {
  56        if (strlen($this->getRequest()->getStr('grep'))) {
  57          $search_mode = 'grep';
  58          $query_string = $this->getRequest()->getStr('grep');
  59          $results = $this->callConduitWithDiffusionRequest(
  60            'diffusion.searchquery',
  61            array(
  62              'grep' => $query_string,
  63              'commit' => $drequest->getStableCommit(),
  64              'path' => $drequest->getPath(),
  65              'limit' => $limit + 1,
  66              'offset' => $page,
  67            ));
  68        } else { // Filename search.
  69          $search_mode = 'find';
  70          $query_string = $this->getRequest()->getStr('find');
  71          $results = $this->callConduitWithDiffusionRequest(
  72            'diffusion.querypaths',
  73            array(
  74              'pattern' => $query_string,
  75              'commit' => $drequest->getStableCommit(),
  76              'path' => $drequest->getPath(),
  77              'limit' => $limit + 1,
  78              'offset' => $page,
  79            ));
  80        }
  81      } catch (ConduitException $ex) {
  82        $err = $ex->getErrorDescription();
  83        if ($err != '') {
  84          return id(new AphrontErrorView())
  85            ->setTitle(pht('Search Error'))
  86            ->appendChild($err);
  87        }
  88      }
  89  
  90      $results = $pager->sliceResults($results);
  91  
  92      if ($search_mode == 'grep') {
  93        $table = $this->renderGrepResults($results);
  94        $header = pht(
  95          'File content matching "%s" under "%s"',
  96          $query_string,
  97          nonempty($drequest->getPath(), '/'));
  98      } else {
  99        $table = $this->renderFindResults($results);
 100        $header = pht(
 101          'Paths matching "%s" under "%s"',
 102          $query_string,
 103          nonempty($drequest->getPath(), '/'));
 104      }
 105  
 106      $box = id(new PHUIObjectBoxView())
 107        ->setHeaderText($header)
 108        ->appendChild($table);
 109  
 110      $pager_box = id(new PHUIBoxView())
 111        ->addMargin(PHUI::MARGIN_LARGE)
 112        ->appendChild($pager);
 113  
 114      return array($box, $pager_box);
 115    }
 116  
 117    private function renderGrepResults(array $results) {
 118      $drequest = $this->getDiffusionRequest();
 119  
 120      require_celerity_resource('syntax-highlighting-css');
 121  
 122      // NOTE: This can be wrong because we may find the string inside the
 123      // comment. But it's correct in most cases and highlighting the whole file
 124      // would be too expensive.
 125      $futures = array();
 126      $engine = PhabricatorSyntaxHighlighter::newEngine();
 127      foreach ($results as $result) {
 128        list($path, $line, $string) = $result;
 129        $futures["{$path}:{$line}"] = $engine->getHighlightFuture(
 130          $engine->getLanguageFromFilename($path),
 131          ltrim($string));
 132      }
 133  
 134      try {
 135        Futures($futures)->limit(8)->resolveAll();
 136      } catch (PhutilSyntaxHighlighterException $ex) {}
 137  
 138      $rows = array();
 139      foreach ($results as $result) {
 140        list($path, $line, $string) = $result;
 141  
 142        $href = $drequest->generateURI(array(
 143          'action' => 'browse',
 144          'path' => $path,
 145          'line' => $line,
 146        ));
 147  
 148        try {
 149          $string = $futures["{$path}:{$line}"]->resolve();
 150        } catch (PhutilSyntaxHighlighterException $ex) {}
 151  
 152        $string = phutil_tag(
 153          'pre',
 154          array('class' => 'PhabricatorMonospaced'),
 155          $string);
 156  
 157        $path = Filesystem::readablePath($path, $drequest->getPath());
 158  
 159        $rows[] = array(
 160          phutil_tag('a', array('href' => $href), $path),
 161          $line,
 162          $string,
 163        );
 164      }
 165  
 166      $table = id(new AphrontTableView($rows))
 167        ->setClassName('remarkup-code')
 168        ->setHeaders(array(pht('Path'), pht('Line'), pht('String')))
 169        ->setColumnClasses(array('', 'n', 'wide'))
 170        ->setNoDataString(
 171          pht(
 172            'The pattern you searched for was not found in the content of any '.
 173            'files.'));
 174  
 175      return $table;
 176    }
 177  
 178    private function renderFindResults(array $results) {
 179      $drequest = $this->getDiffusionRequest();
 180  
 181      $rows = array();
 182      foreach ($results as $result) {
 183        $href = $drequest->generateURI(array(
 184          'action' => 'browse',
 185          'path' => $result,
 186        ));
 187  
 188        $readable = Filesystem::readablePath($result, $drequest->getPath());
 189  
 190        $rows[] = array(
 191          phutil_tag('a', array('href' => $href), $readable),
 192        );
 193      }
 194  
 195      $table = id(new AphrontTableView($rows))
 196        ->setHeaders(array(pht('Path')))
 197        ->setColumnClasses(array('wide'))
 198        ->setNoDataString(
 199          pht(
 200            'The pattern you searched for did not match the names of any '.
 201            'files.'));
 202  
 203      return $table;
 204    }
 205  
 206  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1