* @author Thomas Löffler * @author Tomas Norre Mikkelsen */ class Tx_TerFe2_Solr_Indexqueue_TerIndexer extends \ApacheSolrForTypo3\Solr\IndexQueue\Indexer { /** * @var \TYPO3\CMS\Extbase\Object\ObjectManager */ protected $objectManager; /** * @var Tx_TerFe2_Domain_Repository_ExtensionRepository */ protected $extensionRepository; public function __construct(array $options = array()) { parent::__construct($options); $this->objectManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Object\ObjectManager::class); $this->extensionRepository = $this->objectManager->get(Tx_TerFe2_Domain_Repository_ExtensionRepository::class); $this->extensionRepository->setShowInsecure(FALSE); } /** * 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 */ 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); } return $document; } /** * @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( $itemRecord['ext_key'], $extension->getLastVersion()->getVersionString() ) ); $typo3Dependency = $this->getDependencyVersionForPackage($extension, 'typo3'); $document->setField('extensionMinTYPO3Version_tIntS', $typo3Dependency['minimum']); $document->setField('extensionMaxTYPO3Version_tIntS', $typo3Dependency['maximum']); $document->setField('outdated_boolS', $extension->getLastVersion()->getReviewState() === -2); } return $document; } /** * 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; } }