[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class HeraldRuleQuery extends PhabricatorCursorPagedPolicyAwareQuery { 4 5 private $ids; 6 private $phids; 7 private $authorPHIDs; 8 private $ruleTypes; 9 private $contentTypes; 10 private $disabled; 11 private $triggerObjectPHIDs; 12 13 private $needConditionsAndActions; 14 private $needAppliedToPHIDs; 15 private $needValidateAuthors; 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 withAuthorPHIDs(array $author_phids) { 28 $this->authorPHIDs = $author_phids; 29 return $this; 30 } 31 32 public function withRuleTypes(array $types) { 33 $this->ruleTypes = $types; 34 return $this; 35 } 36 37 public function withContentTypes(array $types) { 38 $this->contentTypes = $types; 39 return $this; 40 } 41 42 public function withExecutableRules($executable) { 43 $this->executable = $executable; 44 return $this; 45 } 46 47 public function withDisabled($disabled) { 48 $this->disabled = $disabled; 49 return $this; 50 } 51 52 public function withTriggerObjectPHIDs(array $phids) { 53 $this->triggerObjectPHIDs = $phids; 54 return $this; 55 } 56 57 public function needConditionsAndActions($need) { 58 $this->needConditionsAndActions = $need; 59 return $this; 60 } 61 62 public function needAppliedToPHIDs(array $phids) { 63 $this->needAppliedToPHIDs = $phids; 64 return $this; 65 } 66 67 public function needValidateAuthors($need) { 68 $this->needValidateAuthors = $need; 69 return $this; 70 } 71 72 public function loadPage() { 73 $table = new HeraldRule(); 74 $conn_r = $table->establishConnection('r'); 75 76 $data = queryfx_all( 77 $conn_r, 78 'SELECT rule.* FROM %T rule %Q %Q %Q', 79 $table->getTableName(), 80 $this->buildWhereClause($conn_r), 81 $this->buildOrderClause($conn_r), 82 $this->buildLimitClause($conn_r)); 83 84 return $table->loadAllFromArray($data); 85 } 86 87 public function willFilterPage(array $rules) { 88 $rule_ids = mpull($rules, 'getID'); 89 90 // Filter out any rules that have invalid adapters, or have adapters the 91 // viewer isn't permitted to see or use (for example, Differential rules 92 // if the user can't use Differential or Differential is not installed). 93 $types = HeraldAdapter::getEnabledAdapterMap($this->getViewer()); 94 foreach ($rules as $key => $rule) { 95 if (empty($types[$rule->getContentType()])) { 96 $this->didRejectResult($rule); 97 unset($rules[$key]); 98 } 99 } 100 101 if ($this->needValidateAuthors) { 102 $this->validateRuleAuthors($rules); 103 } 104 105 if ($this->needConditionsAndActions) { 106 $conditions = id(new HeraldCondition())->loadAllWhere( 107 'ruleID IN (%Ld)', 108 $rule_ids); 109 $conditions = mgroup($conditions, 'getRuleID'); 110 111 $actions = id(new HeraldAction())->loadAllWhere( 112 'ruleID IN (%Ld)', 113 $rule_ids); 114 $actions = mgroup($actions, 'getRuleID'); 115 116 foreach ($rules as $rule) { 117 $rule->attachActions(idx($actions, $rule->getID(), array())); 118 $rule->attachConditions(idx($conditions, $rule->getID(), array())); 119 } 120 } 121 122 if ($this->needAppliedToPHIDs) { 123 $conn_r = id(new HeraldRule())->establishConnection('r'); 124 $applied = queryfx_all( 125 $conn_r, 126 'SELECT * FROM %T WHERE ruleID IN (%Ld) AND phid IN (%Ls)', 127 HeraldRule::TABLE_RULE_APPLIED, 128 $rule_ids, 129 $this->needAppliedToPHIDs); 130 131 $map = array(); 132 foreach ($applied as $row) { 133 $map[$row['ruleID']][$row['phid']] = true; 134 } 135 136 foreach ($rules as $rule) { 137 foreach ($this->needAppliedToPHIDs as $phid) { 138 $rule->setRuleApplied( 139 $phid, 140 isset($map[$rule->getID()][$phid])); 141 } 142 } 143 } 144 145 $object_phids = array(); 146 foreach ($rules as $rule) { 147 if ($rule->isObjectRule()) { 148 $object_phids[] = $rule->getTriggerObjectPHID(); 149 } 150 } 151 152 if ($object_phids) { 153 $objects = id(new PhabricatorObjectQuery()) 154 ->setParentQuery($this) 155 ->setViewer($this->getViewer()) 156 ->withPHIDs($object_phids) 157 ->execute(); 158 $objects = mpull($objects, null, 'getPHID'); 159 } else { 160 $objects = array(); 161 } 162 163 foreach ($rules as $key => $rule) { 164 if ($rule->isObjectRule()) { 165 $object = idx($objects, $rule->getTriggerObjectPHID()); 166 if (!$object) { 167 unset($rules[$key]); 168 continue; 169 } 170 $rule->attachTriggerObject($object); 171 } 172 } 173 174 return $rules; 175 } 176 177 private function buildWhereClause($conn_r) { 178 $where = array(); 179 180 if ($this->ids) { 181 $where[] = qsprintf( 182 $conn_r, 183 'rule.id IN (%Ld)', 184 $this->ids); 185 } 186 187 if ($this->phids) { 188 $where[] = qsprintf( 189 $conn_r, 190 'rule.phid IN (%Ls)', 191 $this->phids); 192 } 193 194 if ($this->authorPHIDs) { 195 $where[] = qsprintf( 196 $conn_r, 197 'rule.authorPHID IN (%Ls)', 198 $this->authorPHIDs); 199 } 200 201 if ($this->ruleTypes) { 202 $where[] = qsprintf( 203 $conn_r, 204 'rule.ruleType IN (%Ls)', 205 $this->ruleTypes); 206 } 207 208 if ($this->contentTypes) { 209 $where[] = qsprintf( 210 $conn_r, 211 'rule.contentType IN (%Ls)', 212 $this->contentTypes); 213 } 214 215 if ($this->disabled !== null) { 216 $where[] = qsprintf( 217 $conn_r, 218 'rule.isDisabled = %d', 219 (int)$this->disabled); 220 } 221 222 if ($this->triggerObjectPHIDs) { 223 $where[] = qsprintf( 224 $conn_r, 225 'rule.triggerObjectPHID IN (%Ls)', 226 $this->triggerObjectPHIDs); 227 } 228 229 $where[] = $this->buildPagingClause($conn_r); 230 231 return $this->formatWhereClause($where); 232 } 233 234 private function validateRuleAuthors(array $rules) { 235 // "Global" and "Object" rules always have valid authors. 236 foreach ($rules as $key => $rule) { 237 if ($rule->isGlobalRule() || $rule->isObjectRule()) { 238 $rule->attachValidAuthor(true); 239 unset($rules[$key]); 240 continue; 241 } 242 } 243 244 if (!$rules) { 245 return; 246 } 247 248 // For personal rules, the author needs to exist and not be disabled. 249 $user_phids = mpull($rules, 'getAuthorPHID'); 250 $users = id(new PhabricatorPeopleQuery()) 251 ->setViewer($this->getViewer()) 252 ->withPHIDs($user_phids) 253 ->execute(); 254 $users = mpull($users, null, 'getPHID'); 255 256 foreach ($rules as $key => $rule) { 257 $author_phid = $rule->getAuthorPHID(); 258 if (empty($users[$author_phid])) { 259 $rule->attachValidAuthor(false); 260 continue; 261 } 262 if (!$users[$author_phid]->isUserActivated()) { 263 $rule->attachValidAuthor(false); 264 continue; 265 } 266 267 $rule->attachValidAuthor(true); 268 $rule->attachAuthor($users[$author_phid]); 269 } 270 } 271 272 public function getQueryApplicationClass() { 273 return 'PhabricatorHeraldApplication'; 274 } 275 276 }
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 |