Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/query/PhabricatorFileChunkQuery.php
12242 views
1
<?php
2
3
final class PhabricatorFileChunkQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $chunkHandles;
7
private $rangeStart;
8
private $rangeEnd;
9
private $isComplete;
10
private $needDataFiles;
11
12
public function withChunkHandles(array $handles) {
13
$this->chunkHandles = $handles;
14
return $this;
15
}
16
17
public function withByteRange($start, $end) {
18
$this->rangeStart = $start;
19
$this->rangeEnd = $end;
20
return $this;
21
}
22
23
public function withIsComplete($complete) {
24
$this->isComplete = $complete;
25
return $this;
26
}
27
28
public function needDataFiles($need) {
29
$this->needDataFiles = $need;
30
return $this;
31
}
32
33
protected function loadPage() {
34
$table = new PhabricatorFileChunk();
35
$conn_r = $table->establishConnection('r');
36
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));
44
45
return $table->loadAllFromArray($data);
46
}
47
48
protected function willFilterPage(array $chunks) {
49
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();
62
}
63
64
foreach ($chunks as $key => $chunk) {
65
$data_phid = $chunk->getDataFilePHID();
66
if (!$data_phid) {
67
$chunk->attachDataFile(null);
68
continue;
69
}
70
71
$file = idx($files, $data_phid);
72
if (!$file) {
73
unset($chunks[$key]);
74
$this->didRejectResult($chunk);
75
continue;
76
}
77
78
$chunk->attachDataFile($file);
79
}
80
81
if (!$chunks) {
82
return $chunks;
83
}
84
}
85
86
return $chunks;
87
}
88
89
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
90
$where = array();
91
92
if ($this->chunkHandles !== null) {
93
$where[] = qsprintf(
94
$conn,
95
'chunkHandle IN (%Ls)',
96
$this->chunkHandles);
97
}
98
99
if ($this->rangeStart !== null) {
100
$where[] = qsprintf(
101
$conn,
102
'byteEnd > %d',
103
$this->rangeStart);
104
}
105
106
if ($this->rangeEnd !== null) {
107
$where[] = qsprintf(
108
$conn,
109
'byteStart < %d',
110
$this->rangeEnd);
111
}
112
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');
122
}
123
}
124
125
$where[] = $this->buildPagingClause($conn);
126
127
return $this->formatWhereClause($conn, $where);
128
}
129
130
public function getQueryApplicationClass() {
131
return 'PhabricatorFilesApplication';
132
}
133
134
}
135
136