Skip to content

Commit c70ad6d

Browse files
committed
[FEATURE] Also support relative dates for commands with a timestamp
Example usage: # Absolute dates: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 1583410470 ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge '2010-01-01' # Relative dates: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this month' ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of last month' ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this year' ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-30 days' ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-2678400 seconds' Related: - https://projekte.in2code.de/issues/78507 - #74
1 parent 475d207 commit c70ad6d

6 files changed

Lines changed: 83 additions & 20 deletions

File tree

Classes/Command/ExtbaseCommandTrait.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
declare(strict_types=1);
44
namespace In2code\Lux\Command;
55

6+
use DateTime;
7+
use In2code\Lux\Exception\DateTimeException;
68
use In2code\Lux\Utility\ConfigurationUtility;
79
use In2code\Lux\Utility\EnvironmentUtility;
810
use TYPO3\CMS\Core\Core\Bootstrap;
911
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
1012
use TYPO3\CMS\Core\Http\ServerRequest;
1113
use TYPO3\CMS\Core\Utility\GeneralUtility;
14+
use TYPO3\CMS\Core\Utility\MathUtility;
1215
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
1316

1417
trait ExtbaseCommandTrait
@@ -23,4 +26,16 @@ public function initializeExtbase(): void
2326
);
2427
}
2528
}
29+
30+
protected function parseTime(string $timeString): DateTime
31+
{
32+
if (MathUtility::canBeInterpretedAsInteger($timeString)) {
33+
return DateTime::createFromFormat('U', $timeString);
34+
}
35+
try {
36+
return new DateTime($timeString);
37+
} catch (\Throwable $exception) {
38+
throw new DateTimeException('Could not parse time: ' . $timeString, 1773128558, $exception);
39+
}
40+
}
2641
}

Classes/Command/LuxCleanupUnknownVisitorsByAgeCommand.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\DBAL\Exception as ExceptionDbal;
77
use In2code\Lux\Domain\Model\Visitor;
88
use In2code\Lux\Domain\Repository\VisitorRepository;
9+
use In2code\Lux\Exception\DateTimeException;
910
use Symfony\Component\Console\Command\Command;
1011
use Symfony\Component\Console\Input\InputArgument;
1112
use Symfony\Component\Console\Input\InputInterface;
@@ -30,19 +31,27 @@ public function configure()
3031
* Remove all unknown visitors where the last update (tstamp) is older than a given timestamp
3132
* !!! Really removes visitors and all rows from related tables from the database
3233
*
33-
* Example command: /vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 1583410470
34+
* Example commands:
35+
* - absolute date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 1583410470
36+
* - absolute date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge '2010-01-01'
37+
* - relative date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this month'
38+
* - relative date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of last month'
39+
* - relative date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this year'
40+
* - relative date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-30 days'
41+
* - relative date: ./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-2678400 seconds'
3442
*
3543
* @param InputInterface $input
3644
* @param OutputInterface $output
3745
* @return int
3846
* @throws InvalidQueryException
3947
* @throws ExceptionDbal
48+
* @throws DateTimeException
4049
*/
4150
public function execute(InputInterface $input, OutputInterface $output): int
4251
{
4352
$this->initializeExtbase();
4453
$visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class);
45-
$visitors = $visitorRepository->findByLastChangeUnknown((int)$input->getArgument('timestamp'));
54+
$visitors = $visitorRepository->findByLastChangeUnknown($this->parseTime($input->getArgument('timestamp')));
4655
/** @var Visitor $visitor */
4756
foreach ($visitors as $visitor) {
4857
$visitorRepository->removeVisitor($visitor);

Classes/Command/LuxCleanupVisitorsByAgeCommand.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Doctrine\DBAL\Exception as ExceptionDbal;
77
use In2code\Lux\Domain\Model\Visitor;
88
use In2code\Lux\Domain\Repository\VisitorRepository;
9+
use In2code\Lux\Exception\DateTimeException;
910
use Symfony\Component\Console\Command\Command;
1011
use Symfony\Component\Console\Input\InputArgument;
1112
use Symfony\Component\Console\Input\InputInterface;
@@ -30,19 +31,26 @@ public function configure()
3031
* Remove all visitors where the last update (tstamp) is older than a given timestamp
3132
* !!! Really removes visitors and all rows from related tables from the database
3233
*
33-
* Example command: ./vendor/bin/typo3 lux:cleanupVisitorsByAge 1583410470
34+
* - absolute date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge 1583410470
35+
* - absolute date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge '2010-01-01'
36+
* - relative date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of this month'
37+
* - relative date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of last month'
38+
* - relative date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of this year'
39+
* - relative date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge -- '-30 days'
40+
* - relative date: ./vendor/bin/typo3 lux:cleanupVisitorsByAge -- '-2678400 seconds'
3441
*
3542
* @param InputInterface $input
3643
* @param OutputInterface $output
3744
* @return int
3845
* @throws InvalidQueryException
3946
* @throws ExceptionDbal
47+
* @throws DateTimeException
4048
*/
4149
public function execute(InputInterface $input, OutputInterface $output): int
4250
{
4351
$this->initializeExtbase();
4452
$visitorRepository = GeneralUtility::makeInstance(VisitorRepository::class);
45-
$visitors = $visitorRepository->findByLastChange((int)$input->getArgument('timestamp'));
53+
$visitors = $visitorRepository->findByLastChange($this->parseTime($input->getArgument('timestamp')));
4654
/** @var Visitor $visitor */
4755
foreach ($visitors as $visitor) {
4856
$visitorRepository->removeVisitor($visitor);

Classes/Domain/Repository/VisitorRepository.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -367,32 +367,35 @@ public function findOnline(FilterDto $filter): QueryResultInterface
367367
/**
368368
* Find visitors where tstamp is older than given timestamp
369369
*
370-
* @param int $timestamp
370+
* @param DateTime $time
371371
* @return QueryResultInterface
372372
* @throws InvalidQueryException
373373
*/
374-
public function findByLastChange(int $timestamp): QueryResultInterface
374+
public function findByLastChange(DateTime $time): QueryResultInterface
375375
{
376376
$query = $this->createQuery();
377-
$query->matching($query->lessThan('tstamp', $timestamp));
377+
$query->matching(
378+
$query->lessThan('tstamp', $time->getTimestamp())
379+
);
378380
return $query->execute();
379381
}
380382

381383
/**
382384
* Find unknown visitors where tstamp is older than given timestamp
383385
*
384-
* @param int $timestamp
386+
* @param DateTime $time
385387
* @return QueryResultInterface
386388
* @throws InvalidQueryException
387389
*/
388-
public function findByLastChangeUnknown(int $timestamp): QueryResultInterface
390+
public function findByLastChangeUnknown(DateTime $time): QueryResultInterface
389391
{
390392
$query = $this->createQuery();
391-
$logicalAnd = [
392-
$query->equals('identified', false),
393-
$query->lessThan('tstamp', $timestamp),
394-
];
395-
$query->matching($query->logicalAnd(...$logicalAnd));
393+
$query->matching(
394+
$query->logicalAnd(
395+
$query->equals('identified', false),
396+
$query->lessThan('tstamp', $time->getTimestamp())
397+
)
398+
);
396399
return $query->execute();
397400
}
398401

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
namespace In2code\Lux\Exception;
5+
6+
use Exception;
7+
8+
class DateTimeException extends Exception
9+
{
10+
}

Documentation/Technical/Commands/Index.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,18 @@ Remove means in this case not deleted=1 but really remove from database.
7676
Example usage:
7777

7878
```
79-
# Remove unknown visitors (and all there related data) older than the given timestamp
80-
# (1640995200 = 2022/01/01 00:00)
81-
./vendor/bin/typo3 lux:cleanupVisitorsByAge 1640995200
79+
# Remove unknown visitors (and all there related data) older than the given time - examples:
80+
81+
# Absolute dates:
82+
./vendor/bin/typo3 lux:cleanupVisitorsByAge 1583410470
83+
./vendor/bin/typo3 lux:cleanupVisitorsByAge '2010-01-01'
84+
85+
# Relative dates:
86+
./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of this month'
87+
./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of last month'
88+
./vendor/bin/typo3 lux:cleanupVisitorsByAge 'First day of this year'
89+
./vendor/bin/typo3 lux:cleanupVisitorsByAge -- '-30 days'
90+
./vendor/bin/typo3 lux:cleanupVisitorsByAge -- '-2678400 seconds'
8291
```
8392

8493
##### \In2code\Lux\Command\LuxCleanupUnknownVisitorsByAgeCommand
@@ -89,9 +98,18 @@ Remove means in this case not deleted=1 but really remove from database.
8998
Example usage:
9099

91100
```
92-
# Remove unknown visitors (and all there related data) older than the given timestamp
93-
# (1640995200 = 2022/01/01 00:00)
94-
/vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 1640995200
101+
# Remove unknown visitors (and all there related data) older than the given time - examples:
102+
103+
# Absolute dates:
104+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 1583410470
105+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge '2010-01-01'
106+
107+
# Relative dates:
108+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this month'
109+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of last month'
110+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge 'First day of this year'
111+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-30 days'
112+
./vendor/bin/typo3 lux:cleanupUnknownVisitorsByAge -- '-2678400 seconds'
95113
```
96114

97115
##### \In2code\Lux\Command\LuxCleanupVisitorByUidCommand

0 commit comments

Comments
 (0)