Skip to content

Commit

Permalink
[TASK] update scheduler task for TYPO3 v11
Browse files Browse the repository at this point in the history
  • Loading branch information
cdaecke committed Dec 29, 2021
1 parent 5f8825f commit 914499c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
24 changes: 7 additions & 17 deletions Classes/Command/Cleanup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,12 @@
*
*/


use Mediadreams\MdUnreadnews\Domain\Repository\UnreadnewsRepository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Object\ObjectManager;

/**
* Class Cleanup
Expand All @@ -35,18 +32,11 @@ class Cleanup extends Command
protected $unreadnewsRepository;

/**
* Cleanup constructor.
* @param null $name
* @param UnreadnewsRepository $unreadnewsRepository
*/
public function __construct($name = null)
public function injectUnreadnewsRepository(UnreadnewsRepository $unreadnewsRepository)
{
parent::__construct($name);

// get objectManager
$this->objectManager = GeneralUtility::makeInstance(ObjectManager::class);

// get unreadnewsRepository
$this->unreadnewsRepository = $this->objectManager->get(UnreadnewsRepository::class);
$this->unreadnewsRepository = $unreadnewsRepository;
}

/**
Expand Down Expand Up @@ -78,12 +68,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
$days = (int)$input->getArgument('days') ? $input->getArgument('days') : 30;
$res = $this->unreadnewsRepository->deletePeriod($days);

if ($res) {
$io->success('All entries, which are older than ' . $days . ' day are deleted.');
if ($res > 0) {
$io->success('All entries, which are older than ' . $days . ' days are deleted.');
} else {
$io->error('An error occurred!');
$io->error('No entries, which are older than ' . $days . ' days are found.');
}

return 0;
return SELF::SUCCESS;
}
}
20 changes: 17 additions & 3 deletions Classes/Domain/Repository/AbstractRepository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Mediadreams\MdUnreadnews\Domain\Repository;

/**
Expand All @@ -17,9 +18,23 @@

/**
* Class AbstractRepository
* @package Mediadreams\MdUnreadnews\Domain\Repository
*/
abstract class AbstractRepository extends Repository
{
/**
* @var Typo3QuerySettings;
*/
protected $querySettings = null;

/**
* @param Typo3QuerySettings $querySettings
*/
public function injectTypo3QuerySettings(Typo3QuerySettings $querySettings)
{
$this->querySettings = $querySettings;
}

/**
* Default orderings
*
Expand All @@ -36,8 +51,7 @@ abstract class AbstractRepository extends Repository
*/
public function initializeObject()
{
$querySettings = $this->objectManager->get(Typo3QuerySettings::class);
$querySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($querySettings);
$this->querySettings->setRespectStoragePage(false);
$this->setDefaultQuerySettings($this->querySettings);
}
}
28 changes: 15 additions & 13 deletions Classes/Domain/Repository/UnreadnewsRepository.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace Mediadreams\MdUnreadnews\Domain\Repository;

/**
Expand All @@ -16,7 +17,8 @@
use TYPO3\CMS\Core\Database\ConnectionPool;

/**
* The repository for Unreadnews
* Class UnreadnewsRepository
* @package Mediadreams\MdUnreadnews\Domain\Repository
*/
class UnreadnewsRepository extends AbstractRepository
{
Expand All @@ -28,8 +30,8 @@ class UnreadnewsRepository extends AbstractRepository
/**
* Check if news is unread for given user
*
* @param int $newsUid: Uid of news record
* @param int $feuserUid: Uid of feuser record
* @param int $newsUid : Uid of news record
* @param int $feuserUid : Uid of feuser record
* @return int
*/
public function isUnread(int $newsUid, int $feuserUid): int
Expand All @@ -45,8 +47,8 @@ public function isUnread(int $newsUid, int $feuserUid): int
/**
* Delete entry for given news and user
*
* @param int $newsUid: Uid of news record
* @param int $feuserUid: Uid of feuser record
* @param int $newsUid : Uid of news record
* @param int $feuserUid : Uid of feuser record
* @return void
*/
public function deleteEntry(int $newsUid, int $feuserUid): void
Expand All @@ -55,7 +57,7 @@ public function deleteEntry(int $newsUid, int $feuserUid): void
->getConnectionForTable(static::TABLE_NAME);

$arrayWhere = [
'news' => $newsUid,
'news' => $newsUid,
'feuser' => $feuserUid,
];

Expand All @@ -65,12 +67,12 @@ public function deleteEntry(int $newsUid, int $feuserUid): void
/**
* Delete entries older than the given days
*
* @param int $days: Amount of days in the past till then all unread information shall be deleted.
* @param int $days : Amount of days in the past till then all unread information shall be deleted.
* @return mixed
*/
public function deletePeriod(int $days)
{
$date = strtotime(-$days.' days');
$date = strtotime(-$days . ' days');

$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable(static::TABLE_NAME);
Expand All @@ -86,8 +88,8 @@ public function deletePeriod(int $days)
/**
* Get number of unread news for a selected category
*
* @param int $categoryUid: CategoryUid of news category
* @param int $feuserUid: Uid of feuser record
* @param int $categoryUid : CategoryUid of news category
* @param int $feuserUid : Uid of feuser record
* @return int
*/
public function getCountForCategory(int $categoryUid, int $feuserUid): int
Expand All @@ -102,9 +104,9 @@ public function getCountForCategory(int $categoryUid, int $feuserUid): int
tx_mdunreadnews_domain_model_unreadnews
ON tx_mdunreadnews_domain_model_unreadnews.news = sys_category_record_mm.uid_foreign
WHERE
tx_mdunreadnews_domain_model_unreadnews.feuser = '.$feuserUid.'
'.$enableFields.'
AND sys_category_record_mm.uid_local = '.$categoryUid.'
tx_mdunreadnews_domain_model_unreadnews.feuser = ' . $feuserUid . '
' . $enableFields . '
AND sys_category_record_mm.uid_local = ' . $categoryUid . '
AND sys_category_record_mm.tablenames = "tx_news_domain_model_news"
AND sys_category_record_mm.fieldname = "categories"
';
Expand Down
10 changes: 0 additions & 10 deletions Configuration/Commands.php

This file was deleted.

16 changes: 16 additions & 0 deletions Configuration/Services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
services:
_defaults:
autowire: true
autoconfigure: true
public: false

Mediadreams\MdUnreadnews\:
resource: "../Classes/*"

Mediadreams\MdUnreadnews\Command\Cleanup:
tags:
- name: 'console.command'
command: 'mdUnreadnews:cleanup'
description: 'Remove old unread information from news records'
# not required, defaults to false
hidden: false

0 comments on commit 914499c

Please sign in to comment.