[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 final class PhabricatorObjectQuery 4 extends PhabricatorCursorPagedPolicyAwareQuery { 5 6 private $phids = array(); 7 private $names = array(); 8 private $types; 9 10 private $namedResults; 11 12 public function withPHIDs(array $phids) { 13 $this->phids = $phids; 14 return $this; 15 } 16 17 public function withNames(array $names) { 18 $this->names = $names; 19 return $this; 20 } 21 22 public function withTypes(array $types) { 23 $this->types = $types; 24 return $this; 25 } 26 27 public function loadPage() { 28 if ($this->namedResults === null) { 29 $this->namedResults = array(); 30 } 31 32 $types = PhabricatorPHIDType::getAllTypes(); 33 if ($this->types) { 34 $types = array_select_keys($types, $this->types); 35 } 36 37 $names = array_unique($this->names); 38 $phids = $this->phids; 39 40 // We allow objects to be named by their PHID in addition to their normal 41 // name so that, e.g., CLI tools which accept object names can also accept 42 // PHIDs and work as users expect. 43 $actually_phids = array(); 44 if ($names) { 45 foreach ($names as $key => $name) { 46 if (!strncmp($name, 'PHID-', 5)) { 47 $actually_phids[] = $name; 48 $phids[] = $name; 49 unset($names[$key]); 50 } 51 } 52 } 53 54 $phids = array_unique($phids); 55 56 if ($names) { 57 $name_results = $this->loadObjectsByName($types, $names); 58 } else { 59 $name_results = array(); 60 } 61 62 if ($phids) { 63 $phid_results = $this->loadObjectsByPHID($types, $phids); 64 } else { 65 $phid_results = array(); 66 } 67 68 foreach ($actually_phids as $phid) { 69 if (isset($phid_results[$phid])) { 70 $name_results[$phid] = $phid_results[$phid]; 71 } 72 } 73 74 $this->namedResults += $name_results; 75 76 return $phid_results + mpull($name_results, null, 'getPHID'); 77 } 78 79 public function getNamedResults() { 80 if ($this->namedResults === null) { 81 throw new Exception('Call execute() before getNamedResults()!'); 82 } 83 return $this->namedResults; 84 } 85 86 private function loadObjectsByName(array $types, array $names) { 87 $groups = array(); 88 foreach ($names as $name) { 89 foreach ($types as $type => $type_impl) { 90 if (!$type_impl->canLoadNamedObject($name)) { 91 continue; 92 } 93 $groups[$type][] = $name; 94 break; 95 } 96 } 97 98 $results = array(); 99 foreach ($groups as $type => $group) { 100 $results += $types[$type]->loadNamedObjects($this, $group); 101 } 102 103 return $results; 104 } 105 106 private function loadObjectsByPHID(array $types, array $phids) { 107 $results = array(); 108 109 $workspace = $this->getObjectsFromWorkspace($phids); 110 111 foreach ($phids as $key => $phid) { 112 if (isset($workspace[$phid])) { 113 $results[$phid] = $workspace[$phid]; 114 unset($phids[$key]); 115 } 116 } 117 118 if (!$phids) { 119 return $results; 120 } 121 122 $groups = array(); 123 foreach ($phids as $phid) { 124 $type = phid_get_type($phid); 125 $groups[$type][] = $phid; 126 } 127 128 foreach ($groups as $type => $group) { 129 if (isset($types[$type])) { 130 $objects = $types[$type]->loadObjects($this, $group); 131 $results += mpull($objects, null, 'getPHID'); 132 } 133 } 134 135 return $results; 136 } 137 138 protected function didFilterResults(array $filtered) { 139 foreach ($this->namedResults as $name => $result) { 140 if (isset($filtered[$result->getPHID()])) { 141 unset($this->namedResults[$name]); 142 } 143 } 144 } 145 146 /** 147 * This query disables policy filtering because it is performed in the 148 * subqueries which actually load objects. We don't need to re-filter 149 * results, since policies have already been applied. 150 */ 151 protected function shouldDisablePolicyFiltering() { 152 return true; 153 } 154 155 public function getQueryApplicationClass() { 156 return null; 157 } 158 159 }
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 |