1
|
|
|
<?php |
2
|
|
|
namespace ApacheSolrForTypo3\Solr\ContentObject; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* Copyright notice |
6
|
|
|
* |
7
|
|
|
* (c) 2017 Timo Hund <[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\Index\Classification\Classification as ClassificationItem; |
28
|
|
|
use ApacheSolrForTypo3\Solr\Domain\Index\Classification\ClassificationService; |
29
|
|
|
use InvalidArgumentException; |
30
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
31
|
|
|
use TYPO3\CMS\Frontend\ContentObject\AbstractContentObject; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* A content object (cObj) to classify content based on a configuration. |
35
|
|
|
* |
36
|
|
|
* Example usage: |
37
|
|
|
* |
38
|
|
|
* keywords = SOLR_CLASSIFICATION # supports stdWrap |
39
|
|
|
* keywords { |
40
|
|
|
* field = __solr_content # a comma separated field. instead of field you can also use "value" |
41
|
|
|
* classes { |
42
|
|
|
* 1 { |
43
|
|
|
* patterns = smartphone, mobile, mobilephone # list of patterns that need to match to assign that class |
44
|
|
|
* class = mobilephone # class that should be assigned when a pattern matches |
45
|
|
|
* } |
46
|
|
|
* } |
47
|
|
|
* } |
48
|
|
|
*/ |
49
|
|
|
class Classification extends AbstractContentObject |
50
|
|
|
{ |
51
|
|
|
const CONTENT_OBJECT_NAME = 'SOLR_CLASSIFICATION'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Executes the SOLR_CLASSIFICATION content object. |
55
|
|
|
* |
56
|
|
|
* Returns mapped classes when the field matches on of the configured patterns ... |
57
|
|
|
* |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function render($conf = []) |
61
|
|
|
{ |
62
|
|
|
|
63
|
|
|
if (!is_array($conf['classes.'])) { |
64
|
|
|
throw new InvalidArgumentException('No class configuration configured for SOLR_CLASSIFICATION object. Given configuration: ' . serialize($conf)); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$configuredMappedClasses = $conf['classes.']; |
68
|
|
|
unset($conf['classes.']); |
69
|
|
|
|
70
|
|
|
$data = ''; |
71
|
|
|
if (isset($conf['value'])) { |
72
|
|
|
$data = $conf['value']; |
73
|
|
|
unset($conf['value']); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if (!empty($conf)) { |
77
|
|
|
$data = $this->cObj->stdWrap($data, $conf); |
78
|
|
|
} |
79
|
|
|
$classifications = $this->buildClassificationsFromConfiguration($configuredMappedClasses); |
80
|
|
|
/** @var $classificationService ClassificationService */ |
81
|
|
|
$classificationService = GeneralUtility::makeInstance(ClassificationService::class); |
82
|
|
|
|
83
|
|
|
return serialize($classificationService->getMatchingClassNames((string)$data, $classifications)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Builds an array of Classification objects from the passed classification configuration. |
88
|
|
|
* |
89
|
|
|
* @param array $configuredMappedClasses |
90
|
|
|
* @return ClassificationItem[] |
91
|
|
|
*/ |
92
|
|
|
protected function buildClassificationsFromConfiguration($configuredMappedClasses) : array |
93
|
|
|
{ |
94
|
|
|
$classifications = []; |
95
|
|
|
foreach ($configuredMappedClasses as $class) { |
96
|
|
|
if ( (empty($class['patterns']) && empty($class['matchPatterns'])) || empty($class['class'])) { |
97
|
|
|
throw new InvalidArgumentException('A class configuration in SOLR_CLASSIFCATION needs to have a pattern and a class configured. Given configuration: ' . serialize($class)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
// @todo deprecate patterns configuration |
101
|
|
|
$patterns = empty($class['patterns']) ? [] : GeneralUtility::trimExplode(',', $class['patterns']); |
102
|
|
|
$matchPatterns = empty($class['matchPatterns']) ? [] : GeneralUtility::trimExplode(',', $class['matchPatterns']); |
103
|
|
|
$matchPatterns = $matchPatterns + $patterns; |
104
|
|
|
$unMatchPatters = empty($class['unmatchPatterns']) ? [] : GeneralUtility::trimExplode(',', $class['unmatchPatterns']); |
105
|
|
|
|
106
|
|
|
$className = $class['class']; |
107
|
|
|
$classifications[] = GeneralUtility::makeInstance( |
108
|
|
|
ClassificationItem::class, |
109
|
|
|
/** @scrutinizer ignore-type */ $matchPatterns, |
110
|
|
|
/** @scrutinizer ignore-type */ $unMatchPatters, |
111
|
|
|
/** @scrutinizer ignore-type */ $className |
112
|
|
|
); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $classifications; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|