Commit 4b737fd8 authored by Andreas Wolf's avatar Andreas Wolf

[TASK] #277: Migrate scheduler tasks to Doctrine

parent 7638c415
......@@ -14,6 +14,7 @@ namespace T3o\TerFe2\Task;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Scheduler\Task;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Backend\Utility\BackendUtility;
......@@ -43,33 +44,47 @@ class CheckForExpiredExtensions extends Task
'typo3v4',
'docteam'
];
$expiringExtensions = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, ext_key, frontend_user',
'tx_terfe2_domain_model_extension',
'NOT deleted AND NOT expire AND versions = 0 AND tstamp <= ' . strtotime('-1 year'),
'',
'frontend_user'
);
$extensionTableConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension');
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->eq('deleted', 0),
$queryBuilder->expr()->eq('expire', 0),
$queryBuilder->expr()->eq('versions', 0),
$queryBuilder->expr()->lte('tstamp', strtotime('-1 year'))
)
->orderBy('frontend_user');
$statement = $queryBuilder->execute();
$statement->execute();
// group extensions by owner
$expiredExtensionsByOwner = [];
foreach ($expiringExtensions as $expiringExtension) {
while ($expiringExtension = $statement->fetch(\PDO::FETCH_ASSOC)) {
if ($expiringExtension['ext_key'] && $expiringExtension['frontend_user']) {
$expiredExtensionsByOwner[$expiringExtension['frontend_user']][] = $expiringExtension;
}
}
$frontendUserConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('fe_users');
foreach ($expiredExtensionsByOwner as $username => $extensions) {
if (in_array($username, $this->blacklistUsers, true)) {
continue;
}
$frontendUser = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
'uid, username, email',
'fe_users',
'username = ' . $this->getDatabaseConnection()->fullQuoteStr($username, 'fe_users')
. BackendUtility::BEenableFields('fe_users')
. BackendUtility::deleteClause('fe_users')
);
$queryBuilder = $frontendUserConnection->createQueryBuilder();
$queryBuilder->select('uid', 'username', 'email')
->from('fe_users')
->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($username)));
$statement = $queryBuilder->execute();
$statement->execute();
$frontendUser = $statement->fetch(\PDO::FETCH_ASSOC);
if (!empty($frontendUser) && GeneralUtility::validEmail($frontendUser['email'])) {
$to = $frontendUser['email'];
$subject = 'Your extension keys are going to expire!';
......@@ -91,12 +106,14 @@ class CheckForExpiredExtensions extends Task
if ($mail->send()) {
// set every extension of the owner to expire in 30 days
foreach ($extensions as $extension) {
$this->getDatabaseConnection()->exec_UPDATEquery(
$extensionTableConnection->update(
'tx_terfe2_domain_model_extension',
'uid = ' . (int)$extension['uid'],
array(
[
'expire' => strtotime('+30 days')
)
],
[
'uid' => (int)$extension['uid']
]
);
}
}
......@@ -104,23 +121,33 @@ class CheckForExpiredExtensions extends Task
}
// remove expired extensions
$expiredExtensions = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid, ext_key',
'tx_terfe2_domain_model_extension',
'NOT deleted AND expire > 0 AND expire <= ' . time() . ' AND versions = 0'
);
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->eq('deleted', 0),
$queryBuilder->expr()->gt('expire', 0),
$queryBuilder->expr()->lt('expire', time()),
$queryBuilder->expr()->eq('versions', 0)
)
->orderBy('frontend_user');
$expiredExtensions = $queryBuilder->execute();
foreach ($expiredExtensions as $expiredExtension) {
$uidsToDelete = [];
while ($expiredExtension = $expiredExtensions->fetch(\PDO::FETCH_ASSOC)) {
// Deleted in ter, then delete the key in the ter_fe2 extension table
if ($expiredExtension['ext_key'] && $this->deleteExtensionKeyInTer($expiredExtension['ext_key'])) {
$this->getDatabaseConnection()->exec_DELETEquery(
'tx_terfe2_domain_model_extension',
'uid = ' . $expiredExtension['uid']
);
$uidsToDelete[] = $expiredExtension['uid'];
}
}
return true;
if (count($uidsToDelete) > 0) {
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->delete('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->in('uid', $uidsToDelete)
);
$queryBuilder->execute()->execute();
}
}
/**
......@@ -130,28 +157,27 @@ class CheckForExpiredExtensions extends Task
protected function deleteExtensionKeyInTer($extensionKey)
{
// check if there are extension versions
$extensionsConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_ter_extensions');
$versions = $this->getDatabaseConnection()->exec_SELECTcountRows(
$versionCount = $extensionsConnection->count(
'extensionkey',
'tx_ter_extensions',
'extensionkey=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extensionKey, 'tx_ter_extensions')
[
'extensionkey' => $extensionKey
]
);
if (!$versions || $versions === 0) {
return $this->getDatabaseConnection()->exec_DELETEquery(
if (!$versionCount || $versionCount === 0) {
return $extensionsConnection->delete(
'tx_ter_extensionkeys',
'extensionkey=' . $GLOBALS['TYPO3_DB']->fullQuoteStr($extensionKey, 'tx_ter_extensions')
[
'extensionkey' => $extensionKey
]
);
}
return false;
}
/**
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
*/
protected function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
}
......@@ -15,6 +15,7 @@ namespace T3o\TerFe2\Task;
* The TYPO3 project - inspiring people to share!
*/
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Extbase\Scheduler\Task;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;
......@@ -119,15 +120,24 @@ class CheckForOutdatedExtensions extends Task
*/
public function getNotOutdatedAndSecureVersions()
{
$rows = $this->getDatabaseConnection()->exec_SELECTgetRows(
'uid',
'tx_terfe2_domain_model_version',
'NOT deleted AND NOT hidden AND review_state >= 0',
'',
'upload_date ASC'
);
return $rows;
$tableName = 'tx_terfe2_domain_model_version';
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($tableName);
$queryBuilder->getRestrictions()->removeAll();
$statement = $queryBuilder
->select('uid')
->from($tableName)
->where(
$queryBuilder->expr()->eq('deleted', 0),
$queryBuilder->expr()->eq('hidden', 0),
$queryBuilder->expr()->gte('review_state', 0)
)
->orderBy('upload_date', 'ASC')
->execute();
$statement->execute();
return $statement->fetchAll(\PDO::FETCH_ASSOC);
}
/**
......@@ -265,22 +275,27 @@ class CheckForOutdatedExtensions extends Task
}
if ($isOutdated) {
$this->getDatabaseConnection()->exec_UPDATEquery(
'tx_terfe2_domain_model_version',
'uid = ' . $version->getUid(),
[
'review_state' => -2
]
);
$connectionPool = GeneralUtility::makeInstance(ConnectionPool::class);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_terfe2_domain_model_version');
$queryBuilder
->update('tx_terfe2_domain_model_version')
->set('review_state', -2)
->where(
$queryBuilder->expr()->eq('uid', $version->getUid())
)
->execute();
if ($version->getExtension() && $version->getExtension()->getUid()) {
$this->getDatabaseConnection()->exec_UPDATEquery(
'tx_terfe2_domain_model_extension',
'uid = ' . $version->getExtension()->getUid(),
[
'tstamp' => time()
]
);
$queryBuilder = $connectionPool->getQueryBuilderForTable('tx_terfe2_domain_model_extension');
$queryBuilder
->update('tx_terfe2_domain_model_extension')
->set('tstamp', time())
->where(
$queryBuilder->expr()->eq('uid', $version->getUid())
)
->execute();
$this->solrIndexQueue->updateItem('tx_terfe2_domain_model_extension', $version->getExtension()->getUid());
}
}
......@@ -304,11 +319,4 @@ class CheckForOutdatedExtensions extends Task
}
}
/**
* @return \TYPO3\CMS\Core\Database\DatabaseConnection
*/
private function getDatabaseConnection()
{
return $GLOBALS['TYPO3_DB'];
}
}
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