Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/files/query/PhabricatorFileSearchEngine.php
12242 views
1
<?php
2
3
final class PhabricatorFileSearchEngine
4
extends PhabricatorApplicationSearchEngine {
5
6
public function getResultTypeDescription() {
7
return pht('Files');
8
}
9
10
public function getApplicationClassName() {
11
return 'PhabricatorFilesApplication';
12
}
13
14
public function canUseInPanelContext() {
15
return false;
16
}
17
18
public function newQuery() {
19
$query = new PhabricatorFileQuery();
20
$query->withIsDeleted(false);
21
return $query;
22
}
23
24
protected function buildCustomSearchFields() {
25
return array(
26
id(new PhabricatorUsersSearchField())
27
->setKey('authorPHIDs')
28
->setAliases(array('author', 'authors'))
29
->setLabel(pht('Authors')),
30
id(new PhabricatorSearchThreeStateField())
31
->setKey('explicit')
32
->setLabel(pht('Upload Source'))
33
->setOptions(
34
pht('(Show All)'),
35
pht('Show Only Manually Uploaded Files'),
36
pht('Hide Manually Uploaded Files')),
37
id(new PhabricatorSearchDateField())
38
->setKey('createdStart')
39
->setLabel(pht('Created After')),
40
id(new PhabricatorSearchDateField())
41
->setKey('createdEnd')
42
->setLabel(pht('Created Before')),
43
id(new PhabricatorSearchTextField())
44
->setLabel(pht('Name Contains'))
45
->setKey('name')
46
->setDescription(pht('Search for files by name substring.')),
47
);
48
}
49
50
protected function getDefaultFieldOrder() {
51
return array(
52
'...',
53
'createdStart',
54
'createdEnd',
55
);
56
}
57
58
protected function buildQueryFromParameters(array $map) {
59
$query = $this->newQuery();
60
61
if ($map['authorPHIDs']) {
62
$query->withAuthorPHIDs($map['authorPHIDs']);
63
}
64
65
if ($map['explicit'] !== null) {
66
$query->showOnlyExplicitUploads($map['explicit']);
67
}
68
69
if ($map['createdStart']) {
70
$query->withDateCreatedAfter($map['createdStart']);
71
}
72
73
if ($map['createdEnd']) {
74
$query->withDateCreatedBefore($map['createdEnd']);
75
}
76
77
if ($map['name'] !== null) {
78
$query->withNameNgrams($map['name']);
79
}
80
81
return $query;
82
}
83
84
protected function getURI($path) {
85
return '/file/'.$path;
86
}
87
88
protected function getBuiltinQueryNames() {
89
$names = array();
90
91
if ($this->requireViewer()->isLoggedIn()) {
92
$names['authored'] = pht('Authored');
93
}
94
95
$names += array(
96
'all' => pht('All'),
97
);
98
99
return $names;
100
}
101
102
public function buildSavedQueryFromBuiltin($query_key) {
103
$query = $this->newSavedQuery();
104
$query->setQueryKey($query_key);
105
106
switch ($query_key) {
107
case 'all':
108
return $query;
109
case 'authored':
110
$author_phid = array($this->requireViewer()->getPHID());
111
return $query
112
->setParameter('authorPHIDs', $author_phid)
113
->setParameter('explicit', true);
114
}
115
116
return parent::buildSavedQueryFromBuiltin($query_key);
117
}
118
119
protected function getRequiredHandlePHIDsForResultList(
120
array $files,
121
PhabricatorSavedQuery $query) {
122
return mpull($files, 'getAuthorPHID');
123
}
124
125
protected function renderResultList(
126
array $files,
127
PhabricatorSavedQuery $query,
128
array $handles) {
129
130
assert_instances_of($files, 'PhabricatorFile');
131
132
$request = $this->getRequest();
133
if ($request) {
134
$highlighted_ids = $request->getStrList('h');
135
} else {
136
$highlighted_ids = array();
137
}
138
139
$viewer = $this->requireViewer();
140
141
$highlighted_ids = array_fill_keys($highlighted_ids, true);
142
143
$list_view = id(new PHUIObjectItemListView())
144
->setUser($viewer);
145
146
foreach ($files as $file) {
147
$id = $file->getID();
148
$phid = $file->getPHID();
149
$name = $file->getName();
150
$file_uri = $this->getApplicationURI("/info/{$phid}/");
151
152
$date_created = phabricator_date($file->getDateCreated(), $viewer);
153
$author_phid = $file->getAuthorPHID();
154
if ($author_phid) {
155
$author_link = $handles[$author_phid]->renderLink();
156
$uploaded = pht('Uploaded by %s on %s', $author_link, $date_created);
157
} else {
158
$uploaded = pht('Uploaded on %s', $date_created);
159
}
160
161
$item = id(new PHUIObjectItemView())
162
->setObject($file)
163
->setObjectName("F{$id}")
164
->setHeader($name)
165
->setHref($file_uri)
166
->addAttribute($uploaded)
167
->addIcon('none', phutil_format_bytes($file->getByteSize()));
168
169
$ttl = $file->getTTL();
170
if ($ttl !== null) {
171
$item->addIcon('blame', pht('Temporary'));
172
}
173
174
if ($file->getIsPartial()) {
175
$item->addIcon('fa-exclamation-triangle orange', pht('Partial'));
176
}
177
178
if (isset($highlighted_ids[$id])) {
179
$item->setEffect('highlighted');
180
}
181
182
$list_view->addItem($item);
183
}
184
185
$list_view->appendChild(id(new PhabricatorGlobalUploadTargetView())
186
->setUser($viewer));
187
188
189
$result = new PhabricatorApplicationSearchResultView();
190
$result->setContent($list_view);
191
192
return $result;
193
}
194
195
protected function getNewUserBody() {
196
$create_button = id(new PHUIButtonView())
197
->setTag('a')
198
->setText(pht('Upload a File'))
199
->setHref('/file/upload/')
200
->setColor(PHUIButtonView::GREEN);
201
202
$icon = $this->getApplication()->getIcon();
203
$app_name = $this->getApplication()->getName();
204
$view = id(new PHUIBigInfoView())
205
->setIcon($icon)
206
->setTitle(pht('Welcome to %s', $app_name))
207
->setDescription(
208
pht('Just a place for files.'))
209
->addAction($create_button);
210
211
return $view;
212
}
213
214
}
215
216