[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/harbormaster/query/ -> HarbormasterBuildQuery.php (source)

   1  <?php
   2  
   3  final class HarbormasterBuildQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $buildStatuses;
   9    private $buildablePHIDs;
  10    private $buildPlanPHIDs;
  11    private $needBuildTargets;
  12  
  13    public function withIDs(array $ids) {
  14      $this->ids = $ids;
  15      return $this;
  16    }
  17  
  18    public function withPHIDs(array $phids) {
  19      $this->phids = $phids;
  20      return $this;
  21    }
  22  
  23    public function withBuildStatuses(array $build_statuses) {
  24      $this->buildStatuses = $build_statuses;
  25      return $this;
  26    }
  27  
  28    public function withBuildablePHIDs(array $buildable_phids) {
  29      $this->buildablePHIDs = $buildable_phids;
  30      return $this;
  31    }
  32  
  33    public function withBuildPlanPHIDs(array $build_plan_phids) {
  34      $this->buildPlanPHIDs = $build_plan_phids;
  35      return $this;
  36    }
  37  
  38    public function needBuildTargets($need_targets) {
  39      $this->needBuildTargets = $need_targets;
  40      return $this;
  41    }
  42  
  43    protected function loadPage() {
  44      $table = new HarbormasterBuild();
  45      $conn_r = $table->establishConnection('r');
  46  
  47      $data = queryfx_all(
  48        $conn_r,
  49        'SELECT * FROM %T %Q %Q %Q',
  50        $table->getTableName(),
  51        $this->buildWhereClause($conn_r),
  52        $this->buildOrderClause($conn_r),
  53        $this->buildLimitClause($conn_r));
  54  
  55      return $table->loadAllFromArray($data);
  56    }
  57  
  58    protected function willFilterPage(array $page) {
  59      $buildables = array();
  60  
  61      $buildable_phids = array_filter(mpull($page, 'getBuildablePHID'));
  62      if ($buildable_phids) {
  63        $buildables = id(new PhabricatorObjectQuery())
  64          ->setViewer($this->getViewer())
  65          ->withPHIDs($buildable_phids)
  66          ->setParentQuery($this)
  67          ->execute();
  68        $buildables = mpull($buildables, null, 'getPHID');
  69      }
  70  
  71      foreach ($page as $key => $build) {
  72        $buildable_phid = $build->getBuildablePHID();
  73        if (empty($buildables[$buildable_phid])) {
  74          unset($page[$key]);
  75          continue;
  76        }
  77        $build->attachBuildable($buildables[$buildable_phid]);
  78      }
  79  
  80      return $page;
  81    }
  82  
  83    protected function didFilterPage(array $page) {
  84      $plans = array();
  85  
  86      $plan_phids = array_filter(mpull($page, 'getBuildPlanPHID'));
  87      if ($plan_phids) {
  88        $plans = id(new PhabricatorObjectQuery())
  89          ->setViewer($this->getViewer())
  90          ->withPHIDs($plan_phids)
  91          ->setParentQuery($this)
  92          ->execute();
  93        $plans = mpull($plans, null, 'getPHID');
  94      }
  95  
  96      foreach ($page as $key => $build) {
  97        $plan_phid = $build->getBuildPlanPHID();
  98        $build->attachBuildPlan(idx($plans, $plan_phid));
  99      }
 100  
 101      $build_phids = mpull($page, 'getPHID');
 102      $commands = id(new HarbormasterBuildCommand())->loadAllWhere(
 103        'targetPHID IN (%Ls) ORDER BY id ASC',
 104        $build_phids);
 105      $commands = mgroup($commands, 'getTargetPHID');
 106      foreach ($page as $build) {
 107        $unprocessed_commands = idx($commands, $build->getPHID(), array());
 108        $build->attachUnprocessedCommands($unprocessed_commands);
 109      }
 110  
 111      if ($this->needBuildTargets) {
 112        $targets = id(new HarbormasterBuildTargetQuery())
 113          ->setViewer($this->getViewer())
 114          ->setParentQuery($this)
 115          ->withBuildPHIDs($build_phids)
 116          ->execute();
 117  
 118        // TODO: Some day, when targets have dependencies, we should toposort
 119        // these. For now, just put them into chronological order.
 120        $targets = array_reverse($targets);
 121  
 122        $targets = mgroup($targets, 'getBuildPHID');
 123        foreach ($page as $build) {
 124          $build_targets = idx($targets, $build->getPHID(), array());
 125  
 126          foreach ($build_targets as $phid => $target) {
 127            if ($target->getBuildGeneration() !== $build->getBuildGeneration()) {
 128              unset($build_targets[$phid]);
 129            }
 130          }
 131  
 132          $build->attachBuildTargets($build_targets);
 133        }
 134      }
 135  
 136      return $page;
 137    }
 138  
 139    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
 140      $where = array();
 141  
 142      if ($this->ids !== null) {
 143        $where[] = qsprintf(
 144          $conn_r,
 145          'id IN (%Ld)',
 146          $this->ids);
 147      }
 148  
 149      if ($this->phids !== null) {
 150        $where[] = qsprintf(
 151          $conn_r,
 152          'phid in (%Ls)',
 153          $this->phids);
 154      }
 155  
 156      if ($this->buildStatuses !== null) {
 157        $where[] = qsprintf(
 158          $conn_r,
 159          'buildStatus in (%Ls)',
 160          $this->buildStatuses);
 161      }
 162  
 163      if ($this->buildablePHIDs !== null) {
 164        $where[] = qsprintf(
 165          $conn_r,
 166          'buildablePHID IN (%Ls)',
 167          $this->buildablePHIDs);
 168      }
 169  
 170      if ($this->buildPlanPHIDs !== null) {
 171        $where[] = qsprintf(
 172          $conn_r,
 173          'buildPlanPHID IN (%Ls)',
 174          $this->buildPlanPHIDs);
 175      }
 176  
 177      $where[] = $this->buildPagingClause($conn_r);
 178  
 179      return $this->formatWhereClause($where);
 180    }
 181  
 182    public function getQueryApplicationClass() {
 183      return 'PhabricatorHarbormasterApplication';
 184    }
 185  
 186  }


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