1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\Domain\Search\ApacheSolrDocument; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 Rafael Kähm <[email protected]> |
8
|
|
|
* All rights reserved |
9
|
|
|
* |
10
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
11
|
|
|
* free software; you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU General Public License as published by |
13
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
14
|
|
|
* (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* The GNU General Public License can be found at |
17
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
18
|
|
|
* |
19
|
|
|
* This script is distributed in the hope that it will be useful, |
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22
|
|
|
* GNU General Public License for more details. |
23
|
|
|
* |
24
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
25
|
|
|
***************************************************************/ |
26
|
|
|
|
27
|
|
|
use ApacheSolrForTypo3\Solr\ConnectionManager; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\Query\QueryBuilder; |
29
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Search\ResultSet\Result\Parser\DocumentEscapeService; |
30
|
|
|
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException; |
31
|
|
|
use ApacheSolrForTypo3\Solr\Search; |
32
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
33
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\Document\Document; |
34
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrCommunicationException; |
35
|
|
|
use ApacheSolrForTypo3\Solr\System\Solr\SolrConnection; |
36
|
|
|
use ApacheSolrForTypo3\Solr\Util; |
37
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
38
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Class Repository |
42
|
|
|
*/ |
43
|
|
|
class Repository implements SingletonInterface |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Search |
48
|
|
|
* |
49
|
|
|
* @var \ApacheSolrForTypo3\Solr\Search |
50
|
|
|
*/ |
51
|
|
|
protected $search; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var DocumentEscapeService |
55
|
|
|
*/ |
56
|
|
|
protected $documentEscapeService = null; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var TypoScriptConfiguration|null |
60
|
|
|
*/ |
61
|
|
|
protected $typoScriptConfiguration = null; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var QueryBuilder |
65
|
|
|
*/ |
66
|
|
|
protected $queryBuilder; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Repository constructor. |
70
|
|
|
* @param DocumentEscapeService|null $documentEscapeService |
71
|
|
|
* @param QueryBuilder|null $queryBuilder |
72
|
|
|
*/ |
73
|
7 |
|
public function __construct(DocumentEscapeService $documentEscapeService = null, TypoScriptConfiguration $typoScriptConfiguration = null, QueryBuilder $queryBuilder = null) |
74
|
|
|
{ |
75
|
7 |
|
$this->typoScriptConfiguration = $typoScriptConfiguration ?? Util::getSolrConfiguration(); |
76
|
7 |
|
$this->documentEscapeService = $documentEscapeService ?? GeneralUtility::makeInstance(DocumentEscapeService::class, /** @scrutinizer ignore-type */ $typoScriptConfiguration); |
77
|
7 |
|
$this->queryBuilder = $queryBuilder ?? GeneralUtility::makeInstance(QueryBuilder::class, /** @scrutinizer ignore-type */ $this->typoScriptConfiguration); |
78
|
7 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns firs found \ApacheSolrForTypo3\Solr\System\Solr\Document\Document for current page by given language id. |
82
|
|
|
* |
83
|
|
|
* @param $languageId |
84
|
|
|
* @return Document|false |
85
|
|
|
*/ |
86
|
1 |
|
public function findOneByPageIdAndByLanguageId($pageId, $languageId) |
87
|
|
|
{ |
88
|
1 |
|
$documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId); |
89
|
1 |
|
return reset($documentCollection); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Returns all found \ApacheSolrForTypo3\Solr\System\Solr\Document\Document[] by given page id and language id. |
94
|
|
|
* Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present. |
95
|
|
|
* |
96
|
|
|
* @param int $pageId |
97
|
|
|
* @param int $languageId |
98
|
|
|
* @return Document[] |
99
|
|
|
*/ |
100
|
6 |
|
public function findByPageIdAndByLanguageId($pageId, $languageId) |
101
|
|
|
{ |
102
|
|
|
try { |
103
|
6 |
|
$this->initializeSearch($pageId, $languageId); |
104
|
2 |
|
$pageQuery = $this->queryBuilder->buildPageQuery($pageId); |
105
|
2 |
|
$response = $this->search->search($pageQuery, 0, 10000); |
106
|
4 |
|
} catch (NoSolrConnectionFoundException $exception) { |
107
|
2 |
|
return []; |
108
|
2 |
|
} catch (SolrCommunicationException $exception) { |
109
|
|
|
return []; |
110
|
|
|
} |
111
|
2 |
|
$data = $response->getParsedData(); |
112
|
|
|
// @extensionScannerIgnoreLine |
113
|
2 |
|
return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $type |
118
|
|
|
* @param int $uid |
119
|
|
|
* @param int $pageId |
120
|
|
|
* @param int $languageId |
121
|
|
|
* @return Document[]|array |
122
|
|
|
*/ |
123
|
|
|
public function findByTypeAndPidAndUidAndLanguageId($type, $uid, $pageId, $languageId): array |
124
|
|
|
{ |
125
|
|
|
try { |
126
|
|
|
$this->initializeSearch($pageId, $languageId); |
127
|
|
|
$recordQuery = $this->queryBuilder->buildRecordQuery($type, $uid, $pageId); |
128
|
|
|
$response = $this->search->search($recordQuery, 0, 10000); |
129
|
|
|
} catch (NoSolrConnectionFoundException $exception) { |
130
|
|
|
return []; |
131
|
|
|
} catch (SolrCommunicationException $exception) { |
132
|
|
|
return []; |
133
|
|
|
} |
134
|
|
|
$data = $response->getParsedData(); |
135
|
|
|
// @extensionScannerIgnoreLine |
136
|
|
|
return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs ?? []); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Initializes Search for given language |
141
|
|
|
* |
142
|
|
|
* @param int $languageId |
143
|
|
|
*/ |
144
|
5 |
|
protected function initializeSearch($pageId, $languageId = 0) |
145
|
|
|
{ |
146
|
5 |
|
if (!is_int($pageId)) { |
147
|
1 |
|
throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926); |
148
|
|
|
} |
149
|
4 |
|
if (!is_int($languageId)) { // @todo: Check if lang id is defined and present? |
|
|
|
|
150
|
1 |
|
throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178); |
151
|
|
|
} |
152
|
|
|
/* @var $connectionManager ConnectionManager */ |
153
|
3 |
|
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
154
|
3 |
|
$solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId); |
155
|
|
|
|
156
|
2 |
|
$this->search = $this->getSearch($solrConnection); |
157
|
2 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Retrieves an instance of the Search object. |
161
|
|
|
* |
162
|
|
|
* @param SolrConnection $solrConnection |
163
|
|
|
* @return Search |
164
|
|
|
*/ |
165
|
1 |
|
protected function getSearch($solrConnection) |
166
|
|
|
{ |
167
|
1 |
|
return GeneralUtility::makeInstance(Search::class, /** @scrutinizer ignore-type */ $solrConnection); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|