@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<PhabricatorProjectTrigger>
5 */
6final class PhabricatorProjectTriggerQuery
7 extends PhabricatorCursorPagedPolicyAwareQuery {
8
9 private $ids;
10 private $phids;
11 private $activeColumnMin;
12 private $activeColumnMax;
13
14 private $needUsage;
15
16 public function withIDs(array $ids) {
17 $this->ids = $ids;
18 return $this;
19 }
20
21 public function withPHIDs(array $phids) {
22 $this->phids = $phids;
23 return $this;
24 }
25
26 public function needUsage($need_usage) {
27 $this->needUsage = $need_usage;
28 return $this;
29 }
30
31 public function withActiveColumnCountBetween($min, $max) {
32 $this->activeColumnMin = $min;
33 $this->activeColumnMax = $max;
34 return $this;
35 }
36
37 public function newResultObject() {
38 return new PhabricatorProjectTrigger();
39 }
40
41 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
42 $where = parent::buildWhereClauseParts($conn);
43
44 if ($this->ids !== null) {
45 $where[] = qsprintf(
46 $conn,
47 'trigger.id IN (%Ld)',
48 $this->ids);
49 }
50
51 if ($this->phids !== null) {
52 $where[] = qsprintf(
53 $conn,
54 'trigger.phid IN (%Ls)',
55 $this->phids);
56 }
57
58 if ($this->activeColumnMin !== null) {
59 $where[] = qsprintf(
60 $conn,
61 'trigger_usage.activeColumnCount >= %d',
62 $this->activeColumnMin);
63 }
64
65 if ($this->activeColumnMax !== null) {
66 $where[] = qsprintf(
67 $conn,
68 'trigger_usage.activeColumnCount <= %d',
69 $this->activeColumnMax);
70 }
71
72 return $where;
73 }
74
75 protected function buildJoinClauseParts(AphrontDatabaseConnection $conn) {
76 $joins = parent::buildJoinClauseParts($conn);
77
78 if ($this->shouldJoinUsageTable()) {
79 $joins[] = qsprintf(
80 $conn,
81 'JOIN %R trigger_usage ON trigger.phid = trigger_usage.triggerPHID',
82 new PhabricatorProjectTriggerUsage());
83 }
84
85 return $joins;
86 }
87
88 private function shouldJoinUsageTable() {
89 if ($this->activeColumnMin !== null) {
90 return true;
91 }
92
93 if ($this->activeColumnMax !== null) {
94 return true;
95 }
96
97 return false;
98 }
99
100 protected function didFilterPage(array $triggers) {
101 if ($this->needUsage) {
102 $usage_map = id(new PhabricatorProjectTriggerUsage())->loadAllWhere(
103 'triggerPHID IN (%Ls)',
104 mpull($triggers, 'getPHID'));
105 $usage_map = mpull($usage_map, null, 'getTriggerPHID');
106
107 foreach ($triggers as $trigger) {
108 $trigger_phid = $trigger->getPHID();
109
110 $usage = idx($usage_map, $trigger_phid);
111 if (!$usage) {
112 $usage = id(new PhabricatorProjectTriggerUsage())
113 ->setTriggerPHID($trigger_phid)
114 ->setExamplePHID(null)
115 ->setColumnCount(0)
116 ->setActiveColumnCount(0);
117 }
118
119 $trigger->attachUsage($usage);
120 }
121 }
122
123 return $triggers;
124 }
125
126 public function getQueryApplicationClass() {
127 return PhabricatorProjectApplication::class;
128 }
129
130 protected function getPrimaryTableAlias() {
131 return 'trigger';
132 }
133
134}