1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-10 08:52:39 +01:00

Move "most recent commit" and "commit count" into DiffusionRepositoryQuery

Summary: Ref T2625. `DiffusionHomeController` currently runs these queries inline. Move them into `DiffusionRepositoryQuery`. Prepareds for ApplicationSearch.

Test Plan: Loaded `/diffusion/`, saw the same content as before.

Reviewers: btrahan

Reviewed By: btrahan

CC: aran

Maniphest Tasks: T2625

Differential Revision: https://secure.phabricator.com/D6914
This commit is contained in:
epriestley 2013-09-10 15:22:41 -07:00
parent 8e45b466da
commit 93c6704059
3 changed files with 99 additions and 34 deletions

View file

@ -41,6 +41,8 @@ final class DiffusionHomeController extends DiffusionController {
$repositories = id(new PhabricatorRepositoryQuery())
->setViewer($user)
->needCommitCounts(true)
->needMostRecentCommits(true)
->execute();
foreach ($repositories as $key => $repo) {
@ -50,25 +52,6 @@ final class DiffusionHomeController extends DiffusionController {
}
$repositories = msort($repositories, 'getName');
$repository_ids = mpull($repositories, 'getID');
$summaries = array();
$commits = array();
if ($repository_ids) {
$summaries = queryfx_all(
id(new PhabricatorRepository())->establishConnection('r'),
'SELECT * FROM %T WHERE repositoryID IN (%Ld)',
PhabricatorRepository::TABLE_SUMMARY,
$repository_ids);
$summaries = ipull($summaries, null, 'repositoryID');
$commit_ids = array_filter(ipull($summaries, 'lastCommitID'));
if ($commit_ids) {
$commit = new PhabricatorRepositoryCommit();
$commits = $commit->loadAllWhere('id IN (%Ld)', $commit_ids);
$commits = mpull($commits, null, 'getRepositoryID');
}
}
$branch = new PhabricatorRepositoryBranch();
$lint_messages = queryfx_all(
$branch->establishConnection('r'),
@ -84,10 +67,9 @@ final class DiffusionHomeController extends DiffusionController {
$show_lint = false;
foreach ($repositories as $repository) {
$id = $repository->getID();
$commit = idx($commits, $id);
$size = idx(idx($summaries, $id, array()), 'size', '-');
if ($size != '-') {
$size = $repository->getCommitCount();
if ($size) {
$size = hsprintf(
'<a href="%s">%s</a>',
DiffusionRequest::generateDiffusionURI(array(
@ -114,9 +96,10 @@ final class DiffusionHomeController extends DiffusionController {
}
$datetime = '';
if ($commit) {
$date = phabricator_date($commit->getEpoch(), $user);
$time = phabricator_time($commit->getEpoch(), $user);
$most_recent_commit = $repository->getMostRecentCommit();
if ($most_recent_commit) {
$date = phabricator_date($most_recent_commit->getEpoch(), $user);
$time = phabricator_time($most_recent_commit->getEpoch(), $user);
$datetime = $date.' '.$time;
}
@ -125,13 +108,13 @@ final class DiffusionHomeController extends DiffusionController {
('/diffusion/'.$repository->getCallsign().'/'),
PhabricatorRepositoryType::getNameForRepositoryType(
$repository->getVersionControlSystem()),
$size,
$size ? $size : null,
$lint_count,
$commit
$most_recent_commit
? DiffusionView::linkCommit(
$repository,
$commit->getCommitIdentifier(),
$commit->getSummary())
$most_recent_commit->getCommitIdentifier(),
$most_recent_commit->getSummary())
: pht('No Commits'),
$datetime
);

View file

@ -7,6 +7,9 @@ final class PhabricatorRepositoryQuery
private $phids;
private $callsigns;
private $needMostRecentCommits;
private $needCommitCounts;
public function withIDs(array $ids) {
$this->ids = $ids;
return $this;
@ -26,19 +29,76 @@ final class PhabricatorRepositoryQuery
return true;
}
public function needCommitCounts($need_counts) {
$this->needCommitCounts = $need_counts;
return $this;
}
public function needMostRecentCommits($need_commits) {
$this->needMostRecentCommits = $need_commits;
return $this;
}
protected function loadPage() {
$table = new PhabricatorRepository();
$conn_r = $table->establishConnection('r');
$data = queryfx_all(
$conn_r,
'SELECT * FROM %T %Q %Q %Q',
'SELECT * FROM %T r %Q %Q %Q %Q',
$table->getTableName(),
$this->buildJoinsClause($conn_r),
$this->buildWhereClause($conn_r),
$this->buildOrderClause($conn_r),
$this->buildLimitClause($conn_r));
return $table->loadAllFromArray($data);
$repositories = $table->loadAllFromArray($data);
if ($this->needCommitCounts) {
$sizes = ipull($data, 'size', 'id');
foreach ($repositories as $id => $repository) {
$repository->attachCommitCount(nonempty($sizes[$id], 0));
}
}
if ($this->needMostRecentCommits) {
$commit_ids = ipull($data, 'lastCommitID', 'id');
$commit_ids = array_filter($commit_ids);
if ($commit_ids) {
$commits = id(new DiffusionCommitQuery())
->setViewer($this->getViewer())
->withIDs($commit_ids)
->execute();
} else {
$commits = array();
}
foreach ($repositories as $id => $repository) {
$commit = null;
if (idx($commit_ids, $id)) {
$commit = idx($commits, $commit_ids[$id]);
}
$repository->attachMostRecentCommit($commit);
}
}
return $repositories;
}
private function buildJoinsClause(AphrontDatabaseConnection $conn_r) {
$joins = array();
$join_summary_table = $this->needCommitCounts ||
$this->needMostRecentCommits;
if ($join_summary_table) {
$joins[] = qsprintf(
$conn_r,
'LEFT JOIN %T summary ON r.id = summary.repositoryID',
PhabricatorRepository::TABLE_SUMMARY);
}
return implode(' ', $joins);
}
private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
@ -47,21 +107,21 @@ final class PhabricatorRepositoryQuery
if ($this->ids) {
$where[] = qsprintf(
$conn_r,
'id IN (%Ld)',
'r.id IN (%Ld)',
$this->ids);
}
if ($this->phids) {
$where[] = qsprintf(
$conn_r,
'phid IN (%Ls)',
'r.phid IN (%Ls)',
$this->phids);
}
if ($this->callsigns) {
$where[] = qsprintf(
$conn_r,
'callsign IN (%Ls)',
'r.callsign IN (%Ls)',
$this->callsigns);
}

View file

@ -35,6 +35,9 @@ final class PhabricatorRepository extends PhabricatorRepositoryDAO
private $sshKeyfile;
private $commitCount = self::ATTACHABLE;
private $mostRecentCommit = self::ATTACHABLE;
public function getConfiguration() {
return array(
self::CONFIG_AUX_PHID => true,
@ -71,6 +74,25 @@ final class PhabricatorRepository extends PhabricatorRepositoryDAO
return $this;
}
public function attachCommitCount($count) {
$this->commitCount = $count;
return $this;
}
public function getCommitCount() {
return $this->assertAttached($this->commitCount);
}
public function attachMostRecentCommit(
PhabricatorRepositoryCommit $commit = null) {
$this->mostRecentCommit = $commit;
return $this;
}
public function getMostRecentCommit() {
return $this->assertAttached($this->mostRecentCommit);
}
public function getDiffusionBrowseURIForPath(
PhabricatorUser $user,
$path,