[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/pholio/query/ -> PholioImageQuery.php (source)

   1  <?php
   2  
   3  final class PholioImageQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $ids;
   7    private $phids;
   8    private $mockIDs;
   9    private $obsolete;
  10  
  11    private $needInlineComments;
  12    private $mockCache = array();
  13  
  14    public function withIDs(array $ids) {
  15      $this->ids = $ids;
  16      return $this;
  17    }
  18  
  19    public function withPHIDs(array $phids) {
  20      $this->phids = $phids;
  21      return $this;
  22    }
  23  
  24    public function withMockIDs(array $mock_ids) {
  25      $this->mockIDs = $mock_ids;
  26      return $this;
  27    }
  28  
  29    public function withObsolete($obsolete) {
  30      $this->obsolete = $obsolete;
  31      return $this;
  32    }
  33  
  34    public function needInlineComments($need_inline_comments) {
  35      $this->needInlineComments = $need_inline_comments;
  36      return $this;
  37    }
  38  
  39    public function setMockCache($mock_cache) {
  40      $this->mockCache = $mock_cache;
  41      return $this;
  42    }
  43    public function getMockCache() {
  44      return $this->mockCache;
  45    }
  46  
  47    protected function loadPage() {
  48      $table = new PholioImage();
  49      $conn_r = $table->establishConnection('r');
  50  
  51      $data = queryfx_all(
  52        $conn_r,
  53        'SELECT * FROM %T %Q %Q %Q',
  54        $table->getTableName(),
  55        $this->buildWhereClause($conn_r),
  56        $this->buildOrderClause($conn_r),
  57        $this->buildLimitClause($conn_r));
  58  
  59      $images = $table->loadAllFromArray($data);
  60  
  61      return $images;
  62    }
  63  
  64    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
  65      $where = array();
  66  
  67      $where[] = $this->buildPagingClause($conn_r);
  68  
  69      if ($this->ids) {
  70        $where[] = qsprintf(
  71          $conn_r,
  72          'id IN (%Ld)',
  73          $this->ids);
  74      }
  75  
  76      if ($this->phids) {
  77        $where[] = qsprintf(
  78          $conn_r,
  79          'phid IN (%Ls)',
  80          $this->phids);
  81      }
  82  
  83      if ($this->mockIDs) {
  84        $where[] = qsprintf(
  85          $conn_r,
  86          'mockID IN (%Ld)',
  87          $this->mockIDs);
  88      }
  89  
  90      if ($this->obsolete !== null) {
  91        $where[] = qsprintf(
  92          $conn_r,
  93          'isObsolete = %d',
  94          $this->obsolete);
  95      }
  96  
  97      return $this->formatWhereClause($where);
  98    }
  99  
 100    protected function willFilterPage(array $images) {
 101      assert_instances_of($images, 'PholioImage');
 102  
 103      if ($this->getMockCache()) {
 104        $mocks = $this->getMockCache();
 105      } else {
 106        $mock_ids = mpull($images, 'getMockID');
 107        // DO NOT set needImages to true; recursion results!
 108        $mocks = id(new PholioMockQuery())
 109          ->setViewer($this->getViewer())
 110          ->withIDs($mock_ids)
 111          ->execute();
 112        $mocks = mpull($mocks, null, 'getID');
 113      }
 114      foreach ($images as $index => $image) {
 115        $mock = idx($mocks, $image->getMockID());
 116        if ($mock) {
 117          $image->attachMock($mock);
 118        } else {
 119          // mock is missing or we can't see it
 120          unset($images[$index]);
 121        }
 122      }
 123  
 124      return $images;
 125    }
 126  
 127    protected function didFilterPage(array $images) {
 128      assert_instances_of($images, 'PholioImage');
 129  
 130      $file_phids = mpull($images, 'getFilePHID');
 131  
 132      $all_files = id(new PhabricatorFileQuery())
 133        ->setParentQuery($this)
 134        ->setViewer($this->getViewer())
 135        ->withPHIDs($file_phids)
 136        ->execute();
 137      $all_files = mpull($all_files, null, 'getPHID');
 138  
 139      if ($this->needInlineComments) {
 140        $all_inline_comments = id(new PholioTransactionComment())
 141          ->loadAllWhere('imageid IN (%Ld)',
 142            mpull($images, 'getID'));
 143        $all_inline_comments = mgroup($all_inline_comments, 'getImageID');
 144      }
 145  
 146      foreach ($images as $image) {
 147        $file = idx($all_files, $image->getFilePHID());
 148        if (!$file) {
 149          $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png');
 150        }
 151        $image->attachFile($file);
 152        if ($this->needInlineComments) {
 153          $inlines = idx($all_inline_comments, $image->getID(), array());
 154          $image->attachInlineComments($inlines);
 155        }
 156      }
 157  
 158      return $images;
 159    }
 160  
 161    public function getQueryApplicationClass() {
 162      return 'PhabricatorPholioApplication';
 163    }
 164  
 165  }


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