TerIndexer.php 9.17 KB
Newer Older
1
<?php
2
namespace T3o\TerFe2\Solr\IndexQueue;
3

4
/*
5
 * This file is part of the TYPO3 CMS project.
6
 *
7 8 9
 * 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.
10
 *
11 12
 * For the full copyright and license information, please read the
 * LICENSE.txt file that was distributed with this source code.
13
 *
14 15
 * The TYPO3 project - inspiring people to share!
 */
16

17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use ApacheSolrForTypo3\Solr\IndexQueue\Item;
19
use TYPO3\CMS\Core\Utility\VersionNumberUtility;
20
use T3o\TerFe2\Utility\VersionUtility;
21 22

/**
23
 * Class \T3o\TerFe2\Solr\IndexQueue\TerIndexer
24 25 26 27 28
 *
 * @author Sascha Egerer <sascha@sascha-egerer.de>
 * @author Thomas Löffler <thomas.loeffler@typo3.org>
 * @author Tomas Norre Mikkelsen <tomasnorre@gmail.com>
 */
Markus Sommer's avatar
Markus Sommer committed
29 30
class TerIndexer extends \ApacheSolrForTypo3\Solr\IndexQueue\Indexer
{
31 32 33


    /**
34
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
35 36 37 38
     */
    protected $objectManager;

    /**
39
     * @var \T3o\TerFe2\Domain\Repository\ExtensionRepository
40 41 42 43
     */
    protected $extensionRepository;


Markus Sommer's avatar
Markus Sommer committed
44 45
    public function __construct(array $options = array())
    {
46 47
        parent::__construct($options);

48
        $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class);
49
        $this->extensionRepository = $this->objectManager->get(\T3o\TerFe2\Domain\Repository\ExtensionRepository::class);
50

Markus Sommer's avatar
Markus Sommer committed
51
        $this->extensionRepository->setShowInsecure(false);
52 53 54
    }

    /**
55 56 57 58 59
     * 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
60
     * @return \Apache_Solr_Document The Solr document converted from the record
61
     */
62 63 64 65 66 67
    protected function itemToDocument(Item $item, $language = 0)
    {
        $document = null;

        $itemRecord = $this->getFullItemRecord($item, $language);
        if (!is_null($itemRecord)) {
Markus Sommer's avatar
Markus Sommer committed
68 69 70 71
            $itemIndexingConfiguration = $this->getItemTypeConfiguration(
                $item,
                $language
            );
72 73 74 75 76

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

78 79
        return $document;
    }
80

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

90
        if ($extension instanceof \T3o\TerFe2\Domain\Model\Extension && $extension->getLastVersion() instanceof \T3o\TerFe2\Domain\Model\Version) {
91 92 93
            $typo3Dependency = $this->getDependencyVersionForPackage($extension, 'typo3');
            $document->setField('extensionMinTYPO3Version_tIntS', $typo3Dependency['minimum']);
            $document->setField('extensionMaxTYPO3Version_tIntS', $typo3Dependency['maximum']);
94

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

100 101
            // is last version insecure
            $document->setField('insecure_boolS', $extension->getLastVersion()->getReviewState() === -1);
102
            $document->setField('outdated_boolS', $extension->getLastVersion()->getReviewState() === -2);
Thomas Löffler's avatar
Thomas Löffler committed
103

Thomas Löffler's avatar
Thomas Löffler committed
104 105 106
            // composer support
            $document->setField('supportsComposer_boolS', false);
            $document->setField('composerName_stringS', '');
107
            if ($extension->getComposerName()) {
Thomas Löffler's avatar
Thomas Löffler committed
108
                $document->setField('supportsComposer_boolS', true);
109
                $document->setField('composerName_stringS', $extension->getComposerName());
Thomas Löffler's avatar
Thomas Löffler committed
110 111
            }

Thomas Löffler's avatar
Thomas Löffler committed
112 113
            // does this extension supports different versions?
            // @todo: use JSON with all versions to get them
114 115
            $document->setField('supports7_boolS', false);
            $document->setField('supports8_boolS', false);
Thomas Löffler's avatar
Thomas Löffler committed
116
            $typo3Support = [];
117
            if (VersionUtility::doesExtensionSupportTypo3Version($extension, 7006000)) {
Thomas Löffler's avatar
Thomas Löffler committed
118
                $typo3Support[] = '7 LTS';
119
                $document->setField('supports7_boolS', true);
Thomas Löffler's avatar
Thomas Löffler committed
120
            }
121
            if (VersionUtility::doesExtensionSupportTypo3Version($extension, 8007000)) {
Thomas Löffler's avatar
Thomas Löffler committed
122
                $typo3Support[] = '8 LTS';
123
                $document->setField('supports8_boolS', true);
Thomas Löffler's avatar
Thomas Löffler committed
124 125 126
            }
            $document->setField('typo3support_stringM', $typo3Support);

127 128 129
            $extensionIcon = $this->getExtensionIcon($itemRecord['ext_key'], $extension->getLastVersion()->getVersionString());
            $document->setField('extensionIcon_stringS', $extensionIcon);

Thomas Löffler's avatar
Thomas Löffler committed
130
            // add documentation link
131 132
            /** @var \T3o\TerFe2\Service\DocumentationService $documentationService */
            $documentationService = GeneralUtility::makeInstance(\T3o\TerFe2\Service\DocumentationService::class);
133
            $documentationLink = $documentationService->getDocumentationLink($extension->getExtKey(), $extension->getLastVersion()->getVersionString(), true);
Thomas Löffler's avatar
Thomas Löffler committed
134
            $document->setField('extensionDocumentationLink_stringS', $documentationLink);
135

136 137 138 139 140
            // use external manual if given
            if ($extension->getExternalManual()) {
                $document->setField('extensionDocumentationLink_stringS', $extension->getExternalManual());
            }

141 142 143 144 145 146
            // has manual
            $document->setField('hasManual_boolS', false);
            if ($documentationLink || $extension->getExternalManual()) {
                $document->setField('hasManual_boolS', true);
            }

147
            // get multiple authors
Thomas Löffler's avatar
Thomas Löffler committed
148 149 150
            $authors = [];
            if ($extension->getLastVersion() && $extension->getLastVersion()->getAuthor()) {
                $authors = GeneralUtility::trimExplode(',', $extension->getLastVersion()->getAuthor()->getName());
151 152 153 154
                if ($extension->getLastVersion()->getAuthor()->getCompany()) {
                    // Add company name to author
                    $authors[] = $extension->getLastVersion()->getAuthor()->getCompany();
                }
Thomas Löffler's avatar
Thomas Löffler committed
155
            }
156
            $document->setField('author_textWstM', $authors);
157 158
        }

159
        return $document;
160 161 162 163 164
    }

    /**
     * get the minimum dependency of an extension for a given $relationName (extension, php version, TYPO3 version...)
     *
165
     * @param \T3o\TerFe2\Domain\Model\Extension $extension
166
     * @param string $relationName The dependency to get the version from (e.g. "typo3" to get the minimum typo3 version)
Thomas Löffler's avatar
Thomas Löffler committed
167
     * @return array
168
     */
Markus Sommer's avatar
Markus Sommer committed
169 170
    protected function getDependencyVersionForPackage(\T3o\TerFe2\Domain\Model\Extension $extension, $relationName)
    {
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
        $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
     */
Markus Sommer's avatar
Markus Sommer committed
197 198
    protected function getExtensionIcon($extensionKey, $extensionVersion)
    {
199 200 201
        $fileTypesToCheck = [
            'svg',
            'png',
202
            'gif'
203
        ];
204 205 206 207

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

208 209 210 211 212 213 214
        foreach ($fileTypesToCheck as $fileType) {
            $iconFile = $this->generateIconFileName(
                $extensionKey,
                $extensionVersion,
                $fileType
            );
            if (file_exists(PATH_site . $iconPath . $iconFile)) {
215
                $icon = \T3o\TerFe2\Utility\FileUtility::getRelativeUrlFromAbsolutePath($iconPath . $iconFile);
216 217
                break;
            }
218 219 220 221 222 223 224 225 226 227 228 229 230
        }

        return $icon;
    }

    /**
     * generate the path to an extension icon for a specific version
     *
     * @param $extension
     * @param $version
     * @param $fileType
     * @return string
     */
Markus Sommer's avatar
Markus Sommer committed
231 232
    protected function generateIconFileName($extension, $version, $fileType)
    {
233 234 235 236 237 238 239
        if (empty($extension) || empty($version) || empty($fileType)) {
            return '';
        }
        $extension = strtolower($extension);
        $fileType = strtolower(trim($fileType, '. '));
        return $extension[0] . '/' . $extension[1] . '/' . $extension . '_' . $version . '.' . $fileType;
    }
240
}