Something went wrong. Try again.
@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
Something went wrong. Try again.
6.1 kB · 286 lines
PHP
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287<?php
/** * Query symbol information (class and function names and location), returning * a list of matching @{class:PhabricatorRepositorySymbol} objects and possibly * attached data. * * @task config Configuring the Query * @task exec Executing the Query * @task internal Internals */final class DiffusionSymbolQuery extends PhabricatorOffsetPagedQuery {
private $viewer; private $context; private $namePrefix; private $name;
private $repositoryPHIDs; private $language; private $type;
private $needPaths; private $needRepositories;
/* -( Configuring the Query )---------------------------------------------- */
/** * @task config */ public function setViewer(PhabricatorUser $viewer) { $this->viewer = $viewer; return $this; }
/** * @task config */ public function getViewer() { return $this->viewer; }
/** * @task config */ public function setContext($context) { $this->context = $context; return $this; }
/** * @task config */ public function setName($name) { $this->name = $name; return $this; }
/** * @task config */ public function setNamePrefix($name_prefix) { $this->namePrefix = $name_prefix; return $this; }
/** * @task config */ public function withRepositoryPHIDs(array $repository_phids) { $this->repositoryPHIDs = $repository_phids; return $this; }
/** * @task config */ public function setLanguage($language) { $this->language = $language; return $this; }
/** * @task config */ public function setType($type) { $this->type = $type; return $this; }
/** * @task config */ public function needPaths($need_paths) { $this->needPaths = $need_paths; return $this; }
/** * @task config */ public function needRepositories($need_repositories) { $this->needRepositories = $need_repositories; return $this; }
/* -( Specialized Query )-------------------------------------------------- */
public function existsSymbolsInRepository($repository_phid) { $this ->withRepositoryPHIDs(array($repository_phid)) ->setLimit(1);
$symbol = new PhabricatorRepositorySymbol(); $conn_r = $symbol->establishConnection('r');
$data = queryfx_all( $conn_r, 'SELECT * FROM %T %Q %Q', $symbol->getTableName(), $this->buildWhereClause($conn_r), $this->buildLimitClause($conn_r));
return (!empty($data)); }
/* -( Executing the Query )------------------------------------------------ */
/** * @task exec */ public function execute() { if ($this->name && $this->namePrefix) { throw new Exception( pht('You can not set both a name and a name prefix!')); } else if (!$this->name && !$this->namePrefix) { throw new Exception( pht('You must set a name or a name prefix!')); }
$symbol = new PhabricatorRepositorySymbol(); $conn_r = $symbol->establishConnection('r');
$data = queryfx_all( $conn_r, 'SELECT * FROM %T %Q %Q %Q', $symbol->getTableName(), $this->buildWhereClause($conn_r), $this->buildOrderClause($conn_r), $this->buildLimitClause($conn_r));
$symbols = $symbol->loadAllFromArray($data);
if ($symbols) { if ($this->needPaths) { $this->loadPaths($symbols); } if ($this->needRepositories) { $symbols = $this->loadRepositories($symbols); }
}
return $symbols; }
/* -( Internals )---------------------------------------------------------- */
/** * @task internal */ private function buildOrderClause($conn_r) { return qsprintf( $conn_r, 'ORDER BY symbolName ASC'); }
/** * @task internal */ protected function buildWhereClause(AphrontDatabaseConnection $conn) { $where = array();
if (isset($this->context)) { $where[] = qsprintf( $conn, 'symbolContext = %s', $this->context); }
if ($this->name) { $where[] = qsprintf( $conn, 'symbolName = %s', $this->name); }
if ($this->namePrefix) { $where[] = qsprintf( $conn, 'symbolName LIKE %>', $this->namePrefix); }
if ($this->repositoryPHIDs) { $where[] = qsprintf( $conn, 'repositoryPHID IN (%Ls)', $this->repositoryPHIDs); }
if ($this->language) { $where[] = qsprintf( $conn, 'symbolLanguage = %s', $this->language); }
if ($this->type) { $where[] = qsprintf( $conn, 'symbolType = %s', $this->type); }
return $this->formatWhereClause($conn, $where); }
/** * @param array<PhabricatorRepositorySymbol> $symbols * @task internal */ private function loadPaths(array $symbols) { assert_instances_of($symbols, PhabricatorRepositorySymbol::class); $path_map = queryfx_all( id(new PhabricatorRepository())->establishConnection('r'), 'SELECT * FROM %T WHERE id IN (%Ld)', PhabricatorRepository::TABLE_PATH, mpull($symbols, 'getPathID')); $path_map = ipull($path_map, 'path', 'id'); foreach ($symbols as $symbol) { $symbol->attachPath(idx($path_map, $symbol->getPathID())); } }
/** * @param array<PhabricatorRepositorySymbol> $symbols * @task internal */ private function loadRepositories(array $symbols) { assert_instances_of($symbols, PhabricatorRepositorySymbol::class);
$repos = id(new PhabricatorRepositoryQuery()) ->setViewer($this->viewer) ->withPHIDs(mpull($symbols, 'getRepositoryPHID')) ->execute(); $repos = mpull($repos, null, 'getPHID');
$visible = array(); foreach ($symbols as $symbol) { $repository = idx($repos, $symbol->getRepositoryPHID()); // repository is null mean "user can't view repo", so hide the symbol if ($repository) { $symbol->attachRepository($repository); $visible[] = $symbol; } } return $visible; }
}