[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  final class ConpherenceThreadQuery
   4    extends PhabricatorCursorPagedPolicyAwareQuery {
   5  
   6    const TRANSACTION_LIMIT = 100;
   7  
   8    private $phids;
   9    private $ids;
  10    private $needWidgetData;
  11    private $needTransactions;
  12    private $needParticipantCache;
  13    private $needFilePHIDs;
  14    private $afterTransactionID;
  15    private $beforeTransactionID;
  16    private $transactionLimit;
  17  
  18    public function needFilePHIDs($need_file_phids) {
  19      $this->needFilePHIDs = $need_file_phids;
  20      return $this;
  21    }
  22  
  23    public function needParticipantCache($participant_cache) {
  24      $this->needParticipantCache = $participant_cache;
  25      return $this;
  26    }
  27  
  28    public function needWidgetData($need_widget_data) {
  29      $this->needWidgetData = $need_widget_data;
  30      return $this;
  31    }
  32  
  33    public function needTransactions($need_transactions) {
  34      $this->needTransactions = $need_transactions;
  35      return $this;
  36    }
  37  
  38    public function withIDs(array $ids) {
  39      $this->ids = $ids;
  40      return $this;
  41    }
  42  
  43    public function withPHIDs(array $phids) {
  44      $this->phids = $phids;
  45      return $this;
  46    }
  47  
  48    public function setAfterTransactionID($id) {
  49      $this->afterTransactionID = $id;
  50      return $this;
  51    }
  52  
  53    public function setBeforeTransactionID($id) {
  54      $this->beforeTransactionID = $id;
  55      return $this;
  56    }
  57  
  58    public function setTransactionLimit($transaction_limit) {
  59      $this->transactionLimit = $transaction_limit;
  60      return $this;
  61    }
  62  
  63    public function getTransactionLimit() {
  64      return $this->transactionLimit;
  65    }
  66  
  67    protected function loadPage() {
  68      $table = new ConpherenceThread();
  69      $conn_r = $table->establishConnection('r');
  70  
  71      $data = queryfx_all(
  72        $conn_r,
  73        'SELECT conpherence_thread.* FROM %T conpherence_thread %Q %Q %Q',
  74        $table->getTableName(),
  75        $this->buildWhereClause($conn_r),
  76        $this->buildOrderClause($conn_r),
  77        $this->buildLimitClause($conn_r));
  78  
  79      $conpherences = $table->loadAllFromArray($data);
  80  
  81      if ($conpherences) {
  82        $conpherences = mpull($conpherences, null, 'getPHID');
  83        $this->loadParticipantsAndInitHandles($conpherences);
  84        if ($this->needParticipantCache) {
  85          $this->loadCoreHandles($conpherences, 'getRecentParticipantPHIDs');
  86        } else if ($this->needWidgetData) {
  87          $this->loadCoreHandles($conpherences, 'getParticipantPHIDs');
  88        }
  89        if ($this->needTransactions) {
  90          $this->loadTransactionsAndHandles($conpherences);
  91        }
  92        if ($this->needFilePHIDs || $this->needWidgetData) {
  93          $this->loadFilePHIDs($conpherences);
  94        }
  95        if ($this->needWidgetData) {
  96          $this->loadWidgetData($conpherences);
  97        }
  98      }
  99  
 100      return $conpherences;
 101    }
 102  
 103    protected function buildWhereClause($conn_r) {
 104      $where = array();
 105  
 106      $where[] = $this->buildPagingClause($conn_r);
 107  
 108      if ($this->ids) {
 109        $where[] = qsprintf(
 110          $conn_r,
 111          'id IN (%Ld)',
 112          $this->ids);
 113      }
 114  
 115      if ($this->phids) {
 116        $where[] = qsprintf(
 117          $conn_r,
 118          'phid IN (%Ls)',
 119          $this->phids);
 120      }
 121  
 122      return $this->formatWhereClause($where);
 123    }
 124  
 125    private function loadParticipantsAndInitHandles(array $conpherences) {
 126      $participants = id(new ConpherenceParticipant())
 127        ->loadAllWhere('conpherencePHID IN (%Ls)', array_keys($conpherences));
 128      $map = mgroup($participants, 'getConpherencePHID');
 129  
 130      foreach ($conpherences as $current_conpherence) {
 131        $conpherence_phid = $current_conpherence->getPHID();
 132  
 133        $conpherence_participants = idx(
 134          $map,
 135          $conpherence_phid,
 136          array());
 137  
 138        $conpherence_participants = mpull(
 139          $conpherence_participants,
 140          null,
 141          'getParticipantPHID');
 142  
 143        $current_conpherence->attachParticipants($conpherence_participants);
 144        $current_conpherence->attachHandles(array());
 145      }
 146  
 147      return $this;
 148    }
 149  
 150    private function loadCoreHandles(
 151      array $conpherences,
 152      $method) {
 153  
 154      $handle_phids = array();
 155      foreach ($conpherences as $conpherence) {
 156        $handle_phids[$conpherence->getPHID()] =
 157          $conpherence->$method();
 158      }
 159      $flat_phids = array_mergev($handle_phids);
 160      $handles = id(new PhabricatorHandleQuery())
 161        ->setViewer($this->getViewer())
 162        ->withPHIDs($flat_phids)
 163        ->execute();
 164      foreach ($handle_phids as $conpherence_phid => $phids) {
 165        $conpherence = $conpherences[$conpherence_phid];
 166        $conpherence->attachHandles(array_select_keys($handles, $phids));
 167      }
 168      return $this;
 169    }
 170  
 171    private function loadTransactionsAndHandles(array $conpherences) {
 172      $query = id(new ConpherenceTransactionQuery())
 173        ->setViewer($this->getViewer())
 174        ->withObjectPHIDs(array_keys($conpherences))
 175        ->needHandles(true);
 176  
 177      // We have to flip these for the underyling query class. The semantics of
 178      // paging are tricky business.
 179      if ($this->afterTransactionID) {
 180        $query->setBeforeID($this->afterTransactionID);
 181      } else if ($this->beforeTransactionID) {
 182        $query->setAfterID($this->beforeTransactionID);
 183      }
 184      if ($this->getTransactionLimit()) {
 185        // fetch an extra for "show older" scenarios
 186        $query->setLimit($this->getTransactionLimit() + 1);
 187      }
 188      $transactions = $query->execute();
 189      $transactions = mgroup($transactions, 'getObjectPHID');
 190      foreach ($conpherences as $phid => $conpherence) {
 191        $current_transactions = idx($transactions, $phid, array());
 192        $handles = array();
 193        foreach ($current_transactions as $transaction) {
 194          $handles += $transaction->getHandles();
 195        }
 196        $conpherence->attachHandles($conpherence->getHandles() + $handles);
 197        $conpherence->attachTransactions($current_transactions);
 198      }
 199      return $this;
 200    }
 201  
 202    private function loadFilePHIDs(array $conpherences) {
 203      $edge_type = PhabricatorEdgeConfig::TYPE_OBJECT_HAS_FILE;
 204      $file_edges = id(new PhabricatorEdgeQuery())
 205        ->withSourcePHIDs(array_keys($conpherences))
 206        ->withEdgeTypes(array($edge_type))
 207        ->execute();
 208      foreach ($file_edges as $conpherence_phid => $data) {
 209        $conpherence = $conpherences[$conpherence_phid];
 210        $conpherence->attachFilePHIDs(array_keys($data[$edge_type]));
 211      }
 212      return $this;
 213    }
 214  
 215    private function loadWidgetData(array $conpherences) {
 216      $participant_phids = array();
 217      $file_phids = array();
 218      foreach ($conpherences as $conpherence) {
 219        $participant_phids[] = array_keys($conpherence->getParticipants());
 220        $file_phids[] = $conpherence->getFilePHIDs();
 221      }
 222      $participant_phids = array_mergev($participant_phids);
 223      $file_phids = array_mergev($file_phids);
 224  
 225      $epochs = CalendarTimeUtil::getCalendarEventEpochs(
 226        $this->getViewer());
 227      $start_epoch = $epochs['start_epoch'];
 228      $end_epoch = $epochs['end_epoch'];
 229      $statuses = id(new PhabricatorCalendarEventQuery())
 230        ->setViewer($this->getViewer())
 231        ->withInvitedPHIDs($participant_phids)
 232        ->withDateRange($start_epoch, $end_epoch)
 233        ->execute();
 234  
 235      $statuses = mgroup($statuses, 'getUserPHID');
 236  
 237      // attached files
 238      $files = array();
 239      $file_author_phids = array();
 240      $authors = array();
 241      if ($file_phids) {
 242        $files = id(new PhabricatorFileQuery())
 243          ->setViewer($this->getViewer())
 244          ->withPHIDs($file_phids)
 245          ->execute();
 246        $files = mpull($files, null, 'getPHID');
 247        $file_author_phids = mpull($files, 'getAuthorPHID', 'getPHID');
 248        $authors = id(new PhabricatorHandleQuery())
 249          ->setViewer($this->getViewer())
 250          ->withPHIDs($file_author_phids)
 251          ->execute();
 252        $authors = mpull($authors, null, 'getPHID');
 253      }
 254  
 255      foreach ($conpherences as $phid => $conpherence) {
 256        $participant_phids = array_keys($conpherence->getParticipants());
 257        $statuses = array_select_keys($statuses, $participant_phids);
 258        $statuses = array_mergev($statuses);
 259        $statuses = msort($statuses, 'getDateFrom');
 260  
 261        $conpherence_files = array();
 262        $files_authors = array();
 263        foreach ($conpherence->getFilePHIDs() as $curr_phid) {
 264          $curr_file = idx($files, $curr_phid);
 265          if (!$curr_file) {
 266            // this file was deleted or user doesn't have permission to see it
 267            // this is generally weird
 268            continue;
 269          }
 270          $conpherence_files[$curr_phid] = $curr_file;
 271          // some files don't have authors so be careful
 272          $current_author = null;
 273          $current_author_phid = idx($file_author_phids, $curr_phid);
 274          if ($current_author_phid) {
 275            $current_author = $authors[$current_author_phid];
 276          }
 277          $files_authors[$curr_phid] = $current_author;
 278        }
 279        $widget_data = array(
 280          'statuses' => $statuses,
 281          'files' => $conpherence_files,
 282          'files_authors' => $files_authors,
 283        );
 284        $conpherence->attachWidgetData($widget_data);
 285      }
 286  
 287      return $this;
 288    }
 289  
 290    public function getQueryApplicationClass() {
 291      return 'PhabricatorConpherenceApplication';
 292    }
 293  
 294  }


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