3 final class PhabricatorObjectQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
6 private $phids = array();
7 private $names = array();
10 private $namedResults;
12 public function withPHIDs(array $phids) {
13 $this->phids
= $phids;
17 public function withNames(array $names) {
18 $this->names
= $names;
22 public function withTypes(array $types) {
23 $this->types
= $types;
27 protected function loadPage() {
28 if ($this->namedResults
=== null) {
29 $this->namedResults
= array();
32 $names = array_unique($this->names
);
33 $phids = $this->phids
;
35 // We allow objects to be named by their PHID in addition to their normal
36 // name so that, e.g., CLI tools which accept object names can also accept
37 // PHIDs and work as users expect.
38 $actually_phids = array();
40 foreach ($names as $key => $name) {
41 if (!strncmp($name, 'PHID-', 5)) {
42 $actually_phids[] = $name;
50 $types = PhabricatorPHIDType
::getAllTypes();
52 $types = array_select_keys($types, $this->types
);
54 $name_results = $this->loadObjectsByName($types, $names);
56 $name_results = array();
60 $phids = array_unique($phids);
62 $phid_types = array();
63 foreach ($phids as $phid) {
64 $phid_type = phid_get_type($phid);
65 $phid_types[$phid_type] = $phid_type;
68 $types = PhabricatorPHIDType
::getTypes($phid_types);
70 $types = array_select_keys($types, $this->types
);
73 $phid_results = $this->loadObjectsByPHID($types, $phids);
75 $phid_results = array();
78 foreach ($actually_phids as $phid) {
79 if (isset($phid_results[$phid])) {
80 $name_results[$phid] = $phid_results[$phid];
84 $this->namedResults +
= $name_results;
86 return $phid_results +
mpull($name_results, null, 'getPHID');
89 public function getNamedResults() {
90 if ($this->namedResults
=== null) {
91 throw new PhutilInvalidStateException('execute');
93 return $this->namedResults
;
96 private function loadObjectsByName(array $types, array $names) {
98 foreach ($names as $name) {
99 foreach ($types as $type => $type_impl) {
100 if (!$type_impl->canLoadNamedObject($name)) {
103 $groups[$type][] = $name;
109 foreach ($groups as $type => $group) {
110 $results +
= $types[$type]->loadNamedObjects($this, $group);
116 private function loadObjectsByPHID(array $types, array $phids) {
120 foreach ($phids as $phid) {
121 $type = phid_get_type($phid);
122 $groups[$type][] = $phid;
125 $in_flight = $this->getPHIDsInFlight();
126 foreach ($groups as $type => $group) {
127 // We check the workspace for each group, because some groups may trigger
128 // other groups to load (for example, transactions load their objects).
129 $workspace = $this->getObjectsFromWorkspace($group);
131 foreach ($group as $key => $phid) {
132 if (isset($workspace[$phid])) {
133 $results[$phid] = $workspace[$phid];
142 // Don't try to load PHIDs which are already "in flight"; this prevents
143 // us from recursing indefinitely if policy checks or edges form a loop.
144 // We will decline to load the corresponding objects.
145 foreach ($group as $key => $phid) {
146 if (isset($in_flight[$phid])) {
151 if ($group && isset($types[$type])) {
152 $this->putPHIDsInFlight($group);
153 $objects = $types[$type]->loadObjects($this, $group);
155 $map = mpull($objects, null, 'getPHID');
156 $this->putObjectsInWorkspace($map);
164 protected function didFilterResults(array $filtered) {
165 foreach ($this->namedResults
as $name => $result) {
166 if (isset($filtered[$result->getPHID()])) {
167 unset($this->namedResults
[$name]);
173 * This query disables policy filtering if the only required capability is
174 * the view capability.
176 * The view capability is always checked in the subqueries, so we do not need
177 * to re-filter results. For any other set of required capabilities, we do.
179 protected function shouldDisablePolicyFiltering() {
180 $view_capability = PhabricatorPolicyCapability
::CAN_VIEW
;
181 if ($this->getRequiredCapabilities() === array($view_capability)) {
187 public function getQueryApplicationClass() {
193 * Select invalid or restricted PHIDs from a list.
195 * PHIDs are invalid if their objects do not exist or can not be seen by the
196 * viewer. This method is generally used to validate that PHIDs affected by
197 * a transaction are valid.
199 * @param PhabricatorUser Viewer.
200 * @param list<phid> List of ostensibly valid PHIDs.
201 * @return list<phid> List of invalid or restricted PHIDs.
203 public static function loadInvalidPHIDsForViewer(
204 PhabricatorUser
$viewer,
211 $objects = id(new PhabricatorObjectQuery())
215 $objects = mpull($objects, null, 'getPHID');
218 foreach ($phids as $phid) {
219 if (empty($objects[$phid])) {