Correct a parameter order swap in "diffusion.historyquery" for Mercurial
[phabricator.git] / src / applications / files / query / PhabricatorFileSearchEngine.php
blob59810c830c37d804fc401e6bb5a339c73a7ac73e
1 <?php
3 final class PhabricatorFileSearchEngine
4 extends PhabricatorApplicationSearchEngine {
6 public function getResultTypeDescription() {
7 return pht('Files');
10 public function getApplicationClassName() {
11 return 'PhabricatorFilesApplication';
14 public function canUseInPanelContext() {
15 return false;
18 public function newQuery() {
19 $query = new PhabricatorFileQuery();
20 $query->withIsDeleted(false);
21 return $query;
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.')),
50 protected function getDefaultFieldOrder() {
51 return array(
52 '...',
53 'createdStart',
54 'createdEnd',
58 protected function buildQueryFromParameters(array $map) {
59 $query = $this->newQuery();
61 if ($map['authorPHIDs']) {
62 $query->withAuthorPHIDs($map['authorPHIDs']);
65 if ($map['explicit'] !== null) {
66 $query->showOnlyExplicitUploads($map['explicit']);
69 if ($map['createdStart']) {
70 $query->withDateCreatedAfter($map['createdStart']);
73 if ($map['createdEnd']) {
74 $query->withDateCreatedBefore($map['createdEnd']);
77 if ($map['name'] !== null) {
78 $query->withNameNgrams($map['name']);
81 return $query;
84 protected function getURI($path) {
85 return '/file/'.$path;
88 protected function getBuiltinQueryNames() {
89 $names = array();
91 if ($this->requireViewer()->isLoggedIn()) {
92 $names['authored'] = pht('Authored');
95 $names += array(
96 'all' => pht('All'),
99 return $names;
102 public function buildSavedQueryFromBuiltin($query_key) {
103 $query = $this->newSavedQuery();
104 $query->setQueryKey($query_key);
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);
116 return parent::buildSavedQueryFromBuiltin($query_key);
119 protected function getRequiredHandlePHIDsForResultList(
120 array $files,
121 PhabricatorSavedQuery $query) {
122 return mpull($files, 'getAuthorPHID');
125 protected function renderResultList(
126 array $files,
127 PhabricatorSavedQuery $query,
128 array $handles) {
130 assert_instances_of($files, 'PhabricatorFile');
132 $request = $this->getRequest();
133 if ($request) {
134 $highlighted_ids = $request->getStrList('h');
135 } else {
136 $highlighted_ids = array();
139 $viewer = $this->requireViewer();
141 $highlighted_ids = array_fill_keys($highlighted_ids, true);
143 $list_view = id(new PHUIObjectItemListView())
144 ->setUser($viewer);
146 foreach ($files as $file) {
147 $id = $file->getID();
148 $phid = $file->getPHID();
149 $name = $file->getName();
150 $file_uri = $this->getApplicationURI("/info/{$phid}/");
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);
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()));
169 $ttl = $file->getTTL();
170 if ($ttl !== null) {
171 $item->addIcon('blame', pht('Temporary'));
174 if ($file->getIsPartial()) {
175 $item->addIcon('fa-exclamation-triangle orange', pht('Partial'));
178 if (isset($highlighted_ids[$id])) {
179 $item->setEffect('highlighted');
182 $list_view->addItem($item);
185 $list_view->appendChild(id(new PhabricatorGlobalUploadTargetView())
186 ->setUser($viewer));
189 $result = new PhabricatorApplicationSearchResultView();
190 $result->setContent($list_view);
192 return $result;
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);
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);
211 return $view;