2018-06-28 14:07:33 +03:00
|
|
|
<?php
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
declare(strict_types=1);
|
2019-12-03 21:57:53 +03:00
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
/**
|
|
|
|
* @copyright 2018 Georg Ehrke <oc.list@georgehrke.com>
|
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2018-06-28 14:07:33 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-02-10 18:04:38 +03:00
|
|
|
* @author Thomas Citharel <nextcloud@tcit.fr>
|
2018-06-28 14:07:33 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-28 14:07:33 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
namespace OCA\DAV\BackgroundJob;
|
|
|
|
|
2020-02-10 18:04:38 +03:00
|
|
|
use DateInterval;
|
|
|
|
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService;
|
2018-06-28 14:07:33 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2020-11-24 16:18:47 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
|
|
|
use OCP\BackgroundJob\Job;
|
2020-02-10 15:52:57 +03:00
|
|
|
use OCP\IConfig;
|
2018-06-28 14:07:33 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use Sabre\VObject\DateTimeParser;
|
|
|
|
use Sabre\VObject\InvalidDataException;
|
|
|
|
|
|
|
|
class RefreshWebcalJob extends Job {
|
|
|
|
|
2020-02-10 18:04:38 +03:00
|
|
|
/**
|
|
|
|
* @var RefreshWebcalService
|
|
|
|
*/
|
|
|
|
private $refreshWebcalService;
|
2018-06-28 14:07:33 +03:00
|
|
|
|
2020-02-10 15:52:57 +03:00
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
|
|
|
/** @var ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* RefreshWebcalJob constructor.
|
|
|
|
*
|
2020-02-10 18:04:38 +03:00
|
|
|
* @param RefreshWebcalService $refreshWebcalService
|
2020-02-10 15:52:57 +03:00
|
|
|
* @param IConfig $config
|
2018-06-28 14:07:33 +03:00
|
|
|
* @param ILogger $logger
|
|
|
|
* @param ITimeFactory $timeFactory
|
|
|
|
*/
|
2020-02-10 15:52:57 +03:00
|
|
|
public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) {
|
2020-11-24 16:18:47 +03:00
|
|
|
parent::__construct($timeFactory);
|
2020-02-10 18:04:38 +03:00
|
|
|
$this->refreshWebcalService = $refreshWebcalService;
|
2020-02-10 15:52:57 +03:00
|
|
|
$this->config = $config;
|
2018-06-28 14:07:33 +03:00
|
|
|
$this->logger = $logger;
|
|
|
|
$this->timeFactory = $timeFactory;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* this function is called at most every hour
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2020-11-24 16:18:47 +03:00
|
|
|
public function execute(IJobList $jobList, ILogger $logger = null) {
|
2020-02-10 18:04:38 +03:00
|
|
|
$subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']);
|
2018-06-28 14:07:33 +03:00
|
|
|
if (!$subscription) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-01-30 14:51:04 +03:00
|
|
|
$this->fixSubscriptionRowTyping($subscription);
|
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
// if no refresh rate was configured, just refresh once a week
|
2020-02-10 15:52:57 +03:00
|
|
|
$defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W');
|
|
|
|
$refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate;
|
|
|
|
|
2018-06-28 14:07:33 +03:00
|
|
|
$subscriptionId = $subscription['id'];
|
|
|
|
|
|
|
|
try {
|
2020-02-10 18:04:38 +03:00
|
|
|
/** @var DateInterval $dateInterval */
|
2020-02-10 15:52:57 +03:00
|
|
|
$dateInterval = DateTimeParser::parseDuration($refreshRate);
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (InvalidDataException $ex) {
|
2018-06-28 14:07:33 +03:00
|
|
|
$this->logger->logException($ex);
|
|
|
|
$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$interval = $this->getIntervalFromDateInterval($dateInterval);
|
|
|
|
if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
parent::execute($jobList, $logger);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array $argument
|
|
|
|
*/
|
|
|
|
protected function run($argument) {
|
2020-02-10 18:04:38 +03:00
|
|
|
$this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']);
|
2018-06-28 14:07:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get total number of seconds from DateInterval object
|
|
|
|
*
|
2020-02-10 18:04:38 +03:00
|
|
|
* @param DateInterval $interval
|
2018-06-28 14:07:33 +03:00
|
|
|
* @return int
|
|
|
|
*/
|
2020-02-10 18:04:38 +03:00
|
|
|
private function getIntervalFromDateInterval(DateInterval $interval):int {
|
2018-06-28 14:07:33 +03:00
|
|
|
return $interval->s
|
|
|
|
+ ($interval->i * 60)
|
|
|
|
+ ($interval->h * 60 * 60)
|
|
|
|
+ ($interval->d * 60 * 60 * 24)
|
|
|
|
+ ($interval->m * 60 * 60 * 24 * 30)
|
|
|
|
+ ($interval->y * 60 * 60 * 24 * 365);
|
|
|
|
}
|
|
|
|
|
2020-01-30 14:51:04 +03:00
|
|
|
/**
|
|
|
|
* Fixes types of rows
|
|
|
|
*
|
|
|
|
* @param array $row
|
|
|
|
*/
|
|
|
|
private function fixSubscriptionRowTyping(array &$row):void {
|
|
|
|
$forceInt = [
|
|
|
|
'id',
|
|
|
|
'lastmodified',
|
2020-02-10 18:04:38 +03:00
|
|
|
RefreshWebcalService::STRIP_ALARMS,
|
|
|
|
RefreshWebcalService::STRIP_ATTACHMENTS,
|
|
|
|
RefreshWebcalService::STRIP_TODOS,
|
2020-01-30 14:51:04 +03:00
|
|
|
];
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($forceInt as $column) {
|
2020-01-30 14:51:04 +03:00
|
|
|
if (isset($row[$column])) {
|
|
|
|
$row[$column] = (int) $row[$column];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-28 14:07:33 +03:00
|
|
|
}
|