Path: blob/master/src/applications/drydock/query/DrydockBlueprintQuery.php
12256 views
<?php12final class DrydockBlueprintQuery extends DrydockQuery {34private $ids;5private $phids;6private $blueprintClasses;7private $datasourceQuery;8private $disabled;9private $authorizedPHIDs;1011private $identifiers;12private $identifierIDs;13private $identifierPHIDs;14private $identifierMap;1516public function withIDs(array $ids) {17$this->ids = $ids;18return $this;19}2021public function withPHIDs(array $phids) {22$this->phids = $phids;23return $this;24}2526public function withBlueprintClasses(array $classes) {27$this->blueprintClasses = $classes;28return $this;29}3031public function withDatasourceQuery($query) {32$this->datasourceQuery = $query;33return $this;34}3536public function withDisabled($disabled) {37$this->disabled = $disabled;38return $this;39}4041public function withAuthorizedPHIDs(array $phids) {42$this->authorizedPHIDs = $phids;43return $this;44}4546public function withNameNgrams($ngrams) {47return $this->withNgramsConstraint(48new DrydockBlueprintNameNgrams(),49$ngrams);50}5152public function withIdentifiers(array $identifiers) {53if (!$identifiers) {54throw new Exception(55pht(56'Can not issue a query with an empty identifier list.'));57}5859$this->identifiers = $identifiers;6061$ids = array();62$phids = array();6364foreach ($identifiers as $identifier) {65if (ctype_digit($identifier)) {66$ids[] = $identifier;67} else {68$phids[] = $identifier;69}70}7172$this->identifierIDs = $ids;73$this->identifierPHIDs = $phids;7475return $this;76}7778public function getIdentifierMap() {79if ($this->identifierMap === null) {80throw new Exception(81pht(82'Execute a query with identifiers before getting the '.83'identifier map.'));84}8586return $this->identifierMap;87}8889public function newResultObject() {90return new DrydockBlueprint();91}9293protected function getPrimaryTableAlias() {94return 'blueprint';95}9697protected function willExecute() {98if ($this->identifiers) {99$this->identifierMap = array();100} else {101$this->identifierMap = null;102}103}104105protected function willFilterPage(array $blueprints) {106$impls = DrydockBlueprintImplementation::getAllBlueprintImplementations();107foreach ($blueprints as $key => $blueprint) {108$impl = idx($impls, $blueprint->getClassName());109if (!$impl) {110$this->didRejectResult($blueprint);111unset($blueprints[$key]);112continue;113}114$impl = clone $impl;115$blueprint->attachImplementation($impl);116}117118if ($this->identifiers) {119$id_map = mpull($blueprints, null, 'getID');120$phid_map = mpull($blueprints, null, 'getPHID');121122$map = $this->identifierMap;123124foreach ($this->identifierIDs as $id) {125if (isset($id_map[$id])) {126$map[$id] = $id_map[$id];127}128}129130foreach ($this->identifierPHIDs as $phid) {131if (isset($phid_map[$phid])) {132$map[$phid] = $phid_map[$phid];133}134}135136// Just for consistency, reorder the map to match input order.137$map = array_select_keys($map, $this->identifiers);138139$this->identifierMap = $map;140}141142return $blueprints;143}144145protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {146$where = parent::buildWhereClauseParts($conn);147148if ($this->ids !== null) {149$where[] = qsprintf(150$conn,151'blueprint.id IN (%Ld)',152$this->ids);153}154155if ($this->phids !== null) {156$where[] = qsprintf(157$conn,158'blueprint.phid IN (%Ls)',159$this->phids);160}161162if ($this->datasourceQuery !== null) {163$where[] = qsprintf(164$conn,165'blueprint.blueprintName LIKE %>',166$this->datasourceQuery);167}168169if ($this->blueprintClasses !== null) {170$where[] = qsprintf(171$conn,172'blueprint.className IN (%Ls)',173$this->blueprintClasses);174}175176if ($this->disabled !== null) {177$where[] = qsprintf(178$conn,179'blueprint.isDisabled = %d',180(int)$this->disabled);181}182183if ($this->identifiers !== null) {184$parts = array();185186if ($this->identifierIDs) {187$parts[] = qsprintf(188$conn,189'blueprint.id IN (%Ld)',190$this->identifierIDs);191}192193if ($this->identifierPHIDs) {194$parts[] = qsprintf(195$conn,196'blueprint.phid IN (%Ls)',197$this->identifierPHIDs);198}199200$where[] = qsprintf(201$conn,202'%LO',203$parts);204}205206return $where;207}208209protected function shouldGroupQueryResultRows() {210if ($this->authorizedPHIDs !== null) {211return true;212}213return parent::shouldGroupQueryResultRows();214}215216protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {217$joins = parent::buildJoinClauseParts($conn);218219if ($this->authorizedPHIDs !== null) {220$joins[] = qsprintf(221$conn,222'JOIN %T authorization223ON authorization.blueprintPHID = blueprint.phid224AND authorization.objectPHID IN (%Ls)225AND authorization.objectAuthorizationState = %s226AND authorization.blueprintAuthorizationState = %s',227id(new DrydockAuthorization())->getTableName(),228$this->authorizedPHIDs,229DrydockAuthorization::OBJECTAUTH_ACTIVE,230DrydockAuthorization::BLUEPRINTAUTH_AUTHORIZED);231}232233return $joins;234}235236}237238239