@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
at recaptime-dev/main 123 lines 2.6 kB view raw
1<?php 2 3final 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 PhutilInvalidStateException('withPaths'); 27 } 28 29 if (!$this->branchIDs) { 30 throw new PhutilInvalidStateException('withBranchIDs'); 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->buildCustomWhereClause($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 protected function buildCustomWhereClause( 73 AphrontDatabaseConnection $conn, 74 $part) { 75 76 $where = array(); 77 78 $where[] = $part; 79 80 if ($this->codes !== null) { 81 $where[] = qsprintf( 82 $conn, 83 'code IN (%Ls)', 84 $this->codes); 85 } 86 87 if ($this->branchIDs !== null) { 88 $where[] = qsprintf( 89 $conn, 90 'branchID IN (%Ld)', 91 $this->branchIDs); 92 } 93 94 return $this->formatWhereClause($conn, $where); 95 } 96 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; 106 } 107 } 108 return array($dirs, $paths); 109 } 110 111 private function processResults(array $data) { 112 $data = ipull($data, 'N', 'path_prefix'); 113 114 // Strip the leading "/" back off each path. 115 $output = array(); 116 foreach ($data as $path => $count) { 117 $output[substr($path, 1)] = $count; 118 } 119 120 return $output; 121 } 122 123}