[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class PhragmentFragmentQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $paths;
   9    private $leadingPath;
  10    private $depths;
  11    private $needLatestVersion;
  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 withPaths(array $paths) {
  24      $this->paths = $paths;
  25      return $this;
  26    }
  27  
  28    public function withLeadingPath($path) {
  29      $this->leadingPath = $path;
  30      return $this;
  31    }
  32  
  33    public function withDepths($depths) {
  34      $this->depths = $depths;
  35      return $this;
  36    }
  37  
  38    public function needLatestVersion($need_latest_version) {
  39      $this->needLatestVersion = $need_latest_version;
  40      return $this;
  41    }
  42  
  43    public function loadPage() {
  44      $table = new PhragmentFragment();
  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 buildWhereClause($conn_r) {
  59      $where = array();
  60  
  61      if ($this->ids) {
  62        $where[] = qsprintf(
  63          $conn_r,
  64          'id IN (%Ld)',
  65          $this->ids);
  66      }
  67  
  68      if ($this->phids) {
  69        $where[] = qsprintf(
  70          $conn_r,
  71          'phid IN (%Ls)',
  72          $this->phids);
  73      }
  74  
  75      if ($this->paths) {
  76        $where[] = qsprintf(
  77          $conn_r,
  78          'path IN (%Ls)',
  79          $this->paths);
  80      }
  81  
  82      if ($this->leadingPath) {
  83        $where[] = qsprintf(
  84          $conn_r,
  85          'path LIKE %>',
  86          $this->leadingPath);
  87      }
  88  
  89      if ($this->depths) {
  90        $where[] = qsprintf(
  91          $conn_r,
  92          'depth IN (%Ld)',
  93          $this->depths);
  94      }
  95  
  96      $where[] = $this->buildPagingClause($conn_r);
  97  
  98      return $this->formatWhereClause($where);
  99    }
 100  
 101    protected function didFilterPage(array $page) {
 102      if ($this->needLatestVersion) {
 103        $versions = array();
 104  
 105        $version_phids = array_filter(mpull($page, 'getLatestVersionPHID'));
 106        if ($version_phids) {
 107          $versions = id(new PhabricatorObjectQuery())
 108            ->setViewer($this->getViewer())
 109            ->withPHIDs($version_phids)
 110            ->setParentQuery($this)
 111            ->execute();
 112          $versions = mpull($versions, null, 'getPHID');
 113        }
 114  
 115        foreach ($page as $key => $fragment) {
 116          $version_phid = $fragment->getLatestVersionPHID();
 117          if (empty($versions[$version_phid])) {
 118            continue;
 119          }
 120          $fragment->attachLatestVersion($versions[$version_phid]);
 121        }
 122      }
 123  
 124      return $page;
 125    }
 126  
 127    public function getQueryApplicationClass() {
 128      return 'PhabricatorPhragmentApplication';
 129    }
 130  }


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