[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class HarbormasterBuildTargetQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $buildPHIDs;
   9    private $buildGenerations;
  10    private $needBuildSteps;
  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 withBuildPHIDs(array $build_phids) {
  23      $this->buildPHIDs = $build_phids;
  24      return $this;
  25    }
  26  
  27    public function withBuildGenerations(array $build_generations) {
  28      $this->buildGenerations = $build_generations;
  29      return $this;
  30    }
  31  
  32    public function needBuildSteps($need_build_steps) {
  33      $this->needBuildSteps = $need_build_steps;
  34      return $this;
  35    }
  36  
  37    protected function loadPage() {
  38      $table = new HarbormasterBuildTarget();
  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    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
  53      $where = array();
  54  
  55      if ($this->ids) {
  56        $where[] = qsprintf(
  57          $conn_r,
  58          'id IN (%Ld)',
  59          $this->ids);
  60      }
  61  
  62      if ($this->phids) {
  63        $where[] = qsprintf(
  64          $conn_r,
  65          'phid in (%Ls)',
  66          $this->phids);
  67      }
  68  
  69      if ($this->buildPHIDs) {
  70        $where[] = qsprintf(
  71          $conn_r,
  72          'buildPHID in (%Ls)',
  73          $this->buildPHIDs);
  74      }
  75  
  76      if ($this->buildGenerations) {
  77        $where[] = qsprintf(
  78          $conn_r,
  79          'buildGeneration in (%Ld)',
  80          $this->buildGenerations);
  81      }
  82  
  83      $where[] = $this->buildPagingClause($conn_r);
  84  
  85      return $this->formatWhereClause($where);
  86    }
  87  
  88    protected function didFilterPage(array $page) {
  89      if ($this->needBuildSteps) {
  90        $step_phids = array();
  91  
  92        foreach ($page as $target) {
  93          $step_phids[] = $target->getBuildStepPHID();
  94        }
  95  
  96        $steps = id(new HarbormasterBuildStepQuery())
  97          ->setViewer($this->getViewer())
  98          ->setParentQuery($this)
  99          ->withPHIDs($step_phids)
 100          ->execute();
 101  
 102        $steps = mpull($steps, null, 'getPHID');
 103  
 104        foreach ($page as $target) {
 105          $target->attachBuildStep(
 106            idx($steps, $target->getBuildStepPHID()));
 107        }
 108      }
 109  
 110      return $page;
 111    }
 112  
 113    protected function willFilterPage(array $page) {
 114      $builds = array();
 115  
 116      $build_phids = array_filter(mpull($page, 'getBuildPHID'));
 117      if ($build_phids) {
 118        $builds = id(new PhabricatorObjectQuery())
 119          ->setViewer($this->getViewer())
 120          ->withPHIDs($build_phids)
 121          ->setParentQuery($this)
 122          ->execute();
 123        $builds = mpull($builds, null, 'getPHID');
 124      }
 125  
 126      foreach ($page as $key => $build_target) {
 127        $build_phid = $build_target->getBuildPHID();
 128        if (empty($builds[$build_phid])) {
 129          unset($page[$key]);
 130          continue;
 131        }
 132        $build_target->attachBuild($builds[$build_phid]);
 133      }
 134  
 135      return $page;
 136    }
 137  
 138    public function getQueryApplicationClass() {
 139      return 'PhabricatorHarbormasterApplication';
 140    }
 141  
 142  }


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