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 | 65 | public function process(array $values) |
|
67 | { |
||
68 | 65 | $results = []; |
|
69 | |||
70 | 65 | foreach ($values as $value) { |
|
71 | 65 | list($rootPageUid, $mountPoint) = GeneralUtility::trimExplode(',', |
|
72 | 65 | $value, true, 2); |
|
73 | 65 | $results[] = $this->getSolrRootlineForPageId($rootPageUid, |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
74 | 65 | $mountPoint); |
|
75 | } |
||
76 | |||
77 | 65 | 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 | 65 | protected function getSolrRootlineForPageId($pageId, $mountPoint = '') |
|
88 | { |
||
89 | 65 | $pageIdRootline = $this->buildPageIdRootline($pageId, $mountPoint); |
|
90 | 65 | $solrRootline = $this->buildSolrHierarchyFromIdRootline($pageIdRootline); |
|
91 | |||
92 | 65 | 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 | 65 | protected function buildPageIdRootline($pageId, $mountPoint = '') |
|
103 | { |
||
104 | 65 | $rootlinePageIds = []; |
|
105 | |||
106 | $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPoint); |
||
107 | 65 | try { |
|
108 | 65 | $rootline = $rootlineUtility->get(); |
|
109 | } catch (\RuntimeException $e) { |
||
110 | 65 | $rootline = []; |
|
111 | 65 | } |
|
112 | 65 | ||
113 | foreach ($rootline as $page) { |
||
114 | if (Site::isRootPage($page)) { |
||
115 | 51 | break; |
|
116 | } |
||
117 | |||
118 | 65 | array_unshift($rootlinePageIds, $page['pid']); |
|
119 | } |
||
120 | 65 | ||
121 | $rootlinePageIds[] = $pageId; |
||
122 | |||
123 | return $rootlinePageIds; |
||
124 | } |
||
125 | } |
||
126 |