|
1
|
|
|
<?php |
|
2
|
|
|
namespace ApacheSolrForTypo3\Solr\FieldProcessor; |
|
3
|
|
|
|
|
4
|
|
|
/*************************************************************** |
|
5
|
|
|
* Copyright notice |
|
6
|
|
|
* |
|
7
|
|
|
* (c) 2011-2015 Michael Knoll <[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\Domain\Site\Site; |
|
28
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
29
|
|
|
use TYPO3\CMS\Core\Utility\RootlineUtility; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* This Processor takes a PID, and resolves its rootline in solr notation. |
|
33
|
|
|
* |
|
34
|
|
|
* Format of this field corresponds to http://wiki.apache.org/solr/HierarchicalFaceting |
|
35
|
|
|
* |
|
36
|
|
|
* Let's say we have a record indexed on page 111 which is a sub page like shown in this page tree: |
|
37
|
|
|
* |
|
38
|
|
|
* 1 |
|
39
|
|
|
* |-10 |
|
40
|
|
|
* |-100 |
|
41
|
|
|
* |-111 |
|
42
|
|
|
* |
|
43
|
|
|
* then we get a rootline 1/10/100/111 |
|
44
|
|
|
* |
|
45
|
|
|
* In Solr hierarchy notation, we get |
|
46
|
|
|
* |
|
47
|
|
|
* 0-1/ |
|
48
|
|
|
* 1-1/10/ |
|
49
|
|
|
* 2-1/10/100/ |
|
50
|
|
|
* 3-1/10/100/11/ |
|
51
|
|
|
* |
|
52
|
|
|
* which is finally saved in a multi-value field. |
|
53
|
|
|
* |
|
54
|
|
|
* @author Michael Knoll <[email protected]> |
|
55
|
|
|
*/ |
|
56
|
|
|
class PageUidToHierarchy extends AbstractHierarchyProcessor implements FieldProcessor |
|
57
|
|
|
{ |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Expects a page ID of a page. Returns a Solr hierarchy notation for the |
|
61
|
|
|
* rootline of the page ID. |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $values Array of values, an array because of multivalued fields |
|
64
|
|
|
* @return array Modified array of values |
|
65
|
|
|
*/ |
|
66
|
37 |
|
public function process(array $values) |
|
67
|
|
|
{ |
|
68
|
37 |
|
$results = []; |
|
69
|
|
|
|
|
70
|
37 |
|
foreach ($values as $value) { |
|
71
|
37 |
|
list($rootPageUid, $mountPoint) = GeneralUtility::trimExplode(',', |
|
72
|
37 |
|
$value, true, 2); |
|
73
|
37 |
|
$results[] = $this->getSolrRootlineForPageId($rootPageUid, |
|
74
|
|
|
$mountPoint); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
37 |
|
return $results; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns a Solr hierarchy notation string for rootline of given PID. |
|
82
|
|
|
* |
|
83
|
|
|
* @param int $pageId Page ID to get a rootline as Solr hierarchy for |
|
84
|
|
|
* @param string $mountPoint The mount point parameter that will be used for building the rootline. |
|
85
|
|
|
* @return array Rootline as Solr hierarchy array |
|
86
|
|
|
*/ |
|
87
|
37 |
|
protected function getSolrRootlineForPageId($pageId, $mountPoint = '') |
|
88
|
|
|
{ |
|
89
|
37 |
|
$pageIdRootline = $this->buildPageIdRootline($pageId, $mountPoint); |
|
90
|
37 |
|
$solrRootline = $this->buildSolrHierarchyFromIdRootline($pageIdRootline); |
|
91
|
|
|
|
|
92
|
37 |
|
return $solrRootline; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Builds a page's rootline of parent page Ids |
|
97
|
|
|
* |
|
98
|
|
|
* @param int $pageId The page Id to build the rootline for |
|
99
|
|
|
* @param string $mountPoint The mount point parameter that will be passed to getRootline(). |
|
100
|
|
|
* @return array Page Id rootline as array |
|
101
|
|
|
*/ |
|
102
|
37 |
|
protected function buildPageIdRootline($pageId, $mountPoint = '') |
|
103
|
|
|
{ |
|
104
|
37 |
|
$rootlinePageIds = []; |
|
105
|
|
|
|
|
106
|
37 |
|
$rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPoint); |
|
107
|
|
|
try { |
|
108
|
37 |
|
$rootline = $rootlineUtility->get(); |
|
109
|
|
|
} catch (\RuntimeException $e) { |
|
110
|
|
|
$rootline = []; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
37 |
|
foreach ($rootline as $page) { |
|
114
|
37 |
|
if (Site::isRootPage($page)) { |
|
115
|
37 |
|
break; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
21 |
|
array_unshift($rootlinePageIds, $page['pid']); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
37 |
|
$rootlinePageIds[] = $pageId; |
|
122
|
|
|
|
|
123
|
37 |
|
return $rootlinePageIds; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|