Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/paste/query/PhabricatorPasteSearchEngine.php
12241 views
1
<?php
2
3
final class PhabricatorPasteSearchEngine
4
extends PhabricatorApplicationSearchEngine {
5
6
public function getResultTypeDescription() {
7
return pht('Pastes');
8
}
9
10
public function getApplicationClassName() {
11
return 'PhabricatorPasteApplication';
12
}
13
14
public function newQuery() {
15
return id(new PhabricatorPasteQuery())
16
->needSnippets(true);
17
}
18
19
protected function buildQueryFromParameters(array $map) {
20
$query = $this->newQuery();
21
22
if ($map['authorPHIDs']) {
23
$query->withAuthorPHIDs($map['authorPHIDs']);
24
}
25
26
if ($map['languages']) {
27
$query->withLanguages($map['languages']);
28
}
29
30
if ($map['createdStart']) {
31
$query->withDateCreatedAfter($map['createdStart']);
32
}
33
34
if ($map['createdEnd']) {
35
$query->withDateCreatedBefore($map['createdEnd']);
36
}
37
38
if ($map['statuses']) {
39
$query->withStatuses($map['statuses']);
40
}
41
42
return $query;
43
}
44
45
protected function buildCustomSearchFields() {
46
return array(
47
id(new PhabricatorUsersSearchField())
48
->setAliases(array('authors'))
49
->setKey('authorPHIDs')
50
->setConduitKey('authors')
51
->setLabel(pht('Authors'))
52
->setDescription(
53
pht('Search for pastes with specific authors.')),
54
id(new PhabricatorSearchStringListField())
55
->setKey('languages')
56
->setLabel(pht('Languages'))
57
->setDescription(
58
pht('Search for pastes highlighted in specific languages.')),
59
id(new PhabricatorSearchDateField())
60
->setKey('createdStart')
61
->setLabel(pht('Created After'))
62
->setDescription(
63
pht('Search for pastes created after a given time.')),
64
id(new PhabricatorSearchDateField())
65
->setKey('createdEnd')
66
->setLabel(pht('Created Before'))
67
->setDescription(
68
pht('Search for pastes created before a given time.')),
69
id(new PhabricatorSearchCheckboxesField())
70
->setKey('statuses')
71
->setLabel(pht('Status'))
72
->setDescription(
73
pht('Search for archived or active pastes.'))
74
->setOptions(
75
id(new PhabricatorPaste())
76
->getStatusNameMap()),
77
);
78
}
79
80
protected function getDefaultFieldOrder() {
81
return array(
82
'...',
83
'createdStart',
84
'createdEnd',
85
);
86
}
87
88
protected function getURI($path) {
89
return '/paste/'.$path;
90
}
91
92
protected function getBuiltinQueryNames() {
93
$names = array(
94
'active' => pht('Active Pastes'),
95
'all' => pht('All Pastes'),
96
);
97
98
if ($this->requireViewer()->isLoggedIn()) {
99
$names['authored'] = pht('Authored');
100
}
101
102
return $names;
103
}
104
105
public function buildSavedQueryFromBuiltin($query_key) {
106
107
$query = $this->newSavedQuery();
108
$query->setQueryKey($query_key);
109
110
switch ($query_key) {
111
case 'active':
112
return $query->setParameter(
113
'statuses',
114
array(
115
PhabricatorPaste::STATUS_ACTIVE,
116
));
117
case 'all':
118
return $query;
119
case 'authored':
120
return $query->setParameter(
121
'authorPHIDs',
122
array($this->requireViewer()->getPHID()));
123
}
124
125
return parent::buildSavedQueryFromBuiltin($query_key);
126
}
127
128
protected function getRequiredHandlePHIDsForResultList(
129
array $pastes,
130
PhabricatorSavedQuery $query) {
131
return mpull($pastes, 'getAuthorPHID');
132
}
133
134
protected function renderResultList(
135
array $pastes,
136
PhabricatorSavedQuery $query,
137
array $handles) {
138
assert_instances_of($pastes, 'PhabricatorPaste');
139
140
$viewer = $this->requireViewer();
141
142
$lang_map = PhabricatorEnv::getEnvConfig('pygments.dropdown-choices');
143
144
$list = new PHUIObjectItemListView();
145
$list->setUser($viewer);
146
foreach ($pastes as $paste) {
147
$created = phabricator_date($paste->getDateCreated(), $viewer);
148
$author = $handles[$paste->getAuthorPHID()]->renderLink();
149
150
$snippet_type = $paste->getSnippet()->getType();
151
$lines = phutil_split_lines($paste->getSnippet()->getContent());
152
153
$preview = id(new PhabricatorSourceCodeView())
154
->setLines($lines)
155
->setTruncatedFirstBytes(
156
$snippet_type == PhabricatorPasteSnippet::FIRST_BYTES)
157
->setTruncatedFirstLines(
158
$snippet_type == PhabricatorPasteSnippet::FIRST_LINES)
159
->setURI(new PhutilURI($paste->getURI()));
160
161
$source_code = phutil_tag(
162
'div',
163
array(
164
'class' => 'phabricator-source-code-summary',
165
),
166
$preview);
167
168
$created = phabricator_datetime($paste->getDateCreated(), $viewer);
169
$line_count = $paste->getSnippet()->getContentLineCount();
170
$line_count = pht(
171
'%s Line(s)',
172
new PhutilNumber($line_count));
173
174
$title = nonempty($paste->getTitle(), pht('(An Untitled Masterwork)'));
175
176
$item = id(new PHUIObjectItemView())
177
->setObjectName('P'.$paste->getID())
178
->setHeader($title)
179
->setHref('/P'.$paste->getID())
180
->setObject($paste)
181
->addByline(pht('Author: %s', $author))
182
->addIcon('none', $created)
183
->addIcon('none', $line_count)
184
->appendChild($source_code);
185
186
if ($paste->isArchived()) {
187
$item->setDisabled(true);
188
}
189
190
$lang_name = $paste->getLanguage();
191
if ($lang_name) {
192
$lang_name = idx($lang_map, $lang_name, $lang_name);
193
$item->addIcon('none', $lang_name);
194
}
195
196
$list->addItem($item);
197
}
198
199
$result = new PhabricatorApplicationSearchResultView();
200
$result->setObjectList($list);
201
$result->setNoDataString(pht('No pastes found.'));
202
203
return $result;
204
}
205
206
protected function getNewUserBody() {
207
$viewer = $this->requireViewer();
208
209
$create_button = id(new PhabricatorPasteEditEngine())
210
->setViewer($viewer)
211
->newNUXButton(pht('Create a Paste'));
212
213
$icon = $this->getApplication()->getIcon();
214
$app_name = $this->getApplication()->getName();
215
$view = id(new PHUIBigInfoView())
216
->setIcon($icon)
217
->setTitle(pht('Welcome to %s', $app_name))
218
->setDescription(
219
pht('Store, share, and embed snippets of code.'))
220
->addAction($create_button);
221
222
return $view;
223
}
224
}
225
226