@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3/**
4 * @extends PhabricatorCursorPagedPolicyAwareQuery<PholioImage>
5 */
6final class PholioImageQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $mockPHIDs;
12 private $mocks;
13
14 private $needInlineComments;
15
16 public function withIDs(array $ids) {
17 $this->ids = $ids;
18 return $this;
19 }
20
21 public function withPHIDs(array $phids) {
22 $this->phids = $phids;
23 return $this;
24 }
25
26 /**
27 * @param array<PholioMock> $mocks
28 */
29 public function withMocks(array $mocks) {
30 assert_instances_of($mocks, PholioMock::class);
31
32 $mocks = mpull($mocks, null, 'getPHID');
33 $this->mocks = $mocks;
34 $this->mockPHIDs = array_keys($mocks);
35
36 return $this;
37 }
38
39 public function withMockPHIDs(array $mock_phids) {
40 $this->mockPHIDs = $mock_phids;
41 return $this;
42 }
43
44 public function needInlineComments($need_inline_comments) {
45 $this->needInlineComments = $need_inline_comments;
46 return $this;
47 }
48
49 public function newResultObject() {
50 return new PholioImage();
51 }
52
53 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
54 $where = parent::buildWhereClauseParts($conn);
55
56 if ($this->ids !== null) {
57 $where[] = qsprintf(
58 $conn,
59 'id IN (%Ld)',
60 $this->ids);
61 }
62
63 if ($this->phids !== null) {
64 $where[] = qsprintf(
65 $conn,
66 'phid IN (%Ls)',
67 $this->phids);
68 }
69
70 if ($this->mockPHIDs !== null) {
71 $where[] = qsprintf(
72 $conn,
73 'mockPHID IN (%Ls)',
74 $this->mockPHIDs);
75 }
76
77 return $where;
78 }
79
80 /**
81 * @param array<PholioImage> $images
82 */
83 protected function willFilterPage(array $images) {
84 assert_instances_of($images, PholioImage::class);
85
86 $mock_phids = array();
87 foreach ($images as $image) {
88 if (!$image->hasMock()) {
89 continue;
90 }
91
92 $mock_phids[] = $image->getMockPHID();
93 }
94
95 if ($mock_phids) {
96 if ($this->mocks) {
97 $mocks = $this->mocks;
98 } else {
99 $mocks = id(new PholioMockQuery())
100 ->setViewer($this->getViewer())
101 ->withPHIDs($mock_phids)
102 ->execute();
103 }
104
105 $mocks = mpull($mocks, null, 'getPHID');
106
107 foreach ($images as $key => $image) {
108 if (!$image->hasMock()) {
109 continue;
110 }
111
112 $mock = idx($mocks, $image->getMockPHID());
113 if (!$mock) {
114 unset($images[$key]);
115 $this->didRejectResult($image);
116 continue;
117 }
118
119 $image->attachMock($mock);
120 }
121 }
122
123 return $images;
124 }
125
126 /**
127 * @param array<PholioImage> $images
128 */
129 protected function didFilterPage(array $images) {
130 assert_instances_of($images, PholioImage::class);
131
132 $file_phids = mpull($images, 'getFilePHID');
133
134 $all_files = id(new PhabricatorFileQuery())
135 ->setParentQuery($this)
136 ->setViewer($this->getViewer())
137 ->withPHIDs($file_phids)
138 ->execute();
139 $all_files = mpull($all_files, null, 'getPHID');
140
141 if ($this->needInlineComments) {
142 // Only load inline comments the viewer has permission to see.
143 $all_inline_comments = id(new PholioTransactionComment())->loadAllWhere(
144 'imageID IN (%Ld)
145 AND (transactionPHID IS NOT NULL OR authorPHID = %s)',
146 mpull($images, 'getID'),
147 $this->getViewer()->getPHID());
148 $all_inline_comments = mgroup($all_inline_comments, 'getImageID');
149 }
150
151 foreach ($images as $image) {
152 $file = idx($all_files, $image->getFilePHID());
153 if (!$file) {
154 $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png');
155 }
156 $image->attachFile($file);
157 if ($this->needInlineComments) {
158 $inlines = idx($all_inline_comments, $image->getID(), array());
159 $image->attachInlineComments($inlines);
160 }
161 }
162
163 return $images;
164 }
165
166 public function getQueryApplicationClass() {
167 return PhabricatorPholioApplication::class;
168 }
169
170}