Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/document/PhabricatorDocumentRef.php
12241 views
1
<?php
2
3
final class PhabricatorDocumentRef
4
extends Phobject {
5
6
private $name;
7
private $mimeType;
8
private $file;
9
private $byteLength;
10
private $snippet;
11
private $symbolMetadata = array();
12
private $blameURI;
13
private $coverage = array();
14
private $data;
15
16
public function setFile(PhabricatorFile $file) {
17
$this->file = $file;
18
return $this;
19
}
20
21
public function getFile() {
22
return $this->file;
23
}
24
25
public function setMimeType($mime_type) {
26
$this->mimeType = $mime_type;
27
return $this;
28
}
29
30
public function getMimeType() {
31
if ($this->mimeType !== null) {
32
return $this->mimeType;
33
}
34
35
if ($this->file) {
36
return $this->file->getMimeType();
37
}
38
39
return null;
40
}
41
42
public function setName($name) {
43
$this->name = $name;
44
return $this;
45
}
46
47
public function getName() {
48
if ($this->name !== null) {
49
return $this->name;
50
}
51
52
if ($this->file) {
53
return $this->file->getName();
54
}
55
56
return null;
57
}
58
59
public function setByteLength($length) {
60
$this->byteLength = $length;
61
return $this;
62
}
63
64
public function getByteLength() {
65
if ($this->byteLength !== null) {
66
return $this->byteLength;
67
}
68
69
if ($this->data !== null) {
70
return strlen($this->data);
71
}
72
73
if ($this->file) {
74
return (int)$this->file->getByteSize();
75
}
76
77
return null;
78
}
79
80
public function setData($data) {
81
$this->data = $data;
82
return $this;
83
}
84
85
public function loadData($begin = null, $end = null) {
86
if ($this->data !== null) {
87
$data = $this->data;
88
89
if ($begin !== null && $end !== null) {
90
$data = substr($data, $begin, $end - $begin);
91
} else if ($begin !== null) {
92
$data = substr($data, $begin);
93
} else if ($end !== null) {
94
$data = substr($data, 0, $end);
95
}
96
97
return $data;
98
}
99
100
if ($this->file) {
101
$iterator = $this->file->getFileDataIterator($begin, $end);
102
103
$result = '';
104
foreach ($iterator as $chunk) {
105
$result .= $chunk;
106
}
107
return $result;
108
}
109
110
throw new PhutilMethodNotImplementedException();
111
}
112
113
public function hasAnyMimeType(array $candidate_types) {
114
$mime_full = $this->getMimeType();
115
116
if (!phutil_nonempty_string($mime_full)) {
117
return false;
118
}
119
120
$mime_parts = explode(';', $mime_full);
121
122
$mime_type = head($mime_parts);
123
$mime_type = $this->normalizeMimeType($mime_type);
124
125
foreach ($candidate_types as $candidate_type) {
126
if ($this->normalizeMimeType($candidate_type) === $mime_type) {
127
return true;
128
}
129
}
130
131
return false;
132
}
133
134
private function normalizeMimeType($mime_type) {
135
$mime_type = trim($mime_type);
136
$mime_type = phutil_utf8_strtolower($mime_type);
137
return $mime_type;
138
}
139
140
public function isProbablyText() {
141
$snippet = $this->getSnippet();
142
return (strpos($snippet, "\0") === false);
143
}
144
145
public function isProbablyJSON() {
146
if (!$this->isProbablyText()) {
147
return false;
148
}
149
150
$snippet = $this->getSnippet();
151
152
// If the file is longer than the snippet, we don't detect the content
153
// as JSON. We could use some kind of heuristic here if we wanted, but
154
// see PHI749 for a false positive.
155
if (strlen($snippet) < $this->getByteLength()) {
156
return false;
157
}
158
159
// If the snippet is the whole file, just check if the snippet is valid
160
// JSON. Note that `phutil_json_decode()` only accepts arrays and objects
161
// as JSON, so this won't misfire on files with content like "3".
162
try {
163
phutil_json_decode($snippet);
164
return true;
165
} catch (Exception $ex) {
166
return false;
167
}
168
}
169
170
public function getSnippet() {
171
if ($this->snippet === null) {
172
$this->snippet = $this->loadData(null, (1024 * 1024 * 1));
173
}
174
175
return $this->snippet;
176
}
177
178
public function setSymbolMetadata(array $metadata) {
179
$this->symbolMetadata = $metadata;
180
return $this;
181
}
182
183
public function getSymbolMetadata() {
184
return $this->symbolMetadata;
185
}
186
187
public function setBlameURI($blame_uri) {
188
$this->blameURI = $blame_uri;
189
return $this;
190
}
191
192
public function getBlameURI() {
193
return $this->blameURI;
194
}
195
196
public function addCoverage($coverage) {
197
$this->coverage[] = array(
198
'data' => $coverage,
199
);
200
return $this;
201
}
202
203
public function getCoverage() {
204
return $this->coverage;
205
}
206
207
}
208
209