@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 NuanceQuery<NuanceSource>
5 */
6final class NuanceSourceQuery
7 extends NuanceQuery {
8
9 private $ids;
10 private $phids;
11 private $types;
12 private $isDisabled;
13 private $hasCursors;
14
15 public function withIDs(array $ids) {
16 $this->ids = $ids;
17 return $this;
18 }
19
20 public function withPHIDs(array $phids) {
21 $this->phids = $phids;
22 return $this;
23 }
24
25 public function withTypes($types) {
26 $this->types = $types;
27 return $this;
28 }
29
30 public function withIsDisabled($disabled) {
31 $this->isDisabled = $disabled;
32 return $this;
33 }
34
35 public function withHasImportCursors($has_cursors) {
36 $this->hasCursors = $has_cursors;
37 return $this;
38 }
39
40 public function withNameNgrams($ngrams) {
41 return $this->withNgramsConstraint(
42 new NuanceSourceNameNgrams(),
43 $ngrams);
44 }
45
46 public function newResultObject() {
47 return new NuanceSource();
48 }
49
50 protected function getPrimaryTableAlias() {
51 return 'source';
52 }
53
54 protected function willFilterPage(array $sources) {
55 $all_types = NuanceSourceDefinition::getAllDefinitions();
56
57 foreach ($sources as $key => $source) {
58 $definition = idx($all_types, $source->getType());
59 if (!$definition) {
60 $this->didRejectResult($source);
61 unset($sources[$key]);
62 continue;
63 }
64 $source->attachDefinition($definition);
65 }
66
67 return $sources;
68 }
69
70
71 protected function buildWhereClauseParts(AphrontDatabaseConnection $conn) {
72 $where = parent::buildWhereClauseParts($conn);
73
74 if ($this->types !== null) {
75 $where[] = qsprintf(
76 $conn,
77 'source.type IN (%Ls)',
78 $this->types);
79 }
80
81 if ($this->ids !== null) {
82 $where[] = qsprintf(
83 $conn,
84 'source.id IN (%Ld)',
85 $this->ids);
86 }
87
88 if ($this->phids !== null) {
89 $where[] = qsprintf(
90 $conn,
91 'source.phid IN (%Ls)',
92 $this->phids);
93 }
94
95 if ($this->isDisabled !== null) {
96 $where[] = qsprintf(
97 $conn,
98 'source.isDisabled = %d',
99 (int)$this->isDisabled);
100 }
101
102 if ($this->hasCursors !== null) {
103 $cursor_types = array();
104
105 $definitions = NuanceSourceDefinition::getAllDefinitions();
106 foreach ($definitions as $key => $definition) {
107 if ($definition->hasImportCursors()) {
108 $cursor_types[] = $key;
109 }
110 }
111
112 if ($this->hasCursors) {
113 if (!$cursor_types) {
114 throw new PhabricatorEmptyQueryException();
115 } else {
116 $where[] = qsprintf(
117 $conn,
118 'source.type IN (%Ls)',
119 $cursor_types);
120 }
121 } else {
122 if (!$cursor_types) {
123 // Apply no constraint.
124 } else {
125 $where[] = qsprintf(
126 $conn,
127 'source.type NOT IN (%Ls)',
128 $cursor_types);
129 }
130 }
131 }
132
133 return $where;
134 }
135
136}