Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / flag / query / PhabricatorFlagQuery.php
blobc6c905465dfde533e92b3bd5cad6fa924656e696
1 <?php
3 final class PhabricatorFlagQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 const GROUP_COLOR = 'color';
7 const GROUP_NONE = 'none';
9 private $ids;
10 private $ownerPHIDs;
11 private $types;
12 private $objectPHIDs;
13 private $colors;
14 private $groupBy = self::GROUP_NONE;
16 private $needHandles;
17 private $needObjects;
19 public function withIDs(array $ids) {
20 $this->ids = $ids;
21 return $this;
24 public function withOwnerPHIDs(array $owner_phids) {
25 $this->ownerPHIDs = $owner_phids;
26 return $this;
29 public function withTypes(array $types) {
30 $this->types = $types;
31 return $this;
34 public function withObjectPHIDs(array $object_phids) {
35 $this->objectPHIDs = $object_phids;
36 return $this;
39 public function withColors(array $colors) {
40 $this->colors = $colors;
41 return $this;
44 /**
45 * NOTE: this is done in PHP and not in MySQL, which means its inappropriate
46 * for large datasets. Pragmatically, this is fine for user flags which are
47 * typically well under 100 flags per user.
49 public function setGroupBy($group) {
50 $this->groupBy = $group;
51 return $this;
54 public function needHandles($need) {
55 $this->needHandles = $need;
56 return $this;
59 public function needObjects($need) {
60 $this->needObjects = $need;
61 return $this;
64 public static function loadUserFlag(PhabricatorUser $user, $object_phid) {
65 // Specifying the type in the query allows us to use a key.
66 return id(new PhabricatorFlagQuery())
67 ->setViewer($user)
68 ->withOwnerPHIDs(array($user->getPHID()))
69 ->withTypes(array(phid_get_type($object_phid)))
70 ->withObjectPHIDs(array($object_phid))
71 ->executeOne();
74 protected function loadPage() {
75 $table = new PhabricatorFlag();
76 $conn_r = $table->establishConnection('r');
78 $data = queryfx_all(
79 $conn_r,
80 'SELECT * FROM %T flag %Q %Q %Q',
81 $table->getTableName(),
82 $this->buildWhereClause($conn_r),
83 $this->buildOrderClause($conn_r),
84 $this->buildLimitClause($conn_r));
86 return $table->loadAllFromArray($data);
89 protected function willFilterPage(array $flags) {
90 if ($this->needObjects) {
91 $objects = id(new PhabricatorObjectQuery())
92 ->setViewer($this->getViewer())
93 ->withPHIDs(mpull($flags, 'getObjectPHID'))
94 ->execute();
95 $objects = mpull($objects, null, 'getPHID');
96 foreach ($flags as $key => $flag) {
97 $object = idx($objects, $flag->getObjectPHID());
98 if ($object) {
99 $flags[$key]->attachObject($object);
100 } else {
101 unset($flags[$key]);
106 if ($this->needHandles) {
107 $handles = id(new PhabricatorHandleQuery())
108 ->setViewer($this->getViewer())
109 ->withPHIDs(mpull($flags, 'getObjectPHID'))
110 ->execute();
112 foreach ($flags as $flag) {
113 $flag->attachHandle($handles[$flag->getObjectPHID()]);
117 switch ($this->groupBy) {
118 case self::GROUP_COLOR:
119 $flags = msort($flags, 'getColor');
120 break;
121 case self::GROUP_NONE:
122 break;
123 default:
124 throw new Exception(
125 pht('Unknown groupBy parameter: %s', $this->groupBy));
126 break;
129 return $flags;
132 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
133 $where = array();
135 if ($this->ids !== null) {
136 $where[] = qsprintf(
137 $conn,
138 'flag.id IN (%Ld)',
139 $this->ids);
142 if ($this->ownerPHIDs) {
143 $where[] = qsprintf(
144 $conn,
145 'flag.ownerPHID IN (%Ls)',
146 $this->ownerPHIDs);
149 if ($this->types) {
150 $where[] = qsprintf(
151 $conn,
152 'flag.type IN (%Ls)',
153 $this->types);
156 if ($this->objectPHIDs) {
157 $where[] = qsprintf(
158 $conn,
159 'flag.objectPHID IN (%Ls)',
160 $this->objectPHIDs);
163 if ($this->colors) {
164 $where[] = qsprintf(
165 $conn,
166 'flag.color IN (%Ld)',
167 $this->colors);
170 $where[] = $this->buildPagingClause($conn);
172 return $this->formatWhereClause($conn, $where);
175 public function getQueryApplicationClass() {
176 return 'PhabricatorFlagsApplication';