Changeset View
Changeset View
Standalone View
Standalone View
src/applications/almanac/query/AlmanacPropertyQuery.php
<?php | <?php | ||||
final class AlmanacPropertyQuery | final class AlmanacPropertyQuery | ||||
extends PhabricatorCursorPagedPolicyAwareQuery { | extends PhabricatorCursorPagedPolicyAwareQuery { | ||||
private $ids; | private $ids; | ||||
private $objectPHIDs; | private $objectPHIDs; | ||||
private $objects; | |||||
private $names; | private $names; | ||||
private $disablePolicyFilteringAndAttachment; | |||||
public function withIDs(array $ids) { | public function withIDs(array $ids) { | ||||
$this->ids = $ids; | $this->ids = $ids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withObjectPHIDs(array $phids) { | public function withObjectPHIDs(array $phids) { | ||||
$this->objectPHIDs = $phids; | $this->objectPHIDs = $phids; | ||||
return $this; | return $this; | ||||
} | } | ||||
public function withNames(array $names) { | public function withObjects(array $objects) { | ||||
$this->names = $names; | $this->objects = mpull($objects, null, 'getPHID'); | ||||
$this->objectPHIDs = array_keys($this->objects); | |||||
return $this; | return $this; | ||||
} | } | ||||
public function setDisablePolicyFilteringAndAttachment($disable) { | public function withNames(array $names) { | ||||
$this->disablePolicyFilteringAndAttachment = $disable; | $this->names = $names; | ||||
return $this; | return $this; | ||||
} | } | ||||
protected function shouldDisablePolicyFiltering() { | public function newResultObject() { | ||||
return $this->disablePolicyFilteringAndAttachment; | return new AlmanacProperty(); | ||||
} | } | ||||
protected function loadPage() { | protected function loadPage() { | ||||
$table = new AlmanacProperty(); | return $this->loadStandardPage($this->newResultObject()); | ||||
$conn_r = $table->establishConnection('r'); | |||||
$data = queryfx_all( | |||||
$conn_r, | |||||
'SELECT * FROM %T %Q %Q %Q', | |||||
$table->getTableName(), | |||||
$this->buildWhereClause($conn_r), | |||||
$this->buildOrderClause($conn_r), | |||||
$this->buildLimitClause($conn_r)); | |||||
return $table->loadAllFromArray($data); | |||||
} | } | ||||
protected function willFilterPage(array $properties) { | protected function willFilterPage(array $properties) { | ||||
if (!$this->disablePolicyFilteringAndAttachment) { | |||||
$object_phids = mpull($properties, 'getObjectPHID'); | $object_phids = mpull($properties, 'getObjectPHID'); | ||||
$object_phids = array_fuse($object_phids); | |||||
if ($this->objects !== null) { | |||||
$object_phids = array_diff_key($object_phids, $this->objects); | |||||
} | |||||
if ($object_phids) { | |||||
$objects = id(new PhabricatorObjectQuery()) | $objects = id(new PhabricatorObjectQuery()) | ||||
->setViewer($this->getViewer()) | ->setViewer($this->getViewer()) | ||||
->setParentQuery($this) | ->setParentQuery($this) | ||||
->withPHIDs($object_phids) | ->withPHIDs($object_phids) | ||||
->execute(); | ->execute(); | ||||
$objects = mpull($objects, null, 'getPHID'); | $objects = mpull($objects, null, 'getPHID'); | ||||
} else { | |||||
$objects = array(); | |||||
} | |||||
$objects += $this->objects; | |||||
foreach ($properties as $key => $property) { | foreach ($properties as $key => $property) { | ||||
$object = idx($objects, $property->getObjectPHID()); | $object = idx($objects, $property->getObjectPHID()); | ||||
if (!$object) { | if (!$object) { | ||||
unset($properties[$key]); | unset($properties[$key]); | ||||
continue; | continue; | ||||
} | } | ||||
$property->attachObject($object); | $property->attachObject($object); | ||||
} | } | ||||
} | |||||
return $properties; | return $properties; | ||||
} | } | ||||
protected function buildWhereClause(AphrontDatabaseConnection $conn_r) { | protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { | ||||
$where = array(); | $where = parent::buildWhereClauseParts($conn); | ||||
if ($this->ids !== null) { | if ($this->ids !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'id IN (%Ld)', | 'id IN (%Ld)', | ||||
$this->ids); | $this->ids); | ||||
} | } | ||||
if ($this->objectPHIDs !== null) { | if ($this->objectPHIDs !== null) { | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'objectPHID IN (%Ls)', | 'objectPHID IN (%Ls)', | ||||
$this->objectPHIDs); | $this->objectPHIDs); | ||||
} | } | ||||
if ($this->names !== null) { | if ($this->names !== null) { | ||||
$hashes = array(); | $hashes = array(); | ||||
foreach ($this->names as $name) { | foreach ($this->names as $name) { | ||||
$hashes[] = PhabricatorHash::digestForIndex($name); | $hashes[] = PhabricatorHash::digestForIndex($name); | ||||
} | } | ||||
$where[] = qsprintf( | $where[] = qsprintf( | ||||
$conn_r, | $conn, | ||||
'fieldIndex IN (%Ls)', | 'fieldIndex IN (%Ls)', | ||||
$hashes); | $hashes); | ||||
} | } | ||||
$where[] = $this->buildPagingClause($conn_r); | return $where; | ||||
return $this->formatWhereClause($where); | |||||
} | } | ||||
public function getQueryApplicationClass() { | public function getQueryApplicationClass() { | ||||
return 'PhabricatorAlmanacApplication'; | return 'PhabricatorAlmanacApplication'; | ||||
} | } | ||||
} | } |