[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/differential/query/ -> DifferentialDiffQuery.php (source)

   1  <?php
   2  
   3  final class DifferentialDiffQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $revisionIDs;
   9    private $needChangesets = false;
  10    private $needArcanistProjects = false;
  11  
  12    public function withIDs(array $ids) {
  13      $this->ids = $ids;
  14      return $this;
  15    }
  16  
  17    public function withPHIDs(array $phids) {
  18      $this->phids = $phids;
  19      return $this;
  20    }
  21  
  22    public function withRevisionIDs(array $revision_ids) {
  23      $this->revisionIDs = $revision_ids;
  24      return $this;
  25    }
  26  
  27    public function needChangesets($bool) {
  28      $this->needChangesets = $bool;
  29      return $this;
  30    }
  31  
  32    public function needArcanistProjects($bool) {
  33      $this->needArcanistProjects = $bool;
  34      return $this;
  35    }
  36  
  37    public function loadPage() {
  38      $table = new DifferentialDiff();
  39      $conn_r = $table->establishConnection('r');
  40  
  41      $data = queryfx_all(
  42        $conn_r,
  43        'SELECT * FROM %T %Q %Q %Q',
  44        $table->getTableName(),
  45        $this->buildWhereClause($conn_r),
  46        $this->buildOrderClause($conn_r),
  47        $this->buildLimitClause($conn_r));
  48  
  49      return $table->loadAllFromArray($data);
  50    }
  51  
  52    public function willFilterPage(array $diffs) {
  53      $revision_ids = array_filter(mpull($diffs, 'getRevisionID'));
  54  
  55      $revisions = array();
  56      if ($revision_ids) {
  57        $revisions = id(new DifferentialRevisionQuery())
  58          ->setViewer($this->getViewer())
  59          ->withIDs($revision_ids)
  60          ->execute();
  61      }
  62  
  63      foreach ($diffs as $key => $diff) {
  64        if (!$diff->getRevisionID()) {
  65          continue;
  66        }
  67  
  68        $revision = idx($revisions, $diff->getRevisionID());
  69        if ($revision) {
  70          $diff->attachRevision($revision);
  71          continue;
  72        }
  73  
  74        unset($diffs[$key]);
  75      }
  76  
  77  
  78      if ($diffs && $this->needChangesets) {
  79        $diffs = $this->loadChangesets($diffs);
  80      }
  81  
  82      if ($diffs && $this->needArcanistProjects) {
  83        $diffs = $this->loadArcanistProjects($diffs);
  84      }
  85  
  86      return $diffs;
  87    }
  88  
  89    private function loadChangesets(array $diffs) {
  90      id(new DifferentialChangesetQuery())
  91        ->setViewer($this->getViewer())
  92        ->setParentQuery($this)
  93        ->withDiffs($diffs)
  94        ->needAttachToDiffs(true)
  95        ->needHunks(true)
  96        ->execute();
  97  
  98      return $diffs;
  99    }
 100  
 101    private function loadArcanistProjects(array $diffs) {
 102      $phids = array_filter(mpull($diffs, 'getArcanistProjectPHID'));
 103      $projects = array();
 104      $project_map = array();
 105      if ($phids) {
 106        $projects = id(new PhabricatorRepositoryArcanistProject())
 107          ->loadAllWhere(
 108            'phid IN (%Ls)',
 109            $phids);
 110        $project_map = mpull($projects, null, 'getPHID');
 111      }
 112  
 113      foreach ($diffs as $diff) {
 114        $project = null;
 115        if ($diff->getArcanistProjectPHID()) {
 116          $project = idx($project_map, $diff->getArcanistProjectPHID());
 117        }
 118        $diff->attachArcanistProject($project);
 119      }
 120  
 121      return $diffs;
 122    }
 123  
 124    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 125      $where = array();
 126  
 127      if ($this->ids) {
 128        $where[] = qsprintf(
 129          $conn_r,
 130          'id IN (%Ld)',
 131          $this->ids);
 132      }
 133  
 134      if ($this->phids) {
 135        $where[] = qsprintf(
 136          $conn_r,
 137          'phid IN (%Ls)',
 138          $this->phids);
 139      }
 140  
 141      if ($this->revisionIDs) {
 142        $where[] = qsprintf(
 143          $conn_r,
 144          'revisionID IN (%Ld)',
 145          $this->revisionIDs);
 146      }
 147  
 148      $where[] = $this->buildPagingClause($conn_r);
 149      return $this->formatWhereClause($where);
 150    }
 151  
 152    public function getQueryApplicationClass() {
 153      return 'PhabricatorDifferentialApplication';
 154    }
 155  
 156  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1