[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class DifferentialChangesetQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $diffs;
   8  
   9    private $needAttachToDiffs;
  10    private $needHunks;
  11  
  12    public function withIDs(array $ids) {
  13      $this->ids = $ids;
  14      return $this;
  15    }
  16  
  17    public function withDiffs(array $diffs) {
  18      assert_instances_of($diffs, 'DifferentialDiff');
  19      $this->diffs = $diffs;
  20      return $this;
  21    }
  22  
  23    public function needAttachToDiffs($attach) {
  24      $this->needAttachToDiffs = $attach;
  25      return $this;
  26    }
  27  
  28    public function needHunks($need) {
  29      $this->needHunks = $need;
  30      return $this;
  31    }
  32  
  33    public function willExecute() {
  34      // If we fail to load any changesets (which is possible in the case of an
  35      // empty commit) we'll never call didFilterPage(). Attach empty changeset
  36      // lists now so that we end up with the right result.
  37      if ($this->needAttachToDiffs) {
  38        foreach ($this->diffs as $diff) {
  39          $diff->attachChangesets(array());
  40        }
  41      }
  42    }
  43  
  44    public function loadPage() {
  45      $table = new DifferentialChangeset();
  46      $conn_r = $table->establishConnection('r');
  47  
  48      $data = queryfx_all(
  49        $conn_r,
  50        'SELECT * FROM %T %Q %Q %Q',
  51        $table->getTableName(),
  52        $this->buildWhereClause($conn_r),
  53        $this->buildOrderClause($conn_r),
  54        $this->buildLimitClause($conn_r));
  55  
  56      return $table->loadAllFromArray($data);
  57    }
  58  
  59    public function willFilterPage(array $changesets) {
  60      // First, attach all the diffs we already have. We can just do this
  61      // directly without worrying about querying for them. When we don't have
  62      // a diff, record that we need to load it.
  63      if ($this->diffs) {
  64        $have_diffs = mpull($this->diffs, null, 'getID');
  65      } else {
  66        $have_diffs = array();
  67      }
  68  
  69      $must_load = array();
  70      foreach ($changesets as $key => $changeset) {
  71        $diff_id = $changeset->getDiffID();
  72        if (isset($have_diffs[$diff_id])) {
  73          $changeset->attachDiff($have_diffs[$diff_id]);
  74        } else {
  75          $must_load[$key] = $changeset;
  76        }
  77      }
  78  
  79      // Load all the diffs we don't have.
  80      $need_diff_ids = mpull($must_load, 'getDiffID');
  81      $more_diffs = array();
  82      if ($need_diff_ids) {
  83        $more_diffs = id(new DifferentialDiffQuery())
  84          ->setViewer($this->getViewer())
  85          ->setParentQuery($this)
  86          ->withIDs($need_diff_ids)
  87          ->execute();
  88        $more_diffs = mpull($more_diffs, null, 'getID');
  89      }
  90  
  91      // Attach the diffs we loaded.
  92      foreach ($must_load as $key => $changeset) {
  93        $diff_id = $changeset->getDiffID();
  94        if (isset($more_diffs[$diff_id])) {
  95          $changeset->attachDiff($more_diffs[$diff_id]);
  96        } else {
  97          // We didn't have the diff, and could not load it (it does not exist,
  98          // or we can't see it), so filter this result out.
  99          unset($changesets[$key]);
 100        }
 101      }
 102  
 103      return $changesets;
 104    }
 105  
 106    public function didFilterPage(array $changesets) {
 107      if ($this->needAttachToDiffs) {
 108        $changeset_groups = mgroup($changesets, 'getDiffID');
 109        foreach ($this->diffs as $diff) {
 110          $diff_changesets = idx($changeset_groups, $diff->getID(), array());
 111          $diff->attachChangesets($diff_changesets);
 112        }
 113      }
 114  
 115      if ($this->needHunks) {
 116        id(new DifferentialHunkQuery())
 117          ->setViewer($this->getViewer())
 118          ->setParentQuery($this)
 119          ->withChangesets($changesets)
 120          ->needAttachToChangesets(true)
 121          ->execute();
 122      }
 123  
 124      return $changesets;
 125    }
 126  
 127    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 128      $where = array();
 129  
 130      if ($this->diffs !== null) {
 131        $where[] = qsprintf(
 132          $conn_r,
 133          'diffID IN (%Ld)',
 134          mpull($this->diffs, 'getID'));
 135      }
 136  
 137      if ($this->ids !== null) {
 138        $where[] = qsprintf(
 139          $conn_r,
 140          'id IN (%Ld)',
 141          $this->ids);
 142      }
 143  
 144      $where[] = $this->buildPagingClause($conn_r);
 145  
 146      return $this->formatWhereClause($where);
 147    }
 148  
 149    public function getQueryApplicationClass() {
 150      return 'PhabricatorDifferentialApplication';
 151    }
 152  
 153    protected function getReversePaging() {
 154      return true;
 155    }
 156  
 157  }


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