[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class DifferentialHunkQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $changesets;
   7    private $shouldAttachToChangesets;
   8  
   9    public function withChangesets(array $changesets) {
  10      assert_instances_of($changesets, 'DifferentialChangeset');
  11      $this->changesets = $changesets;
  12      return $this;
  13    }
  14  
  15    public function needAttachToChangesets($attach) {
  16      $this->shouldAttachToChangesets = $attach;
  17      return $this;
  18    }
  19  
  20    public function willExecute() {
  21      // If we fail to load any hunks at all (for example, because all of
  22      // the requested changesets are directories or empty files and have no
  23      // hunks) we'll never call didFilterPage(), and thus never have an
  24      // opportunity to attach hunks. Attach empty hunk lists now so that we
  25      // end up with the right result.
  26      if ($this->shouldAttachToChangesets) {
  27        foreach ($this->changesets as $changeset) {
  28          $changeset->attachHunks(array());
  29        }
  30      }
  31    }
  32  
  33    public function loadPage() {
  34      $all_results = array();
  35  
  36      // Load modern hunks.
  37      $table = new DifferentialHunkModern();
  38      $conn_r = $table->establishConnection('r');
  39  
  40      $modern_data = queryfx_all(
  41        $conn_r,
  42        'SELECT * FROM %T %Q %Q %Q',
  43        $table->getTableName(),
  44        $this->buildWhereClause($conn_r),
  45        $this->buildOrderClause($conn_r),
  46        $this->buildLimitClause($conn_r));
  47      $modern_results = $table->loadAllFromArray($modern_data);
  48  
  49  
  50      // Now, load legacy hunks.
  51      $table = new DifferentialHunkLegacy();
  52      $conn_r = $table->establishConnection('r');
  53  
  54      $legacy_data = queryfx_all(
  55        $conn_r,
  56        'SELECT * FROM %T %Q %Q %Q',
  57        $table->getTableName(),
  58        $this->buildWhereClause($conn_r),
  59        $this->buildOrderClause($conn_r),
  60        $this->buildLimitClause($conn_r));
  61      $legacy_results = $table->loadAllFromArray($legacy_data);
  62  
  63      // Strip all the IDs off since they're not unique and nothing should be
  64      // using them.
  65      return array_values(array_merge($legacy_results, $modern_results));
  66    }
  67  
  68    public function willFilterPage(array $hunks) {
  69      $changesets = mpull($this->changesets, null, 'getID');
  70      foreach ($hunks as $key => $hunk) {
  71        $changeset = idx($changesets, $hunk->getChangesetID());
  72        if (!$changeset) {
  73          unset($hunks[$key]);
  74        }
  75        $hunk->attachChangeset($changeset);
  76      }
  77  
  78      return $hunks;
  79    }
  80  
  81    public function didFilterPage(array $hunks) {
  82      if ($this->shouldAttachToChangesets) {
  83        $hunk_groups = mgroup($hunks, 'getChangesetID');
  84        foreach ($this->changesets as $changeset) {
  85          $hunks = idx($hunk_groups, $changeset->getID(), array());
  86          $changeset->attachHunks($hunks);
  87        }
  88      }
  89  
  90      return $hunks;
  91    }
  92  
  93    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
  94      $where = array();
  95  
  96      if (!$this->changesets) {
  97        throw new Exception(
  98          pht('You must load hunks via changesets, with withChangesets()!'));
  99      }
 100  
 101      $where[] = qsprintf(
 102        $conn_r,
 103        'changesetID IN (%Ld)',
 104        mpull($this->changesets, 'getID'));
 105  
 106      $where[] = $this->buildPagingClause($conn_r);
 107  
 108      return $this->formatWhereClause($where);
 109    }
 110  
 111    public function getQueryApplicationClass() {
 112      return 'PhabricatorDifferentialApplication';
 113    }
 114  
 115    protected function getReversePaging() {
 116      return true;
 117    }
 118  
 119  }


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