@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<HeraldTranscript>
5 */
6final class HeraldTranscriptQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $objectPHIDs;
12 private $needPartialRecords;
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 withObjectPHIDs(array $phids) {
25 $this->objectPHIDs = $phids;
26 return $this;
27 }
28
29 public function needPartialRecords($need_partial) {
30 $this->needPartialRecords = $need_partial;
31 return $this;
32 }
33
34 protected function loadPage() {
35 $transcript = new HeraldTranscript();
36 $conn = $transcript->establishConnection('r');
37
38 // NOTE: Transcripts include a potentially enormous amount of serialized
39 // data, so we're loading only some of the fields here if the caller asked
40 // for partial records.
41
42 if ($this->needPartialRecords) {
43 $fields = array(
44 'id',
45 'phid',
46 'objectPHID',
47 'time',
48 'duration',
49 'dryRun',
50 'host',
51 );
52 $fields = qsprintf($conn, '%LC', $fields);
53 } else {
54 $fields = qsprintf($conn, '*');
55 }
56
57 $rows = queryfx_all(
58 $conn,
59 'SELECT %Q FROM %T t %Q %Q %Q',
60 $fields,
61 $transcript->getTableName(),
62 $this->buildWhereClause($conn),
63 $this->buildOrderClause($conn),
64 $this->buildLimitClause($conn));
65
66 $transcripts = $transcript->loadAllFromArray($rows);
67
68 if ($this->needPartialRecords) {
69 // Make sure nothing tries to write these; they aren't complete.
70 foreach ($transcripts as $transcript) {
71 $transcript->makeEphemeral();
72 }
73 }
74
75 return $transcripts;
76 }
77
78 protected function willFilterPage(array $transcripts) {
79 $phids = mpull($transcripts, 'getObjectPHID');
80
81 $objects = id(new PhabricatorObjectQuery())
82 ->setViewer($this->getViewer())
83 ->withPHIDs($phids)
84 ->execute();
85
86 foreach ($transcripts as $key => $transcript) {
87 $object_phid = $transcript->getObjectPHID();
88
89 if (!$object_phid) {
90 $transcript->attachObject(null);
91 continue;
92 }
93
94 $object = idx($objects, $object_phid);
95 if (!$object) {
96 $this->didRejectResult($transcript);
97 unset($transcripts[$key]);
98 }
99
100 $transcript->attachObject($object);
101 }
102
103 return $transcripts;
104 }
105
106 protected function buildWhereClause(AphrontDatabaseConnection $conn) {
107 $where = array();
108
109 if ($this->ids) {
110 $where[] = qsprintf(
111 $conn,
112 'id IN (%Ld)',
113 $this->ids);
114 }
115
116 if ($this->phids) {
117 $where[] = qsprintf(
118 $conn,
119 'phid IN (%Ls)',
120 $this->phids);
121 }
122
123 if ($this->objectPHIDs) {
124 $where[] = qsprintf(
125 $conn,
126 'objectPHID in (%Ls)',
127 $this->objectPHIDs);
128 }
129
130 $where[] = $this->buildPagingClause($conn);
131
132 return $this->formatWhereClause($conn, $where);
133 }
134
135 public function getQueryApplicationClass() {
136 return PhabricatorHeraldApplication::class;
137 }
138
139}