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

Move checkForExpiredExtensions to a command

parent 1ab7b9af
Pipeline #9456 failed with stages
in 3 minutes and 32 seconds
<?php
namespace T3o\TerFe2\Task;
declare(strict_types = 1);
namespace T3o\TerFe2\Command;
/*
* This file is part of the TYPO3 CMS project.
* This file is part of a TYPO3 extension.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
......@@ -14,51 +15,43 @@ namespace T3o\TerFe2\Task;
* The TYPO3 project - inspiring people to share!
*/
use Psr\Log\LoggerInterface;
use T3o\Ter\Api\ExtensionKey;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Log\LogManager;
use TYPO3\CMS\Core\Mail\MailMessage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Scheduler\Task;
use TYPO3\CMS\Fluid\View\StandaloneView;
/**
* Class CheckForExpiredExtensions
*/
class CheckForExpiredExtensions extends Task
class CheckForExpiredExtensions extends Command implements \Psr\Log\LoggerAwareInterface
{
use \Psr\Log\LoggerAwareTrait;
/**
* @var array
*/
protected $blacklistUsers = [];
protected $blacklistUsers = [
'abandoned_extensions',
'typo3v4',
'docteam'
];
/**
* @var LoggerInterface
*/
protected $logger;
protected function configure()
{
$this->setDescription('Checks all extension keys if there is no upload since a year.');
}
/**
* Execute Task
*
* @return bool
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
public function execute()
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->logger = GeneralUtility::makeInstance(LogManager::class)->getLogger(__CLASS__);
$this->logger->info('Task CheckForExpiredExtensions started');
$this->blacklistUsers = [
'abandoned_extensions',
'typo3v4',
'docteam'
];
$io = new SymfonyStyle($input, $output);
$io->section('Fetching extension keys without versions');
$expiredExtensionsByOwner = $this->getExpiredExtensionsByOwner();
$this->logger->info(sprintf('Found %d expired extensions', count($expiredExtensionsByOwner)));
$io->success('Found ' . count($expiredExtensionsByOwner) . ' expired extensions.');
foreach ($expiredExtensionsByOwner as $username => $extensions) {
if (in_array($username, $this->blacklistUsers, true)) {
......@@ -76,15 +69,13 @@ class CheckForExpiredExtensions extends Task
return true;
}
/**
* @return array
*/
private function getExpiredExtensionsByOwner(): array
protected function getExpiredExtensionsByOwner(): ?array
{
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension')
->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
$queryBuilder
->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->eq('deleted', 0),
......@@ -103,23 +94,22 @@ class CheckForExpiredExtensions extends Task
$expiredExtensionsByOwner[$expiringExtension['frontend_user']][] = $expiringExtension;
}
}
return $expiredExtensionsByOwner;
}
/**
* @param $username
* @param $extensions
* @return mixed
*/
private function notifyUser($username, $extensions)
private function notifyUser(string $username, array $extensions)
{
$frontendUserConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('fe_users');
$queryBuilder = $frontendUserConnection->createQueryBuilder();
$queryBuilder->select('uid', 'username', 'email')
$queryBuilder
->select('uid', 'username', 'email')
->from('fe_users')
->where($queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($username)));
->where(
$queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($username))
);
$statement = $queryBuilder->execute();
$statement->execute();
......@@ -130,56 +120,42 @@ class CheckForExpiredExtensions extends Task
$subject = 'Your extension keys are going to expire!';
/** @var StandaloneView $body */
$body = GeneralUtility::makeInstance(StandaloneView::class);
$body->setTemplatePathAndFilename(
GeneralUtility::getFileAbsFileName(
'EXT:ter_fe2/Resources/Private/Templates/Mail/ExpiredExtensions.html'
)
);
$body->setTemplatePathAndFilename(GeneralUtility::getFileAbsFileName('EXT:ter_fe2/Resources/Private/Templates/Mail/ExpiredExtensions.html'));
$body->assign('extensions', $extensions);
$body->assign('user', $frontendUser);
$emailBody = $body->render();
/** @var MailMessage $mail */
$mail = GeneralUtility::makeInstance(MailMessage::class);
$mail->addFrom('maintenance@typo3.org');
$mail->setTo($to);
$mail->setSubject($subject);
$mail->setBody($body->render());
$mail->html($emailBody);
if ($mail->send()) {
$this->logger->info(sprintf(
'Sent email to user %s (%s)',
$frontendUser['username'],
$to
));
$this->logger->info(sprintf('Sent email to user %s (%s)', $frontendUser['username'], $to));
// set every extension of the owner to expire in 30 days
$this->updateExpirationTimeOfExtensions($extensions);
}
} else {
$this->logger->warning(sprintf(
'Could not find frontend user with username %s',
$username
));
$this->logger->warning(sprintf('Could not find frontend user with username %s', $username));
}
return $queryBuilder;
}
/**
* @param $extensions
* @param $extensionTableConnection
*/
private function updateExpirationTimeOfExtensions($extensions)
private function updateExpirationTimeOfExtensions(array $extensions)
{
$extensionTableConnection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionForTable('tx_terfe2_domain_model_extension');
foreach ($extensions as $extension) {
$extensionTableConnection->update(
'tx_terfe2_domain_model_extension',
[
'expire' => strtotime('+30 days')
],
[
'uid' => (int)$extension['uid']
]
);
$extensionTableConnection
->update(
'tx_terfe2_domain_model_extension',
[
'expire' => strtotime('+30 days')
],
[
'uid' => (int)$extension['uid']
]
);
$this->logger->info(
sprintf('Updated expiration time of extension %s (uid %d)', $extension['ext_key'], $extension['uid'])
......@@ -193,7 +169,8 @@ class CheckForExpiredExtensions extends Task
->getConnectionForTable('tx_terfe2_domain_model_extension');
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->select('uid', 'ext_key', 'frontend_user')
$queryBuilder
->select('uid', 'ext_key', 'frontend_user')
->from('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->eq('deleted', 0),
......@@ -206,24 +183,20 @@ class CheckForExpiredExtensions extends Task
$uidsToDelete = [];
while ($expiredExtension = $expiredExtensions->fetch(\PDO::FETCH_ASSOC)) {
$extensionKey = new ExtensionKey($expiredExtension['ext_key']);
$extensionKey = new \T3o\Ter\Api\ExtensionKey($expiredExtension['ext_key']);
if (!$extensionKey->hasUploadedVersions()) {
$uidsToDelete[] = $expiredExtension['uid'];
}
}
if (count($uidsToDelete) > 0) {
$queryBuilder = $extensionTableConnection->createQueryBuilder();
$queryBuilder->delete('tx_terfe2_domain_model_extension')
->where(
$queryBuilder->expr()->in('uid', $uidsToDelete)
);
$queryBuilder->delete('tx_terfe2_domain_model_extension')->where($queryBuilder->expr()->in('uid', $uidsToDelete));
$queryBuilder->execute();
$this->logger->info(sprintf(
'Deleted %d expired extensions (uids %s)',
count($uidsToDelete),
implode(', ', $uidsToDelete)
));
$this->logger->info(
sprintf('Deleted %d expired extensions (uids %s)', count($uidsToDelete), implode(', ', $uidsToDelete))
);
}
}
}
......@@ -20,7 +20,6 @@ use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use T3o\TerFe2\Domain\Repository\CombinedExtensionRepository;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
......@@ -28,19 +27,6 @@ class CheckForOutdatedExtensions extends Command implements \Psr\Log\LoggerAware
{
use \Psr\Log\LoggerAwareTrait;
/**
* @var CombinedExtensionRepository
*/
protected $combinedExtensionRepository;
public function __construct(
string $name = null,
CombinedExtensionRepository $combinedExtensionRepository = null
) {
$this->combinedExtensionRepository = $combinedExtensionRepository ?? GeneralUtility::makeInstance(CombinedExtensionRepository::class);
parent::__construct($name);
}
protected function configure()
{
$this->setDescription('Checks all versions if they are outdated (not supporting an actively supported TYPO3 CMS version).');
......
......@@ -26,4 +26,7 @@ return [
'ter:checkForOutdatedExtensions' => [
'class' => \T3o\TerFe2\Command\CheckForOutdatedExtensions::class
],
'ter:checkForExpiredExtensions' => [
'class' => \T3o\TerFe2\Command\CheckForExpiredExtensions::class
],
];
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