Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / files / query / PhabricatorFileAttachmentQuery.php
blob5fb37dc32e8a4fc655894c7e0c116d8e1d1c5b28
1 <?php
3 final class PhabricatorFileAttachmentQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $objectPHIDs;
7 private $filePHIDs;
8 private $needFiles;
9 private $visibleFiles;
11 public function withObjectPHIDs(array $object_phids) {
12 $this->objectPHIDs = $object_phids;
13 return $this;
16 public function withFilePHIDs(array $file_phids) {
17 $this->filePHIDs = $file_phids;
18 return $this;
21 public function withVisibleFiles($visible_files) {
22 $this->visibleFiles = $visible_files;
23 return $this;
26 public function needFiles($need) {
27 $this->needFiles = $need;
28 return $this;
31 public function newResultObject() {
32 return new PhabricatorFileAttachment();
35 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
36 $where = parent::buildWhereClauseParts($conn);
38 if ($this->objectPHIDs !== null) {
39 $where[] = qsprintf(
40 $conn,
41 'attachments.objectPHID IN (%Ls)',
42 $this->objectPHIDs);
45 if ($this->filePHIDs !== null) {
46 $where[] = qsprintf(
47 $conn,
48 'attachments.filePHID IN (%Ls)',
49 $this->filePHIDs);
52 return $where;
55 protected function willFilterPage(array $attachments) {
56 $viewer = $this->getViewer();
57 $object_phids = array();
59 foreach ($attachments as $attachment) {
60 $object_phid = $attachment->getObjectPHID();
61 $object_phids[$object_phid] = $object_phid;
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();
75 foreach ($attachments as $key => $attachment) {
76 $object_phid = $attachment->getObjectPHID();
77 $object = idx($objects, $object_phid);
79 if (!$object) {
80 $this->didRejectResult($attachment);
81 unset($attachments[$key]);
82 continue;
85 $attachment->attachObject($object);
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;
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();
106 foreach ($attachments as $key => $attachment) {
107 $file_phid = $attachment->getFilePHID();
108 $file = idx($files, $file_phid);
110 if ($this->visibleFiles && !$file) {
111 $this->didRejectResult($attachment);
112 unset($attachments[$key]);
113 continue;
116 $attachment->attachFile($file);
120 return $attachments;
123 protected function getPrimaryTableAlias() {
124 return 'attachments';
127 public function getQueryApplicationClass() {
128 return 'PhabricatorFilesApplication';