PhabricatorSpacesNamespaceQuery.php 6.6 KB
Newer Older
1 2 3 4 5
<?php

final class PhabricatorSpacesNamespaceQuery
  extends PhabricatorCursorPagedPolicyAwareQuery {

6 7
  const KEY_ALL = 'spaces.all';
  const KEY_DEFAULT = 'spaces.default';
epriestley's avatar
epriestley committed
8
  const KEY_VIEWER = 'spaces.viewer';
9
  const KEY_MAGIC_ALL_USER_SPACE = 'spaces.magicalluserspace';
10

11 12 13
  private $ids;
  private $phids;
  private $isDefaultNamespace;
epriestley's avatar
epriestley committed
14
  private $isArchived;
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

  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;
  }

epriestley's avatar
epriestley committed
31 32 33 34 35
  public function withIsArchived($archived) {
    $this->isArchived = $archived;
    return $this;
  }

36
  public function getQueryApplicationClass() {
37
    return 'PhabricatorSpacesApplication';
38 39 40
  }

  protected function loadPage() {
epriestley's avatar
epriestley committed
41
    return $this->loadStandardPage(new PhabricatorSpacesNamespace());
42 43
  }

epriestley's avatar
epriestley committed
44 45
  protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
    $where = parent::buildWhereClauseParts($conn);
46 47 48

    if ($this->ids !== null) {
      $where[] = qsprintf(
epriestley's avatar
epriestley committed
49
        $conn,
50 51 52 53 54 55
        'id IN (%Ld)',
        $this->ids);
    }

    if ($this->phids !== null) {
      $where[] = qsprintf(
epriestley's avatar
epriestley committed
56
        $conn,
57 58 59 60 61 62 63
        'phid IN (%Ls)',
        $this->phids);
    }

    if ($this->isDefaultNamespace !== null) {
      if ($this->isDefaultNamespace) {
        $where[] = qsprintf(
epriestley's avatar
epriestley committed
64
          $conn,
65 66 67
          'isDefaultNamespace = 1');
      } else {
        $where[] = qsprintf(
epriestley's avatar
epriestley committed
68
          $conn,
69 70 71 72
          'isDefaultNamespace IS NULL');
      }
    }

epriestley's avatar
epriestley committed
73 74 75 76 77 78 79 80
    if ($this->isArchived !== null) {
      $where[] = qsprintf(
        $conn,
        'isArchived = %d',
        (int)$this->isArchived);
    }

    return $where;
81 82
  }

83 84 85 86 87 88 89 90 91
  public static function destroySpacesCache() {
    $cache = PhabricatorCaches::getRequestCache();
    $cache->deleteKeys(
      array(
        self::KEY_ALL,
        self::KEY_DEFAULT,
      ));
  }

92 93 94 95
  public static function getSpacesExist() {
    return (bool)self::getAllSpaces();
  }

epriestley's avatar
epriestley committed
96 97 98 99 100 101 102 103
  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);
104 105

    unset($spaces[self::getMagicAllUsersSpace()->getPHID()]);
epriestley's avatar
epriestley committed
106 107 108
    return (count($spaces) > 1);
  }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
  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;
  }

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
  // See the comment in PhabricatorCursorPagedPolicyAwareQuery's call of this
  // function to understand what is going on here.
  public static function getMagicAllUsersSpace() {
    $cache = PhabricatorCaches::getRequestCache();
    $cache_key = self::KEY_MAGIC_ALL_USER_SPACE;

    $magic_space = $cache->getKey($cache_key, false);
    if ($magic_space === false) {
      $magic_space = null;

      $spaces = self::getAllSpaces();
      if (isset($spaces['PHID-SPCE-enba4g3iew4gdmka77kj'])) {
	// PHID from phab.ccu
        $magic_space = $spaces['PHID-SPCE-enba4g3iew4gdmka77kj'];
      } else if (isset($spaces['PHID-SPCE-7r6by3wz32pwnzpw55yl'])) {
	// PHID from test-phab.ccu
        $magic_space = $spaces['PHID-SPCE-7r6by3wz32pwnzpw55yl'];
      } else if (isset($spaces['PHID-SPCE-d4pabaw5r2yagpnsnuj7'])) {
	// PHID from @daniels development environment
        $magic_space = $spaces['PHID-SPCE-d4pabaw5r2yagpnsnuj7'];
      }

      $cache->setKey($cache_key, $magic_space);
    }

    return $magic_space;
  }

153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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) {
epriestley's avatar
epriestley committed
176
    $cache = PhabricatorCaches::getRequestCache();
177
    $cache_key = self::KEY_VIEWER.'('.$viewer->getCacheFragment().')';
178

epriestley's avatar
epriestley committed
179 180 181 182 183 184 185 186 187 188 189 190 191
    $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;
        }
192
      }
epriestley's avatar
epriestley committed
193 194

      $cache->setKey($cache_key, $result);
195 196 197 198 199
    }

    return $result;
  }

epriestley's avatar
epriestley committed
200 201 202 203 204 205 206 207 208 209 210 211 212

  public static function getViewerActiveSpaces(PhabricatorUser $viewer) {
    $spaces = self::getViewerSpaces($viewer);

    foreach ($spaces as $key => $space) {
      if ($space->getIsArchived()) {
        unset($spaces[$key]);
      }
    }

    return $spaces;
  }

213 214 215 216 217
  public static function getSpaceOptionsForViewer(
    PhabricatorUser $viewer,
    $space_phid) {

    $viewer_spaces = self::getViewerSpaces($viewer);
218
    $viewer_spaces = msort($viewer_spaces, 'getNamespaceName');
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237

    $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;
  }
epriestley's avatar
epriestley committed
238 239


240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
  /**
   * 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;
  }

269
}