@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.)
hq.recaptime.dev/wiki/Phorge
phorge
phabricator
1<?php
2
3final class PhabricatorPhurlURLSearchEngine
4 extends PhabricatorApplicationSearchEngine {
5
6 public function getResultTypeDescription() {
7 return pht('Phurl URLs');
8 }
9
10 public function getApplicationClassName() {
11 return PhabricatorPhurlApplication::class;
12 }
13
14 public function newQuery() {
15 return new PhabricatorPhurlURLQuery();
16 }
17
18 protected function shouldShowOrderField() {
19 return true;
20 }
21
22 protected function buildCustomSearchFields() {
23 return array(
24 id(new PhabricatorSearchDatasourceField())
25 ->setLabel(pht('Created By'))
26 ->setKey('authorPHIDs')
27 ->setDescription(
28 pht('Search for Phurl URLs created by specific authors.'))
29 ->setDatasource(new PhabricatorPeopleUserFunctionDatasource()),
30 id(new PhabricatorSearchTextField())
31 ->setLabel(pht('Name Contains'))
32 ->setKey('name')
33 ->setDescription(pht('Search for Phurl URLs by name substring.')),
34 id(new PhabricatorSearchStringListField())
35 ->setLabel(pht('Aliases'))
36 ->setKey('aliases')
37 ->setDescription(pht('Search for Phurl URLs by alias.')),
38 id(new PhabricatorSearchStringListField())
39 ->setLabel(pht('Long URLs'))
40 ->setKey('longurls')
41 ->setDescription(
42 pht('Search for Phurl URLs by the non-shortened URL.')),
43 );
44 }
45
46 protected function buildQueryFromParameters(array $map) {
47 $query = $this->newQuery();
48
49 if ($map['authorPHIDs']) {
50 $query->withAuthorPHIDs($map['authorPHIDs']);
51 }
52
53 if ($map['name'] !== null) {
54 $query->withNameNgrams($map['name']);
55 }
56
57 if ($map['aliases']) {
58 $query->withAliases($map['aliases']);
59 }
60
61 if ($map['longurls']) {
62 $query->withLongURLs($map['longurls']);
63 }
64
65 return $query;
66 }
67
68 protected function getURI($path) {
69 return '/phurl/'.$path;
70 }
71
72 protected function getBuiltinQueryNames() {
73 $names = array(
74 'all' => pht('All URLs'),
75 'authored' => pht('Authored'),
76 );
77
78 return $names;
79 }
80
81 public function buildSavedQueryFromBuiltin($query_key) {
82 $query = $this->newSavedQuery();
83 $query->setQueryKey($query_key);
84 $viewer = $this->requireViewer();
85
86 switch ($query_key) {
87 case 'authored':
88 return $query->setParameter('authorPHIDs', array($viewer->getPHID()));
89 case 'all':
90 return $query;
91 }
92
93 return parent::buildSavedQueryFromBuiltin($query_key);
94 }
95
96 /**
97 * @param array<PhabricatorPhurlURL> $urls
98 * @param PhabricatorSavedQuery $query
99 * @param array<PhabricatorObjectHandle> $handles
100 */
101 protected function renderResultList(
102 array $urls,
103 PhabricatorSavedQuery $query,
104 array $handles) {
105
106 assert_instances_of($urls, PhabricatorPhurlURL::class);
107 $viewer = $this->requireViewer();
108 $list = new PHUIObjectItemListView();
109 $handles = $viewer->loadHandles(mpull($urls, 'getAuthorPHID'));
110
111 foreach ($urls as $url) {
112 $name = $url->getName();
113
114 $item = id(new PHUIObjectItemView())
115 ->setUser($viewer)
116 ->setObject($url)
117 ->setObjectName('U'.$url->getID())
118 ->setHeader($name)
119 ->setHref('/U'.$url->getID())
120 ->addAttribute($url->getAlias())
121 ->addAttribute($url->getLongURL());
122
123 $list->addItem($item);
124 }
125
126 $result = new PhabricatorApplicationSearchResultView();
127 $result->setObjectList($list);
128 $result->setNoDataString(pht('No URLs found.'));
129
130 return $result;
131 }
132
133 protected function getNewUserBody() {
134 $create_uri = id(new PhabricatorPhurlURLEditEngine())
135 ->getEditURI();
136
137 $create_button = id(new PHUIButtonView())
138 ->setTag('a')
139 ->setText(pht('Shorten a URL'))
140 ->setHref($create_uri)
141 ->setColor(PHUIButtonView::GREEN);
142
143 $icon = $this->getApplication()->getIcon();
144 $app_name = $this->getApplication()->getName();
145 $view = id(new PHUIBigInfoView())
146 ->setIcon($icon)
147 ->setTitle(pht('Welcome to %s', $app_name))
148 ->setDescription(
149 pht('Create reusable, memorable, shorter URLs for easy accessibility.'))
150 ->addAction($create_button);
151
152 return $view;
153 }
154}