Path: blob/master/src/applications/differential/query/DifferentialChangesetQuery.php
12262 views
<?php12final class DifferentialChangesetQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45private $ids;6private $phids;7private $diffPHIDs;89private $diffs;1011private $needAttachToDiffs;12private $needHunks;1314public function withIDs(array $ids) {15$this->ids = $ids;16return $this;17}1819public function withPHIDs(array $phids) {20$this->phids = $phids;21return $this;22}2324public function withDiffs(array $diffs) {25assert_instances_of($diffs, 'DifferentialDiff');26$this->diffs = $diffs;27return $this;28}2930public function withDiffPHIDs(array $phids) {31$this->diffPHIDs = $phids;32return $this;33}3435public function needAttachToDiffs($attach) {36$this->needAttachToDiffs = $attach;37return $this;38}3940public function needHunks($need) {41$this->needHunks = $need;42return $this;43}4445protected function willExecute() {46// If we fail to load any changesets (which is possible in the case of an47// empty commit) we'll never call didFilterPage(). Attach empty changeset48// lists now so that we end up with the right result.49if ($this->needAttachToDiffs) {50foreach ($this->diffs as $diff) {51$diff->attachChangesets(array());52}53}54}5556public function newResultObject() {57return new DifferentialChangeset();58}5960protected function willFilterPage(array $changesets) {61// First, attach all the diffs we already have. We can just do this62// directly without worrying about querying for them. When we don't have63// a diff, record that we need to load it.64if ($this->diffs) {65$have_diffs = mpull($this->diffs, null, 'getID');66} else {67$have_diffs = array();68}6970$must_load = array();71foreach ($changesets as $key => $changeset) {72$diff_id = $changeset->getDiffID();73if (isset($have_diffs[$diff_id])) {74$changeset->attachDiff($have_diffs[$diff_id]);75} else {76$must_load[$key] = $changeset;77}78}7980// Load all the diffs we don't have.81$need_diff_ids = mpull($must_load, 'getDiffID');82$more_diffs = array();83if ($need_diff_ids) {84$more_diffs = id(new DifferentialDiffQuery())85->setViewer($this->getViewer())86->setParentQuery($this)87->withIDs($need_diff_ids)88->execute();89$more_diffs = mpull($more_diffs, null, 'getID');90}9192// Attach the diffs we loaded.93foreach ($must_load as $key => $changeset) {94$diff_id = $changeset->getDiffID();95if (isset($more_diffs[$diff_id])) {96$changeset->attachDiff($more_diffs[$diff_id]);97} else {98// We didn't have the diff, and could not load it (it does not exist,99// or we can't see it), so filter this result out.100unset($changesets[$key]);101}102}103104return $changesets;105}106107protected function didFilterPage(array $changesets) {108if ($this->needAttachToDiffs) {109$changeset_groups = mgroup($changesets, 'getDiffID');110foreach ($this->diffs as $diff) {111$diff_changesets = idx($changeset_groups, $diff->getID(), array());112$diff->attachChangesets($diff_changesets);113}114}115116if ($this->needHunks) {117id(new DifferentialHunkQuery())118->setViewer($this->getViewer())119->setParentQuery($this)120->withChangesets($changesets)121->needAttachToChangesets(true)122->execute();123}124125return $changesets;126}127128protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {129$where = parent::buildWhereClauseParts($conn);130131if ($this->diffs !== null) {132$where[] = qsprintf(133$conn,134'diffID IN (%Ld)',135mpull($this->diffs, 'getID'));136}137138if ($this->ids !== null) {139$where[] = qsprintf(140$conn,141'id IN (%Ld)',142$this->ids);143}144145if ($this->phids !== null) {146$where[] = qsprintf(147$conn,148'phid IN (%Ls)',149$this->phids);150}151152if ($this->diffPHIDs !== null) {153$diff_ids = queryfx_all(154$conn,155'SELECT id FROM %R WHERE phid IN (%Ls)',156new DifferentialDiff(),157$this->diffPHIDs);158$diff_ids = ipull($diff_ids, 'id', null);159160if (!$diff_ids) {161throw new PhabricatorEmptyQueryException();162}163164$where[] = qsprintf(165$conn,166'diffID IN (%Ld)',167$diff_ids);168}169170return $where;171}172173public function getQueryApplicationClass() {174return 'PhabricatorDifferentialApplication';175}176177}178179180