From 4f441a9f7860ae0b43fa3e4f3e7e646bec5671c3 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Wed, 24 Feb 2021 13:14:42 +0100 Subject: [PATCH 1/6] Implement unified search provider Signed-off-by: Philipp Fischbeck --- lib/AppInfo/Application.php | 24 +++++++++++ lib/Search/Provider.php | 85 +++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 lib/AppInfo/Application.php create mode 100644 lib/Search/Provider.php diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php new file mode 100644 index 000000000..bf1450617 --- /dev/null +++ b/lib/AppInfo/Application.php @@ -0,0 +1,24 @@ +registerSearchProvider(Provider::class); + } + + public function boot(IBootContext $context): void { + } +} diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php new file mode 100644 index 000000000..2ebfa4e75 --- /dev/null +++ b/lib/Search/Provider.php @@ -0,0 +1,85 @@ +l = $il10n; + $this->urlGenerator = $urlGenerator; + $this->recipeDb = $recipeDb; + $this->recipeService = $recipeService; + } + + public function getId(): string { + return Application::APP_ID; + } + + public function getName(): string { + return $this->l->t('Recipes'); + } + + public function getOrder(string $route, array $routeParameters): int { + if (strpos($route, 'files' . '.') === 0) { + return 25; + } elseif (strpos($route, Application::APP_ID . '.') === 0) { + return -1; + } + return 4; + } + + public function search(IUser $user, ISearchQuery $query): SearchResult { + $recipes = $this->recipeService->findRecipesInSearchIndex($query->getTerm()); + $result = array_map( + function (array $recipe) use ($user) : SearchResultEntry { + $id = $recipe['recipe_id']; + + $subline = ''; + $category = $this->recipeDb->getCategoryOfRecipe($id, $user->getUID()); + if ($category !== null) { + // TRANSLATORS Will be shown in search results, listing the recipe category, e.g., 'in Salads' + $subline = $this->l->t('in %s', [$category]); + } + + return new SearchResultEntry( + // Thumb image + $this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $id, 'size' => 'thumb']), + // Name as title + $recipe['name'], + // Category as subline + $subline, + // Link to Vue route of recipe + $this->urlGenerator->linkToRouteAbsolute('cookbook.main.index') . '#/recipe/' . $id + ); + }, $recipes + ); + + return SearchResult::complete( + $this->getName(), + $result + ); + } +} From 4d7d9002b81101406cf509002d2651de16b818e4 Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Wed, 17 Mar 2021 22:05:53 +0100 Subject: [PATCH 2/6] Support NC <= 19 Signed-off-by: Philipp Fischbeck --- lib/AppInfo/Application.php | 25 ++++---- lib/Search/Provider.php | 115 +++++++++++++++++++----------------- 2 files changed, 75 insertions(+), 65 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index bf1450617..384fc7c85 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -7,18 +7,23 @@ use OCP\AppFramework\Bootstrap\IBootContext; use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; +use OCP\Util; -class Application extends App implements IBootstrap { - public const APP_ID = 'cookbook'; - - public function __construct(array $urlParams = []) { - parent::__construct(self::APP_ID, $urlParams); - } +// IBootstrap requies NC >= 20 +// Remove conditional once we end support for NC 19 +if (Util::getVersion()[0] >= 20) { + class Application extends App implements IBootstrap { + public const APP_ID = 'cookbook'; + + public function __construct(array $urlParams = []) { + parent::__construct(self::APP_ID, $urlParams); + } - public function register(IRegistrationContext $context): void { - $context->registerSearchProvider(Provider::class); - } + public function register(IRegistrationContext $context): void { + $context->registerSearchProvider(Provider::class); + } - public function boot(IBootContext $context): void { + public function boot(IBootContext $context): void { + } } } diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php index 2ebfa4e75..cae623ffa 100644 --- a/lib/Search/Provider.php +++ b/lib/Search/Provider.php @@ -12,74 +12,79 @@ use OCP\Search\ISearchQuery; use OCP\Search\SearchResult; use OCP\Search\SearchResultEntry; +use OCP\Util; -class Provider implements IProvider { +// IProvider requies NC >= 20 +// Remove conditional once we end support for NC 19 +if (Util::getVersion()[0] >= 20) { + class Provider implements IProvider { - /** @var IL10N */ - private $l; + /** @var IL10N */ + private $l; - /** @var IUrlGenerator */ - private $urlGenerator; + /** @var IUrlGenerator */ + private $urlGenerator; - /** @var RecipeDb */ - private $recipeDb; + /** @var RecipeDb */ + private $recipeDb; - /** @var RecipeService */ - private $recipeService; + /** @var RecipeService */ + private $recipeService; - public function __construct(IL10n $il10n, IUrlGenerator $urlGenerator, RecipeDb $recipeDb, RecipeService $recipeService) { - $this->l = $il10n; - $this->urlGenerator = $urlGenerator; - $this->recipeDb = $recipeDb; - $this->recipeService = $recipeService; - } + public function __construct(IL10n $il10n, IUrlGenerator $urlGenerator, RecipeDb $recipeDb, RecipeService $recipeService) { + $this->l = $il10n; + $this->urlGenerator = $urlGenerator; + $this->recipeDb = $recipeDb; + $this->recipeService = $recipeService; + } - public function getId(): string { - return Application::APP_ID; - } + public function getId(): string { + return Application::APP_ID; + } - public function getName(): string { - return $this->l->t('Recipes'); - } + public function getName(): string { + return $this->l->t('Recipes'); + } - public function getOrder(string $route, array $routeParameters): int { - if (strpos($route, 'files' . '.') === 0) { - return 25; - } elseif (strpos($route, Application::APP_ID . '.') === 0) { - return -1; + public function getOrder(string $route, array $routeParameters): int { + if (strpos($route, 'files' . '.') === 0) { + return 25; + } elseif (strpos($route, Application::APP_ID . '.') === 0) { + return -1; + } + return 4; } - return 4; - } - public function search(IUser $user, ISearchQuery $query): SearchResult { - $recipes = $this->recipeService->findRecipesInSearchIndex($query->getTerm()); - $result = array_map( - function (array $recipe) use ($user) : SearchResultEntry { - $id = $recipe['recipe_id']; + public function search(IUser $user, ISearchQuery $query): SearchResult { + $recipes = $this->recipeService->findRecipesInSearchIndex($query->getTerm()); + $result = array_map( + function (array $recipe) use ($user) : SearchResultEntry { + $id = $recipe['recipe_id']; - $subline = ''; - $category = $this->recipeDb->getCategoryOfRecipe($id, $user->getUID()); - if ($category !== null) { - // TRANSLATORS Will be shown in search results, listing the recipe category, e.g., 'in Salads' - $subline = $this->l->t('in %s', [$category]); - } + $subline = ''; + $category = $this->recipeDb->getCategoryOfRecipe($id, $user->getUID()); + if ($category !== null) { + // TRANSLATORS Will be shown in search results, listing the recipe category, e.g., 'in Salads' + $subline = $this->l->t('in %s', [$category]); + } - return new SearchResultEntry( - // Thumb image - $this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $id, 'size' => 'thumb']), - // Name as title - $recipe['name'], - // Category as subline - $subline, - // Link to Vue route of recipe - $this->urlGenerator->linkToRouteAbsolute('cookbook.main.index') . '#/recipe/' . $id - ); - }, $recipes - ); + return new SearchResultEntry( + // Thumb image + $this->urlGenerator->linkToRoute('cookbook.recipe.image', ['id' => $id, 'size' => 'thumb']), + // Name as title + $recipe['name'], + // Category as subline + $subline, + // Link to Vue route of recipe + $this->urlGenerator->linkToRouteAbsolute('cookbook.main.index') . '#/recipe/' . $id + ); + }, $recipes + ); - return SearchResult::complete( - $this->getName(), - $result - ); + return SearchResult::complete( + $this->getName(), + $result + ); + } } } From 520dbd7cceaa102a10cc2ace2abb276e215af57f Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 22 Mar 2021 17:32:08 +0100 Subject: [PATCH 3/6] Provide class definitions for NC 19 Signed-off-by: Philipp Fischbeck --- lib/AppInfo/Application.php | 8 ++++++++ lib/Search/Provider.php | 3 +++ 2 files changed, 11 insertions(+) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 384fc7c85..47bfecc1d 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -26,4 +26,12 @@ public function register(IRegistrationContext $context): void { public function boot(IBootContext $context): void { } } +} else { + class Application extends App { + public const APP_ID = 'cookbook'; + + public function __construct(array $urlParams = []) { + parent::__construct(self::APP_ID, $urlParams); + } + } } diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php index cae623ffa..2ae5044ca 100644 --- a/lib/Search/Provider.php +++ b/lib/Search/Provider.php @@ -87,4 +87,7 @@ function (array $recipe) use ($user) : SearchResultEntry { ); } } +} else { + class Provider { + } } From 86dace58c9247165226625bb56010c3df22c0e1f Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Mon, 22 Mar 2021 17:36:45 +0100 Subject: [PATCH 4/6] Add changelog entry Signed-off-by: Philipp Fischbeck --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89614e07d..ed3c51475 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ ## [Unreleased] +### Added +- Make recipes searchable through unified search + [#611](https://github.com/nextcloud/cookbook/pull/611) @PFischbeck + ### Fixed - Calling reindex [#653](https://github.com/nextcloud/cookbook/pull/653) @seyfeb From 599c868664cf06049dc1511519470090108787ea Mon Sep 17 00:00:00 2001 From: Philipp Fischbeck Date: Sat, 27 Mar 2021 21:14:15 +0100 Subject: [PATCH 5/6] Fix IURLGenerator case Co-authored-by: Christian Signed-off-by: Philipp Fischbeck --- lib/Search/Provider.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php index 2ae5044ca..6c9015b5a 100644 --- a/lib/Search/Provider.php +++ b/lib/Search/Provider.php @@ -7,7 +7,7 @@ use OCA\Cookbook\Service\RecipeService; use OCP\IL10N; use OCP\IUser; -use OCP\IUrlGenerator; +use OCP\IURLGenerator; use OCP\Search\IProvider; use OCP\Search\ISearchQuery; use OCP\Search\SearchResult; @@ -22,7 +22,7 @@ class Provider implements IProvider { /** @var IL10N */ private $l; - /** @var IUrlGenerator */ + /** @var IURLGenerator */ private $urlGenerator; /** @var RecipeDb */ @@ -31,7 +31,8 @@ class Provider implements IProvider { /** @var RecipeService */ private $recipeService; - public function __construct(IL10n $il10n, IUrlGenerator $urlGenerator, RecipeDb $recipeDb, RecipeService $recipeService) { + public function __construct(IL10n $il10n, IURLGenerator $urlGenerator, + RecipeDb $recipeDb, RecipeService $recipeService) { $this->l = $il10n; $this->urlGenerator = $urlGenerator; $this->recipeDb = $recipeDb; From a69938d9b27f09649bc511caa687848e8ffbacd2 Mon Sep 17 00:00:00 2001 From: Christian Wolf Date: Fri, 2 Apr 2021 19:11:42 +0200 Subject: [PATCH 6/6] Corrected styling according to php-cs-fixer (auto-fix) Signed-off-by: Christian Wolf --- lib/AppInfo/Application.php | 6 ++++-- lib/Search/Provider.php | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 47bfecc1d..e3256ba6e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -12,7 +12,8 @@ // IBootstrap requies NC >= 20 // Remove conditional once we end support for NC 19 if (Util::getVersion()[0] >= 20) { - class Application extends App implements IBootstrap { + class Application extends App implements IBootstrap +{ public const APP_ID = 'cookbook'; public function __construct(array $urlParams = []) { @@ -27,7 +28,8 @@ public function boot(IBootContext $context): void { } } } else { - class Application extends App { + class Application extends App +{ public const APP_ID = 'cookbook'; public function __construct(array $urlParams = []) { diff --git a/lib/Search/Provider.php b/lib/Search/Provider.php index 6c9015b5a..26bb5c73c 100644 --- a/lib/Search/Provider.php +++ b/lib/Search/Provider.php @@ -17,7 +17,8 @@ // IProvider requies NC >= 20 // Remove conditional once we end support for NC 19 if (Util::getVersion()[0] >= 20) { - class Provider implements IProvider { + class Provider implements IProvider +{ /** @var IL10N */ private $l; @@ -89,6 +90,7 @@ function (array $recipe) use ($user) : SearchResultEntry { } } } else { - class Provider { + class Provider +{ } }