@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
at recaptime-dev/main 173 lines 4.0 kB view raw
1<?php 2 3/** 4 * @extends DrydockQuery<DrydockLease> 5 */ 6final class DrydockLeaseQuery extends DrydockQuery { 7 8 private $ids; 9 private $phids; 10 private $resourcePHIDs; 11 private $ownerPHIDs; 12 private $statuses; 13 private $datasourceQuery; 14 private $needUnconsumedCommands; 15 private $minModified; 16 private $maxModified; 17 18 public function withIDs(array $ids) { 19 $this->ids = $ids; 20 return $this; 21 } 22 23 public function withPHIDs(array $phids) { 24 $this->phids = $phids; 25 return $this; 26 } 27 28 public function withResourcePHIDs(array $phids) { 29 $this->resourcePHIDs = $phids; 30 return $this; 31 } 32 33 public function withOwnerPHIDs(array $phids) { 34 $this->ownerPHIDs = $phids; 35 return $this; 36 } 37 38 public function withStatuses(array $statuses) { 39 $this->statuses = $statuses; 40 return $this; 41 } 42 43 public function withDatasourceQuery($query) { 44 $this->datasourceQuery = $query; 45 return $this; 46 } 47 48 public function withDateModifiedBetween($min_epoch, $max_epoch) { 49 $this->minModified = $min_epoch; 50 $this->maxModified = $max_epoch; 51 return $this; 52 } 53 54 public function needUnconsumedCommands($need) { 55 $this->needUnconsumedCommands = $need; 56 return $this; 57 } 58 59 public function newResultObject() { 60 return new DrydockLease(); 61 } 62 63 protected function willFilterPage(array $leases) { 64 $resource_phids = array_filter(mpull($leases, 'getResourcePHID')); 65 if ($resource_phids) { 66 $resources = id(new DrydockResourceQuery()) 67 ->setParentQuery($this) 68 ->setViewer($this->getViewer()) 69 ->withPHIDs(array_unique($resource_phids)) 70 ->execute(); 71 $resources = mpull($resources, null, 'getPHID'); 72 } else { 73 $resources = array(); 74 } 75 76 foreach ($leases as $key => $lease) { 77 $resource = null; 78 if ($lease->getResourcePHID()) { 79 $resource = idx($resources, $lease->getResourcePHID()); 80 if (!$resource) { 81 $this->didRejectResult($lease); 82 unset($leases[$key]); 83 continue; 84 } 85 } 86 $lease->attachResource($resource); 87 } 88 89 return $leases; 90 } 91 92 protected function didFilterPage(array $leases) { 93 if ($this->needUnconsumedCommands) { 94 $commands = id(new DrydockCommandQuery()) 95 ->setViewer($this->getViewer()) 96 ->setParentQuery($this) 97 ->withTargetPHIDs(mpull($leases, 'getPHID')) 98 ->withConsumed(false) 99 ->execute(); 100 $commands = mgroup($commands, 'getTargetPHID'); 101 102 foreach ($leases as $lease) { 103 $list = idx($commands, $lease->getPHID(), array()); 104 $lease->attachUnconsumedCommands($list); 105 } 106 } 107 108 return $leases; 109 } 110 111 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 112 $where = parent::buildWhereClauseParts($conn); 113 114 if ($this->resourcePHIDs !== null) { 115 $where[] = qsprintf( 116 $conn, 117 'resourcePHID IN (%Ls)', 118 $this->resourcePHIDs); 119 } 120 121 if ($this->ownerPHIDs !== null) { 122 $where[] = qsprintf( 123 $conn, 124 'ownerPHID IN (%Ls)', 125 $this->ownerPHIDs); 126 } 127 128 if ($this->ids !== null) { 129 $where[] = qsprintf( 130 $conn, 131 'id IN (%Ld)', 132 $this->ids); 133 } 134 135 if ($this->phids !== null) { 136 $where[] = qsprintf( 137 $conn, 138 'phid IN (%Ls)', 139 $this->phids); 140 } 141 142 if ($this->statuses !== null) { 143 $where[] = qsprintf( 144 $conn, 145 'status IN (%Ls)', 146 $this->statuses); 147 } 148 149 if ($this->datasourceQuery !== null) { 150 $where[] = qsprintf( 151 $conn, 152 'id = %d', 153 (int)$this->datasourceQuery); 154 } 155 156 if ($this->minModified !== null) { 157 $where[] = qsprintf( 158 $conn, 159 'dateModified >= %d', 160 $this->minModified); 161 } 162 163 if ($this->maxModified !== null) { 164 $where[] = qsprintf( 165 $conn, 166 'dateModified <= %d', 167 $this->maxModified); 168 } 169 170 return $where; 171 } 172 173}