Remove product literal strings in "pht()", part 5
[phabricator.git] / src / applications / files / query / PhabricatorFileChunkQuery.php
blob43988605696780ce989241cca20ec29c5c08ddce
1 <?php
3 final class PhabricatorFileChunkQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $chunkHandles;
7 private $rangeStart;
8 private $rangeEnd;
9 private $isComplete;
10 private $needDataFiles;
12 public function withChunkHandles(array $handles) {
13 $this->chunkHandles = $handles;
14 return $this;
17 public function withByteRange($start, $end) {
18 $this->rangeStart = $start;
19 $this->rangeEnd = $end;
20 return $this;
23 public function withIsComplete($complete) {
24 $this->isComplete = $complete;
25 return $this;
28 public function needDataFiles($need) {
29 $this->needDataFiles = $need;
30 return $this;
33 protected function loadPage() {
34 $table = new PhabricatorFileChunk();
35 $conn_r = $table->establishConnection('r');
37 $data = queryfx_all(
38 $conn_r,
39 'SELECT * FROM %T %Q %Q %Q',
40 $table->getTableName(),
41 $this->buildWhereClause($conn_r),
42 $this->buildOrderClause($conn_r),
43 $this->buildLimitClause($conn_r));
45 return $table->loadAllFromArray($data);
48 protected function willFilterPage(array $chunks) {
50 if ($this->needDataFiles) {
51 $file_phids = mpull($chunks, 'getDataFilePHID');
52 $file_phids = array_filter($file_phids);
53 if ($file_phids) {
54 $files = id(new PhabricatorFileQuery())
55 ->setViewer($this->getViewer())
56 ->setParentQuery($this)
57 ->withPHIDs($file_phids)
58 ->execute();
59 $files = mpull($files, null, 'getPHID');
60 } else {
61 $files = array();
64 foreach ($chunks as $key => $chunk) {
65 $data_phid = $chunk->getDataFilePHID();
66 if (!$data_phid) {
67 $chunk->attachDataFile(null);
68 continue;
71 $file = idx($files, $data_phid);
72 if (!$file) {
73 unset($chunks[$key]);
74 $this->didRejectResult($chunk);
75 continue;
78 $chunk->attachDataFile($file);
81 if (!$chunks) {
82 return $chunks;
86 return $chunks;
89 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
90 $where = array();
92 if ($this->chunkHandles !== null) {
93 $where[] = qsprintf(
94 $conn,
95 'chunkHandle IN (%Ls)',
96 $this->chunkHandles);
99 if ($this->rangeStart !== null) {
100 $where[] = qsprintf(
101 $conn,
102 'byteEnd > %d',
103 $this->rangeStart);
106 if ($this->rangeEnd !== null) {
107 $where[] = qsprintf(
108 $conn,
109 'byteStart < %d',
110 $this->rangeEnd);
113 if ($this->isComplete !== null) {
114 if ($this->isComplete) {
115 $where[] = qsprintf(
116 $conn,
117 'dataFilePHID IS NOT NULL');
118 } else {
119 $where[] = qsprintf(
120 $conn,
121 'dataFilePHID IS NULL');
125 $where[] = $this->buildPagingClause($conn);
127 return $this->formatWhereClause($conn, $where);
130 public function getQueryApplicationClass() {
131 return 'PhabricatorFilesApplication';