Changeset View
Changeset View
Standalone View
Standalone View
src/applications/diviner/query/DivinerAtomQuery.php
| <?php | <?php | ||||
| final class DivinerAtomQuery extends PhabricatorCursorPagedPolicyAwareQuery { | final class DivinerAtomQuery extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
| private $ids; | private $ids; | ||||
| private $phids; | private $phids; | ||||
| private $bookPHIDs; | private $bookPHIDs; | ||||
| private $names; | private $names; | ||||
| private $types; | private $types; | ||||
| private $contexts; | private $contexts; | ||||
| private $indexes; | private $indexes; | ||||
| private $isDocumentable; | private $isDocumentable; | ||||
| private $isGhost; | private $isGhost; | ||||
| private $nodeHashes; | private $nodeHashes; | ||||
| private $titles; | private $titles; | ||||
| private $nameContains; | private $nameContains; | ||||
| private $repositoryPHIDs; | |||||
| private $needAtoms; | private $needAtoms; | ||||
| private $needExtends; | private $needExtends; | ||||
| private $needChildren; | private $needChildren; | ||||
| private $needRepositories; | |||||
| public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
| $this->ids = $ids; | $this->ids = $ids; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function withPHIDs(array $phids) { | public function withPHIDs(array $phids) { | ||||
| $this->phids = $phids; | $this->phids = $phids; | ||||
| ▲ Show 20 Lines • Show All 75 Lines • ▼ Show 20 Lines | public function needExtends($need) { | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function withIsDocumentable($documentable) { | public function withIsDocumentable($documentable) { | ||||
| $this->isDocumentable = $documentable; | $this->isDocumentable = $documentable; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| public function withRepositoryPHIDs(array $repository_phids) { | |||||
| $this->repositoryPHIDs = $repository_phids; | |||||
| return $this; | |||||
| } | |||||
| public function needRepositories($need_repositories) { | |||||
| $this->needRepositories = $need_repositories; | |||||
| return $this; | |||||
| } | |||||
| protected function loadPage() { | protected function loadPage() { | ||||
| $table = new DivinerLiveSymbol(); | $table = new DivinerLiveSymbol(); | ||||
| $conn_r = $table->establishConnection('r'); | $conn_r = $table->establishConnection('r'); | ||||
| $data = queryfx_all( | $data = queryfx_all( | ||||
| $conn_r, | $conn_r, | ||||
| 'SELECT * FROM %T %Q %Q %Q', | 'SELECT * FROM %T %Q %Q %Q', | ||||
| $table->getTableName(), | $table->getTableName(), | ||||
| $this->buildWhereClause($conn_r), | $this->buildWhereClause($conn_r), | ||||
| $this->buildOrderClause($conn_r), | $this->buildOrderClause($conn_r), | ||||
| $this->buildLimitClause($conn_r)); | $this->buildLimitClause($conn_r)); | ||||
| return $table->loadAllFromArray($data); | return $table->loadAllFromArray($data); | ||||
| } | } | ||||
| protected function willFilterPage(array $atoms) { | protected function willFilterPage(array $atoms) { | ||||
| assert_instances_of($atoms, 'DivinerLiveSymbol'); | |||||
| $books = array_unique(mpull($atoms, 'getBookPHID')); | $books = array_unique(mpull($atoms, 'getBookPHID')); | ||||
| $books = id(new DivinerBookQuery()) | $books = id(new DivinerBookQuery()) | ||||
| ->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
| ->withPHIDs($books) | ->withPHIDs($books) | ||||
| ->execute(); | ->execute(); | ||||
| $books = mpull($books, null, 'getPHID'); | $books = mpull($books, null, 'getPHID'); | ||||
| ▲ Show 20 Lines • Show All 116 Lines • ▼ Show 20 Lines | if ($this->needChildren) { | ||||
| $children = mpull($children, null, 'getNodeHash'); | $children = mpull($children, null, 'getNodeHash'); | ||||
| } else { | } else { | ||||
| $children = array(); | $children = array(); | ||||
| } | } | ||||
| $this->attachAllChildren($atoms, $children, $this->needExtends); | $this->attachAllChildren($atoms, $children, $this->needExtends); | ||||
| } | } | ||||
| if ($this->needRepositories) { | |||||
| $repositories = id(new PhabricatorRepositoryQuery()) | |||||
| ->setViewer($this->getViewer()) | |||||
| ->withPHIDs(mpull($atoms, 'getRepositoryPHID')) | |||||
| ->execute(); | |||||
| $repositories = mpull($repositories, null, 'getPHID'); | |||||
| foreach ($atoms as $key => $atom) { | |||||
| if ($atom->getRepositoryPHID() === null) { | |||||
| $atom->attachRepository(null); | |||||
| continue; | |||||
| } | |||||
| $repository = idx($repositories, $atom->getRepositoryPHID()); | |||||
| if (!$repository) { | |||||
| $this->didRejectResult($atom); | |||||
| unset($atom[$key]); | |||||
| continue; | |||||
| } | |||||
| $atom->attachRepository($repository); | |||||
| } | |||||
| } | |||||
| return $atoms; | return $atoms; | ||||
| } | } | ||||
| protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | ||||
| $where = array(); | $where = array(); | ||||
| if ($this->ids) { | if ($this->ids) { | ||||
| $where[] = qsprintf( | $where[] = qsprintf( | ||||
| ▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | if ($this->nameContains) { | ||||
| // the column has binary collation. Eventually, this should move into | // the column has binary collation. Eventually, this should move into | ||||
| // fulltext. | // fulltext. | ||||
| $where[] = qsprintf( | $where[] = qsprintf( | ||||
| $conn_r, | $conn_r, | ||||
| 'CONVERT(name USING utf8) LIKE %~', | 'CONVERT(name USING utf8) LIKE %~', | ||||
| $this->nameContains); | $this->nameContains); | ||||
| } | } | ||||
| if ($this->repositoryPHIDs) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'repositoryPHID IN (%Ls)', | |||||
| $this->repositoryPHIDs); | |||||
| } | |||||
| $where[] = $this->buildPagingClause($conn_r); | $where[] = $this->buildPagingClause($conn_r); | ||||
| return $this->formatWhereClause($where); | return $this->formatWhereClause($where); | ||||
| } | } | ||||
| /** | /** | ||||
| * Walk a list of atoms and collect all the node hashes of the atoms' | * Walk a list of atoms and collect all the node hashes of the atoms' | ||||
| * children. When recursing, also walk up the tree and collect children of | * children. When recursing, also walk up the tree and collect children of | ||||
| ▲ Show 20 Lines • Show All 74 Lines • Show Last 20 Lines | |||||