timohund /
ext-solr
| 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 | 67 | public function process(array $values) |
|||
| 67 | { |
||||
| 68 | 67 | $results = []; |
|||
| 69 | |||||
| 70 | 67 | foreach ($values as $value) { |
|||
| 71 | 67 | list($rootPageUid, $mountPoint) = GeneralUtility::trimExplode(',', |
|||
| 72 | 67 | $value, true, 2); |
|||
| 73 | 67 | $results[] = $this->getSolrRootlineForPageId($rootPageUid, |
|||
| 74 | 67 | $mountPoint); |
|||
| 75 | } |
||||
| 76 | |||||
| 77 | 67 | 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 | 67 | protected function getSolrRootlineForPageId($pageId, $mountPoint = '') |
|||
| 88 | { |
||||
| 89 | 67 | $pageIdRootline = $this->buildPageIdRootline($pageId, $mountPoint); |
|||
| 90 | 67 | $solrRootline = $this->buildSolrHierarchyFromIdRootline($pageIdRootline); |
|||
| 91 | |||||
| 92 | 67 | 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 | 67 | protected function buildPageIdRootline($pageId, $mountPoint = '') |
|||
| 103 | { |
||||
| 104 | 67 | $rootlinePageIds = []; |
|||
| 105 | |||||
| 106 | $rootlineUtility = GeneralUtility::makeInstance(RootlineUtility::class, $pageId, $mountPoint); |
||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
$pageId of type integer is incompatible with the type array|array<mixed,mixed> expected by parameter $constructorArguments of TYPO3\CMS\Core\Utility\G...Utility::makeInstance().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 107 | 67 | try { |
|||
| 108 | 67 | $rootline = $rootlineUtility->get(); |
|||
| 109 | } catch (\RuntimeException $e) { |
||||
| 110 | 67 | $rootline = []; |
|||
| 111 | 67 | } |
|||
| 112 | 67 | ||||
| 113 | foreach ($rootline as $page) { |
||||
| 114 | if (Site::isRootPage($page)) { |
||||
| 115 | 53 | break; |
|||
| 116 | } |
||||
| 117 | |||||
| 118 | 67 | array_unshift($rootlinePageIds, $page['pid']); |
|||
| 119 | } |
||||
| 120 | 67 | ||||
| 121 | $rootlinePageIds[] = $pageId; |
||||
| 122 | |||||
| 123 | return $rootlinePageIds; |
||||
| 124 | } |
||||
| 125 | } |
||||
| 126 |