Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / pholio / query / PholioImageQuery.php
blob0d64540f91b455c4cdf06f0e688fc9b47887b26a
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 loadPage() {
48 return $this->loadStandardPage($this->newResultObject());
51 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
52 $where = parent::buildWhereClauseParts($conn);
54 if ($this->ids !== null) {
55 $where[] = qsprintf(
56 $conn,
57 'id IN (%Ld)',
58 $this->ids);
61 if ($this->phids !== null) {
62 $where[] = qsprintf(
63 $conn,
64 'phid IN (%Ls)',
65 $this->phids);
68 if ($this->mockPHIDs !== null) {
69 $where[] = qsprintf(
70 $conn,
71 'mockPHID IN (%Ls)',
72 $this->mockPHIDs);
75 return $where;
78 protected function willFilterPage(array $images) {
79 assert_instances_of($images, 'PholioImage');
81 $mock_phids = array();
82 foreach ($images as $image) {
83 if (!$image->hasMock()) {
84 continue;
87 $mock_phids[] = $image->getMockPHID();
90 if ($mock_phids) {
91 if ($this->mocks) {
92 $mocks = $this->mocks;
93 } else {
94 $mocks = id(new PholioMockQuery())
95 ->setViewer($this->getViewer())
96 ->withPHIDs($mock_phids)
97 ->execute();
100 $mocks = mpull($mocks, null, 'getPHID');
102 foreach ($images as $key => $image) {
103 if (!$image->hasMock()) {
104 continue;
107 $mock = idx($mocks, $image->getMockPHID());
108 if (!$mock) {
109 unset($images[$key]);
110 $this->didRejectResult($image);
111 continue;
114 $image->attachMock($mock);
118 return $images;
121 protected function didFilterPage(array $images) {
122 assert_instances_of($images, 'PholioImage');
124 $file_phids = mpull($images, 'getFilePHID');
126 $all_files = id(new PhabricatorFileQuery())
127 ->setParentQuery($this)
128 ->setViewer($this->getViewer())
129 ->withPHIDs($file_phids)
130 ->execute();
131 $all_files = mpull($all_files, null, 'getPHID');
133 if ($this->needInlineComments) {
134 // Only load inline comments the viewer has permission to see.
135 $all_inline_comments = id(new PholioTransactionComment())->loadAllWhere(
136 'imageID IN (%Ld)
137 AND (transactionPHID IS NOT NULL OR authorPHID = %s)',
138 mpull($images, 'getID'),
139 $this->getViewer()->getPHID());
140 $all_inline_comments = mgroup($all_inline_comments, 'getImageID');
143 foreach ($images as $image) {
144 $file = idx($all_files, $image->getFilePHID());
145 if (!$file) {
146 $file = PhabricatorFile::loadBuiltin($this->getViewer(), 'missing.png');
148 $image->attachFile($file);
149 if ($this->needInlineComments) {
150 $inlines = idx($all_inline_comments, $image->getID(), array());
151 $image->attachInlineComments($inlines);
155 return $images;
158 public function getQueryApplicationClass() {
159 return 'PhabricatorPholioApplication';