|
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\NoSolrConnectionFoundException; |
|
29
|
|
|
use ApacheSolrForTypo3\Solr\Query; |
|
30
|
|
|
use ApacheSolrForTypo3\Solr\Search; |
|
31
|
|
|
use ApacheSolrForTypo3\Solr\Site; |
|
32
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
|
33
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Class ApacheSolrDocumentRepository uses connection to Solr Server |
|
37
|
|
|
*/ |
|
38
|
|
|
class Repository implements SingletonInterface |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Search |
|
43
|
|
|
* |
|
44
|
|
|
* @var \ApacheSolrForTypo3\Solr\Search |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $search; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Returns firs found Apache_Solr_Document for current page by given language id. |
|
50
|
|
|
* |
|
51
|
|
|
* @param $languageId |
|
52
|
|
|
* @return \Apache_Solr_Document|false |
|
53
|
|
|
*/ |
|
54
|
|
|
public function findOneByPageIdAndByLanguageId($pageId, $languageId) |
|
55
|
|
|
{ |
|
56
|
|
|
$documentCollection = $this->findByPageIdAndByLanguageId($pageId, $languageId); |
|
57
|
|
|
return reset($documentCollection); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns all found Apache_Solr_Document[] by given page id and language id. |
|
62
|
|
|
* Returns empty array if nothing found, e.g. if no language or no page(or no index for page) is present. |
|
63
|
|
|
* |
|
64
|
|
|
* @param int $pageId |
|
65
|
|
|
* @param int $languageId |
|
66
|
|
|
* @return \Apache_Solr_Document[] |
|
67
|
|
|
*/ |
|
68
|
|
|
public function findByPageIdAndByLanguageId($pageId, $languageId) |
|
69
|
|
|
{ |
|
70
|
|
|
try { |
|
71
|
|
|
$this->initializeSearch($pageId, $languageId); |
|
72
|
|
|
$this->search->search($this->getQueryForPage($pageId), 0, 10000); |
|
73
|
|
|
} catch (NoSolrConnectionFoundException $exception) { |
|
74
|
|
|
return []; |
|
75
|
|
|
} |
|
76
|
|
|
return $this->search->getResultDocumentsEscaped(); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Initializes Search for given language |
|
81
|
|
|
* |
|
82
|
|
|
* @param int $languageId |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function initializeSearch($pageId, $languageId = 0) |
|
85
|
|
|
{ |
|
86
|
|
|
if (!is_int($pageId)) { |
|
87
|
|
|
throw new \InvalidArgumentException('Invalid page ID = ' . $pageId, 1487332926); |
|
88
|
|
|
} |
|
89
|
|
|
if (!is_int($languageId)) { // @todo: Check if lang id is defined and present? |
|
90
|
|
|
throw new \InvalidArgumentException('Invalid language ID = ' . $languageId, 1487335178); |
|
91
|
|
|
} |
|
92
|
|
|
/* @var $connectionManager ConnectionManager */ |
|
93
|
|
|
$connectionManager = GeneralUtility::makeInstance(ConnectionManager::class); |
|
94
|
|
|
$solrConnection = $connectionManager->getConnectionByPageId($pageId, $languageId); |
|
95
|
|
|
|
|
96
|
|
|
$this->search = GeneralUtility::makeInstance(Search::class, $solrConnection); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns Query for Saearch which finds document for given page. |
|
101
|
|
|
* Note: The Connection is per language as recommended in ext-solr docs. |
|
102
|
|
|
* |
|
103
|
|
|
* @return Query |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function getQueryForPage($pageId) |
|
106
|
|
|
{ |
|
107
|
|
|
/* @var Query $query */ |
|
108
|
|
|
$query = GeneralUtility::makeInstance(Query::class, ''); |
|
109
|
|
|
$query->setQueryType('standard'); |
|
110
|
|
|
$query->useRawQueryString(true); |
|
111
|
|
|
$query->setQueryString('*:*'); |
|
112
|
|
|
$query->addFilter('(type:pages AND uid:' . $pageId . ') OR (*:* AND pid:' . $pageId . ' NOT type:pages)'); |
|
113
|
|
|
$query->addFilter('siteHash:' . Site::getSiteByPageId($pageId)->getSiteHash()); |
|
114
|
|
|
$query->setFieldList('*'); |
|
115
|
|
|
$query->setSorting('type asc, title asc'); |
|
116
|
|
|
|
|
117
|
|
|
return $query; |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|