2012-02-29 06:10:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
final class PhabricatorAuditCommitQuery {
|
|
|
|
|
|
|
|
private $offset;
|
|
|
|
private $limit;
|
|
|
|
|
2012-03-13 19:18:01 +01:00
|
|
|
private $commitPHIDs;
|
2012-02-29 06:10:39 +01:00
|
|
|
private $authorPHIDs;
|
|
|
|
private $packagePHIDs;
|
2012-04-08 02:24:35 +02:00
|
|
|
private $identifiers = array();
|
2012-02-29 06:10:39 +01:00
|
|
|
|
|
|
|
private $needCommitData;
|
2012-10-03 01:44:29 +02:00
|
|
|
private $needAudits;
|
2012-02-29 06:10:39 +01:00
|
|
|
|
|
|
|
private $status = 'status-any';
|
|
|
|
const STATUS_ANY = 'status-any';
|
|
|
|
const STATUS_OPEN = 'status-open';
|
2013-02-28 16:55:06 +01:00
|
|
|
const STATUS_CONCERN = 'status-concern';
|
2012-02-29 06:10:39 +01:00
|
|
|
|
|
|
|
public function withAuthorPHIDs(array $author_phids) {
|
|
|
|
$this->authorPHIDs = $author_phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function withPackagePHIDs(array $phids) {
|
|
|
|
$this->packagePHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-03-13 19:18:01 +01:00
|
|
|
public function withCommitPHIDs(array $phids) {
|
|
|
|
$this->commitPHIDs = $phids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
public function withStatus($status) {
|
|
|
|
$this->status = $status;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-04-08 02:24:35 +02:00
|
|
|
public function withIdentifiers($repository_id, array $identifiers) {
|
|
|
|
$this->identifiers[] = array($repository_id, $identifiers);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
public function needCommitData($need) {
|
|
|
|
$this->needCommitData = $need;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-10-03 01:44:29 +02:00
|
|
|
public function needAudits($need) {
|
|
|
|
$this->needAudits = $need;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
public function setOffset($offset) {
|
|
|
|
$this->offset = $offset;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setLimit($limit) {
|
|
|
|
$this->limit = $limit;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function execute() {
|
|
|
|
|
|
|
|
$table = new PhabricatorRepositoryCommit();
|
|
|
|
$conn_r = $table->establishConnection('r');
|
|
|
|
|
2012-03-07 04:47:53 +01:00
|
|
|
$join = $this->buildJoinClause($conn_r);
|
2012-02-29 06:10:39 +01:00
|
|
|
$where = $this->buildWhereClause($conn_r);
|
|
|
|
$order = $this->buildOrderClause($conn_r);
|
|
|
|
$limit = $this->buildLimitClause($conn_r);
|
|
|
|
|
|
|
|
$data = queryfx_all(
|
|
|
|
$conn_r,
|
2012-03-07 04:47:53 +01:00
|
|
|
'SELECT c.* FROM %T c %Q %Q %Q %Q',
|
2012-02-29 06:10:39 +01:00
|
|
|
$table->getTableName(),
|
2012-03-07 04:47:53 +01:00
|
|
|
$join,
|
2012-02-29 06:10:39 +01:00
|
|
|
$where,
|
|
|
|
$order,
|
|
|
|
$limit);
|
|
|
|
|
|
|
|
$commits = $table->loadAllFromArray($data);
|
|
|
|
|
|
|
|
if ($this->needCommitData && $commits) {
|
|
|
|
$data = id(new PhabricatorRepositoryCommitData())->loadAllWhere(
|
|
|
|
'commitID in (%Ld)',
|
|
|
|
mpull($commits, 'getID'));
|
|
|
|
$data = mpull($data, null, 'getCommitID');
|
|
|
|
foreach ($commits as $commit) {
|
|
|
|
if (idx($data, $commit->getID())) {
|
|
|
|
$commit->attachCommitData($data[$commit->getID()]);
|
|
|
|
} else {
|
|
|
|
$commit->attachCommitData(new PhabricatorRepositoryCommitData());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-03 01:44:29 +02:00
|
|
|
if ($this->needAudits && $commits) {
|
|
|
|
$audits = id(new PhabricatorAuditComment())->loadAllWhere(
|
|
|
|
'targetPHID in (%Ls)',
|
|
|
|
mpull($commits, 'getPHID'));
|
|
|
|
$audits = mgroup($audits, 'getTargetPHID');
|
|
|
|
foreach ($commits as $commit) {
|
|
|
|
$commit->attachAudits(idx($audits, $commit->getPHID(), array()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
return $commits;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildOrderClause($conn_r) {
|
2012-03-07 04:47:53 +01:00
|
|
|
return 'ORDER BY c.epoch DESC';
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildJoinClause($conn_r) {
|
|
|
|
$join = array();
|
|
|
|
|
|
|
|
if ($this->packagePHIDs) {
|
|
|
|
$join[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'JOIN %T req ON c.phid = req.commitPHID',
|
|
|
|
id(new PhabricatorRepositoryAuditRequest())->getTableName());
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($join) {
|
|
|
|
$join = implode(' ', $join);
|
|
|
|
} else {
|
|
|
|
$join = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $join;
|
2012-02-29 06:10:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function buildWhereClause($conn_r) {
|
|
|
|
$where = array();
|
|
|
|
|
2012-03-13 19:18:01 +01:00
|
|
|
if ($this->commitPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'c.phid IN (%Ls)',
|
|
|
|
$this->commitPHIDs);
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
if ($this->authorPHIDs) {
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2012-03-07 04:47:53 +01:00
|
|
|
'c.authorPHID IN (%Ls)',
|
2012-02-29 06:10:39 +01:00
|
|
|
$this->authorPHIDs);
|
|
|
|
}
|
|
|
|
|
2012-03-07 04:47:53 +01:00
|
|
|
if ($this->packagePHIDs) {
|
2012-02-29 06:10:39 +01:00
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2012-03-07 04:47:53 +01:00
|
|
|
'req.auditorPHID in (%Ls)',
|
|
|
|
$this->packagePHIDs);
|
2012-02-29 06:10:39 +01:00
|
|
|
}
|
|
|
|
|
2012-04-08 02:24:35 +02:00
|
|
|
if ($this->identifiers) {
|
|
|
|
$clauses = array();
|
|
|
|
foreach ($this->identifiers as $spec) {
|
|
|
|
list($repository_id, $identifiers) = $spec;
|
|
|
|
if ($identifiers) {
|
|
|
|
$clauses[] = qsprintf(
|
|
|
|
$conn_r,
|
|
|
|
'c.repositoryID = %d AND c.commitIdentifier IN (%Ls)',
|
|
|
|
$repository_id,
|
|
|
|
$identifiers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($clauses) {
|
|
|
|
$where[] = '('.implode(') OR (', $clauses).')';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-29 06:10:39 +01:00
|
|
|
$status = $this->status;
|
|
|
|
switch ($status) {
|
2013-02-28 16:55:06 +01:00
|
|
|
case self::STATUS_CONCERN:
|
2012-02-29 06:10:39 +01:00
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-03-02 01:42:03 +01:00
|
|
|
'c.auditStatus = %d',
|
2012-02-29 06:10:39 +01:00
|
|
|
PhabricatorAuditCommitStatusConstants::CONCERN_RAISED);
|
|
|
|
break;
|
2013-02-28 16:55:06 +01:00
|
|
|
case self::STATUS_OPEN:
|
|
|
|
$where[] = qsprintf(
|
|
|
|
$conn_r,
|
2013-03-02 01:42:03 +01:00
|
|
|
'c.auditStatus IN (%Ld)',
|
2013-02-28 16:55:06 +01:00
|
|
|
PhabricatorAuditCommitStatusConstants::getOpenStatusConstants());
|
|
|
|
break;
|
2012-02-29 06:10:39 +01:00
|
|
|
case self::STATUS_ANY:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new Exception("Unknown status '{$status}'!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($where) {
|
|
|
|
$where = 'WHERE ('.implode(') AND (', $where).')';
|
|
|
|
} else {
|
|
|
|
$where = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $where;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function buildLimitClause($conn_r) {
|
|
|
|
if ($this->limit && $this->offset) {
|
|
|
|
return qsprintf($conn_r, 'LIMIT %d, %d', $this->offset, $this->limit);
|
|
|
|
} else if ($this->limit) {
|
|
|
|
return qsprintf($conn_r, 'LIMIT %d', $this->limit);
|
|
|
|
} else if ($this->offset) {
|
|
|
|
return qsprintf($conn_r, 'LIMIT %d, %d', $this->offset, PHP_INT_MAX);
|
|
|
|
} else {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|