Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/flag/query/PhabricatorFlagQuery.php
12262 views
1
<?php
2
3
final class PhabricatorFlagQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
const GROUP_COLOR = 'color';
7
const GROUP_NONE = 'none';
8
9
private $ids;
10
private $ownerPHIDs;
11
private $types;
12
private $objectPHIDs;
13
private $colors;
14
private $groupBy = self::GROUP_NONE;
15
16
private $needHandles;
17
private $needObjects;
18
19
public function withIDs(array $ids) {
20
$this->ids = $ids;
21
return $this;
22
}
23
24
public function withOwnerPHIDs(array $owner_phids) {
25
$this->ownerPHIDs = $owner_phids;
26
return $this;
27
}
28
29
public function withTypes(array $types) {
30
$this->types = $types;
31
return $this;
32
}
33
34
public function withObjectPHIDs(array $object_phids) {
35
$this->objectPHIDs = $object_phids;
36
return $this;
37
}
38
39
public function withColors(array $colors) {
40
$this->colors = $colors;
41
return $this;
42
}
43
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.
48
*/
49
public function setGroupBy($group) {
50
$this->groupBy = $group;
51
return $this;
52
}
53
54
public function needHandles($need) {
55
$this->needHandles = $need;
56
return $this;
57
}
58
59
public function needObjects($need) {
60
$this->needObjects = $need;
61
return $this;
62
}
63
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();
72
}
73
74
protected function loadPage() {
75
$table = new PhabricatorFlag();
76
$conn_r = $table->establishConnection('r');
77
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));
85
86
return $table->loadAllFromArray($data);
87
}
88
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]);
102
}
103
}
104
}
105
106
if ($this->needHandles) {
107
$handles = id(new PhabricatorHandleQuery())
108
->setViewer($this->getViewer())
109
->withPHIDs(mpull($flags, 'getObjectPHID'))
110
->execute();
111
112
foreach ($flags as $flag) {
113
$flag->attachHandle($handles[$flag->getObjectPHID()]);
114
}
115
}
116
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;
127
}
128
129
return $flags;
130
}
131
132
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
133
$where = array();
134
135
if ($this->ids !== null) {
136
$where[] = qsprintf(
137
$conn,
138
'flag.id IN (%Ld)',
139
$this->ids);
140
}
141
142
if ($this->ownerPHIDs) {
143
$where[] = qsprintf(
144
$conn,
145
'flag.ownerPHID IN (%Ls)',
146
$this->ownerPHIDs);
147
}
148
149
if ($this->types) {
150
$where[] = qsprintf(
151
$conn,
152
'flag.type IN (%Ls)',
153
$this->types);
154
}
155
156
if ($this->objectPHIDs) {
157
$where[] = qsprintf(
158
$conn,
159
'flag.objectPHID IN (%Ls)',
160
$this->objectPHIDs);
161
}
162
163
if ($this->colors) {
164
$where[] = qsprintf(
165
$conn,
166
'flag.color IN (%Ld)',
167
$this->colors);
168
}
169
170
$where[] = $this->buildPagingClause($conn);
171
172
return $this->formatWhereClause($conn, $where);
173
}
174
175
public function getQueryApplicationClass() {
176
return 'PhabricatorFlagsApplication';
177
}
178
179
}
180
181