1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Superdesk Web Publisher ElasticSearch Bundle. |
7
|
|
|
* |
8
|
|
|
* Copyright 2017 Sourcefabric z.ú. and contributors. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please see the |
11
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* @copyright 2017 Sourcefabric z.ú |
14
|
|
|
* @license http://www.superdesk.org/license |
15
|
|
|
*/ |
16
|
|
|
|
17
|
|
|
namespace SWP\Bundle\ElasticSearchBundle\Controller\Api; |
18
|
|
|
|
19
|
|
|
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface; |
20
|
|
|
use SWP\Bundle\ElasticSearchBundle\Criteria\Criteria; |
21
|
|
|
use SWP\Bundle\ElasticSearchBundle\Repository\ArticleRepository; |
22
|
|
|
use SWP\Component\Common\Response\ResourcesListResponse; |
23
|
|
|
use SWP\Component\Common\Response\ResponseContext; |
24
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
25
|
|
|
use Symfony\Component\HttpFoundation\Request; |
26
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
27
|
|
|
|
28
|
|
|
class ArticleSearchController extends Controller |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @Route("/api/{version}/content/articles/", methods={"GET"}, options={"expose"=true}, defaults={"version"="v2"}, name="swp_api_content_list_articles") |
32
|
|
|
*/ |
33
|
|
|
public function searchAction(Request $request, RepositoryManagerInterface $repositoryManager) |
34
|
|
|
{ |
35
|
|
|
$criteria = $this->createCriteriaFrom($request); |
36
|
|
|
$extraFields = $this->get('service_container')->getParameter('env(ELASTICA_ARTICLE_EXTRA_FIELDS)'); |
37
|
|
|
|
38
|
|
|
$options = [ |
39
|
|
|
'sortNestedPath' => 'articleStatistics.pageViewsNumber', |
40
|
|
|
]; |
41
|
|
|
|
42
|
|
|
/** @var ArticleRepository $repository */ |
43
|
|
|
$repository = $repositoryManager->getRepository($this->getParameter('swp.model.article.class')); |
44
|
|
|
$articles = $repository->findByCriteria($criteria, json_decode($extraFields, true)); |
45
|
|
|
$paginator = $this->get('knp_paginator'); |
46
|
|
|
$pagination = $paginator->paginate( |
47
|
|
|
$articles, |
48
|
|
|
$request->query->get('page', 1), |
49
|
|
|
$criteria->getPagination()->getItemsPerPage(), |
50
|
|
|
$options |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$responseContext = new ResponseContext(); |
54
|
|
|
$responseContext->setSerializationGroups($this->getSerializationGroups()); |
55
|
|
|
|
56
|
|
|
return new ResourcesListResponse($pagination, $responseContext); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function createCriteriaFrom(Request $request): Criteria |
60
|
|
|
{ |
61
|
|
|
return Criteria::fromQueryParameters( |
62
|
|
|
$request->query->get('term', ''), |
63
|
|
|
array_merge($this->createDefaultCriteria($request), $this->createAdditionalCriteria($request) |
64
|
|
|
)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function createDefaultCriteria(Request $request): array |
68
|
|
|
{ |
69
|
|
|
$currentTenant = $this->get('swp_multi_tenancy.tenant_context')->getTenant(); |
70
|
|
|
|
71
|
|
|
return [ |
72
|
|
|
'page' => $request->query->get('page'), |
73
|
|
|
'sort' => $request->query->get('sorting'), |
74
|
|
|
'limit' => $request->query->get('limit', 10), |
75
|
|
|
'tenantCode' => $currentTenant->getCode(), |
76
|
|
|
]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
protected function getSerializationGroups(): array |
80
|
|
|
{ |
81
|
|
|
return [ |
82
|
|
|
'Default', |
83
|
|
|
'api_articles_list', |
84
|
|
|
'api_articles_featuremedia', |
85
|
|
|
'api_article_authors', |
86
|
|
|
'api_article_media_list', |
87
|
|
|
'api_article_media_renditions', |
88
|
|
|
'api_image_details', |
89
|
|
|
'api_routes_list', |
90
|
|
|
'api_tenant_list', |
91
|
|
|
'api_articles_statistics_list', |
92
|
|
|
]; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
protected function createAdditionalCriteria(Request $request): array |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'routes' => array_filter((array) $request->query->get('route', [])), |
99
|
|
|
'statuses' => array_filter((array) $request->query->get('status', [])), |
100
|
|
|
'authors' => array_filter((array) $request->query->get('author', [])), |
101
|
|
|
'publishedBefore' => $request->query->has('published_before') ? new \DateTime($request->query->get('published_before')) : null, |
102
|
|
|
'publishedAfter' => $request->query->has('published_after') ? new \DateTime($request->query->get('published_after')) : null, |
103
|
|
|
'publishedAt' => $request->query->get('published_at'), |
104
|
|
|
'sources' => array_filter((array) $request->query->get('source', [])), |
105
|
|
|
'metadata' => array_filter((array) $request->query->get('metadata', [])), |
106
|
|
|
'keywords' => array_filter((array) $request->query->get('keywords', [])), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.