[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class HarbormasterBuildableQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $ids; 7 private $phids; 8 private $buildablePHIDs; 9 private $containerPHIDs; 10 private $manualBuildables; 11 12 private $needContainerObjects; 13 private $needContainerHandles; 14 private $needBuildableHandles; 15 private $needBuilds; 16 17 public function withIDs(array $ids) { 18 $this->ids = $ids; 19 return $this; 20 } 21 22 public function withPHIDs(array $phids) { 23 $this->phids = $phids; 24 return $this; 25 } 26 27 public function withBuildablePHIDs(array $buildable_phids) { 28 $this->buildablePHIDs = $buildable_phids; 29 return $this; 30 } 31 32 public function withContainerPHIDs(array $container_phids) { 33 $this->containerPHIDs = $container_phids; 34 return $this; 35 } 36 37 public function withManualBuildables($manual) { 38 $this->manualBuildables = $manual; 39 return $this; 40 } 41 42 public function needContainerObjects($need) { 43 $this->needContainerObjects = $need; 44 return $this; 45 } 46 47 public function needContainerHandles($need) { 48 $this->needContainerHandles = $need; 49 return $this; 50 } 51 52 public function needBuildableHandles($need) { 53 $this->needBuildableHandles = $need; 54 return $this; 55 } 56 57 public function needBuilds($need) { 58 $this->needBuilds = $need; 59 return $this; 60 } 61 62 protected function loadPage() { 63 $table = new HarbormasterBuildable(); 64 $conn_r = $table->establishConnection('r'); 65 66 $data = queryfx_all( 67 $conn_r, 68 'SELECT * FROM %T %Q %Q %Q', 69 $table->getTableName(), 70 $this->buildWhereClause($conn_r), 71 $this->buildOrderClause($conn_r), 72 $this->buildLimitClause($conn_r)); 73 74 return $table->loadAllFromArray($data); 75 } 76 77 protected function willFilterPage(array $page) { 78 $buildables = array(); 79 80 $buildable_phids = array_filter(mpull($page, 'getBuildablePHID')); 81 if ($buildable_phids) { 82 $buildables = id(new PhabricatorObjectQuery()) 83 ->setViewer($this->getViewer()) 84 ->withPHIDs($buildable_phids) 85 ->setParentQuery($this) 86 ->execute(); 87 $buildables = mpull($buildables, null, 'getPHID'); 88 } 89 90 foreach ($page as $key => $buildable) { 91 $buildable_phid = $buildable->getBuildablePHID(); 92 if (empty($buildables[$buildable_phid])) { 93 unset($page[$key]); 94 continue; 95 } 96 $buildable->attachBuildableObject($buildables[$buildable_phid]); 97 } 98 99 return $page; 100 } 101 102 protected function didFilterPage(array $page) { 103 if ($this->needContainerObjects || $this->needContainerHandles) { 104 $container_phids = array_filter(mpull($page, 'getContainerPHID')); 105 106 if ($this->needContainerObjects) { 107 $containers = array(); 108 109 if ($container_phids) { 110 $containers = id(new PhabricatorObjectQuery()) 111 ->setViewer($this->getViewer()) 112 ->withPHIDs($container_phids) 113 ->setParentQuery($this) 114 ->execute(); 115 $containers = mpull($containers, null, 'getPHID'); 116 } 117 118 foreach ($page as $key => $buildable) { 119 $container_phid = $buildable->getContainerPHID(); 120 $buildable->attachContainerObject(idx($containers, $container_phid)); 121 } 122 } 123 124 if ($this->needContainerHandles) { 125 $handles = array(); 126 127 if ($container_phids) { 128 $handles = id(new PhabricatorHandleQuery()) 129 ->setViewer($this->getViewer()) 130 ->withPHIDs($container_phids) 131 ->setParentQuery($this) 132 ->execute(); 133 } 134 135 foreach ($page as $key => $buildable) { 136 $container_phid = $buildable->getContainerPHID(); 137 $buildable->attachContainerHandle(idx($handles, $container_phid)); 138 } 139 } 140 } 141 142 if ($this->needBuildableHandles) { 143 $handles = array(); 144 145 $handle_phids = array_filter(mpull($page, 'getBuildablePHID')); 146 if ($handle_phids) { 147 $handles = id(new PhabricatorHandleQuery()) 148 ->setViewer($this->getViewer()) 149 ->withPHIDs($handle_phids) 150 ->setParentQuery($this) 151 ->execute(); 152 } 153 154 foreach ($page as $key => $buildable) { 155 $handle_phid = $buildable->getBuildablePHID(); 156 $buildable->attachBuildableHandle(idx($handles, $handle_phid)); 157 } 158 } 159 160 if ($this->needBuilds) { 161 $builds = id(new HarbormasterBuildQuery()) 162 ->setViewer($this->getViewer()) 163 ->setParentQuery($this) 164 ->withBuildablePHIDs(mpull($page, 'getPHID')) 165 ->execute(); 166 $builds = mgroup($builds, 'getBuildablePHID'); 167 foreach ($page as $key => $buildable) { 168 $buildable->attachBuilds(idx($builds, $buildable->getPHID(), array())); 169 } 170 } 171 172 return $page; 173 } 174 175 private function buildWhereClause(AphrontDatabaseConnection $conn_r) { 176 $where = array(); 177 178 if ($this->ids !== null) { 179 $where[] = qsprintf( 180 $conn_r, 181 'id IN (%Ld)', 182 $this->ids); 183 } 184 185 if ($this->phids !== null) { 186 $where[] = qsprintf( 187 $conn_r, 188 'phid IN (%Ls)', 189 $this->phids); 190 } 191 192 if ($this->buildablePHIDs !== null) { 193 $where[] = qsprintf( 194 $conn_r, 195 'buildablePHID IN (%Ls)', 196 $this->buildablePHIDs); 197 } 198 199 if ($this->containerPHIDs !== null) { 200 $where[] = qsprintf( 201 $conn_r, 202 'containerPHID in (%Ls)', 203 $this->containerPHIDs); 204 } 205 206 if ($this->manualBuildables !== null) { 207 $where[] = qsprintf( 208 $conn_r, 209 'isManualBuildable = %d', 210 (int)$this->manualBuildables); 211 } 212 213 $where[] = $this->buildPagingClause($conn_r); 214 215 return $this->formatWhereClause($where); 216 } 217 218 public function getQueryApplicationClass() { 219 return 'PhabricatorHarbormasterApplication'; 220 } 221 222 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |