[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/applications/conpherence/query/ -> ConpherenceParticipantQuery.php (source)

   1  <?php
   2  
   3  /**
   4   * Query class that answers these questions:
   5   *
   6   * - Q: What are the conpherences to show when I land on /conpherence/ ?
   7   * - A:
   8   *
   9   *     id(new ConpherenceParticipantQuery())
  10   *     ->withParticipantPHIDs(array($my_phid))
  11   *     ->execute();
  12   *
  13   * - Q: What are the next set of conpherences as I scroll up (more recent) or
  14   *      down (less recent) this list of conpherences?
  15   * - A:
  16   *
  17   *     id(new ConpherenceParticipantQuery())
  18   *     ->withParticipantPHIDs(array($my_phid))
  19   *     ->withParticipantCursor($top_participant)
  20   *     ->setOrder(ConpherenceParticipantQuery::ORDER_NEWER)
  21   *     ->execute();
  22   *
  23   *     -or-
  24   *
  25   *     id(new ConpherenceParticipantQuery())
  26   *     ->withParticipantPHIDs(array($my_phid))
  27   *     ->withParticipantCursor($bottom_participant)
  28   *     ->setOrder(ConpherenceParticipantQuery::ORDER_OLDER)
  29   *     ->execute();
  30   *
  31   * For counts of read, un-read, or all conpherences by participant, see
  32   * @{class:ConpherenceParticipantCountQuery}.
  33   */
  34  final class ConpherenceParticipantQuery extends PhabricatorOffsetPagedQuery {
  35  
  36    const LIMIT = 100;
  37    const ORDER_NEWER = 'newer';
  38    const ORDER_OLDER = 'older';
  39  
  40    private $participantPHIDs;
  41    private $participantCursor;
  42    private $order = self::ORDER_OLDER;
  43  
  44    public function withParticipantPHIDs(array $phids) {
  45      $this->participantPHIDs = $phids;
  46      return $this;
  47    }
  48  
  49    public function withParticipantCursor(ConpherenceParticipant $participant) {
  50      $this->participantCursor = $participant;
  51      return $this;
  52    }
  53  
  54    public function setOrder($order) {
  55      $this->order = $order;
  56      return $this;
  57    }
  58  
  59    public function execute() {
  60      $table = new ConpherenceParticipant();
  61      $conn_r = $table->establishConnection('r');
  62  
  63      $data = queryfx_all(
  64        $conn_r,
  65        'SELECT * FROM %T participant %Q %Q %Q',
  66        $table->getTableName(),
  67        $this->buildWhereClause($conn_r),
  68        $this->buildOrderClause($conn_r),
  69        $this->buildLimitClause($conn_r));
  70  
  71      $participants = $table->loadAllFromArray($data);
  72  
  73      $participants = mpull($participants, null, 'getConpherencePHID');
  74  
  75      if ($this->order == self::ORDER_NEWER) {
  76        $participants = array_reverse($participants);
  77      }
  78  
  79      return $participants;
  80    }
  81  
  82    private function buildWhereClause($conn_r) {
  83      $where = array();
  84  
  85      if ($this->participantPHIDs) {
  86        $where[] = qsprintf(
  87          $conn_r,
  88          'participantPHID IN (%Ls)',
  89          $this->participantPHIDs);
  90      }
  91  
  92      if ($this->participantCursor) {
  93        $date_touched = $this->participantCursor->getDateTouched();
  94        $id = $this->participantCursor->getID();
  95        if ($this->order == self::ORDER_OLDER) {
  96          $compare_date = '<';
  97          $compare_id = '<=';
  98        } else {
  99          $compare_date = '>';
 100          $compare_id = '>=';
 101        }
 102        $where[] = qsprintf(
 103          $conn_r,
 104          '(dateTouched %Q %d OR (dateTouched = %d AND id %Q %d))',
 105          $compare_date,
 106          $date_touched,
 107          $date_touched,
 108          $compare_id,
 109          $id);
 110      }
 111  
 112      return $this->formatWhereClause($where);
 113    }
 114  
 115    private function buildOrderClause(AphrontDatabaseConnection $conn_r) {
 116      $order_word = ($this->order == self::ORDER_OLDER) ? 'DESC' : 'ASC';
 117      // if these are different direction we won't get as efficient a query
 118      // see http://dev.mysql.com/doc/refman/5.5/en/order-by-optimization.html
 119      $order = qsprintf(
 120        $conn_r,
 121        'ORDER BY dateTouched %Q, id %Q',
 122        $order_word,
 123        $order_word);
 124  
 125      return $order;
 126    }
 127  
 128  }


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