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 2 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\Domain\Site\SiteRepository; |
31
|
|
|
use ApacheSolrForTypo3\Solr\NoSolrConnectionFoundException; |
32
|
|
|
use ApacheSolrForTypo3\Solr\Query; |
33
|
|
|
use ApacheSolrForTypo3\Solr\Search; |
34
|
|
|
use ApacheSolrForTypo3\Solr\System\Configuration\TypoScriptConfiguration; |
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 ApacheSolrDocumentRepository uses connection to Solr Server |
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
|
6 |
|
public function __construct(DocumentEscapeService $documentEscapeService = null, TypoScriptConfiguration $typoScriptConfiguration = null, QueryBuilder $queryBuilder = null) |
74
|
|
|
{ |
75
|
6 |
|
$this->typoScriptConfiguration = is_null($typoScriptConfiguration) ? Util::getSolrConfiguration() : $typoScriptConfiguration; |
76
|
6 |
|
$this->documentEscapeService = is_null($documentEscapeService) ? GeneralUtility::makeInstance(DocumentEscapeService::class, $typoScriptConfiguration) : $documentEscapeService; |
77
|
6 |
|
$this->queryBuilder = is_null($queryBuilder) ? GeneralUtility::makeInstance(QueryBuilder::class, $this->typoScriptConfiguration) : $queryBuilder; |
78
|
6 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Returns firs found Apache_Solr_Document for current page by given language id. |
82
|
|
|
* |
83
|
|
|
* @param $languageId |
84
|
|
|
* @return \Apache_Solr_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 Apache_Solr_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 \Apache_Solr_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
|
|
|
} |
109
|
2 |
|
$data = $response->getParsedData(); |
110
|
2 |
|
return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $type |
115
|
|
|
* @param int $uid |
116
|
|
|
* @param int $pageId |
117
|
|
|
* @param int $languageId |
118
|
|
|
* @return \Apache_Solr_Document[]|array |
119
|
|
|
*/ |
120
|
|
|
public function findByTypeAndPidAndUidAndLanguageId($type, $uid, $pageId, $languageId): array |
121
|
|
|
{ |
122
|
|
|
try { |
123
|
|
|
$this->initializeSearch($pageId, $languageId); |
124
|
|
|
$recordQuery = $this->queryBuilder->buildRecordQuery($type, $uid, $pageId); |
125
|
|
|
$response = $this->search->search($recordQuery, 0, 10000); |
126
|
|
|
} catch (NoSolrConnectionFoundException $exception) { |
127
|
|
|
return []; |
128
|
|
|
} |
129
|
|
|
$data = $response->getParsedData(); |
130
|
|
|
return $this->documentEscapeService->applyHtmlSpecialCharsOnAllFields($data->response->docs); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Initializes Search for given language |
135
|
|
|
* |
136
|
|
|
* @param int $languageId |
137
|
|
|
*/ |
138
|
5 |
|
protected function initializeSearch($pageId, $languageId = 0) |
139
|
|
|
{ |
140
|
5 |
|
if (!is_int($pageId)) { |
141
|
1 |
|
throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926); |
142
|
|
|
} |
143
|
4 |
|
if (!is_int($languageId)) { // @todo: Check if lang id is defined and present? |
144
|
1 |
|
throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178); |
145
|
|
|
} |
146
|
|
|
/* @var $connectionManager ConnectionManager */ |
147
|
3 |
|
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
148
|
3 |
|
$solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId); |
149
|
|
|
|
150
|
2 |
|
$this->search = $this->getSearch($solrConnection); |
151
|
2 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Retrieves an instance of the Search object. |
155
|
|
|
* |
156
|
|
|
* @param SolrConnection $solrConnection |
157
|
|
|
* @return Search |
158
|
|
|
*/ |
159
|
1 |
|
protected function getSearch($solrConnection) |
160
|
|
|
{ |
161
|
1 |
|
return GeneralUtility::makeInstance(Search::class, $solrConnection); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|