Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/paste/query/PhabricatorPasteQuery.php
12241 views
1
<?php
2
3
final class PhabricatorPasteQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $ids;
7
private $phids;
8
private $authorPHIDs;
9
private $parentPHIDs;
10
11
private $needContent;
12
private $needRawContent;
13
private $needSnippets;
14
private $languages;
15
private $includeNoLanguage;
16
private $dateCreatedAfter;
17
private $dateCreatedBefore;
18
private $statuses;
19
20
21
public function withIDs(array $ids) {
22
$this->ids = $ids;
23
return $this;
24
}
25
26
public function withPHIDs(array $phids) {
27
$this->phids = $phids;
28
return $this;
29
}
30
31
public function withAuthorPHIDs(array $phids) {
32
$this->authorPHIDs = $phids;
33
return $this;
34
}
35
36
public function withParentPHIDs(array $phids) {
37
$this->parentPHIDs = $phids;
38
return $this;
39
}
40
41
public function needContent($need_content) {
42
$this->needContent = $need_content;
43
return $this;
44
}
45
46
public function needRawContent($need_raw_content) {
47
$this->needRawContent = $need_raw_content;
48
return $this;
49
}
50
51
public function needSnippets($need_snippets) {
52
$this->needSnippets = $need_snippets;
53
return $this;
54
}
55
56
public function withLanguages(array $languages) {
57
$this->includeNoLanguage = false;
58
foreach ($languages as $key => $language) {
59
if ($language === null) {
60
$languages[$key] = '';
61
continue;
62
}
63
}
64
$this->languages = $languages;
65
return $this;
66
}
67
68
public function withDateCreatedBefore($date_created_before) {
69
$this->dateCreatedBefore = $date_created_before;
70
return $this;
71
}
72
73
public function withDateCreatedAfter($date_created_after) {
74
$this->dateCreatedAfter = $date_created_after;
75
return $this;
76
}
77
78
public function withStatuses(array $statuses) {
79
$this->statuses = $statuses;
80
return $this;
81
}
82
83
public function newResultObject() {
84
return new PhabricatorPaste();
85
}
86
87
protected function didFilterPage(array $pastes) {
88
if ($this->needRawContent) {
89
$pastes = $this->loadRawContent($pastes);
90
}
91
92
if ($this->needContent) {
93
$pastes = $this->loadContent($pastes);
94
}
95
96
if ($this->needSnippets) {
97
$pastes = $this->loadSnippets($pastes);
98
}
99
100
return $pastes;
101
}
102
103
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
104
$where = parent::buildWhereClauseParts($conn);
105
106
if ($this->ids !== null) {
107
$where[] = qsprintf(
108
$conn,
109
'paste.id IN (%Ld)',
110
$this->ids);
111
}
112
113
if ($this->phids !== null) {
114
$where[] = qsprintf(
115
$conn,
116
'paste.phid IN (%Ls)',
117
$this->phids);
118
}
119
120
if ($this->authorPHIDs !== null) {
121
$where[] = qsprintf(
122
$conn,
123
'paste.authorPHID IN (%Ls)',
124
$this->authorPHIDs);
125
}
126
127
if ($this->parentPHIDs !== null) {
128
$where[] = qsprintf(
129
$conn,
130
'paste.parentPHID IN (%Ls)',
131
$this->parentPHIDs);
132
}
133
134
if ($this->languages !== null) {
135
$where[] = qsprintf(
136
$conn,
137
'paste.language IN (%Ls)',
138
$this->languages);
139
}
140
141
if ($this->dateCreatedAfter !== null) {
142
$where[] = qsprintf(
143
$conn,
144
'paste.dateCreated >= %d',
145
$this->dateCreatedAfter);
146
}
147
148
if ($this->dateCreatedBefore !== null) {
149
$where[] = qsprintf(
150
$conn,
151
'paste.dateCreated <= %d',
152
$this->dateCreatedBefore);
153
}
154
155
if ($this->statuses !== null) {
156
$where[] = qsprintf(
157
$conn,
158
'paste.status IN (%Ls)',
159
$this->statuses);
160
}
161
162
return $where;
163
}
164
165
protected function getPrimaryTableAlias() {
166
return 'paste';
167
}
168
169
private function getContentCacheKey(PhabricatorPaste $paste) {
170
return implode(
171
':',
172
array(
173
'P'.$paste->getID(),
174
$paste->getFilePHID(),
175
$paste->getLanguage(),
176
PhabricatorHash::digestForIndex($paste->getTitle()),
177
));
178
}
179
180
private function getSnippetCacheKey(PhabricatorPaste $paste) {
181
return implode(
182
':',
183
array(
184
'P'.$paste->getID(),
185
$paste->getFilePHID(),
186
$paste->getLanguage(),
187
'snippet',
188
'v2.1',
189
PhabricatorHash::digestForIndex($paste->getTitle()),
190
));
191
}
192
193
private function loadRawContent(array $pastes) {
194
$file_phids = mpull($pastes, 'getFilePHID');
195
$files = id(new PhabricatorFileQuery())
196
->setParentQuery($this)
197
->setViewer($this->getViewer())
198
->withPHIDs($file_phids)
199
->execute();
200
$files = mpull($files, null, 'getPHID');
201
202
foreach ($pastes as $key => $paste) {
203
$file = idx($files, $paste->getFilePHID());
204
if (!$file) {
205
unset($pastes[$key]);
206
continue;
207
}
208
try {
209
$paste->attachRawContent($file->loadFileData());
210
} catch (Exception $ex) {
211
// We can hit various sorts of file storage issues here. Just drop the
212
// paste if the file is dead.
213
unset($pastes[$key]);
214
continue;
215
}
216
}
217
218
return $pastes;
219
}
220
221
private function loadContent(array $pastes) {
222
$cache = new PhabricatorKeyValueDatabaseCache();
223
224
$cache = new PhutilKeyValueCacheProfiler($cache);
225
$cache->setProfiler(PhutilServiceProfiler::getInstance());
226
227
$keys = array();
228
foreach ($pastes as $paste) {
229
$keys[] = $this->getContentCacheKey($paste);
230
}
231
232
$caches = $cache->getKeys($keys);
233
234
$need_raw = array();
235
$have_cache = array();
236
foreach ($pastes as $paste) {
237
$key = $this->getContentCacheKey($paste);
238
if (isset($caches[$key])) {
239
$paste->attachContent(phutil_safe_html($caches[$key]));
240
$have_cache[$paste->getPHID()] = true;
241
} else {
242
$need_raw[$key] = $paste;
243
}
244
}
245
246
if (!$need_raw) {
247
return $pastes;
248
}
249
250
$write_data = array();
251
252
$have_raw = $this->loadRawContent($need_raw);
253
$have_raw = mpull($have_raw, null, 'getPHID');
254
foreach ($pastes as $key => $paste) {
255
$paste_phid = $paste->getPHID();
256
if (isset($have_cache[$paste_phid])) {
257
continue;
258
}
259
260
if (empty($have_raw[$paste_phid])) {
261
unset($pastes[$key]);
262
continue;
263
}
264
265
$content = $this->buildContent($paste);
266
$paste->attachContent($content);
267
$write_data[$this->getContentCacheKey($paste)] = (string)$content;
268
}
269
270
if ($write_data) {
271
$cache->setKeys($write_data);
272
}
273
274
return $pastes;
275
}
276
277
private function loadSnippets(array $pastes) {
278
$cache = new PhabricatorKeyValueDatabaseCache();
279
280
$cache = new PhutilKeyValueCacheProfiler($cache);
281
$cache->setProfiler(PhutilServiceProfiler::getInstance());
282
283
$keys = array();
284
foreach ($pastes as $paste) {
285
$keys[] = $this->getSnippetCacheKey($paste);
286
}
287
288
$caches = $cache->getKeys($keys);
289
290
$need_raw = array();
291
$have_cache = array();
292
foreach ($pastes as $paste) {
293
$key = $this->getSnippetCacheKey($paste);
294
if (isset($caches[$key])) {
295
$snippet_data = phutil_json_decode($caches[$key]);
296
$snippet = new PhabricatorPasteSnippet(
297
phutil_safe_html($snippet_data['content']),
298
$snippet_data['type'],
299
$snippet_data['contentLineCount']);
300
$paste->attachSnippet($snippet);
301
$have_cache[$paste->getPHID()] = true;
302
} else {
303
$need_raw[$key] = $paste;
304
}
305
}
306
307
if (!$need_raw) {
308
return $pastes;
309
}
310
311
$write_data = array();
312
313
$have_raw = $this->loadRawContent($need_raw);
314
$have_raw = mpull($have_raw, null, 'getPHID');
315
foreach ($pastes as $key => $paste) {
316
$paste_phid = $paste->getPHID();
317
if (isset($have_cache[$paste_phid])) {
318
continue;
319
}
320
321
if (empty($have_raw[$paste_phid])) {
322
unset($pastes[$key]);
323
continue;
324
}
325
326
$snippet = $this->buildSnippet($paste);
327
$paste->attachSnippet($snippet);
328
$snippet_data = array(
329
'content' => (string)$snippet->getContent(),
330
'type' => (string)$snippet->getType(),
331
'contentLineCount' => $snippet->getContentLineCount(),
332
);
333
$write_data[$this->getSnippetCacheKey($paste)] = phutil_json_encode(
334
$snippet_data);
335
}
336
337
if ($write_data) {
338
$cache->setKeys($write_data);
339
}
340
341
return $pastes;
342
}
343
344
private function buildContent(PhabricatorPaste $paste) {
345
return $this->highlightSource(
346
$paste->getRawContent(),
347
$paste->getTitle(),
348
$paste->getLanguage());
349
}
350
351
private function buildSnippet(PhabricatorPaste $paste) {
352
$snippet_type = PhabricatorPasteSnippet::FULL;
353
$snippet = $paste->getRawContent();
354
355
$lines = phutil_split_lines($snippet);
356
$line_count = count($lines);
357
358
if (strlen($snippet) > 1024) {
359
$snippet_type = PhabricatorPasteSnippet::FIRST_BYTES;
360
$snippet = id(new PhutilUTF8StringTruncator())
361
->setMaximumBytes(1024)
362
->setTerminator('')
363
->truncateString($snippet);
364
}
365
366
if ($line_count > 5) {
367
$snippet_type = PhabricatorPasteSnippet::FIRST_LINES;
368
$snippet = implode('', array_slice($lines, 0, 5));
369
}
370
371
return new PhabricatorPasteSnippet(
372
$this->highlightSource(
373
$snippet,
374
$paste->getTitle(),
375
$paste->getLanguage()),
376
$snippet_type,
377
$line_count);
378
}
379
380
private function highlightSource($source, $title, $language) {
381
if (empty($language)) {
382
return PhabricatorSyntaxHighlighter::highlightWithFilename(
383
$title,
384
$source);
385
} else {
386
return PhabricatorSyntaxHighlighter::highlightWithLanguage(
387
$language,
388
$source);
389
}
390
}
391
392
public function getQueryApplicationClass() {
393
return 'PhabricatorPasteApplication';
394
}
395
396
}
397
398