[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/phragment/query/ -> PhragmentSnapshotQuery.php (source)

   1  <?php
   2  
   3  final class PhragmentSnapshotQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $primaryFragmentPHIDs;
   9    private $names;
  10  
  11    public function withIDs(array $ids) {
  12      $this->ids = $ids;
  13      return $this;
  14    }
  15  
  16    public function withPHIDs(array $phids) {
  17      $this->phids = $phids;
  18      return $this;
  19    }
  20  
  21    public function withPrimaryFragmentPHIDs(array $primary_fragment_phids) {
  22      $this->primaryFragmentPHIDs = $primary_fragment_phids;
  23      return $this;
  24    }
  25  
  26    public function withNames(array $names) {
  27      $this->names = $names;
  28      return $this;
  29    }
  30  
  31    public function loadPage() {
  32      $table = new PhragmentSnapshot();
  33      $conn_r = $table->establishConnection('r');
  34  
  35      $data = queryfx_all(
  36        $conn_r,
  37        'SELECT * FROM %T %Q %Q %Q',
  38        $table->getTableName(),
  39        $this->buildWhereClause($conn_r),
  40        $this->buildOrderClause($conn_r),
  41        $this->buildLimitClause($conn_r));
  42  
  43      return $table->loadAllFromArray($data);
  44    }
  45  
  46    protected function buildWhereClause($conn_r) {
  47      $where = array();
  48  
  49      if ($this->ids) {
  50        $where[] = qsprintf(
  51          $conn_r,
  52          'id IN (%Ld)',
  53          $this->ids);
  54      }
  55  
  56      if ($this->phids) {
  57        $where[] = qsprintf(
  58          $conn_r,
  59          'phid IN (%Ls)',
  60          $this->phids);
  61      }
  62  
  63      if ($this->primaryFragmentPHIDs) {
  64        $where[] = qsprintf(
  65          $conn_r,
  66          'primaryFragmentPHID IN (%Ls)',
  67          $this->primaryFragmentPHIDs);
  68      }
  69  
  70      if ($this->names) {
  71        $where[] = qsprintf(
  72          $conn_r,
  73          'name IN (%Ls)',
  74          $this->names);
  75      }
  76  
  77      $where[] = $this->buildPagingClause($conn_r);
  78  
  79      return $this->formatWhereClause($where);
  80    }
  81  
  82    protected function willFilterPage(array $page) {
  83      $fragments = array();
  84  
  85      $fragment_phids = array_filter(mpull($page, 'getPrimaryFragmentPHID'));
  86      if ($fragment_phids) {
  87        $fragments = id(new PhabricatorObjectQuery())
  88          ->setViewer($this->getViewer())
  89          ->withPHIDs($fragment_phids)
  90          ->setParentQuery($this)
  91          ->execute();
  92        $fragments = mpull($fragments, null, 'getPHID');
  93      }
  94  
  95      foreach ($page as $key => $snapshot) {
  96        $fragment_phid = $snapshot->getPrimaryFragmentPHID();
  97        if (empty($fragments[$fragment_phid])) {
  98          unset($page[$key]);
  99          continue;
 100        }
 101        $snapshot->attachPrimaryFragment($fragments[$fragment_phid]);
 102      }
 103  
 104      return $page;
 105    }
 106  
 107    public function getQueryApplicationClass() {
 108      return 'PhabricatorPhragmentApplication';
 109    }
 110  
 111  }


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