2018-03-09 15:53:28 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-03-09 15:53:28 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-03-09 15:53:28 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2018-03-09 15:53:28 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-03-09 15:53:28 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OC\Core\Controller;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\JSONResponse;
|
2020-06-16 16:22:36 +03:00
|
|
|
use OCP\ILogger;
|
2018-03-09 15:53:28 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\ISearch;
|
2020-06-16 16:22:36 +03:00
|
|
|
use OCP\Search\Result;
|
2018-03-09 15:53:28 +03:00
|
|
|
|
|
|
|
class SearchController extends Controller {
|
|
|
|
|
|
|
|
/** @var ISearch */
|
|
|
|
private $searcher;
|
2020-06-16 16:22:36 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
2018-03-09 15:53:28 +03:00
|
|
|
|
2020-06-16 16:22:36 +03:00
|
|
|
public function __construct(
|
|
|
|
string $appName,
|
|
|
|
IRequest $request,
|
|
|
|
ISearch $search,
|
|
|
|
ILogger $logger
|
|
|
|
) {
|
2018-03-09 15:53:28 +03:00
|
|
|
parent::__construct($appName, $request);
|
|
|
|
|
|
|
|
$this->searcher = $search;
|
2020-06-16 16:22:36 +03:00
|
|
|
$this->logger = $logger;
|
2018-03-09 15:53:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @NoAdminRequired
|
2021-03-19 16:47:30 +03:00
|
|
|
* @NoCSRFRequired
|
2018-03-09 15:53:28 +03:00
|
|
|
*/
|
|
|
|
public function search(string $query, array $inApps = [], int $page = 1, int $size = 30): JSONResponse {
|
|
|
|
$results = $this->searcher->searchPaged($query, $inApps, $page, $size);
|
|
|
|
|
2020-06-16 16:22:36 +03:00
|
|
|
$results = array_filter($results, function (Result $result) {
|
|
|
|
if (json_encode($result, JSON_HEX_TAG) === false) {
|
|
|
|
$this->logger->warning("Skipping search result due to invalid encoding: {type: " . $result->type . ", id: " . $result->id . "}");
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-03-09 15:53:28 +03:00
|
|
|
return new JSONResponse($results);
|
|
|
|
}
|
|
|
|
}
|