TerIndexer.php 6.45 KB
Newer Older
1
<?php
2
3
4

/**
 * This file is part of the TYPO3 CMS project.
5
 *
6
7
8
 * It is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License, either version 2
 * of the License, or any later version.
9
 *
10
11
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
12
 *
13
14
 * The TYPO3 project - inspiring people to share!
 */
15

16
use TYPO3\CMS\Core\Utility\GeneralUtility;
17
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
18
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
19
20
21
22
23
24
25
26
27

/**
 * Class Tx_TerFe2_Solr_Indexqueue_TerIndexer
 *
 * @author Sascha Egerer <sascha@sascha-egerer.de>
 * @author Thomas Löffler <thomas.loeffler@typo3.org>
 * @author Tomas Norre Mikkelsen <tomasnorre@gmail.com>
 */
class Tx_TerFe2_Solr_Indexqueue_TerIndexer extends \ApacheSolrForTypo3\Solr\IndexQueue\Indexer {
28
29
30


    /**
31
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
32
33
34
35
     */
    protected $objectManager;

    /**
36
     * @var Tx_TerFe2_Domain_Repository_ExtensionRepository
37
38
39
40
41
42
43
     */
    protected $extensionRepository;


    public function __construct(array $options = array()) {
        parent::__construct($options);

44
45
        $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
        $this->extensionRepository = $this->objectManager->get(Tx_TerFe2_Domain_Repository_ExtensionRepository::class);
46
47
48
49
50

        $this->extensionRepository->setShowInsecure(FALSE);
    }

    /**
51
52
53
54
55
56
     * Converts an item array (record) to a Solr document by mapping the
     * record's fields onto Solr document fields as configured in TypoScript.
     *
     * @param Item $item An index queue item
     * @param int $language Language Id
     * @return Apache_Solr_Document The Solr document converted from the record
57
     */
58
59
60
61
62
63
64
65
66
67
68
69
70
    protected function itemToDocument(Item $item, $language = 0)
    {
        $document = null;

        $itemRecord = $this->getFullItemRecord($item, $language);
        if (!is_null($itemRecord)) {
            $itemIndexingConfiguration = $this->getItemTypeConfiguration($item,
                $language);

            $document = $this->getBaseDocument($item, $itemRecord);
            $document = $this->addDocumentFieldsFromTyposcript($document, $itemIndexingConfiguration, $itemRecord);
            $document = $this->addSpecialFields($document, $itemRecord);
        }
71

72
73
        return $document;
    }
74

75
76
77
78
79
80
81
82
83
84
85
86
87
    /**
     * @param \Apache_Solr_Document $document
     * @param array $itemRecord
     * @return \Apache_Solr_Document
     */
    public function addSpecialFields(Apache_Solr_Document $document, array $itemRecord)
    {
        $extension = $this->extensionRepository->findByUid($itemRecord['uid']);

        if ($extension instanceof Tx_TerFe2_Domain_Model_Extension) {
            $document->setField(
                'extensionIcon_stringS',
                $this->getExtensionIcon(
88
89
                    $itemRecord['ext_key'],
                    $extension->getLastVersion()->getVersionString()
90
91
                )
            );
92

93
94
95
            $typo3Dependency = $this->getDependencyVersionForPackage($extension, 'typo3');
            $document->setField('extensionMinTYPO3Version_tIntS', $typo3Dependency['minimum']);
            $document->setField('extensionMaxTYPO3Version_tIntS', $typo3Dependency['maximum']);
96

97
98
99
100
101
            $dependencyString = '';
            $dependencyString .= $typo3Dependency['minimum'] > 0 ? VersionNumberUtility::convertIntegerToVersionNumber($typo3Dependency['minimum']) : '';
            $dependencyString .= $typo3Dependency['maximum'] > 0 ? ' - ' . VersionNumberUtility::convertIntegerToVersionNumber($typo3Dependency['maximum']) : '';
            $document->setField('extensionDependency_stringS', $dependencyString ?: 'N/A');

102
            $document->setField('outdated_boolS', $extension->getLastVersion()->getReviewState() === -2);
103
104
        }

105
        return $document;
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    }

    /**
     * get the minimum dependency of an extension for a given $relationName (extension, php version, TYPO3 version...)
     *
     * @param Tx_TerFe2_Domain_Model_Extension $extension
     * @param string $relationName The dependency to get the version from (e.g. "typo3" to get the minimum typo3 version)
     * @return string
     */
    protected function getDependencyVersionForPackage(Tx_TerFe2_Domain_Model_Extension $extension, $relationName) {
        $versions = array(
            'minimum' => 0,
            'maximum' => 0
        );

        foreach ($extension->getLastVersion()->getSoftwareRelations() as $relation) {
            if ($relation->getRelationType() === 'depends' && $relation->getRelationKey() === $relationName) {
                $versions = array(
                    'minimum' => $relation->getMinimumVersion(),
                    'maximum' => $relation->getMaximumVersion()
                );
                break;
            }
        }

        return $versions;
    }

    /**
     * get the relative path for the extension icon
     * could be png or gif
     *
     * @param $extensionKey
     * @param $extensionVersion
     * @return string
     */
    protected function getExtensionIcon($extensionKey, $extensionVersion) {

        // check if there is a gif or a png icon or if there is no icon
        $iconFilePng = $this->generateIconFileName(
            $extensionKey,
            $extensionVersion,
            'png'
        );
        $iconFileGif = $this->generateIconFileName(
            $extensionKey,
            $extensionVersion,
            'gif'
        );

        $iconPath = 'fileadmin/ter/';
        $icon = '';

        if (file_exists(PATH_site.$iconPath.$iconFilePng)) {
            $icon = Tx_TerFe2_Utility_File::getRelativeUrlFromAbsolutePath($iconPath.$iconFilePng);
        } elseif(file_exists(PATH_site.$iconPath.$iconFileGif)) {
            $icon = Tx_TerFe2_Utility_File::getRelativeUrlFromAbsolutePath($iconPath.$iconFileGif);
        }

        return $icon;
    }

    /**
     * generate the path to an extension icon for a specific version
     *
     * @param $extension
     * @param $version
     * @param $fileType
     * @return string
     */
    protected function generateIconFileName($extension, $version, $fileType) {
        if (empty($extension) || empty($version) || empty($fileType)) {
            return '';
        }
        $extension = strtolower($extension);
        $fileType = strtolower(trim($fileType, '. '));
        return $extension[0] . '/' . $extension[1] . '/' . $extension . '_' . $version . '.' . $fileType;
    }
184
}