Path: blob/master/src/applications/diviner/query/DivinerBookQuery.php
12256 views
<?php12final class DivinerBookQuery extends PhabricatorCursorPagedPolicyAwareQuery {34private $ids;5private $phids;6private $names;7private $nameLike;8private $namePrefix;9private $repositoryPHIDs;1011private $needProjectPHIDs;12private $needRepositories;1314public function withIDs(array $ids) {15$this->ids = $ids;16return $this;17}1819public function withPHIDs(array $phids) {20$this->phids = $phids;21return $this;22}2324public function withNameLike($name) {25$this->nameLike = $name;26return $this;27}2829public function withNames(array $names) {30$this->names = $names;31return $this;32}3334public function withNamePrefix($prefix) {35$this->namePrefix = $prefix;36return $this;37}3839public function withRepositoryPHIDs(array $repository_phids) {40$this->repositoryPHIDs = $repository_phids;41return $this;42}4344public function needProjectPHIDs($need_phids) {45$this->needProjectPHIDs = $need_phids;46return $this;47}4849public function needRepositories($need_repositories) {50$this->needRepositories = $need_repositories;51return $this;52}5354protected function loadPage() {55$table = new DivinerLiveBook();56$conn_r = $table->establishConnection('r');5758$data = queryfx_all(59$conn_r,60'SELECT * FROM %T %Q %Q %Q',61$table->getTableName(),62$this->buildWhereClause($conn_r),63$this->buildOrderClause($conn_r),64$this->buildLimitClause($conn_r));6566return $table->loadAllFromArray($data);67}6869protected function didFilterPage(array $books) {70assert_instances_of($books, 'DivinerLiveBook');7172if ($this->needRepositories) {73$repositories = id(new PhabricatorRepositoryQuery())74->setViewer($this->getViewer())75->withPHIDs(mpull($books, 'getRepositoryPHID'))76->execute();77$repositories = mpull($repositories, null, 'getPHID');7879foreach ($books as $key => $book) {80if ($book->getRepositoryPHID() === null) {81$book->attachRepository(null);82continue;83}8485$repository = idx($repositories, $book->getRepositoryPHID());8687if (!$repository) {88$this->didRejectResult($book);89unset($books[$key]);90continue;91}9293$book->attachRepository($repository);94}95}9697if ($this->needProjectPHIDs) {98$edge_query = id(new PhabricatorEdgeQuery())99->withSourcePHIDs(mpull($books, 'getPHID'))100->withEdgeTypes(101array(102PhabricatorProjectObjectHasProjectEdgeType::EDGECONST,103));104$edge_query->execute();105106foreach ($books as $book) {107$project_phids = $edge_query->getDestinationPHIDs(108array(109$book->getPHID(),110));111$book->attachProjectPHIDs($project_phids);112}113}114115return $books;116}117118protected function buildWhereClause(AphrontDatabaseConnection $conn) {119$where = array();120121if ($this->ids) {122$where[] = qsprintf(123$conn,124'id IN (%Ld)',125$this->ids);126}127128if ($this->phids) {129$where[] = qsprintf(130$conn,131'phid IN (%Ls)',132$this->phids);133}134135if ($this->nameLike !== null && strlen($this->nameLike)) {136$where[] = qsprintf(137$conn,138'name LIKE %~',139$this->nameLike);140}141142if ($this->names !== null) {143$where[] = qsprintf(144$conn,145'name IN (%Ls)',146$this->names);147}148149if ($this->namePrefix !== null && strlen($this->namePrefix)) {150$where[] = qsprintf(151$conn,152'name LIKE %>',153$this->namePrefix);154}155156if ($this->repositoryPHIDs !== null) {157$where[] = qsprintf(158$conn,159'repositoryPHID IN (%Ls)',160$this->repositoryPHIDs);161}162163$where[] = $this->buildPagingClause($conn);164165return $this->formatWhereClause($conn, $where);166}167168public function getQueryApplicationClass() {169return 'PhabricatorDivinerApplication';170}171172public function getOrderableColumns() {173return parent::getOrderableColumns() + array(174'name' => array(175'column' => 'name',176'type' => 'string',177'reverse' => true,178'unique' => true,179),180);181}182183protected function newPagingMapFromPartialObject($object) {184return array(185'id' => (int)$object->getID(),186'name' => $object->getName(),187);188}189190public function getBuiltinOrders() {191return array(192'name' => array(193'vector' => array('name'),194'name' => pht('Name'),195),196) + parent::getBuiltinOrders();197}198199}200201202