Changeset View
Changeset View
Standalone View
Standalone View
src/applications/packages/query/PackagesSignatureQuery.php
- This file was added.
| <?php | |||||
| final class PackagesSignatureQuery | |||||
| extends PhabricatorCursorPagedPolicyAwareQuery { | |||||
| private $ids; | |||||
| private $phids; | |||||
| private $signerPHIDs; | |||||
| private $versionPHIDs; | |||||
| private $packagePHIDs; | |||||
| public function withIDs(array $ids) { | |||||
| $this->ids = $ids; | |||||
| return $this; | |||||
| } | |||||
| public function withPHIDs(array $phids) { | |||||
| $this->phids = $phids; | |||||
| return $this; | |||||
| } | |||||
| public function withSignerPHIDs(array $phids) { | |||||
| $this->signerPHIDs = $phids; | |||||
| return $this; | |||||
| } | |||||
| public function withVersionPHIDs(array $phids) { | |||||
| $this->versionPHIDs = $phids; | |||||
| return $this; | |||||
| } | |||||
| public function withPackagePHIDs(array $phids) { | |||||
| $this->packagePHIDs = $phids; | |||||
| return $this; | |||||
| } | |||||
| protected function loadPage() { | |||||
| $table = new PackagesSignature(); | |||||
| $conn_r = $table->establishConnection('r'); | |||||
| $rows = queryfx_all( | |||||
| $conn_r, | |||||
| 'SELECT * FROM %T packages_signature %Q %Q %Q %Q', | |||||
| $table->getTableName(), | |||||
| $this->buildJoinClause($conn_r), | |||||
| $this->buildWhereClause($conn_r), | |||||
| $this->buildOrderClause($conn_r), | |||||
| $this->buildLimitClause($conn_r)); | |||||
| return $table->loadAllFromArray($rows); | |||||
| } | |||||
| protected function buildJoinClause(AphrontDatabaseConnection $conn_r) { | |||||
| $joins = array(); | |||||
| if ($this->packagePHIDs !== null) { | |||||
| $joins[] = qsprintf( | |||||
| $conn_r, | |||||
| 'JOIN %T v ON v.phid = packages_signature.versionPHID', | |||||
| id(new PackagesVersion())->getTableName()); | |||||
| } | |||||
| return implode(' ', $joins); | |||||
| } | |||||
| protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | |||||
| $where = array(); | |||||
| $where[] = $this->buildPagingClause($conn_r); | |||||
| if ($this->ids !== null) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'id IN (%Ld)', | |||||
| $this->ids); | |||||
| } | |||||
| if ($this->phids !== null) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'phid IN (%Ls)', | |||||
| $this->phids); | |||||
| } | |||||
| if ($this->signerPHIDs !== null) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'signerPHID in (%Ls)', | |||||
| $this->signerPHIDs); | |||||
| } | |||||
| if ($this->versionPHIDs !== null) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'versionPHID in (%Ls)', | |||||
| $this->versionPHIDs); | |||||
| } | |||||
| if ($this->packagePHIDs !== null) { | |||||
| $where[] = qsprintf( | |||||
| $conn_r, | |||||
| 'v.packagePHID in (%Ls)', | |||||
| $this->packagePHIDs); | |||||
| } | |||||
| return $this->formatWhereClause($where); | |||||
| } | |||||
| // I've copied this pattern from HarbormasterBuildStepQuery | |||||
| protected function willFilterPage(array $page) { | |||||
| $versions = array(); | |||||
| $version_phids = array_filter(mpull($page, 'getVersionPHID')); | |||||
| if ($version_phids) { | |||||
| $versions = id(new PhabricatorObjectQuery()) | |||||
| ->setViewer($this->getViewer()) | |||||
| ->withPHIDs($version_phids) | |||||
| ->withTypes(array(PackagesVersionPHIDType::TYPECONST)) | |||||
| ->setParentQuery($this) | |||||
| ->execute(); | |||||
| $versions = mpull($versions, null, 'getPHID'); | |||||
| } | |||||
| foreach ($page as $key => $signature) { | |||||
| $version_phid = $signature->getVersionPHID(); | |||||
| if (empty($versions[$version_phid])) { | |||||
| unset($page[$key]); | |||||
| continue; | |||||
| } | |||||
| $signature->attachVersion($versions[$version_phid]); | |||||
| } | |||||
| return $page; | |||||
| } | |||||
| public function getQueryApplicationClass() { | |||||
| return 'PhabricatorPackagesApplication'; | |||||
| } | |||||
| } | |||||