Path: blob/master/src/applications/diffusion/query/DiffusionLintCountQuery.php
12242 views
<?php12final class DiffusionLintCountQuery extends PhabricatorQuery {34private $branchIDs;5private $paths;6private $codes;78public function withBranchIDs(array $branch_ids) {9$this->branchIDs = $branch_ids;10return $this;11}1213public function withPaths(array $paths) {14$this->paths = $paths;15return $this;16}1718public function withCodes(array $codes) {19$this->codes = $codes;20return $this;21}2223public function execute() {24if (!$this->paths) {25throw new PhutilInvalidStateException('withPaths');26}2728if (!$this->branchIDs) {29throw new PhutilInvalidStateException('withBranchIDs');30}3132$conn_r = id(new PhabricatorRepositoryCommit())->establishConnection('r');3334$this->paths = array_unique($this->paths);35list($dirs, $paths) = $this->processPaths();3637$parts = array();38foreach ($dirs as $dir) {39$parts[$dir] = qsprintf(40$conn_r,41'path LIKE %>',42$dir);43}44foreach ($paths as $path) {45$parts[$path] = qsprintf(46$conn_r,47'path = %s',48$path);49}5051$queries = array();52foreach ($parts as $key => $part) {53$queries[] = qsprintf(54$conn_r,55'SELECT %s path_prefix, COUNT(*) N FROM %T %Q',56$key,57PhabricatorRepository::TABLE_LINTMESSAGE,58$this->buildCustomWhereClause($conn_r, $part));59}6061$huge_union_query = '('.implode(') UNION ALL (', $queries).')';6263$data = queryfx_all(64$conn_r,65'%Q',66$huge_union_query);6768return $this->processResults($data);69}7071protected function buildCustomWhereClause(72AphrontDatabaseConnection $conn,73$part) {7475$where = array();7677$where[] = $part;7879if ($this->codes !== null) {80$where[] = qsprintf(81$conn,82'code IN (%Ls)',83$this->codes);84}8586if ($this->branchIDs !== null) {87$where[] = qsprintf(88$conn,89'branchID IN (%Ld)',90$this->branchIDs);91}9293return $this->formatWhereClause($conn, $where);94}9596private function processPaths() {97$dirs = array();98$paths = array();99foreach ($this->paths as $path) {100$path = '/'.$path;101if (substr($path, -1) == '/') {102$dirs[] = $path;103} else {104$paths[] = $path;105}106}107return array($dirs, $paths);108}109110private function processResults(array $data) {111$data = ipull($data, 'N', 'path_prefix');112113// Strip the leading "/" back off each path.114$output = array();115foreach ($data as $path => $count) {116$output[substr($path, 1)] = $count;117}118119return $output;120}121122}123124125