3 final class PhabricatorSpacesNamespaceQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery
{
6 const KEY_ALL
= 'spaces.all';
7 const KEY_DEFAULT
= 'spaces.default';
8 const KEY_VIEWER
= 'spaces.viewer';
12 private $isDefaultNamespace;
15 public function withIDs(array $ids) {
20 public function withPHIDs(array $phids) {
21 $this->phids
= $phids;
25 public function withIsDefaultNamespace($default) {
26 $this->isDefaultNamespace
= $default;
30 public function withIsArchived($archived) {
31 $this->isArchived
= $archived;
35 public function getQueryApplicationClass() {
36 return 'PhabricatorSpacesApplication';
39 protected function loadPage() {
40 return $this->loadStandardPage(new PhabricatorSpacesNamespace());
43 protected function buildWhereClauseParts(AphrontDatabaseConnection
$conn) {
44 $where = parent
::buildWhereClauseParts($conn);
46 if ($this->ids
!== null) {
53 if ($this->phids
!== null) {
60 if ($this->isDefaultNamespace
!== null) {
61 if ($this->isDefaultNamespace
) {
64 'isDefaultNamespace = 1');
68 'isDefaultNamespace IS NULL');
72 if ($this->isArchived
!== null) {
76 (int)$this->isArchived
);
82 public static function destroySpacesCache() {
83 $cache = PhabricatorCaches
::getRequestCache();
91 public static function getSpacesExist() {
92 return (bool)self
::getAllSpaces();
95 public static function getViewerSpacesExist(PhabricatorUser
$viewer) {
96 if (!self
::getSpacesExist()) {
100 // If the viewer has access to only one space, pretend spaces simply don't
102 $spaces = self
::getViewerSpaces($viewer);
103 return (count($spaces) > 1);
106 public static function getAllSpaces() {
107 $cache = PhabricatorCaches
::getRequestCache();
108 $cache_key = self
::KEY_ALL
;
110 $spaces = $cache->getKey($cache_key);
111 if ($spaces === null) {
112 $spaces = id(new PhabricatorSpacesNamespaceQuery())
113 ->setViewer(PhabricatorUser
::getOmnipotentUser())
115 $spaces = mpull($spaces, null, 'getPHID');
116 $cache->setKey($cache_key, $spaces);
122 public static function getDefaultSpace() {
123 $cache = PhabricatorCaches
::getRequestCache();
124 $cache_key = self
::KEY_DEFAULT
;
126 $default_space = $cache->getKey($cache_key, false);
127 if ($default_space === false) {
128 $default_space = null;
130 $spaces = self
::getAllSpaces();
131 foreach ($spaces as $space) {
132 if ($space->getIsDefaultNamespace()) {
133 $default_space = $space;
138 $cache->setKey($cache_key, $default_space);
141 return $default_space;
144 public static function getViewerSpaces(PhabricatorUser
$viewer) {
145 $cache = PhabricatorCaches
::getRequestCache();
146 $cache_key = self
::KEY_VIEWER
.'('.$viewer->getCacheFragment().')';
148 $result = $cache->getKey($cache_key);
149 if ($result === null) {
150 $spaces = self
::getAllSpaces();
153 foreach ($spaces as $key => $space) {
154 $can_see = PhabricatorPolicyFilter
::hasCapability(
157 PhabricatorPolicyCapability
::CAN_VIEW
);
159 $result[$key] = $space;
163 $cache->setKey($cache_key, $result);
170 public static function getViewerActiveSpaces(PhabricatorUser
$viewer) {
171 $spaces = self
::getViewerSpaces($viewer);
173 foreach ($spaces as $key => $space) {
174 if ($space->getIsArchived()) {
175 unset($spaces[$key]);
182 public static function getSpaceOptionsForViewer(
183 PhabricatorUser
$viewer,
186 $viewer_spaces = self
::getViewerSpaces($viewer);
187 $viewer_spaces = msort($viewer_spaces, 'getNamespaceName');
190 foreach ($viewer_spaces as $space) {
192 // Skip archived spaces, unless the object is already in that space.
193 if ($space->getIsArchived()) {
194 if ($space->getPHID() != $space_phid) {
199 $map[$space->getPHID()] = pht(
201 $space->getMonogram(),
202 $space->getNamespaceName());
210 * Get the Space PHID for an object, if one exists.
212 * This is intended to simplify performing a bunch of redundant checks; you
213 * can intentionally pass any value in (including `null`).
218 public static function getObjectSpacePHID($object) {
223 if (!($object instanceof PhabricatorSpacesInterface
)) {
227 $space_phid = $object->getSpacePHID();
228 if ($space_phid === null) {
229 $default_space = self
::getDefaultSpace();
230 if ($default_space) {
231 $space_phid = $default_space->getPHID();