Correct Aphlict websocket URI construction after PHP8 compatibility changes
[phabricator.git] / src / applications / pholio / query / PholioImageQuery.php
blob69d890ab985895028c9a30206847fa1ad0926bd8
1 <?php
3 final class PholioImageQuery
4 extends PhabricatorCursorPagedPolicyAwareQuery {
6 private $ids;
7 private $phids;
8 private $mockPHIDs;
9 private $mocks;
11 private $needInlineComments;
13 public function withIDs(array $ids) {
14 $this->ids = $ids;
15 return $this;
18 public function withPHIDs(array $phids) {
19 $this->phids = $phids;
20 return $this;
23 public function withMocks(array $mocks) {
24 assert_instances_of($mocks, 'PholioMock');
26 $mocks = mpull($mocks, null, 'getPHID');
27 $this->mocks = $mocks;
28 $this->mockPHIDs = array_keys($mocks);
30 return $this;
33 public function withMockPHIDs(array $mock_phids) {
34 $this->mockPHIDs = $mock_phids;
35 return $this;
38 public function needInlineComments($need_inline_comments) {
39 $this->needInlineComments = $need_inline_comments;
40 return $this;
43 public function newResultObject() {
44 return new PholioImage();
47 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
48 $where = parent::buildWhereClauseParts($conn);
50 if ($this->ids !== null) {
51 $where[] = qsprintf(
52 $conn,
53 'id IN (%Ld)',
54 $this->ids);
57 if ($this->phids !== null) {
58 $where[] = qsprintf(
59 $conn,
60 'phid IN (%Ls)',
61 $this->phids);
64 if ($this->mockPHIDs !== null) {
65 $where[] = qsprintf(
66 $conn,
67 'mockPHID IN (%Ls)',
68 $this->mockPHIDs);
71 return $where;
74 protected function willFilterPage(array $images) {
75 assert_instances_of($images, 'PholioImage');
77 $mock_phids = array();
78 foreach ($images as $image) {
79 if (!$image->hasMock()) {
80 continue;
83 $mock_phids[] = $image->getMockPHID();
86 if ($mock_phids) {
87 if ($this->mocks) {
88 $mocks = $this->mocks;
89 } else {
90 $mocks = id(new PholioMockQuery())
91 ->setViewer($this->getViewer())
92 ->withPHIDs($mock_phids)
93 ->execute();
96 $mocks = mpull($mocks, null, 'getPHID');
98 foreach ($images as $key => $image) {
99 if (!$image->hasMock()) {
100 continue;
103 $mock = idx($mocks, $image->getMockPHID());
104 if (!$mock) {
105 unset($images[$key]);
106 $this->didRejectResult($image);
107 continue;
110 $image->attachMock($mock);
114 return $images;
117 protected function didFilterPage(array $images) {
118 assert_instances_of($images, 'PholioImage');
120 $file_phids = mpull($images, 'getFilePHID');
122 $all_files = id(new PhabricatorFileQuery())
123 ->setParentQuery($this)
124 ->setViewer($this->getViewer())
125 ->withPHIDs($file_phids)
126 ->execute();
127 $all_files = mpull($all_files, null, 'getPHID');
129 if ($this->needInlineComments) {
130 // Only load inline comments the viewer has permission to see.
131 $all_inline_comments = id(new PholioTransactionComment())->loadAllWhere(
132 'imageID IN (%Ld)
133 AND (transactionPHID IS NOT NULL OR authorPHID = %s)',
134 mpull($images, 'getID'),
135 $this->getViewer()->getPHID());
136 $all_inline_comments = mgroup($all_inline_comments, 'getImageID');
139 foreach ($images as $image) {
140 $file = idx($all_files, $image->getFilePHID());
141 if (!$file) {
142 $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png');
144 $image->attachFile($file);
145 if ($this->needInlineComments) {
146 $inlines = idx($all_inline_comments, $image->getID(), array());
147 $image->attachInlineComments($inlines);
151 return $images;
154 public function getQueryApplicationClass() {
155 return 'PhabricatorPholioApplication';