[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/conduit/ -> DiffusionSearchQueryConduitAPIMethod.php (source)

   1  <?php
   2  
   3  final class DiffusionSearchQueryConduitAPIMethod
   4    extends DiffusionQueryConduitAPIMethod {
   5  
   6    public function getAPIMethodName() {
   7      return 'diffusion.searchquery';
   8    }
   9  
  10    public function getMethodDescription() {
  11      return 'Search (grep) a repository at a specific path and commit.';
  12    }
  13  
  14    public function defineReturnType() {
  15      return 'array';
  16    }
  17  
  18    protected function defineCustomParamTypes() {
  19      return array(
  20        'path' => 'required string',
  21        'commit' => 'optional string',
  22        'grep' => 'required string',
  23        'limit' => 'optional int',
  24        'offset' => 'optional int',
  25      );
  26    }
  27  
  28    protected function defineCustomErrorTypes() {
  29      return array(
  30        'ERR-GREP-COMMAND' => 'Grep command failed.',
  31      );
  32    }
  33  
  34    protected function getResult(ConduitAPIRequest $request) {
  35      try {
  36        $results = parent::getResult($request);
  37      } catch (CommandException $ex) {
  38        throw id(new ConduitException('ERR-GREP-COMMAND'))
  39          ->setErrorDescription($ex->getStderr());
  40      }
  41  
  42      $offset = $request->getValue('offset');
  43      $results = array_slice($results, $offset);
  44  
  45      return $results;
  46    }
  47  
  48    protected function getGitResult(ConduitAPIRequest $request) {
  49      $drequest = $this->getDiffusionRequest();
  50      $path = $drequest->getPath();
  51      $grep = $request->getValue('grep');
  52      $repository = $drequest->getRepository();
  53      $limit = $request->getValue('limit');
  54      $offset = $request->getValue('offset');
  55  
  56      $results = array();
  57      $future = $repository->getLocalCommandFuture(
  58        // NOTE: --perl-regexp is available only with libpcre compiled in.
  59        'grep --extended-regexp --null -n --no-color -e %s %s -- %s',
  60        $grep,
  61        $drequest->getStableCommit(),
  62        $path);
  63  
  64      $binary_pattern = '/Binary file [^:]*:(.+) matches/';
  65      $lines = new LinesOfALargeExecFuture($future);
  66      foreach ($lines as $line) {
  67        $result = null;
  68        if (preg_match('/[^:]*:(.+)\0(.+)\0(.*)/', $line, $result)) {
  69          $results[] = array_slice($result, 1);
  70        } else if (preg_match($binary_pattern, $line, $result)) {
  71          list(, $path) = $result;
  72          $results[] = array($path, null, pht('Binary file'));
  73        } else {
  74          $results[] = array(null, null, $line);
  75        }
  76        if (count($results) >= $offset + $limit) {
  77          break;
  78        }
  79      }
  80      unset($lines);
  81  
  82      return $results;
  83    }
  84  
  85    protected function getMercurialResult(ConduitAPIRequest $request) {
  86      $drequest = $this->getDiffusionRequest();
  87      $path = $drequest->getPath();
  88      $grep = $request->getValue('grep');
  89      $repository = $drequest->getRepository();
  90      $limit = $request->getValue('limit');
  91      $offset = $request->getValue('offset');
  92  
  93      $results = array();
  94      $future = $repository->getLocalCommandFuture(
  95        'grep --rev %s --print0 --line-number %s %s',
  96        hgsprintf('ancestors(%s)', $drequest->getStableCommit()),
  97        $grep,
  98        $path);
  99  
 100      $lines = id(new LinesOfALargeExecFuture($future))->setDelimiter("\0");
 101      $parts = array();
 102      foreach ($lines as $line) {
 103        $parts[] = $line;
 104        if (count($parts) == 4) {
 105          list($path, $char_offset, $line, $string) = $parts;
 106          $results[] = array($path, $line, $string);
 107          if (count($results) >= $offset + $limit) {
 108            break;
 109          }
 110          $parts = array();
 111        }
 112      }
 113      unset($lines);
 114  
 115      return $results;
 116    }
 117  
 118  }


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