[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/feed/query/ -> PhabricatorFeedQuery.php (source)

   1  <?php
   2  
   3  final class PhabricatorFeedQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    private $filterPHIDs;
   7    private $chronologicalKeys;
   8  
   9    public function setFilterPHIDs(array $phids) {
  10      $this->filterPHIDs = $phids;
  11      return $this;
  12    }
  13  
  14    public function withChronologicalKeys(array $keys) {
  15      $this->chronologicalKeys = $keys;
  16      return $this;
  17    }
  18  
  19    protected function loadPage() {
  20      $story_table = new PhabricatorFeedStoryData();
  21      $conn = $story_table->establishConnection('r');
  22  
  23      $data = queryfx_all(
  24        $conn,
  25        'SELECT story.* FROM %T story %Q %Q %Q %Q %Q',
  26        $story_table->getTableName(),
  27        $this->buildJoinClause($conn),
  28        $this->buildWhereClause($conn),
  29        $this->buildGroupClause($conn),
  30        $this->buildOrderClause($conn),
  31        $this->buildLimitClause($conn));
  32  
  33      return $data;
  34    }
  35  
  36    protected function willFilterPage(array $data) {
  37      return PhabricatorFeedStory::loadAllFromRows($data, $this->getViewer());
  38    }
  39  
  40    private function buildJoinClause(AphrontDatabaseConnection $conn_r) {
  41      // NOTE: We perform this join unconditionally (even if we have no filter
  42      // PHIDs) to omit rows which have no story references. These story data
  43      // rows are notifications or realtime alerts.
  44  
  45      $ref_table = new PhabricatorFeedStoryReference();
  46      return qsprintf(
  47        $conn_r,
  48        'JOIN %T ref ON ref.chronologicalKey = story.chronologicalKey',
  49        $ref_table->getTableName());
  50    }
  51  
  52    private function buildWhereClause(AphrontDatabaseConnection $conn_r) {
  53      $where = array();
  54  
  55      if ($this->filterPHIDs) {
  56        $where[] = qsprintf(
  57          $conn_r,
  58          'ref.objectPHID IN (%Ls)',
  59          $this->filterPHIDs);
  60      }
  61  
  62      if ($this->chronologicalKeys) {
  63        // NOTE: We want to use integers in the query so we can take advantage
  64        // of keys, but can't use %d on 32-bit systems. Make sure all the keys
  65        // are integers and then format them raw.
  66  
  67        $keys = $this->chronologicalKeys;
  68        foreach ($keys as $key) {
  69          if (!ctype_digit($key)) {
  70            throw new Exception("Key '{$key}' is not a valid chronological key!");
  71          }
  72        }
  73  
  74        $where[] = qsprintf(
  75          $conn_r,
  76          'ref.chronologicalKey IN (%Q)',
  77          implode(', ', $keys));
  78      }
  79  
  80      $where[] = $this->buildPagingClause($conn_r);
  81  
  82      return $this->formatWhereClause($where);
  83    }
  84  
  85    private function buildGroupClause(AphrontDatabaseConnection $conn_r) {
  86      return qsprintf(
  87        $conn_r,
  88        'GROUP BY '.($this->filterPHIDs
  89          ? 'ref.chronologicalKey'
  90          : 'story.chronologicalKey'));
  91    }
  92  
  93    protected function getPagingColumn() {
  94      return ($this->filterPHIDs
  95        ? 'ref.chronologicalKey'
  96        : 'story.chronologicalKey');
  97    }
  98  
  99    protected function getPagingValue($item) {
 100      if ($item instanceof PhabricatorFeedStory) {
 101        return $item->getChronologicalKey();
 102      }
 103      return $item['chronologicalKey'];
 104    }
 105  
 106    public function getQueryApplicationClass() {
 107      return 'PhabricatorFeedApplication';
 108    }
 109  
 110  }


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