Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / diffusion / query / DiffusionLintCountQuery.php
blob2c1a2a9867ef5fd331720858b3b94af28b494261
1 <?php
3 final class DiffusionLintCountQuery extends PhabricatorQuery {
5 private $branchIDs;
6 private $paths;
7 private $codes;
9 public function withBranchIDs(array $branch_ids) {
10 $this->branchIDs = $branch_ids;
11 return $this;
14 public function withPaths(array $paths) {
15 $this->paths = $paths;
16 return $this;
19 public function withCodes(array $codes) {
20 $this->codes = $codes;
21 return $this;
24 public function execute() {
25 if (!$this->paths) {
26 throw new PhutilInvalidStateException('withPaths');
29 if (!$this->branchIDs) {
30 throw new PhutilInvalidStateException('withBranchIDs');
33 $conn_r = id(new PhabricatorRepositoryCommit())->establishConnection('r');
35 $this->paths = array_unique($this->paths);
36 list($dirs, $paths) = $this->processPaths();
38 $parts = array();
39 foreach ($dirs as $dir) {
40 $parts[$dir] = qsprintf(
41 $conn_r,
42 'path LIKE %>',
43 $dir);
45 foreach ($paths as $path) {
46 $parts[$path] = qsprintf(
47 $conn_r,
48 'path = %s',
49 $path);
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->buildCustomWhereClause($conn_r, $part));
62 $huge_union_query = '('.implode(') UNION ALL (', $queries).')';
64 $data = queryfx_all(
65 $conn_r,
66 '%Q',
67 $huge_union_query);
69 return $this->processResults($data);
72 protected function buildCustomWhereClause(
73 AphrontDatabaseConnection $conn,
74 $part) {
76 $where = array();
78 $where[] = $part;
80 if ($this->codes !== null) {
81 $where[] = qsprintf(
82 $conn,
83 'code IN (%Ls)',
84 $this->codes);
87 if ($this->branchIDs !== null) {
88 $where[] = qsprintf(
89 $conn,
90 'branchID IN (%Ld)',
91 $this->branchIDs);
94 return $this->formatWhereClause($conn, $where);
97 private function processPaths() {
98 $dirs = array();
99 $paths = array();
100 foreach ($this->paths as $path) {
101 $path = '/'.$path;
102 if (substr($path, -1) == '/') {
103 $dirs[] = $path;
104 } else {
105 $paths[] = $path;
108 return array($dirs, $paths);
111 private function processResults(array $data) {
112 $data = ipull($data, 'N', 'path_prefix');
114 // Strip the leading "/" back off each path.
115 $output = array();
116 foreach ($data as $path => $count) {
117 $output[substr($path, 1)] = $count;
120 return $output;