3232use OCP \AppFramework \Http \JSONResponse ;
3333use OCP \IRequest ;
3434use OCP \IUserSession ;
35+ use OCP \Route \IRouter ;
3536use OCP \Search \ISearchQuery ;
37+ use Symfony \Component \Routing \Exception \ResourceNotFoundException ;
3638
3739class UnifiedSearchController extends Controller {
3840
@@ -42,26 +44,33 @@ class UnifiedSearchController extends Controller {
4244 /** @var IUserSession */
4345 private $ userSession ;
4446
47+ /** @var IRouter */
48+ private $ router ;
49+
4550 public function __construct (IRequest $ request ,
4651 IUserSession $ userSession ,
47- SearchComposer $ composer ) {
52+ SearchComposer $ composer ,
53+ IRouter $ router ) {
4854 parent ::__construct ('core ' , $ request );
4955
5056 $ this ->composer = $ composer ;
5157 $ this ->userSession = $ userSession ;
58+ $ this ->router = $ router ;
5259 }
5360
5461 /**
5562 * @NoAdminRequired
5663 * @NoCSRFRequired
57- *
64+ *
5865 * @param string $from the url the user is currently at
59- *
66+ *
6067 * @return JSONResponse
6168 */
62- public function getProviders (string $ from ): JSONResponse {
69+ public function getProviders (string $ from = '' ): JSONResponse {
70+ [$ route , $ parameters ] = $ this ->getRouteInformation ($ from );
71+
6372 return new JSONResponse (
64- $ this ->composer ->getProviders ($ from )
73+ $ this ->composer ->getProviders ($ route , $ parameters )
6574 );
6675 }
6776
@@ -74,17 +83,20 @@ public function getProviders(string $from): JSONResponse {
7483 * @param int|null $sortOrder
7584 * @param int|null $limit
7685 * @param int|string|null $cursor
86+ * @param string $from
7787 *
7888 * @return JSONResponse
7989 */
8090 public function search (string $ providerId ,
8191 string $ term = '' ,
8292 ?int $ sortOrder = null ,
8393 ?int $ limit = null ,
84- $ cursor = null ): JSONResponse {
94+ $ cursor = null ,
95+ string $ from = '' ): JSONResponse {
8596 if (empty (trim ($ term ))) {
8697 return new JSONResponse (null , Http::STATUS_BAD_REQUEST );
8798 }
99+ [$ route , $ routeParameters ] = $ this ->getRouteInformation ($ from );
88100
89101 return new JSONResponse (
90102 $ this ->composer ->search (
@@ -94,9 +106,45 @@ public function search(string $providerId,
94106 $ term ,
95107 $ sortOrder ?? ISearchQuery::SORT_DATE_DESC ,
96108 $ limit ?? SearchQuery::LIMIT_DEFAULT ,
97- $ cursor
109+ $ cursor ,
110+ $ route ,
111+ $ routeParameters
98112 )
99113 )
100114 );
101115 }
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+ }
102150}
0 commit comments