Path: blob/master/src/applications/files/query/PhabricatorFileChunkQuery.php
12242 views
<?php12final class PhabricatorFileChunkQuery3extends PhabricatorCursorPagedPolicyAwareQuery {45private $chunkHandles;6private $rangeStart;7private $rangeEnd;8private $isComplete;9private $needDataFiles;1011public function withChunkHandles(array $handles) {12$this->chunkHandles = $handles;13return $this;14}1516public function withByteRange($start, $end) {17$this->rangeStart = $start;18$this->rangeEnd = $end;19return $this;20}2122public function withIsComplete($complete) {23$this->isComplete = $complete;24return $this;25}2627public function needDataFiles($need) {28$this->needDataFiles = $need;29return $this;30}3132protected function loadPage() {33$table = new PhabricatorFileChunk();34$conn_r = $table->establishConnection('r');3536$data = queryfx_all(37$conn_r,38'SELECT * FROM %T %Q %Q %Q',39$table->getTableName(),40$this->buildWhereClause($conn_r),41$this->buildOrderClause($conn_r),42$this->buildLimitClause($conn_r));4344return $table->loadAllFromArray($data);45}4647protected function willFilterPage(array $chunks) {4849if ($this->needDataFiles) {50$file_phids = mpull($chunks, 'getDataFilePHID');51$file_phids = array_filter($file_phids);52if ($file_phids) {53$files = id(new PhabricatorFileQuery())54->setViewer($this->getViewer())55->setParentQuery($this)56->withPHIDs($file_phids)57->execute();58$files = mpull($files, null, 'getPHID');59} else {60$files = array();61}6263foreach ($chunks as $key => $chunk) {64$data_phid = $chunk->getDataFilePHID();65if (!$data_phid) {66$chunk->attachDataFile(null);67continue;68}6970$file = idx($files, $data_phid);71if (!$file) {72unset($chunks[$key]);73$this->didRejectResult($chunk);74continue;75}7677$chunk->attachDataFile($file);78}7980if (!$chunks) {81return $chunks;82}83}8485return $chunks;86}8788protected function buildWhereClause(AphrontDatabaseConnection $conn) {89$where = array();9091if ($this->chunkHandles !== null) {92$where[] = qsprintf(93$conn,94'chunkHandle IN (%Ls)',95$this->chunkHandles);96}9798if ($this->rangeStart !== null) {99$where[] = qsprintf(100$conn,101'byteEnd > %d',102$this->rangeStart);103}104105if ($this->rangeEnd !== null) {106$where[] = qsprintf(107$conn,108'byteStart < %d',109$this->rangeEnd);110}111112if ($this->isComplete !== null) {113if ($this->isComplete) {114$where[] = qsprintf(115$conn,116'dataFilePHID IS NOT NULL');117} else {118$where[] = qsprintf(119$conn,120'dataFilePHID IS NULL');121}122}123124$where[] = $this->buildPagingClause($conn);125126return $this->formatWhereClause($conn, $where);127}128129public function getQueryApplicationClass() {130return 'PhabricatorFilesApplication';131}132133}134135136