[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/releeph/query/ -> ReleephRequestQuery.php (source)

   1  <?php
   2  
   3  final class ReleephRequestQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $requestedCommitPHIDs;
   7    private $ids;
   8    private $phids;
   9    private $severities;
  10    private $requestorPHIDs;
  11    private $branchIDs;
  12    private $requestedObjectPHIDs;
  13  
  14    const STATUS_ALL          = 'status-all';
  15    const STATUS_OPEN         = 'status-open';
  16    const STATUS_REQUESTED    = 'status-requested';
  17    const STATUS_NEEDS_PULL   = 'status-needs-pull';
  18    const STATUS_REJECTED     = 'status-rejected';
  19    const STATUS_ABANDONED    = 'status-abandoned';
  20    const STATUS_PULLED       = 'status-pulled';
  21    const STATUS_NEEDS_REVERT = 'status-needs-revert';
  22    const STATUS_REVERTED     = 'status-reverted';
  23  
  24    private $status = self::STATUS_ALL;
  25  
  26    public function withIDs(array $ids) {
  27      $this->ids = $ids;
  28      return $this;
  29    }
  30  
  31    public function withPHIDs(array $phids) {
  32      $this->phids = $phids;
  33      return $this;
  34    }
  35  
  36    public function withBranchIDs(array $branch_ids) {
  37      $this->branchIDs = $branch_ids;
  38      return $this;
  39    }
  40  
  41    public function withStatus($status) {
  42      $this->status = $status;
  43      return $this;
  44    }
  45  
  46    public function withRequestedCommitPHIDs(array $requested_commit_phids) {
  47      $this->requestedCommitPHIDs = $requested_commit_phids;
  48      return $this;
  49    }
  50  
  51    public function withRequestorPHIDs(array $phids) {
  52      $this->requestorPHIDs = $phids;
  53      return $this;
  54    }
  55  
  56    public function withSeverities(array $severities) {
  57      $this->severities = $severities;
  58      return $this;
  59    }
  60  
  61    public function withRequestedObjectPHIDs(array $phids) {
  62      $this->requestedObjectPHIDs = $phids;
  63      return $this;
  64    }
  65  
  66    public function loadPage() {
  67      $table = new ReleephRequest();
  68      $conn_r = $table->establishConnection('r');
  69  
  70      $data = queryfx_all(
  71        $conn_r,
  72        'SELECT * FROM %T %Q %Q %Q',
  73        $table->getTableName(),
  74        $this->buildWhereClause($conn_r),
  75        $this->buildOrderClause($conn_r),
  76        $this->buildLimitClause($conn_r));
  77  
  78      return $table->loadAllFromArray($data);
  79    }
  80  
  81    public function willFilterPage(array $requests) {
  82      // Load requested objects: you must be able to see an object to see
  83      // requests for it.
  84      $object_phids = mpull($requests, 'getRequestedObjectPHID');
  85      $objects = id(new PhabricatorObjectQuery())
  86        ->setViewer($this->getViewer())
  87        ->setParentQuery($this)
  88        ->withPHIDs($object_phids)
  89        ->execute();
  90  
  91      foreach ($requests as $key => $request) {
  92        $object_phid = $request->getRequestedObjectPHID();
  93        $object = idx($objects, $object_phid);
  94        if (!$object) {
  95          unset($requests[$key]);
  96          continue;
  97        }
  98        $request->attachRequestedObject($object);
  99      }
 100  
 101      if ($this->severities) {
 102        $severities = array_fuse($this->severities);
 103        foreach ($requests as $key => $request) {
 104  
 105          // NOTE: Facebook uses a custom field here.
 106          if (ReleephDefaultFieldSelector::isFacebook()) {
 107            $severity = $request->getDetail('severity');
 108          } else {
 109            $severity = $request->getDetail('releeph:severity');
 110          }
 111  
 112          if (empty($severities[$severity])) {
 113            unset($requests[$key]);
 114          }
 115        }
 116      }
 117  
 118      $branch_ids = array_unique(mpull($requests, 'getBranchID'));
 119      $branches = id(new ReleephBranchQuery())
 120        ->withIDs($branch_ids)
 121        ->setViewer($this->getViewer())
 122        ->execute();
 123      $branches = mpull($branches, null, 'getID');
 124      foreach ($requests as $key => $request) {
 125        $branch = idx($branches, $request->getBranchID());
 126        if (!$branch) {
 127          unset($requests[$key]);
 128          continue;
 129        }
 130        $request->attachBranch($branch);
 131      }
 132  
 133      // TODO: These should be serviced by the query, but are not currently
 134      // denormalized anywhere. For now, filter them here instead. Note that
 135      // we must perform this filtering *after* querying and attaching branches,
 136      // because request status depends on the product.
 137  
 138      $keep_status = array_fuse($this->getKeepStatusConstants());
 139      if ($keep_status) {
 140        foreach ($requests as $key => $request) {
 141          if (empty($keep_status[$request->getStatus()])) {
 142            unset($requests[$key]);
 143          }
 144        }
 145      }
 146  
 147      return $requests;
 148    }
 149  
 150    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 151      $where = array();
 152  
 153      if ($this->ids !== null) {
 154        $where[] = qsprintf(
 155          $conn_r,
 156          'id IN (%Ld)',
 157          $this->ids);
 158      }
 159  
 160      if ($this->phids !== null) {
 161        $where[] = qsprintf(
 162          $conn_r,
 163          'phid IN (%Ls)',
 164          $this->phids);
 165      }
 166  
 167      if ($this->branchIDs !== null) {
 168        $where[] = qsprintf(
 169          $conn_r,
 170          'branchID IN (%Ld)',
 171          $this->branchIDs);
 172      }
 173  
 174      if ($this->requestedCommitPHIDs !== null) {
 175        $where[] = qsprintf(
 176          $conn_r,
 177          'requestCommitPHID IN (%Ls)',
 178          $this->requestedCommitPHIDs);
 179      }
 180  
 181      if ($this->requestorPHIDs !== null) {
 182        $where[] = qsprintf(
 183          $conn_r,
 184          'requestUserPHID IN (%Ls)',
 185          $this->requestorPHIDs);
 186      }
 187  
 188      if ($this->requestedObjectPHIDs !== null) {
 189        $where[] = qsprintf(
 190          $conn_r,
 191          'requestedObjectPHID IN (%Ls)',
 192          $this->requestedObjectPHIDs);
 193      }
 194  
 195      $where[] = $this->buildPagingClause($conn_r);
 196  
 197      return $this->formatWhereClause($where);
 198    }
 199  
 200    private function getKeepStatusConstants() {
 201      switch ($this->status) {
 202        case self::STATUS_ALL:
 203          return array();
 204        case self::STATUS_OPEN:
 205          return array(
 206            ReleephRequestStatus::STATUS_REQUESTED,
 207            ReleephRequestStatus::STATUS_NEEDS_PICK,
 208            ReleephRequestStatus::STATUS_NEEDS_REVERT,
 209          );
 210        case self::STATUS_REQUESTED:
 211          return array(
 212            ReleephRequestStatus::STATUS_REQUESTED,
 213          );
 214        case self::STATUS_NEEDS_PULL:
 215          return array(
 216            ReleephRequestStatus::STATUS_NEEDS_PICK,
 217          );
 218        case self::STATUS_REJECTED:
 219          return array(
 220            ReleephRequestStatus::STATUS_REJECTED,
 221          );
 222        case self::STATUS_ABANDONED:
 223          return array(
 224            ReleephRequestStatus::STATUS_ABANDONED,
 225          );
 226        case self::STATUS_PULLED:
 227          return array(
 228            ReleephRequestStatus::STATUS_PICKED,
 229          );
 230        case self::STATUS_NEEDS_REVERT:
 231          return array(
 232            ReleephRequestStatus::NEEDS_REVERT,
 233          );
 234        case self::STATUS_REVERTED:
 235          return array(
 236            ReleephRequestStatus::REVERTED,
 237          );
 238        default:
 239          throw new Exception("Unknown status '{$this->status}'!");
 240      }
 241    }
 242  
 243    public function getQueryApplicationClass() {
 244      return 'PhabricatorReleephApplication';
 245    }
 246  
 247  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1