Changeset View
Changeset View
Standalone View
Standalone View
src/applications/owners/query/PhabricatorOwnersPackageQuery.php
<?php | <?php | ||||
final class PhabricatorOwnersPackageQuery | final class PhabricatorOwnersPackageQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $phids; | private $phids; | ||||
private $ownerPHIDs; | private $ownerPHIDs; | ||||
private $repositoryPHIDs; | private $repositoryPHIDs; | ||||
private $namePrefix; | |||||
/** | /** | ||||
* Owners are direct owners, and members of owning projects. | * Owners are direct owners, and members of owning projects. | ||||
*/ | */ | ||||
public function withOwnerPHIDs(array $phids) { | public function withOwnerPHIDs(array $phids) { | ||||
$this->ownerPHIDs = $phids; | $this->ownerPHIDs = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withPHIDs(array $phids) { | public function withPHIDs(array $phids) { | ||||
$this->phids = $phids; | $this->phids = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withRepositoryPHIDs(array $phids) { | public function withRepositoryPHIDs(array $phids) { | ||||
$this->repositoryPHIDs = $phids; | $this->repositoryPHIDs = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function loadPage() { | public function withNamePrefix($prefix) { | ||||
$table = new PhabricatorOwnersPackage(); | $this->namePrefix = $prefix; | ||||
$conn_r = $table->establishConnection('r'); | return $this; | ||||
} | |||||
$data = queryfx_all( | public function newResultObject() { | ||||
$conn_r, | return new PhabricatorOwnersPackage(); | ||||
'SELECT p.* FROM %T p %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($data); | protected function loadPage() { | ||||
return $this->loadStandardPage(new PhabricatorOwnersPackage()); | |||||
} | } | ||||
protected function buildJoinClause(AphrontDatabaseConnection $conn_r) { | protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) { | ||||
$joins = array(); | $joins = parent::buildJoinClauseParts($conn); | ||||
if ($this->ownerPHIDs !== null) { | if ($this->ownerPHIDs !== null) { | ||||
$joins[] = qsprintf( | $joins[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'JOIN %T o ON o.packageID = p.id', | 'JOIN %T o ON o.packageID = p.id', | ||||
id(new PhabricatorOwnersOwner())->getTableName()); | id(new PhabricatorOwnersOwner())->getTableName()); | ||||
} | } | ||||
if ($this->repositoryPHIDs !== null) { | if ($this->repositoryPHIDs !== null) { | ||||
$joins[] = qsprintf( | $joins[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'JOIN %T rpath ON rpath.packageID = p.id', | 'JOIN %T rpath ON rpath.packageID = p.id', | ||||
id(new PhabricatorOwnersPath())->getTableName()); | id(new PhabricatorOwnersPath())->getTableName()); | ||||
} | } | ||||
return implode(' ', $joins); | return $joins; | ||||
} | } | ||||
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = array(); | $where = parent::buildWhereClauseParts($conn); | ||||
if ($this->phids !== null) { | if ($this->phids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'p.phid IN (%Ls)', | 'p.phid IN (%Ls)', | ||||
$this->phids); | $this->phids); | ||||
} | } | ||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'p.id IN (%Ld)', | 'p.id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
if ($this->repositoryPHIDs !== null) { | if ($this->repositoryPHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'rpath.repositoryPHID IN (%Ls)', | 'rpath.repositoryPHID IN (%Ls)', | ||||
$this->repositoryPHIDs); | $this->repositoryPHIDs); | ||||
} | } | ||||
if ($this->ownerPHIDs !== null) { | if ($this->ownerPHIDs !== null) { | ||||
$base_phids = $this->ownerPHIDs; | $base_phids = $this->ownerPHIDs; | ||||
$query = new PhabricatorProjectQuery(); | $projects = id(new PhabricatorProjectQuery()) | ||||
$query->setViewer($this->getViewer()); | ->setViewer($this->getViewer()) | ||||
$query->withMemberPHIDs($base_phids); | ->withMemberPHIDs($base_phids) | ||||
$projects = $query->execute(); | ->execute(); | ||||
$project_phids = mpull($projects, 'getPHID'); | $project_phids = mpull($projects, 'getPHID'); | ||||
$all_phids = array_merge($base_phids, $project_phids); | $all_phids = array_merge($base_phids, $project_phids); | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'o.userPHID IN (%Ls)', | 'o.userPHID IN (%Ls)', | ||||
$all_phids); | $all_phids); | ||||
} | } | ||||
$where[] = $this->buildPagingClause($conn_r); | if (strlen($this->namePrefix)) { | ||||
return $this->formatWhereClause($where); | // NOTE: This is a hacky mess, but this column is currently case | ||||
// sensitive and unique. | |||||
$where[] = qsprintf( | |||||
$conn, | |||||
'LOWER(p.name) LIKE %>', | |||||
phutil_utf8_strtolower($this->namePrefix)); | |||||
} | |||||
return $where; | |||||
} | |||||
protected function shouldGroupQueryResultRows() { | |||||
if ($this->repositoryPHIDs) { | |||||
return true; | |||||
} | |||||
if ($this->ownerPHIDs) { | |||||
return true; | |||||
} | |||||
return parent::shouldGroupQueryResultRows(); | |||||
} | |||||
public function getBuiltinOrders() { | |||||
return array( | |||||
'name' => array( | |||||
'vector' => array('name'), | |||||
'name' => pht('Name'), | |||||
), | |||||
) + parent::getBuiltinOrders(); | |||||
} | |||||
public function getOrderableColumns() { | |||||
return parent::getOrderableColumns() + array( | |||||
'name' => array( | |||||
'table' => $this->getPrimaryTableAlias(), | |||||
'column' => 'name', | |||||
'type' => 'string', | |||||
'unique' => true, | |||||
'reverse' => true, | |||||
), | |||||
); | |||||
} | |||||
protected function getPagingValueMap($cursor, array $keys) { | |||||
$package = $this->loadCursorObject($cursor); | |||||
return array( | |||||
'id' => $package->getID(), | |||||
'name' => $package->getName(), | |||||
); | |||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorOwnersApplication'; | return 'PhabricatorOwnersApplication'; | ||||
} | } | ||||
protected function getPrimaryTableAlias() { | |||||
return 'p'; | |||||
} | |||||
} | } |