forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorSpacesNamespaceQuery.php
238 lines (190 loc) · 5.48 KB
/
PhabricatorSpacesNamespaceQuery.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
final class PhabricatorSpacesNamespaceQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
const KEY_ALL = 'spaces.all';
const KEY_DEFAULT = 'spaces.default';
const KEY_VIEWER = 'spaces.viewer';
private $ids;
private $phids;
private $isDefaultNamespace;
private $isArchived;
public function withIDs(array $ids) {
$this->ids = $ids;
return $this;
}
public function withPHIDs(array $phids) {
$this->phids = $phids;
return $this;
}
public function withIsDefaultNamespace($default) {
$this->isDefaultNamespace = $default;
return $this;
}
public function withIsArchived($archived) {
$this->isArchived = $archived;
return $this;
}
public function getQueryApplicationClass() {
return 'PhabricatorSpacesApplication';
}
protected function loadPage() {
return $this->loadStandardPage(new PhabricatorSpacesNamespace());
}
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
$where = parent::buildWhereClauseParts($conn);
if ($this->ids !== null) {
$where[] = qsprintf(
$conn,
'id IN (%Ld)',
$this->ids);
}
if ($this->phids !== null) {
$where[] = qsprintf(
$conn,
'phid IN (%Ls)',
$this->phids);
}
if ($this->isDefaultNamespace !== null) {
if ($this->isDefaultNamespace) {
$where[] = qsprintf(
$conn,
'isDefaultNamespace = 1');
} else {
$where[] = qsprintf(
$conn,
'isDefaultNamespace IS NULL');
}
}
if ($this->isArchived !== null) {
$where[] = qsprintf(
$conn,
'isArchived = %d',
(int)$this->isArchived);
}
return $where;
}
public static function destroySpacesCache() {
$cache = PhabricatorCaches::getRequestCache();
$cache->deleteKeys(
array(
self::KEY_ALL,
self::KEY_DEFAULT,
));
}
public static function getSpacesExist() {
return (bool)self::getAllSpaces();
}
public static function getViewerSpacesExist(PhabricatorUser $viewer) {
if (!self::getSpacesExist()) {
return false;
}
// If the viewer has access to only one space, pretend spaces simply don't
// exist.
$spaces = self::getViewerSpaces($viewer);
return (count($spaces) > 1);
}
public static function getAllSpaces() {
$cache = PhabricatorCaches::getRequestCache();
$cache_key = self::KEY_ALL;
$spaces = $cache->getKey($cache_key);
if ($spaces === null) {
$spaces = id(new PhabricatorSpacesNamespaceQuery())
->setViewer(PhabricatorUser::getOmnipotentUser())
->execute();
$spaces = mpull($spaces, null, 'getPHID');
$cache->setKey($cache_key, $spaces);
}
return $spaces;
}
public static function getDefaultSpace() {
$cache = PhabricatorCaches::getRequestCache();
$cache_key = self::KEY_DEFAULT;
$default_space = $cache->getKey($cache_key, false);
if ($default_space === false) {
$default_space = null;
$spaces = self::getAllSpaces();
foreach ($spaces as $space) {
if ($space->getIsDefaultNamespace()) {
$default_space = $space;
break;
}
}
$cache->setKey($cache_key, $default_space);
}
return $default_space;
}
public static function getViewerSpaces(PhabricatorUser $viewer) {
$cache = PhabricatorCaches::getRequestCache();
$cache_key = self::KEY_VIEWER.'('.$viewer->getCacheFragment().')';
$result = $cache->getKey($cache_key);
if ($result === null) {
$spaces = self::getAllSpaces();
$result = array();
foreach ($spaces as $key => $space) {
$can_see = PhabricatorPolicyFilter::hasCapability(
$viewer,
$space,
PhabricatorPolicyCapability::CAN_VIEW);
if ($can_see) {
$result[$key] = $space;
}
}
$cache->setKey($cache_key, $result);
}
return $result;
}
public static function getViewerActiveSpaces(PhabricatorUser $viewer) {
$spaces = self::getViewerSpaces($viewer);
foreach ($spaces as $key => $space) {
if ($space->getIsArchived()) {
unset($spaces[$key]);
}
}
return $spaces;
}
public static function getSpaceOptionsForViewer(
PhabricatorUser $viewer,
$space_phid) {
$viewer_spaces = self::getViewerSpaces($viewer);
$viewer_spaces = msort($viewer_spaces, 'getNamespaceName');
$map = array();
foreach ($viewer_spaces as $space) {
// Skip archived spaces, unless the object is already in that space.
if ($space->getIsArchived()) {
if ($space->getPHID() != $space_phid) {
continue;
}
}
$map[$space->getPHID()] = pht(
'Space %s: %s',
$space->getMonogram(),
$space->getNamespaceName());
}
return $map;
}
/**
* Get the Space PHID for an object, if one exists.
*
* This is intended to simplify performing a bunch of redundant checks; you
* can intentionally pass any value in (including `null`).
*
* @param wild
* @return phid|null
*/
public static function getObjectSpacePHID($object) {
if (!$object) {
return null;
}
if (!($object instanceof PhabricatorSpacesInterface)) {
return null;
}
$space_phid = $object->getSpacePHID();
if ($space_phid === null) {
$default_space = self::getDefaultSpace();
if ($default_space) {
$space_phid = $default_space->getPHID();
}
}
return $space_phid;
}
}