@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
at recaptime-dev/main 165 lines 3.8 kB view raw
1<?php 2 3/** 4 * @extends PhabricatorCursorPagedPolicyAwareQuery<PholioMock> 5 */ 6final class PholioMockQuery 7 extends PhabricatorCursorPagedPolicyAwareQuery { 8 9 private $ids; 10 private $phids; 11 private $authorPHIDs; 12 private $statuses; 13 14 private $needCoverFiles; 15 private $needImages; 16 private $needInlineComments; 17 private $needTokenCounts; 18 19 public function withIDs(array $ids) { 20 $this->ids = $ids; 21 return $this; 22 } 23 24 public function withPHIDs(array $phids) { 25 $this->phids = $phids; 26 return $this; 27 } 28 29 public function withAuthorPHIDs(array $author_phids) { 30 $this->authorPHIDs = $author_phids; 31 return $this; 32 } 33 34 public function withStatuses(array $statuses) { 35 $this->statuses = $statuses; 36 return $this; 37 } 38 39 public function needCoverFiles($need_cover_files) { 40 $this->needCoverFiles = $need_cover_files; 41 return $this; 42 } 43 44 public function needImages($need_images) { 45 $this->needImages = $need_images; 46 return $this; 47 } 48 49 public function needInlineComments($need_inline_comments) { 50 $this->needInlineComments = $need_inline_comments; 51 return $this; 52 } 53 54 public function needTokenCounts($need) { 55 $this->needTokenCounts = $need; 56 return $this; 57 } 58 59 public function newResultObject() { 60 return new PholioMock(); 61 } 62 63 protected function loadPage() { 64 if ($this->needInlineComments && !$this->needImages) { 65 throw new Exception( 66 pht( 67 'You can not query for inline comments without also querying for '. 68 'images.')); 69 } 70 71 return $this->loadStandardPage(new PholioMock()); 72 } 73 74 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) { 75 $where = parent::buildWhereClauseParts($conn); 76 77 if ($this->ids !== null) { 78 $where[] = qsprintf( 79 $conn, 80 'mock.id IN (%Ld)', 81 $this->ids); 82 } 83 84 if ($this->phids !== null) { 85 $where[] = qsprintf( 86 $conn, 87 'mock.phid IN (%Ls)', 88 $this->phids); 89 } 90 91 if ($this->authorPHIDs !== null) { 92 $where[] = qsprintf( 93 $conn, 94 'mock.authorPHID in (%Ls)', 95 $this->authorPHIDs); 96 } 97 98 if ($this->statuses !== null) { 99 $where[] = qsprintf( 100 $conn, 101 'mock.status IN (%Ls)', 102 $this->statuses); 103 } 104 105 return $where; 106 } 107 108 protected function didFilterPage(array $mocks) { 109 $viewer = $this->getViewer(); 110 111 if ($this->needImages) { 112 $images = id(new PholioImageQuery()) 113 ->setViewer($viewer) 114 ->withMocks($mocks) 115 ->needInlineComments($this->needInlineComments) 116 ->execute(); 117 118 $image_groups = mgroup($images, 'getMockPHID'); 119 foreach ($mocks as $mock) { 120 $images = idx($image_groups, $mock->getPHID(), array()); 121 $mock->attachImages($images); 122 } 123 } 124 125 if ($this->needCoverFiles) { 126 $cover_files = id(new PhabricatorFileQuery()) 127 ->setViewer($viewer) 128 ->withPHIDs(mpull($mocks, 'getCoverPHID')) 129 ->execute(); 130 $cover_files = mpull($cover_files, null, 'getPHID'); 131 132 foreach ($mocks as $mock) { 133 $file = idx($cover_files, $mock->getCoverPHID()); 134 if (!$file) { 135 $file = PhabricatorFile::loadBuiltin( 136 $viewer, 137 'missing.png'); 138 } 139 $mock->attachCoverFile($file); 140 } 141 } 142 143 if ($this->needTokenCounts) { 144 $counts = id(new PhabricatorTokenCountQuery()) 145 ->withObjectPHIDs(mpull($mocks, 'getPHID')) 146 ->execute(); 147 148 foreach ($mocks as $mock) { 149 $token_count = idx($counts, $mock->getPHID(), 0); 150 $mock->attachTokenCount($token_count); 151 } 152 } 153 154 return $mocks; 155 } 156 157 public function getQueryApplicationClass() { 158 return PhabricatorPholioApplication::class; 159 } 160 161 protected function getPrimaryTableAlias() { 162 return 'mock'; 163 } 164 165}