@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<PhrictionContent>
5 */
6final class PhrictionContentQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $documentPHIDs;
12 private $versions;
13
14 public function withIDs(array $ids) {
15 $this->ids = $ids;
16 return $this;
17 }
18
19 public function withPHIDs(array $phids) {
20 $this->phids = $phids;
21 return $this;
22 }
23
24 public function withDocumentPHIDs(array $phids) {
25 $this->documentPHIDs = $phids;
26 return $this;
27 }
28
29 public function withVersions(array $versions) {
30 $this->versions = $versions;
31 return $this;
32 }
33
34 public function newResultObject() {
35 return new PhrictionContent();
36 }
37
38 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
39 $where = parent::buildWhereClauseParts($conn);
40
41 if ($this->ids !== null) {
42 $where[] = qsprintf(
43 $conn,
44 'c.id IN (%Ld)',
45 $this->ids);
46 }
47
48 if ($this->phids !== null) {
49 $where[] = qsprintf(
50 $conn,
51 'c.phid IN (%Ls)',
52 $this->phids);
53 }
54
55 if ($this->versions !== null) {
56 $where[] = qsprintf(
57 $conn,
58 'version IN (%Ld)',
59 $this->versions);
60 }
61
62 if ($this->documentPHIDs !== null) {
63 $where[] = qsprintf(
64 $conn,
65 'd.phid IN (%Ls)',
66 $this->documentPHIDs);
67 }
68
69 return $where;
70 }
71
72 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
73 $joins = parent::buildJoinClauseParts($conn);
74
75 if ($this->shouldJoinDocumentTable()) {
76 $joins[] = qsprintf(
77 $conn,
78 'JOIN %T d ON d.phid = c.documentPHID',
79 id(new PhrictionDocument())->getTableName());
80 }
81
82 return $joins;
83 }
84
85 protected function willFilterPage(array $contents) {
86 $document_phids = mpull($contents, 'getDocumentPHID');
87
88 $documents = id(new PhrictionDocumentQuery())
89 ->setViewer($this->getViewer())
90 ->setParentQuery($this)
91 ->withPHIDs($document_phids)
92 ->execute();
93 $documents = mpull($documents, null, 'getPHID');
94
95 foreach ($contents as $key => $content) {
96 $document_phid = $content->getDocumentPHID();
97
98 $document = idx($documents, $document_phid);
99 if (!$document) {
100 unset($contents[$key]);
101 $this->didRejectResult($content);
102 continue;
103 }
104
105 $content->attachDocument($document);
106 }
107
108 return $contents;
109 }
110
111 private function shouldJoinDocumentTable() {
112 if ($this->documentPHIDs !== null) {
113 return true;
114 }
115
116 return false;
117 }
118
119 protected function getPrimaryTableAlias() {
120 return 'c';
121 }
122
123 public function getQueryApplicationClass() {
124 return PhabricatorPhrictionApplication::class;
125 }
126
127}