Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/query/PhabricatorFileAttachmentQuery.php
12242 views
1
<?php
2
3
final class PhabricatorFileAttachmentQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
private $objectPHIDs;
7
private $filePHIDs;
8
private $needFiles;
9
private $visibleFiles;
10
11
public function withObjectPHIDs(array $object_phids) {
12
$this->objectPHIDs = $object_phids;
13
return $this;
14
}
15
16
public function withFilePHIDs(array $file_phids) {
17
$this->filePHIDs = $file_phids;
18
return $this;
19
}
20
21
public function withVisibleFiles($visible_files) {
22
$this->visibleFiles = $visible_files;
23
return $this;
24
}
25
26
public function needFiles($need) {
27
$this->needFiles = $need;
28
return $this;
29
}
30
31
public function newResultObject() {
32
return new PhabricatorFileAttachment();
33
}
34
35
protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
36
$where = parent::buildWhereClauseParts($conn);
37
38
if ($this->objectPHIDs !== null) {
39
$where[] = qsprintf(
40
$conn,
41
'attachments.objectPHID IN (%Ls)',
42
$this->objectPHIDs);
43
}
44
45
if ($this->filePHIDs !== null) {
46
$where[] = qsprintf(
47
$conn,
48
'attachments.filePHID IN (%Ls)',
49
$this->filePHIDs);
50
}
51
52
return $where;
53
}
54
55
protected function willFilterPage(array $attachments) {
56
$viewer = $this->getViewer();
57
$object_phids = array();
58
59
foreach ($attachments as $attachment) {
60
$object_phid = $attachment->getObjectPHID();
61
$object_phids[$object_phid] = $object_phid;
62
}
63
64
if ($object_phids) {
65
$objects = id(new PhabricatorObjectQuery())
66
->setViewer($viewer)
67
->setParentQuery($this)
68
->withPHIDs($object_phids)
69
->execute();
70
$objects = mpull($objects, null, 'getPHID');
71
} else {
72
$objects = array();
73
}
74
75
foreach ($attachments as $key => $attachment) {
76
$object_phid = $attachment->getObjectPHID();
77
$object = idx($objects, $object_phid);
78
79
if (!$object) {
80
$this->didRejectResult($attachment);
81
unset($attachments[$key]);
82
continue;
83
}
84
85
$attachment->attachObject($object);
86
}
87
88
if ($this->needFiles) {
89
$file_phids = array();
90
foreach ($attachments as $attachment) {
91
$file_phid = $attachment->getFilePHID();
92
$file_phids[$file_phid] = $file_phid;
93
}
94
95
if ($file_phids) {
96
$files = id(new PhabricatorFileQuery())
97
->setViewer($viewer)
98
->setParentQuery($this)
99
->withPHIDs($file_phids)
100
->execute();
101
$files = mpull($files, null, 'getPHID');
102
} else {
103
$files = array();
104
}
105
106
foreach ($attachments as $key => $attachment) {
107
$file_phid = $attachment->getFilePHID();
108
$file = idx($files, $file_phid);
109
110
if ($this->visibleFiles && !$file) {
111
$this->didRejectResult($attachment);
112
unset($attachments[$key]);
113
continue;
114
}
115
116
$attachment->attachFile($file);
117
}
118
}
119
120
return $attachments;
121
}
122
123
protected function getPrimaryTableAlias() {
124
return 'attachments';
125
}
126
127
public function getQueryApplicationClass() {
128
return 'PhabricatorFilesApplication';
129
}
130
131
}
132
133