Remove product literal strings in "pht()", part 18
[phabricator.git] / src / applications / spaces / query / PhabricatorSpacesNamespaceQuery.php
blobee11dcdd06b4e54a9db1bac65752b40a9be04fca
1 <?php
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';
10 private $ids;
11 private $phids;
12 private $isDefaultNamespace;
13 private $isArchived;
15 public function withIDs(array $ids) {
16 $this->ids = $ids;
17 return $this;
20 public function withPHIDs(array $phids) {
21 $this->phids = $phids;
22 return $this;
25 public function withIsDefaultNamespace($default) {
26 $this->isDefaultNamespace = $default;
27 return $this;
30 public function withIsArchived($archived) {
31 $this->isArchived = $archived;
32 return $this;
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) {
47 $where[] = qsprintf(
48 $conn,
49 'id IN (%Ld)',
50 $this->ids);
53 if ($this->phids !== null) {
54 $where[] = qsprintf(
55 $conn,
56 'phid IN (%Ls)',
57 $this->phids);
60 if ($this->isDefaultNamespace !== null) {
61 if ($this->isDefaultNamespace) {
62 $where[] = qsprintf(
63 $conn,
64 'isDefaultNamespace = 1');
65 } else {
66 $where[] = qsprintf(
67 $conn,
68 'isDefaultNamespace IS NULL');
72 if ($this->isArchived !== null) {
73 $where[] = qsprintf(
74 $conn,
75 'isArchived = %d',
76 (int)$this->isArchived);
79 return $where;
82 public static function destroySpacesCache() {
83 $cache = PhabricatorCaches::getRequestCache();
84 $cache->deleteKeys(
85 array(
86 self::KEY_ALL,
87 self::KEY_DEFAULT,
88 ));
91 public static function getSpacesExist() {
92 return (bool)self::getAllSpaces();
95 public static function getViewerSpacesExist(PhabricatorUser $viewer) {
96 if (!self::getSpacesExist()) {
97 return false;
100 // If the viewer has access to only one space, pretend spaces simply don't
101 // exist.
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())
114 ->execute();
115 $spaces = mpull($spaces, null, 'getPHID');
116 $cache->setKey($cache_key, $spaces);
119 return $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;
134 break;
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();
152 $result = array();
153 foreach ($spaces as $key => $space) {
154 $can_see = PhabricatorPolicyFilter::hasCapability(
155 $viewer,
156 $space,
157 PhabricatorPolicyCapability::CAN_VIEW);
158 if ($can_see) {
159 $result[$key] = $space;
163 $cache->setKey($cache_key, $result);
166 return $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]);
179 return $spaces;
182 public static function getSpaceOptionsForViewer(
183 PhabricatorUser $viewer,
184 $space_phid) {
186 $viewer_spaces = self::getViewerSpaces($viewer);
187 $viewer_spaces = msort($viewer_spaces, 'getNamespaceName');
189 $map = array();
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) {
195 continue;
199 $map[$space->getPHID()] = pht(
200 'Space %s: %s',
201 $space->getMonogram(),
202 $space->getNamespaceName());
205 return $map;
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`).
215 * @param wild
216 * @return phid|null
218 public static function getObjectSpacePHID($object) {
219 if (!$object) {
220 return null;
223 if (!($object instanceof PhabricatorSpacesInterface)) {
224 return null;
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();
235 return $space_phid;