1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2025-01-24 05:28:18 +01:00

Make DifferentialRevisionQuery policy-aware

Summary:
Ref T603. Ref T2625. Makes `DifferentialRevisionQuery` do policy checks.

Note that it still uses inefficient offset-based paging, but it's rare to page through revisions. I'll switch to cursor paging in a future diff.

Test Plan: Viewed a bunch of Differential interfaces, home page, etc. This shouldn't actually materially impact anything.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T603, T2625

Differential Revision: https://secure.phabricator.com/D6344
This commit is contained in:
epriestley 2013-07-03 05:43:52 -07:00
parent 58884b94dc
commit 0c2e38e81c

View file

@ -12,7 +12,8 @@
* @task exec Query Execution * @task exec Query Execution
* @task internal Internals * @task internal Internals
*/ */
final class DifferentialRevisionQuery { final class DifferentialRevisionQuery
extends PhabricatorCursorPagedPolicyAwareQuery {
private $pathIDs = array(); private $pathIDs = array();
@ -50,24 +51,11 @@ final class DifferentialRevisionQuery {
*/ */
const ORDER_PATH_MODIFIED = 'order-path-modified'; const ORDER_PATH_MODIFIED = 'order-path-modified';
private $limit = 1000;
private $offset = 0;
private $needRelationships = false; private $needRelationships = false;
private $needActiveDiffs = false; private $needActiveDiffs = false;
private $needDiffIDs = false; private $needDiffIDs = false;
private $needCommitPHIDs = false; private $needCommitPHIDs = false;
private $needHashes = false; private $needHashes = false;
private $viewer;
public function setViewer(PhabricatorUser $viewer) {
$this->viewer = $viewer;
return $this;
}
public function getViewer() {
return $this->viewer;
}
/* -( Query Configuration )------------------------------------------------ */ /* -( Query Configuration )------------------------------------------------ */
@ -267,32 +255,6 @@ final class DifferentialRevisionQuery {
} }
/**
* Set result limit. If unspecified, defaults to 1000.
*
* @param int Result limit.
* @return this
* @task config
*/
public function setLimit($limit) {
$this->limit = $limit;
return $this;
}
/**
* Set result offset. If unspecified, defaults to 0.
*
* @param int Result offset.
* @return this
* @task config
*/
public function setOffset($offset) {
$this->offset = $offset;
return $this;
}
/** /**
* Set whether or not the query will load and attach relationships. * Set whether or not the query will load and attach relationships.
* *
@ -372,47 +334,49 @@ final class DifferentialRevisionQuery {
* @return list List of matching DifferentialRevision objects. * @return list List of matching DifferentialRevision objects.
* @task exec * @task exec
*/ */
public function execute() { public function loadPage() {
$table = new DifferentialRevision(); $table = new DifferentialRevision();
$conn_r = $table->establishConnection('r'); $conn_r = $table->establishConnection('r');
$data = $this->loadData(); $data = $this->loadData();
$revisions = $table->loadAllFromArray($data); return $table->loadAllFromArray($data);
}
if ($revisions) { public function willFilterPage(array $revisions) {
if ($this->needRelationships) { if (!$revisions) {
$this->loadRelationships($conn_r, $revisions); return $revisions;
} }
if ($this->needCommitPHIDs) { $table = new DifferentialRevision();
$this->loadCommitPHIDs($conn_r, $revisions); $conn_r = $table->establishConnection('r');
}
$need_active = $this->needActiveDiffs; if ($this->needRelationships) {
$need_ids = $need_active || $this->loadRelationships($conn_r, $revisions);
$this->needDiffIDs; }
if ($need_ids) { if ($this->needCommitPHIDs) {
$this->loadDiffIDs($conn_r, $revisions); $this->loadCommitPHIDs($conn_r, $revisions);
} }
if ($need_active) { $need_active = $this->needActiveDiffs;
$this->loadActiveDiffs($conn_r, $revisions); $need_ids = $need_active || $this->needDiffIDs;
}
if ($this->needHashes) { if ($need_ids) {
$this->loadHashes($conn_r, $revisions); $this->loadDiffIDs($conn_r, $revisions);
} }
if ($need_active) {
$this->loadActiveDiffs($conn_r, $revisions);
}
if ($this->needHashes) {
$this->loadHashes($conn_r, $revisions);
} }
return $revisions; return $revisions;
} }
public function executeOne() {
return head($this->execute());
}
private function loadData() { private function loadData() {
$table = new DifferentialRevision(); $table = new DifferentialRevision();
$conn_r = $table->establishConnection('r'); $conn_r = $table->establishConnection('r');
@ -513,18 +477,6 @@ final class DifferentialRevisionQuery {
$limit); $limit);
} }
private function buildLimitClause(AphrontDatabaseConnection $conn_r) {
$limit = '';
if ($this->offset || $this->limit) {
$limit = qsprintf(
$conn_r,
'LIMIT %d, %d',
(int)$this->offset,
$this->limit);
}
return $limit;
}
/* -( Internals )---------------------------------------------------------- */ /* -( Internals )---------------------------------------------------------- */
@ -723,13 +675,8 @@ final class DifferentialRevisionQuery {
"Unknown revision status filter constant '{$this->status}'!"); "Unknown revision status filter constant '{$this->status}'!");
} }
if ($where) { $where[] = $this->buildPagingCLause($conn_r);
$where = 'WHERE '.implode(' AND ', $where); return $this->formatWhereClause($where);
} else {
$where = '';
}
return $where;
} }
@ -741,8 +688,7 @@ final class DifferentialRevisionQuery {
$this->pathIDs, $this->pathIDs,
$this->ccs, $this->ccs,
$this->reviewers, $this->reviewers,
$this->subscribers, $this->subscribers);
$this->responsibles);
$needs_distinct = (count($join_triggers) > 1); $needs_distinct = (count($join_triggers) > 1);