Path: blob/master/src/applications/differential/query/DifferentialDiffQuery.php
12256 views
<?php12final class DifferentialDiffQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45private $ids;6private $phids;7private $revisionIDs;8private $revisionPHIDs;9private $commitPHIDs;10private $hasRevision;1112private $needChangesets = false;13private $needProperties;1415public function withIDs(array $ids) {16$this->ids = $ids;17return $this;18}1920public function withPHIDs(array $phids) {21$this->phids = $phids;22return $this;23}2425public function withRevisionIDs(array $revision_ids) {26$this->revisionIDs = $revision_ids;27return $this;28}2930public function withRevisionPHIDs(array $revision_phids) {31$this->revisionPHIDs = $revision_phids;32return $this;33}3435public function withCommitPHIDs(array $phids) {36$this->commitPHIDs = $phids;37return $this;38}3940public function withHasRevision($has_revision) {41$this->hasRevision = $has_revision;42return $this;43}4445public function needChangesets($bool) {46$this->needChangesets = $bool;47return $this;48}4950public function needProperties($need_properties) {51$this->needProperties = $need_properties;52return $this;53}5455public function newResultObject() {56return new DifferentialDiff();57}5859protected function willFilterPage(array $diffs) {60$revision_ids = array_filter(mpull($diffs, 'getRevisionID'));6162$revisions = array();63if ($revision_ids) {64$revisions = id(new DifferentialRevisionQuery())65->setViewer($this->getViewer())66->withIDs($revision_ids)67->execute();68}6970foreach ($diffs as $key => $diff) {71if (!$diff->getRevisionID()) {72continue;73}7475$revision = idx($revisions, $diff->getRevisionID());76if ($revision) {77$diff->attachRevision($revision);78continue;79}8081unset($diffs[$key]);82}838485if ($diffs && $this->needChangesets) {86$diffs = $this->loadChangesets($diffs);87}8889return $diffs;90}9192protected function didFilterPage(array $diffs) {93if ($this->needProperties) {94$properties = id(new DifferentialDiffProperty())->loadAllWhere(95'diffID IN (%Ld)',96mpull($diffs, 'getID'));9798$properties = mgroup($properties, 'getDiffID');99foreach ($diffs as $diff) {100$map = idx($properties, $diff->getID(), array());101$map = mpull($map, 'getData', 'getName');102$diff->attachDiffProperties($map);103}104}105106return $diffs;107}108109private function loadChangesets(array $diffs) {110id(new DifferentialChangesetQuery())111->setViewer($this->getViewer())112->setParentQuery($this)113->withDiffs($diffs)114->needAttachToDiffs(true)115->needHunks(true)116->execute();117118return $diffs;119}120121protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {122$where = parent::buildWhereClauseParts($conn);123124if ($this->ids !== null) {125$where[] = qsprintf(126$conn,127'id IN (%Ld)',128$this->ids);129}130131if ($this->phids !== null) {132$where[] = qsprintf(133$conn,134'phid IN (%Ls)',135$this->phids);136}137138if ($this->revisionIDs !== null) {139$where[] = qsprintf(140$conn,141'revisionID IN (%Ld)',142$this->revisionIDs);143}144145if ($this->commitPHIDs !== null) {146$where[] = qsprintf(147$conn,148'commitPHID IN (%Ls)',149$this->commitPHIDs);150}151152if ($this->hasRevision !== null) {153if ($this->hasRevision) {154$where[] = qsprintf(155$conn,156'revisionID IS NOT NULL');157} else {158$where[] = qsprintf(159$conn,160'revisionID IS NULL');161}162}163164if ($this->revisionPHIDs !== null) {165$viewer = $this->getViewer();166167$revisions = id(new DifferentialRevisionQuery())168->setViewer($viewer)169->setParentQuery($this)170->withPHIDs($this->revisionPHIDs)171->execute();172$revision_ids = mpull($revisions, 'getID');173if (!$revision_ids) {174throw new PhabricatorEmptyQueryException();175}176177$where[] = qsprintf(178$conn,179'revisionID IN (%Ls)',180$revision_ids);181}182183return $where;184}185186public function getQueryApplicationClass() {187return 'PhabricatorDifferentialApplication';188}189190}191192193