Path: blob/master/src/applications/diffusion/query/DiffusionSymbolQuery.php
12242 views
<?php12/**3* Query symbol information (class and function names and location), returning4* a list of matching @{class:PhabricatorRepositorySymbol} objects and possibly5* attached data.6*7* @task config Configuring the Query8* @task exec Executing the Query9* @task internal Internals10*/11final class DiffusionSymbolQuery extends PhabricatorOffsetPagedQuery {1213private $viewer;14private $context;15private $namePrefix;16private $name;1718private $repositoryPHIDs;19private $language;20private $type;2122private $needPaths;23private $needRepositories;242526/* -( Configuring the Query )---------------------------------------------- */2728/**29* @task config30*/31public function setViewer(PhabricatorUser $viewer) {32$this->viewer = $viewer;33return $this;34}3536/**37* @task config38*/39public function getViewer() {40return $this->viewer;41}4243/**44* @task config45*/46public function setContext($context) {47$this->context = $context;48return $this;49}505152/**53* @task config54*/55public function setName($name) {56$this->name = $name;57return $this;58}596061/**62* @task config63*/64public function setNamePrefix($name_prefix) {65$this->namePrefix = $name_prefix;66return $this;67}686970/**71* @task config72*/73public function withRepositoryPHIDs(array $repository_phids) {74$this->repositoryPHIDs = $repository_phids;75return $this;76}777879/**80* @task config81*/82public function setLanguage($language) {83$this->language = $language;84return $this;85}868788/**89* @task config90*/91public function setType($type) {92$this->type = $type;93return $this;94}959697/**98* @task config99*/100public function needPaths($need_paths) {101$this->needPaths = $need_paths;102return $this;103}104105106/**107* @task config108*/109public function needRepositories($need_repositories) {110$this->needRepositories = $need_repositories;111return $this;112}113114115/* -( Specialized Query )-------------------------------------------------- */116117public function existsSymbolsInRepository($repository_phid) {118$this119->withRepositoryPHIDs(array($repository_phid))120->setLimit(1);121122$symbol = new PhabricatorRepositorySymbol();123$conn_r = $symbol->establishConnection('r');124125$data = queryfx_all(126$conn_r,127'SELECT * FROM %T %Q %Q',128$symbol->getTableName(),129$this->buildWhereClause($conn_r),130$this->buildLimitClause($conn_r));131132return (!empty($data));133}134135/* -( Executing the Query )------------------------------------------------ */136137138/**139* @task exec140*/141public function execute() {142if ($this->name && $this->namePrefix) {143throw new Exception(144pht('You can not set both a name and a name prefix!'));145} else if (!$this->name && !$this->namePrefix) {146throw new Exception(147pht('You must set a name or a name prefix!'));148}149150$symbol = new PhabricatorRepositorySymbol();151$conn_r = $symbol->establishConnection('r');152153$data = queryfx_all(154$conn_r,155'SELECT * FROM %T %Q %Q %Q',156$symbol->getTableName(),157$this->buildWhereClause($conn_r),158$this->buildOrderClause($conn_r),159$this->buildLimitClause($conn_r));160161$symbols = $symbol->loadAllFromArray($data);162163if ($symbols) {164if ($this->needPaths) {165$this->loadPaths($symbols);166}167if ($this->needRepositories) {168$symbols = $this->loadRepositories($symbols);169}170171}172173174return $symbols;175}176177178/* -( Internals )---------------------------------------------------------- */179180181/**182* @task internal183*/184private function buildOrderClause($conn_r) {185return qsprintf(186$conn_r,187'ORDER BY symbolName ASC');188}189190191/**192* @task internal193*/194protected function buildWhereClause(AphrontDatabaseConnection $conn) {195$where = array();196197if (isset($this->context)) {198$where[] = qsprintf(199$conn,200'symbolContext = %s',201$this->context);202}203204if ($this->name) {205$where[] = qsprintf(206$conn,207'symbolName = %s',208$this->name);209}210211if ($this->namePrefix) {212$where[] = qsprintf(213$conn,214'symbolName LIKE %>',215$this->namePrefix);216}217218if ($this->repositoryPHIDs) {219$where[] = qsprintf(220$conn,221'repositoryPHID IN (%Ls)',222$this->repositoryPHIDs);223}224225if ($this->language) {226$where[] = qsprintf(227$conn,228'symbolLanguage = %s',229$this->language);230}231232if ($this->type) {233$where[] = qsprintf(234$conn,235'symbolType = %s',236$this->type);237}238239return $this->formatWhereClause($conn, $where);240}241242243/**244* @task internal245*/246private function loadPaths(array $symbols) {247assert_instances_of($symbols, 'PhabricatorRepositorySymbol');248$path_map = queryfx_all(249id(new PhabricatorRepository())->establishConnection('r'),250'SELECT * FROM %T WHERE id IN (%Ld)',251PhabricatorRepository::TABLE_PATH,252mpull($symbols, 'getPathID'));253$path_map = ipull($path_map, 'path', 'id');254foreach ($symbols as $symbol) {255$symbol->attachPath(idx($path_map, $symbol->getPathID()));256}257}258259260/**261* @task internal262*/263private function loadRepositories(array $symbols) {264assert_instances_of($symbols, 'PhabricatorRepositorySymbol');265266$repos = id(new PhabricatorRepositoryQuery())267->setViewer($this->viewer)268->withPHIDs(mpull($symbols, 'getRepositoryPHID'))269->execute();270$repos = mpull($repos, null, 'getPHID');271272$visible = array();273foreach ($symbols as $symbol) {274$repository = idx($repos, $symbol->getRepositoryPHID());275// repository is null mean "user can't view repo", so hide the symbol276if ($repository) {277$symbol->attachRepository($repository);278$visible[] = $symbol;279}280}281return $visible;282}283284}285286287