Commit b422f47e authored by Thomas Löffler's avatar Thomas Löffler
Browse files

Add task to import all keys without version into terfe2

This will start the expire process.
parent 3acb8229
Pipeline #2608 passed with stages
in 2 minutes and 4 seconds
<?php
namespace T3o\TerFe2\Task;
use T3o\TerFe2\Utility\Category;
/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class ImportAllExtensionKeysTask extends \TYPO3\CMS\Extbase\Scheduler\Task
{
/**
* PID for all extension related records
*
* @var int $pid
*/
protected $pid = 2;
/**
* executes the importer
*
* @return bool
*/
public function execute()
{
$extensionKeysFromTer = $this->getAllExtensionKeysFromTer();
foreach ($extensionKeysFromTer as $extensionData) {
if (!$this->extensionExists($extensionData)) {
$versionCount = $this->getVersionCount($extensionData['extensionkey']);
if ($versionCount === 0) {
$this->createExtension($extensionData);
}
}
}
return true;
}
/**
* Gets the extension key data out of ter tables
*
* @return array $extData
*/
public function getAllExtensionKeysFromTer(): array
{
$extData = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
'*',
'tx_ter_extensionkeys',
'1',
false,
'crdate'
);
return $extData;
}
/**
* @param array $extData
*
* @return int $uid extension uid
*/
public function extensionExists($extData)
{
$extRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'uid',
'tx_terfe2_domain_model_extension',
'NOT deleted AND ext_key = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['extensionkey'], 'tx_terfe2_domain_model_extension')
);
return !empty($extRec);
}
/**
* @param $extensionKey
* @return int
*/
public function getVersionCount($extensionKey)
{
$versionCounter = $GLOBALS['TYPO3_DB']->exec_SELECTcountRows(
'uid',
'tx_ter_extensions',
'extensionkey = ' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extensionKey, 'tx_ter_extensions')
);
return (int)$versionCounter;
}
/**
* @param array $extData
*
* @return void
*/
public function createExtension($extData)
{
$insertExtension = array(
'pid' => $this->pid,
'ext_key' => $extData['extensionkey'],
'last_upload' => 0,
'versions' => 0,
'last_version' => 0,
'frontend_user' => $extData['ownerusername'],
'crdate' => $extData['crdate'],
'tstamp' => $extData['tstamp'],
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_terfe2_domain_model_extension', $insertExtension);
}
}
<?php
namespace T3o\TerFe2\Task;
use T3o\TerFe2\Utility\Category;
/*
* This file is part of the TYPO3 CMS project.
*
* 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.
*
* For the full copyright and license information, please read the
* LICENSE.txt file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
class ImportAllExtensionsTask extends \TYPO3\CMS\Extbase\Scheduler\Task
{
/**
* PID for all extension related records
*
* @var int $pid
*/
protected $pid = 459;
/**
* executes the importer
*
* @return bool
*/
public function execute()
{
$extensionsFromTer = $this->getAllExtensionsFromTer();
foreach ($extensionsFromTer as $extensionData) {
$this->updateState($extensionData);
if (!$this->versionExists($extensionData)) {
$extensionData = $this->getExtensionDataOfExtUid($extensionData['uid']);
$extUid = $this->extensionExists($extensionData);
$this->saveExtension($extUid, $extensionData);
\TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Extension "' . $extensionData['extensionkey'] . '" saved in ter_fe2', 'ter_fe2', 1);
// update the EXT:solr Index Queue
if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('solr')) {
$indexQueue = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('tx_solr_indexqueue_Queue');
$indexQueue->updateItem('tx_terfe2_domain_model_extension', $extUid);
}
}
\TYPO3\CMS\Core\Utility\GeneralUtility::sysLog('Extension "' . $extensionData['extensionkey'] . '" saved in ter_fe2', 'ter_fe2', 1);
}
return true;
}
public function updateState($extData)
{
$versionRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'tx_terfe2_domain_model_version.uid AS versionUid, state',
'tx_terfe2_domain_model_version
LEFT JOIN tx_terfe2_domain_model_extension ON tx_terfe2_domain_model_extension.uid = tx_terfe2_domain_model_version.extension',
'ext_key = "' . $extData['extensionkey'] . '" AND version_string = "' . $extData['version'] . '" AND tx_terfe2_domain_model_extension.deleted = 0 AND tx_terfe2_domain_model_version.deleted = 0'
);
$extensionModel = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class);
$states = $extensionModel->getDefaultState(null);
if ($versionRec['versionUid'] && $versionRec['state'] != $extData['state']) {
if (!array_key_exists($extData['state'], $states)) {
$extData['state'] = 'n/a';
}
$updateVersion = array(
'state' => $extData['state']
);
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_terfe2_domain_model_version', 'uid = ' . $versionRec['versionUid'], $updateVersion);
}
}
/**
* Gets the extension data out of ter tables
*
* @return array $extData
*/
public function getAllExtensionsFromTer()
{
$extData = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows(
'uid,extensionkey,version,state',
'tx_ter_extensions',
'1',
false,
'crdate'
);
return $extData;
}
/**
* Gets the extension data out of ter tables
*
* @param int $extUid
* @return array $extData
*/
public function getExtensionDataOfExtUid($extUid)
{
$extData = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'tx_ter_extensions.*, tx_ter_extensiondetails.*',
'tx_ter_extensions
LEFT JOIN tx_ter_extensiondetails ON tx_ter_extensions.uid = tx_ter_extensiondetails.extensionuid',
'tx_ter_extensiondetails.extensionuid = ' . (int)$extUid
);
return $extData;
}
/**
* checks if a version with a specific version
* string exists
*
* @param array $extData
*
* @return boolean
*/
public function versionExists($extData)
{
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'tx_terfe2_domain_model_version.uid',
'tx_terfe2_domain_model_version
LEFT JOIN tx_terfe2_domain_model_extension ON tx_terfe2_domain_model_extension.uid = tx_terfe2_domain_model_version.extension',
'NOT tx_terfe2_domain_model_version.deleted
AND tx_terfe2_domain_model_version.version_string = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['version'], '') . '"
AND tx_terfe2_domain_model_extension.ext_key = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['extensionkey'], '') . '"'
);
return (boolean)$GLOBALS['TYPO3_DB']->sql_num_rows($res);
}
/**
* @param array $extData
*
* @return int $uid extension uid
*/
public function extensionExists($extData)
{
$extRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'uid',
'tx_terfe2_domain_model_extension',
'NOT deleted AND ext_key = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['extensionkey'], '') . '"'
);
if ($extRec) {
return $extRec['uid'];
}
return $this->createExtension($extData);
}
/**
* @param array $extData
*
* @return int $uid extension uid of new record
*/
public function createExtension($extData)
{
$insertExtension = array(
'pid' => $this->pid,
'ext_key' => $extData['extensionkey'],
'last_upload' => time(),
'last_maintained' => time(),
'versions' => 0,
'last_version' => 0,
'frontend_user' => $extData['lastuploadbyusername'],
'crdate' => time(),
'tstamp' => time()
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_terfe2_domain_model_extension', $insertExtension);
$extUid = $GLOBALS['TYPO3_DB']->sql_insert_id();
return $extUid;
}
/**
* @param int $extUid
* @param array $extData
*/
public function saveExtension($extUid, $extData)
{
$versionUid = $this->createVersion($extUid, $extData);
if ($versionUid) {
$this->addRelations($versionUid, $extData['dependencies']);
$this->updateExtension($versionUid, $extUid);
}
}
/**
* @param int $extUid
* @param array $extData
*
* @return int $versionUid
*/
public function createVersion($extUid, $extData)
{
/** @var \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extensionModel */
$extensionModel = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension::class);
$states = $extensionModel->getDefaultState();
$categories = Category::getDefaultCategories();
$insertVersion = [
'pid' => $this->pid,
'extension' => $extUid,
'title' => $extData['title'],
'description' => $extData['description'],
'author' => $this->createAuthor($extData),
'version_number' => \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger($extData['version']),
'version_string' => $extData['version'],
'upload_date' => $extData['lastuploaddate'],
'upload_comment' => $extData['uploadcomment'],
'file_hash' => $extData['t3xfilemd5'],
'download_counter' => $extData['downloadcounter'],
'frontend_download_counter' => 0,
'state' => isset($states[(string)$extData['state']]) ? (string)$extData['state'] : 'n/a',
'em_category' => isset($categories[(string)$extData['category']]) ? (string)$extData['category'] : '',
'load_order' => $extData['loadorder'],
'priority' => $extData['priority'],
'shy' => (boolean)$extData['shy'],
'uploadfolder' => $extData['uploadfolder'] ? $extData['uploadfolder'] : '',
'create_dirs' => $extData['createdirs'] ? $extData['createdirs'] : '',
'modify_tables' => $extData['modifytables'] ? $extData['modifytables'] : '',
'lock_type' => $extData['locktype'] ? $extData['locktype'] : '',
'clear_cache_on_load' => (boolean)$extData['clearcacheonload'],
'cgl_compliance' => $extData['codingguidelinescompliance'] ? $extData['codingguidelinescompliance'] : '',
'cgl_compliance_note' => $extData['codingguidelinescompliancenote'] ? $extData['codingguidelinescompliancenote'] : '',
'review_state' => $extData['reviewstate'],
'manual' => '',
'has_manual' => (boolean)$extData['ismanualincluded'],
'software_relations' => 0,
'extension_provider' => 'file',
'has_zip_file' => 0,
'has_images' => 0,
't3x_file_size' => @filesize(PATH_site . 'fileadmin/ter/' . $extData['extensionkey'] . '_' . $extData['version_string'] . '.t3x'),
'zip_file_size' => 0
];
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_terfe2_domain_model_version', $insertVersion);
return $GLOBALS['TYPO3_DB']->sql_insert_id();
}
/**
* @param int $versionUid
* @param int $extUid
*/
public function updateExtension($versionUid, $extUid)
{
$updateExtension = array(
'tstamp' => time(),
'versions' => $this->getNumberOfVersions($extUid),
'last_version' => $versionUid
);
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_terfe2_domain_model_extension', 'uid = ' . $extUid, $updateExtension);
}
/**
* @param int $extUid
*
* @return int $numberOfVersions
*/
public function getNumberOfVersions($extUid)
{
$res = $GLOBALS['TYPO3_DB']->exec_SELECTquery(
'uid',
'tx_terfe2_domain_model_version',
'extension = ' . $extUid . ' AND NOT deleted'
);
return $GLOBALS['TYPO3_DB']->sql_num_rows($res);
}
/**
* @param int $versionUid
* @param string $dependencies
*/
public function addRelations($versionUid, $dependencies)
{
$countRelations = 0;
$dependencies = unserialize($dependencies);
if (!empty($dependencies)) {
foreach ($dependencies as $relation) {
$relationType = $relation['kind'];
$relationKey = $relation['extensionKey'];
$version = $this->getVersionByRange($relation['version']);
if ($relationKey) {
$insertRelation = array(
'relation_type' => $relationType,
'relation_key' => $relationKey,
'minimum_version' => $version[0],
'maximum_version' => $version[1],
'version' => $versionUid,
'related_extension' => $this->getUidOfRelatedExtension($relationKey)
);
$countRelations++;
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_terfe2_domain_model_relation', $insertRelation);
}
}
}
$updateVersion = array(
'software_relations' => $countRelations
);
$GLOBALS['TYPO3_DB']->exec_UPDATEquery('tx_terfe2_domain_model_version', 'uid = ' . $versionUid, $updateVersion);
}
/**
* @param string $extKey
*
* @return int $uidOfRelatedExtension
*/
public function getUidOfRelatedExtension($extKey)
{
$extRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'uid',
'tx_terfe2_domain_model_extension',
'ext_key = "' . mysql_real_escape_string($extKey) . '" AND NOT deleted'
);
if ($extRec['uid']) {
return $extRec['uid'];
}
return 0;
}
/**
* @param array $extData
*
* @return int $authorUid
*/
public function getAuthor($extData)
{
return $this->authorExists($extData);
}
/**
* @param array $extData
*
* @return int $authorUid
*/
public function authorExists($extData)
{
$authorRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'uid',
'tx_terfe2_domain_model_author',
'NOT deleted
AND username = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['lastuploadbyusername'], '') . '"
AND name = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['authorname'], '') . '"
AND email = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['authoremail'], '') . '"
AND company = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extData['authorcompany'], '') . '"'
);
if ($authorRec['uid']) {
return $authorRec['uid'];
}
return $this->createAuthor($extData);
}
/**
* @param array $extData
*
* @return int $authorUid
*/
public function createAuthor($extData)
{
$insertAuthor = array(
'name' => $extData['authorname'],
'email' => $extData['authoremail'],
'company' => $extData['authorcompany'],
'username' => $extData['lastuploadbyusername'],
'frontend_user' => $this->getFeUserUidFromUsername($extData['lastuploadbyusername'])
);
$GLOBALS['TYPO3_DB']->exec_INSERTquery('tx_terfe2_domain_model_author', $insertAuthor);
return $GLOBALS['TYPO3_DB']->sql_insert_id();
}
/**
* @param string $username
*
* @return int $feUserUid
*/
public function getFeUserUidFromUsername($username)
{
$userRec = $GLOBALS['TYPO3_DB']->exec_SELECTgetSingleRow(
'uid',
'fe_users',
'username = "' . $GLOBALS['TYPO3_DB']->fullQuoteStr($username, '') . '"'
);
if ($userRec['uid']) {
return $userRec['uid'];
}
return false;
}
/**
* Returns an array with minimum and maximum version number from range
*
* @param string $version Range of versions
* @return array Minumum and maximum version number
*/
protected function getVersionByRange($version)
{
$version = \TYPO3\CMS\Extbase\Utility\ArrayUtility::trimExplode('-', $version);
$minimum = (!empty($version[0]) ? \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger($version[0]) : 0);
$maximum = (!empty($version[1]) ? \TYPO3\CMS\Core\Utility\VersionNumberUtility::convertVersionNumberToInteger($version[1]) : 0);
return array($minimum, $maximum);
}
}
......@@ -64,7 +64,7 @@ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\T3o\TerFe2\Task
];
// Register import all extensions
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\T3o\TerFe2\Task\ImportAllExtensionsTask::class] = [
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['scheduler']['tasks'][\T3o\TerFe2\Task\ImportAllExtensionKeysTask::class] = [
'extension' => 'ter_fe2',
'title' => 'LLL:EXT:ter_fe2/Resources/Private/Language/locallang_db.xlf:tx_terfe2_task_importallextensionstask.name',
'description' => 'LLL:EXT:ter_fe2/Resources/Private/Language/locallang_db.xlf:tx_terfe2_task_importallextensionstask.description',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment