@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @extends PhabricatorCursorPagedPolicyAwareQuery<AlmanacProperty>
5 */
6final class AlmanacPropertyQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $objectPHIDs;
11 private $objects;
12 private $names;
13
14 public function withIDs(array $ids) {
15 $this->ids = $ids;
16 return $this;
17 }
18
19 public function withObjectPHIDs(array $phids) {
20 $this->objectPHIDs = $phids;
21 return $this;
22 }
23
24 public function withObjects(array $objects) {
25 $this->objects = mpull($objects, null, 'getPHID');
26 $this->objectPHIDs = array_keys($this->objects);
27 return $this;
28 }
29
30 public function withNames(array $names) {
31 $this->names = $names;
32 return $this;
33 }
34
35 public function newResultObject() {
36 return new AlmanacProperty();
37 }
38
39 protected function willFilterPage(array $properties) {
40 $object_phids = mpull($properties, 'getObjectPHID');
41
42 $object_phids = array_fuse($object_phids);
43
44 if ($this->objects !== null) {
45 $object_phids = array_diff_key($object_phids, $this->objects);
46 }
47
48 if ($object_phids) {
49 $objects = id(new PhabricatorObjectQuery())
50 ->setViewer($this->getViewer())
51 ->setParentQuery($this)
52 ->withPHIDs($object_phids)
53 ->execute();
54 $objects = mpull($objects, null, 'getPHID');
55 } else {
56 $objects = array();
57 }
58
59 $objects += $this->objects;
60
61 foreach ($properties as $key => $property) {
62 $object = idx($objects, $property->getObjectPHID());
63 if (!$object) {
64 unset($properties[$key]);
65 continue;
66 }
67 $property->attachObject($object);
68 }
69
70 return $properties;
71 }
72
73 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
74 $where = parent::buildWhereClauseParts($conn);
75
76 if ($this->ids !== null) {
77 $where[] = qsprintf(
78 $conn,
79 'id IN (%Ld)',
80 $this->ids);
81 }
82
83 if ($this->objectPHIDs !== null) {
84 $where[] = qsprintf(
85 $conn,
86 'objectPHID IN (%Ls)',
87 $this->objectPHIDs);
88 }
89
90 if ($this->names !== null) {
91 $hashes = array();
92 foreach ($this->names as $name) {
93 $hashes[] = PhabricatorHash::digestForIndex($name);
94 }
95 $where[] = qsprintf(
96 $conn,
97 'fieldIndex IN (%Ls)',
98 $hashes);
99 }
100
101 return $where;
102 }
103
104 public function getQueryApplicationClass() {
105 return PhabricatorAlmanacApplication::class;
106 }
107
108}