[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/diffusion/query/ -> DiffusionLintCountQuery.php (source)

   1  <?php
   2  
   3  final class DiffusionLintCountQuery extends PhabricatorQuery {
   4  
   5    private $branchIDs;
   6    private $paths;
   7    private $codes;
   8  
   9    public function withBranchIDs(array $branch_ids) {
  10      $this->branchIDs = $branch_ids;
  11      return $this;
  12    }
  13  
  14    public function withPaths(array $paths) {
  15      $this->paths = $paths;
  16      return $this;
  17    }
  18  
  19    public function withCodes(array $codes) {
  20      $this->codes = $codes;
  21      return $this;
  22    }
  23  
  24    public function execute() {
  25      if (!$this->paths) {
  26        throw new Exception(pht('Call withPaths() before execute()!'));
  27      }
  28  
  29      if (!$this->branchIDs) {
  30        throw new Exception(pht('Call withBranchIDs() before execute()!'));
  31      }
  32  
  33      $conn_r = id(new PhabricatorRepositoryCommit())->establishConnection('r');
  34  
  35      $this->paths = array_unique($this->paths);
  36      list($dirs, $paths) = $this->processPaths();
  37  
  38      $parts = array();
  39      foreach ($dirs as $dir) {
  40        $parts[$dir] = qsprintf(
  41          $conn_r,
  42          'path LIKE %>',
  43          $dir);
  44      }
  45      foreach ($paths as $path) {
  46        $parts[$path] = qsprintf(
  47          $conn_r,
  48          'path = %s',
  49          $path);
  50      }
  51  
  52      $queries = array();
  53      foreach ($parts as $key => $part) {
  54        $queries[] = qsprintf(
  55          $conn_r,
  56          'SELECT %s path_prefix, COUNT(*) N FROM %T %Q',
  57          $key,
  58          PhabricatorRepository::TABLE_LINTMESSAGE,
  59          $this->buildWhereClause($conn_r, $part));
  60      }
  61  
  62      $huge_union_query = '('.implode(') UNION ALL (', $queries).')';
  63  
  64      $data = queryfx_all(
  65        $conn_r,
  66        '%Q',
  67        $huge_union_query);
  68  
  69      return $this->processResults($data);
  70    }
  71  
  72    private function buildWhereClause(AphrontDatabaseConnection $conn_r, $part) {
  73      $where = array();
  74  
  75      $where[] = $part;
  76  
  77      if ($this->codes !== null) {
  78        $where[] = qsprintf(
  79          $conn_r,
  80          'code IN (%Ls)',
  81          $this->codes);
  82      }
  83  
  84      if ($this->branchIDs !== null) {
  85        $where[] = qsprintf(
  86          $conn_r,
  87          'branchID IN (%Ld)',
  88          $this->branchIDs);
  89      }
  90  
  91      return $this->formatWhereClause($where);
  92    }
  93  
  94    private function processPaths() {
  95      $dirs = array();
  96      $paths = array();
  97      foreach ($this->paths as $path) {
  98        $path = '/'.$path;
  99        if (substr($path, -1) == '/') {
 100          $dirs[] = $path;
 101        } else {
 102          $paths[] = $path;
 103        }
 104      }
 105      return array($dirs, $paths);
 106    }
 107  
 108    private function processResults(array $data) {
 109      $data = ipull($data, 'N', 'path_prefix');
 110  
 111      // Strip the leading "/" back off each path.
 112      $output = array();
 113      foreach ($data as $path => $count) {
 114        $output[substr($path, 1)] = $count;
 115      }
 116  
 117      return $output;
 118    }
 119  
 120  }


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