|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2020 Christoph Wurst <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author 2020 Christoph Wurst <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
*/ |
|
25
|
|
|
|
|
26
|
|
|
namespace OC\Core\Controller; |
|
27
|
|
|
|
|
28
|
|
|
use OC\Search\SearchComposer; |
|
29
|
|
|
use OC\Search\SearchQuery; |
|
30
|
|
|
use OCP\AppFramework\Controller; |
|
31
|
|
|
use OCP\AppFramework\Http; |
|
32
|
|
|
use OCP\AppFramework\Http\JSONResponse; |
|
33
|
|
|
use OCP\IRequest; |
|
34
|
|
|
use OCP\IUserSession; |
|
35
|
|
|
use OCP\Route\IRouter; |
|
36
|
|
|
use OCP\Search\ISearchQuery; |
|
37
|
|
|
use Symfony\Component\Routing\Exception\ResourceNotFoundException; |
|
38
|
|
|
|
|
39
|
|
|
class UnifiedSearchController extends Controller { |
|
40
|
|
|
|
|
41
|
|
|
/** @var SearchComposer */ |
|
42
|
|
|
private $composer; |
|
43
|
|
|
|
|
44
|
|
|
/** @var IUserSession */ |
|
45
|
|
|
private $userSession; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IRouter */ |
|
48
|
|
|
private $router; |
|
49
|
|
|
|
|
50
|
|
|
public function __construct(IRequest $request, |
|
51
|
|
|
IUserSession $userSession, |
|
52
|
|
|
SearchComposer $composer, |
|
53
|
|
|
IRouter $router) { |
|
54
|
|
|
parent::__construct('core', $request); |
|
55
|
|
|
|
|
56
|
|
|
$this->composer = $composer; |
|
57
|
|
|
$this->userSession = $userSession; |
|
58
|
|
|
$this->router = $router; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @NoAdminRequired |
|
63
|
|
|
* @NoCSRFRequired |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $from the url the user is currently at |
|
66
|
|
|
* |
|
67
|
|
|
* @return JSONResponse |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getProviders(string $from = ''): JSONResponse { |
|
70
|
|
|
[$route, $parameters] = $this->getRouteInformation($from); |
|
71
|
|
|
|
|
72
|
|
|
return new JSONResponse( |
|
73
|
|
|
$this->composer->getProviders($route, $parameters) |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @NoAdminRequired |
|
79
|
|
|
* @NoCSRFRequired |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $providerId |
|
82
|
|
|
* @param string $term |
|
83
|
|
|
* @param int|null $sortOrder |
|
84
|
|
|
* @param int|null $limit |
|
85
|
|
|
* @param int|string|null $cursor |
|
86
|
|
|
* @param string $from |
|
87
|
|
|
* |
|
88
|
|
|
* @return JSONResponse |
|
89
|
|
|
*/ |
|
90
|
|
|
public function search(string $providerId, |
|
91
|
|
|
string $term = '', |
|
92
|
|
|
?int $sortOrder = null, |
|
93
|
|
|
?int $limit = null, |
|
94
|
|
|
$cursor = null, |
|
95
|
|
|
string $from = ''): JSONResponse { |
|
96
|
|
|
if (empty(trim($term))) { |
|
97
|
|
|
return new JSONResponse(null, Http::STATUS_BAD_REQUEST); |
|
98
|
|
|
} |
|
99
|
|
|
[$route, $routeParameters] = $this->getRouteInformation($from); |
|
100
|
|
|
|
|
101
|
|
|
return new JSONResponse( |
|
102
|
|
|
$this->composer->search( |
|
103
|
|
|
$this->userSession->getUser(), |
|
104
|
|
|
$providerId, |
|
105
|
|
|
new SearchQuery( |
|
106
|
|
|
$term, |
|
107
|
|
|
$sortOrder ?? ISearchQuery::SORT_DATE_DESC, |
|
108
|
|
|
$limit ?? SearchQuery::LIMIT_DEFAULT, |
|
109
|
|
|
$cursor, |
|
110
|
|
|
$route, |
|
111
|
|
|
$routeParameters |
|
112
|
|
|
) |
|
113
|
|
|
) |
|
114
|
|
|
); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
protected function getRouteInformation(string $url): array { |
|
118
|
|
|
$routeStr = ''; |
|
119
|
|
|
$parameters = []; |
|
120
|
|
|
|
|
121
|
|
|
if ($url !== '') { |
|
122
|
|
|
$urlParts = parse_url($url); |
|
123
|
|
|
|
|
124
|
|
|
try { |
|
125
|
|
|
$parameters = $this->router->findMatchingRoute($urlParts['path']); |
|
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
// contacts.PageController.index => contacts.Page.index |
|
128
|
|
|
$route = $parameters['caller']; |
|
129
|
|
|
if (substr($route[1], -10) === 'Controller') { |
|
130
|
|
|
$route[1] = substr($route[1], 0, -10); |
|
131
|
|
|
} |
|
132
|
|
|
$routeStr = implode('.', $route); |
|
133
|
|
|
|
|
134
|
|
|
// cleanup |
|
135
|
|
|
unset($parameters['_route'], $parameters['action'], $parameters['caller']); |
|
136
|
|
|
} catch (ResourceNotFoundException $exception) { |
|
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if (isset($urlParts['query'])) { |
|
140
|
|
|
parse_str($urlParts['query'], $queryParameters); |
|
141
|
|
|
$parameters = array_merge($parameters, $queryParameters); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
return [ |
|
146
|
|
|
$routeStr, |
|
147
|
|
|
$parameters, |
|
148
|
|
|
]; |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
|