Changeset View
Changeset View
Standalone View
Standalone View
src/applications/spaces/query/PhabricatorSpacesNamespaceQuery.php
| <?php | <?php | ||||
| final class PhabricatorSpacesNamespaceQuery | final class PhabricatorSpacesNamespaceQuery | ||||
| extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
| const KEY_ALL = 'spaces.all'; | |||||
| const KEY_DEFAULT = 'spaces.default'; | |||||
| private $ids; | private $ids; | ||||
| private $phids; | private $phids; | ||||
| private $isDefaultNamespace; | private $isDefaultNamespace; | ||||
| public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
| $this->ids = $ids; | $this->ids = $ids; | ||||
| return $this; | return $this; | ||||
| } | } | ||||
| ▲ Show 20 Lines • Show All 55 Lines • ▼ Show 20 Lines | if ($this->isDefaultNamespace !== null) { | ||||
| 'isDefaultNamespace IS NULL'); | 'isDefaultNamespace IS NULL'); | ||||
| } | } | ||||
| } | } | ||||
| $where[] = $this->buildPagingClause($conn_r); | $where[] = $this->buildPagingClause($conn_r); | ||||
| return $this->formatWhereClause($where); | return $this->formatWhereClause($where); | ||||
| } | } | ||||
| public static function destroySpacesCache() { | |||||
| $cache = PhabricatorCaches::getRequestCache(); | |||||
| $cache->deleteKeys( | |||||
| array( | |||||
| self::KEY_ALL, | |||||
| self::KEY_DEFAULT, | |||||
| )); | |||||
| } | |||||
| 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) { | |||||
| $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; | |||||
| } | |||||
| } | |||||
| return $result; | |||||
| } | |||||
| } | } | ||||