forked from phacility/phabricator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhabricatorFeedSearchEngine.php
162 lines (129 loc) · 4.18 KB
/
PhabricatorFeedSearchEngine.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
final class PhabricatorFeedSearchEngine
extends PhabricatorApplicationSearchEngine {
public function getResultTypeDescription() {
return pht('Feed Stories');
}
public function getApplicationClassName() {
return 'PhabricatorFeedApplication';
}
public function newQuery() {
return new PhabricatorFeedQuery();
}
protected function shouldShowOrderField() {
return false;
}
protected function buildCustomSearchFields() {
return array(
id(new PhabricatorUsersSearchField())
->setLabel(pht('Include Users'))
->setKey('userPHIDs'),
// NOTE: This query is not executed with EdgeLogic, so we can't use
// a fancy logical datasource.
id(new PhabricatorSearchDatasourceField())
->setDatasource(new PhabricatorProjectDatasource())
->setLabel(pht('Include Projects'))
->setKey('projectPHIDs'),
id(new PhabricatorSearchDateControlField())
->setLabel(pht('Occurs After'))
->setKey('rangeStart'),
id(new PhabricatorSearchDateControlField())
->setLabel(pht('Occurs Before'))
->setKey('rangeEnd'),
// NOTE: This is a legacy field retained only for backward
// compatibility. If the projects field used EdgeLogic, we could use
// `viewerprojects()` to execute an equivalent query.
id(new PhabricatorSearchCheckboxesField())
->setKey('viewerProjects')
->setOptions(
array(
'self' => pht('Include stories about projects I am a member of.'),
)),
);
}
protected function buildQueryFromParameters(array $map) {
$query = $this->newQuery();
$phids = array();
if ($map['userPHIDs']) {
$phids += array_fuse($map['userPHIDs']);
}
if ($map['projectPHIDs']) {
$phids += array_fuse($map['projectPHIDs']);
}
// NOTE: This value may be `true` for older saved queries, or
// `array('self')` for newer ones.
$viewer_projects = $map['viewerProjects'];
if ($viewer_projects) {
$viewer = $this->requireViewer();
$projects = id(new PhabricatorProjectQuery())
->setViewer($viewer)
->withMemberPHIDs(array($viewer->getPHID()))
->execute();
$phids += array_fuse(mpull($projects, 'getPHID'));
}
if ($phids) {
$query->withFilterPHIDs($phids);
}
$range_min = $map['rangeStart'];
if ($range_min) {
$range_min = $range_min->getEpoch();
}
$range_max = $map['rangeEnd'];
if ($range_max) {
$range_max = $range_max->getEpoch();
}
if ($range_min && $range_max) {
if ($range_min > $range_max) {
throw new PhabricatorSearchConstraintException(
pht(
'The specified "Occurs Before" date is earlier in time than the '.
'specified "Occurs After" date, so this query can never match '.
'any results.'));
}
}
if ($range_min || $range_max) {
$query->withEpochInRange($range_min, $range_max);
}
return $query;
}
protected function getURI($path) {
return '/feed/'.$path;
}
protected function getBuiltinQueryNames() {
$names = array(
'all' => pht('All Stories'),
);
if ($this->requireViewer()->isLoggedIn()) {
$names['projects'] = pht('Tags');
}
return $names;
}
public function buildSavedQueryFromBuiltin($query_key) {
$query = $this->newSavedQuery();
$query->setQueryKey($query_key);
switch ($query_key) {
case 'all':
return $query;
case 'projects':
return $query->setParameter('viewerProjects', array('self'));
}
return parent::buildSavedQueryFromBuiltin($query_key);
}
protected function renderResultList(
array $objects,
PhabricatorSavedQuery $query,
array $handles) {
$builder = new PhabricatorFeedBuilder($objects);
if ($this->isPanelContext()) {
$builder->setShowHovercards(false);
} else {
$builder->setShowHovercards(true);
}
$builder->setUser($this->requireViewer());
$view = $builder->buildView();
$list = phutil_tag_div('phabricator-feed-frame', $view);
$result = new PhabricatorApplicationSearchResultView();
$result->setContent($list);
return $result;
}
}