Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/diviner/query/DivinerBookQuery.php
12256 views
1
<?php
2
3
final class DivinerBookQuery extends PhabricatorCursorPagedPolicyAwareQuery {
4
5
private $ids;
6
private $phids;
7
private $names;
8
private $nameLike;
9
private $namePrefix;
10
private $repositoryPHIDs;
11
12
private $needProjectPHIDs;
13
private $needRepositories;
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 withNameLike($name) {
26
$this->nameLike = $name;
27
return $this;
28
}
29
30
public function withNames(array $names) {
31
$this->names = $names;
32
return $this;
33
}
34
35
public function withNamePrefix($prefix) {
36
$this->namePrefix = $prefix;
37
return $this;
38
}
39
40
public function withRepositoryPHIDs(array $repository_phids) {
41
$this->repositoryPHIDs = $repository_phids;
42
return $this;
43
}
44
45
public function needProjectPHIDs($need_phids) {
46
$this->needProjectPHIDs = $need_phids;
47
return $this;
48
}
49
50
public function needRepositories($need_repositories) {
51
$this->needRepositories = $need_repositories;
52
return $this;
53
}
54
55
protected function loadPage() {
56
$table = new DivinerLiveBook();
57
$conn_r = $table->establishConnection('r');
58
59
$data = queryfx_all(
60
$conn_r,
61
'SELECT * FROM %T %Q %Q %Q',
62
$table->getTableName(),
63
$this->buildWhereClause($conn_r),
64
$this->buildOrderClause($conn_r),
65
$this->buildLimitClause($conn_r));
66
67
return $table->loadAllFromArray($data);
68
}
69
70
protected function didFilterPage(array $books) {
71
assert_instances_of($books, 'DivinerLiveBook');
72
73
if ($this->needRepositories) {
74
$repositories = id(new PhabricatorRepositoryQuery())
75
->setViewer($this->getViewer())
76
->withPHIDs(mpull($books, 'getRepositoryPHID'))
77
->execute();
78
$repositories = mpull($repositories, null, 'getPHID');
79
80
foreach ($books as $key => $book) {
81
if ($book->getRepositoryPHID() === null) {
82
$book->attachRepository(null);
83
continue;
84
}
85
86
$repository = idx($repositories, $book->getRepositoryPHID());
87
88
if (!$repository) {
89
$this->didRejectResult($book);
90
unset($books[$key]);
91
continue;
92
}
93
94
$book->attachRepository($repository);
95
}
96
}
97
98
if ($this->needProjectPHIDs) {
99
$edge_query = id(new PhabricatorEdgeQuery())
100
->withSourcePHIDs(mpull($books, 'getPHID'))
101
->withEdgeTypes(
102
array(
103
PhabricatorProjectObjectHasProjectEdgeType::EDGECONST,
104
));
105
$edge_query->execute();
106
107
foreach ($books as $book) {
108
$project_phids = $edge_query->getDestinationPHIDs(
109
array(
110
$book->getPHID(),
111
));
112
$book->attachProjectPHIDs($project_phids);
113
}
114
}
115
116
return $books;
117
}
118
119
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
120
$where = array();
121
122
if ($this->ids) {
123
$where[] = qsprintf(
124
$conn,
125
'id IN (%Ld)',
126
$this->ids);
127
}
128
129
if ($this->phids) {
130
$where[] = qsprintf(
131
$conn,
132
'phid IN (%Ls)',
133
$this->phids);
134
}
135
136
if ($this->nameLike !== null && strlen($this->nameLike)) {
137
$where[] = qsprintf(
138
$conn,
139
'name LIKE %~',
140
$this->nameLike);
141
}
142
143
if ($this->names !== null) {
144
$where[] = qsprintf(
145
$conn,
146
'name IN (%Ls)',
147
$this->names);
148
}
149
150
if ($this->namePrefix !== null && strlen($this->namePrefix)) {
151
$where[] = qsprintf(
152
$conn,
153
'name LIKE %>',
154
$this->namePrefix);
155
}
156
157
if ($this->repositoryPHIDs !== null) {
158
$where[] = qsprintf(
159
$conn,
160
'repositoryPHID IN (%Ls)',
161
$this->repositoryPHIDs);
162
}
163
164
$where[] = $this->buildPagingClause($conn);
165
166
return $this->formatWhereClause($conn, $where);
167
}
168
169
public function getQueryApplicationClass() {
170
return 'PhabricatorDivinerApplication';
171
}
172
173
public function getOrderableColumns() {
174
return parent::getOrderableColumns() + array(
175
'name' => array(
176
'column' => 'name',
177
'type' => 'string',
178
'reverse' => true,
179
'unique' => true,
180
),
181
);
182
}
183
184
protected function newPagingMapFromPartialObject($object) {
185
return array(
186
'id' => (int)$object->getID(),
187
'name' => $object->getName(),
188
);
189
}
190
191
public function getBuiltinOrders() {
192
return array(
193
'name' => array(
194
'vector' => array('name'),
195
'name' => pht('Name'),
196
),
197
) + parent::getBuiltinOrders();
198
}
199
200
}
201
202