Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / phid / query / PhabricatorHandleQuery.php
blob957602838a7956e1085d41ee36887ea34744e83e
1 <?php
3 final class PhabricatorHandleQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $objectCapabilities;
7 private $phids = array();
9 public function withPHIDs(array $phids) {
10 $this->phids = $phids;
11 return $this;
14 public function requireObjectCapabilities(array $capabilities) {
15 $this->objectCapabilities = $capabilities;
16 return $this;
19 protected function getRequiredObjectCapabilities() {
20 if ($this->objectCapabilities) {
21 return $this->objectCapabilities;
23 return $this->getRequiredCapabilities();
26 protected function loadPage() {
27 $types = PhabricatorPHIDType::getAllTypes();
29 $phids = array_unique($this->phids);
30 if (!$phids) {
31 return array();
34 $object_query = id(new PhabricatorObjectQuery())
35 ->withPHIDs($phids)
36 ->setParentQuery($this)
37 ->requireCapabilities($this->getRequiredObjectCapabilities())
38 ->setViewer($this->getViewer());
40 // We never want the subquery to raise policy exceptions, even if this
41 // query is being executed via executeOne(). Policy exceptions are not
42 // meaningful or relevant for handles, which load in an "Unknown" or
43 // "Restricted" state after encountering a policy violation.
44 $object_query->setRaisePolicyExceptions(false);
46 $objects = $object_query->execute();
47 $filtered = $object_query->getPolicyFilteredPHIDs();
49 $groups = array();
50 foreach ($phids as $phid) {
51 $type = phid_get_type($phid);
52 $groups[$type][] = $phid;
55 $results = array();
56 foreach ($groups as $type => $phid_group) {
57 $handles = array();
58 foreach ($phid_group as $key => $phid) {
59 if (isset($handles[$phid])) {
60 unset($phid_group[$key]);
61 // The input had a duplicate PHID; just skip it.
62 continue;
64 $handles[$phid] = id(new PhabricatorObjectHandle())
65 ->setType($type)
66 ->setPHID($phid);
67 if (isset($objects[$phid])) {
68 $handles[$phid]->setComplete(true);
69 } else if (isset($filtered[$phid])) {
70 $handles[$phid]->setPolicyFiltered(true);
74 if (isset($types[$type])) {
75 $type_objects = array_select_keys($objects, $phid_group);
76 if ($type_objects) {
77 $have_object_phids = array_keys($type_objects);
78 $types[$type]->loadHandles(
79 $this,
80 array_select_keys($handles, $have_object_phids),
81 $type_objects);
85 $results += $handles;
88 return $results;
91 public function getQueryApplicationClass() {
92 return null;