@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<PhabricatorFeedStoryData>
5 */
6final class PhabricatorFeedQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $filterPHIDs;
10 private $chronologicalKeys;
11 private $rangeMin;
12 private $rangeMax;
13
14 public function withFilterPHIDs(array $phids) {
15 $this->filterPHIDs = $phids;
16 return $this;
17 }
18
19 public function withChronologicalKeys(array $keys) {
20 $this->chronologicalKeys = $keys;
21 return $this;
22 }
23
24 /**
25 * @param int|null $range_min Minimum epoch value of feed stories
26 * @param int|null $range_max Maximum epoch value of feed stories
27 */
28 public function withEpochInRange($range_min, $range_max) {
29 if ($range_min && $range_max && $range_min > $range_max) {
30 throw new PhutilArgumentUsageException(
31 pht('Feed query minimum range must be lower than maximum range.'));
32 }
33 $this->rangeMin = $range_min;
34 $this->rangeMax = $range_max;
35 return $this;
36 }
37
38 public function newResultObject() {
39 return new PhabricatorFeedStoryData();
40 }
41
42 protected function loadPage() {
43 // NOTE: We return raw rows from this method, which is a little unusual.
44 return $this->loadStandardPageRows($this->newResultObject());
45 }
46
47 protected function willFilterPage(array $data) {
48 $stories = PhabricatorFeedStory::loadAllFromRows($data, $this->getViewer());
49
50 foreach ($stories as $key => $story) {
51 if (!$story->isVisibleInFeed()) {
52 unset($stories[$key]);
53 }
54 }
55
56 return $stories;
57 }
58
59 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
60 $joins = parent::buildJoinClauseParts($conn);
61
62 // NOTE: We perform this join unconditionally (even if we have no filter
63 // PHIDs) to omit rows which have no story references. These story data
64 // rows are notifications or realtime alerts.
65
66 $ref_table = new PhabricatorFeedStoryReference();
67 $joins[] = qsprintf(
68 $conn,
69 'JOIN %T ref ON ref.chronologicalKey = story.chronologicalKey',
70 $ref_table->getTableName());
71
72 return $joins;
73 }
74
75 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
76 $where = parent::buildWhereClauseParts($conn);
77
78 if ($this->filterPHIDs !== null) {
79 $where[] = qsprintf(
80 $conn,
81 'ref.objectPHID IN (%Ls)',
82 $this->filterPHIDs);
83 }
84
85 if ($this->chronologicalKeys !== null) {
86 // NOTE: We can't use "%d" to format these large integers on 32-bit
87 // systems. Historically, we formatted these into integers in an
88 // awkward way because MySQL could sometimes (?) fail to use the proper
89 // keys if the values were formatted as strings instead of integers.
90
91 // After the "qsprintf()" update to use PhutilQueryString, we can no
92 // longer do this in a sneaky way. However, the MySQL key issue also
93 // no longer appears to reproduce across several systems. So: just use
94 // strings until problems turn up?
95
96 $where[] = qsprintf(
97 $conn,
98 'ref.chronologicalKey IN (%Ls)',
99 $this->chronologicalKeys);
100 }
101
102 // NOTE: We may not have 64-bit PHP, so do the shifts in MySQL instead.
103 // From EXPLAIN, it appears like MySQL is smart enough to compute the
104 // result and make use of keys to execute the query.
105
106 if ($this->rangeMin !== null) {
107 $where[] = qsprintf(
108 $conn,
109 'ref.chronologicalKey >= (%d << 32)',
110 $this->rangeMin);
111 }
112
113 if ($this->rangeMax !== null) {
114 $where[] = qsprintf(
115 $conn,
116 'ref.chronologicalKey < (%d << 32)',
117 $this->rangeMax);
118 }
119
120 return $where;
121 }
122
123 protected function buildGroupClause(AphrontDatabaseConnection $conn) {
124 if ($this->filterPHIDs !== null) {
125 return qsprintf($conn, 'GROUP BY ref.chronologicalKey');
126 } else {
127 return qsprintf($conn, 'GROUP BY story.chronologicalKey');
128 }
129 }
130
131 protected function getDefaultOrderVector() {
132 return array('key');
133 }
134
135 public function getBuiltinOrders() {
136 return array(
137 'newest' => array(
138 'vector' => array('key'),
139 'name' => pht('Creation (Newest First)'),
140 'aliases' => array('created'),
141 ),
142 'oldest' => array(
143 'vector' => array('-key'),
144 'name' => pht('Creation (Oldest First)'),
145 ),
146 );
147 }
148
149 public function getOrderableColumns() {
150 $table = ($this->filterPHIDs ? 'ref' : 'story');
151 return array(
152 'key' => array(
153 'table' => $table,
154 'column' => 'chronologicalKey',
155 'type' => 'string',
156 'unique' => true,
157 ),
158 );
159 }
160
161 protected function applyExternalCursorConstraintsToQuery(
162 PhabricatorCursorPagedPolicyAwareQuery $subquery,
163 $cursor) {
164 $subquery->withChronologicalKeys(array($cursor));
165 }
166
167 protected function newExternalCursorStringForResult($object) {
168 return $object->getChronologicalKey();
169 }
170
171 protected function newPagingMapFromPartialObject($object) {
172 // This query is unusual, and the "object" is a raw result row.
173 return array(
174 'key' => $object['chronologicalKey'],
175 );
176 }
177
178 protected function getPrimaryTableAlias() {
179 return 'story';
180 }
181
182 public function getQueryApplicationClass() {
183 return PhabricatorFeedApplication::class;
184 }
185
186}