|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* @copyright 2018 Georg Ehrke <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* @author Georg Ehrke <[email protected]> |
|
9
|
|
|
* @author Roeland Jago Douma <[email protected]> |
|
10
|
|
|
* @author Thomas Citharel <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license GNU AGPL version 3 or any later version |
|
13
|
|
|
* |
|
14
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
16
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
17
|
|
|
* License, or (at your option) any later version. |
|
18
|
|
|
* |
|
19
|
|
|
* This program is distributed in the hope that it will be useful, |
|
20
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
21
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
22
|
|
|
* GNU Affero General Public License for more details. |
|
23
|
|
|
* |
|
24
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
25
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
26
|
|
|
* |
|
27
|
|
|
*/ |
|
28
|
|
|
|
|
29
|
|
|
namespace OCA\DAV\BackgroundJob; |
|
30
|
|
|
|
|
31
|
|
|
use DateInterval; |
|
32
|
|
|
use OC\BackgroundJob\Job; |
|
33
|
|
|
use OCA\DAV\CalDAV\WebcalCaching\RefreshWebcalService; |
|
34
|
|
|
use OCP\AppFramework\Utility\ITimeFactory; |
|
35
|
|
|
use OCP\IConfig; |
|
36
|
|
|
use OCP\ILogger; |
|
37
|
|
|
use Sabre\VObject\DateTimeParser; |
|
38
|
|
|
use Sabre\VObject\InvalidDataException; |
|
39
|
|
|
|
|
40
|
|
|
class RefreshWebcalJob extends Job { |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @var RefreshWebcalService |
|
44
|
|
|
*/ |
|
45
|
|
|
private $refreshWebcalService; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var IConfig |
|
49
|
|
|
*/ |
|
50
|
|
|
private $config; |
|
51
|
|
|
|
|
52
|
|
|
/** @var ILogger */ |
|
53
|
|
|
private $logger; |
|
54
|
|
|
|
|
55
|
|
|
/** @var ITimeFactory */ |
|
56
|
|
|
private $timeFactory; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* RefreshWebcalJob constructor. |
|
60
|
|
|
* |
|
61
|
|
|
* @param RefreshWebcalService $refreshWebcalService |
|
62
|
|
|
* @param IConfig $config |
|
63
|
|
|
* @param ILogger $logger |
|
64
|
|
|
* @param ITimeFactory $timeFactory |
|
65
|
|
|
*/ |
|
66
|
|
|
public function __construct(RefreshWebcalService $refreshWebcalService, IConfig $config, ILogger $logger, ITimeFactory $timeFactory) { |
|
67
|
|
|
$this->refreshWebcalService = $refreshWebcalService; |
|
68
|
|
|
$this->config = $config; |
|
69
|
|
|
$this->logger = $logger; |
|
70
|
|
|
$this->timeFactory = $timeFactory; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* this function is called at most every hour |
|
75
|
|
|
* |
|
76
|
|
|
* @inheritdoc |
|
77
|
|
|
*/ |
|
78
|
|
|
public function execute($jobList, ILogger $logger = null) { |
|
79
|
|
|
$subscription = $this->refreshWebcalService->getSubscription($this->argument['principaluri'], $this->argument['uri']); |
|
80
|
|
|
if (!$subscription) { |
|
81
|
|
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
$this->fixSubscriptionRowTyping($subscription); |
|
85
|
|
|
|
|
86
|
|
|
// if no refresh rate was configured, just refresh once a week |
|
87
|
|
|
$defaultRefreshRate = $this->config->getAppValue('dav', 'calendarSubscriptionRefreshRate', 'P1W'); |
|
88
|
|
|
$refreshRate = $subscription[RefreshWebcalService::REFRESH_RATE] ?? $defaultRefreshRate; |
|
89
|
|
|
|
|
90
|
|
|
$subscriptionId = $subscription['id']; |
|
91
|
|
|
|
|
92
|
|
|
try { |
|
93
|
|
|
/** @var DateInterval $dateInterval */ |
|
94
|
|
|
$dateInterval = DateTimeParser::parseDuration($refreshRate); |
|
95
|
|
|
} catch(InvalidDataException $ex) { |
|
96
|
|
|
$this->logger->logException($ex); |
|
97
|
|
|
$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid"); |
|
98
|
|
|
return; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
$interval = $this->getIntervalFromDateInterval($dateInterval); |
|
102
|
|
|
if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) { |
|
103
|
|
|
return; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
parent::execute($jobList, $logger); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param array $argument |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function run($argument) { |
|
113
|
|
|
$this->refreshWebcalService->refreshSubscription($argument['principaluri'], $argument['uri']); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* get total number of seconds from DateInterval object |
|
118
|
|
|
* |
|
119
|
|
|
* @param DateInterval $interval |
|
120
|
|
|
* @return int |
|
121
|
|
|
*/ |
|
122
|
|
|
private function getIntervalFromDateInterval(DateInterval $interval):int { |
|
123
|
|
|
return $interval->s |
|
124
|
|
|
+ ($interval->i * 60) |
|
125
|
|
|
+ ($interval->h * 60 * 60) |
|
126
|
|
|
+ ($interval->d * 60 * 60 * 24) |
|
127
|
|
|
+ ($interval->m * 60 * 60 * 24 * 30) |
|
128
|
|
|
+ ($interval->y * 60 * 60 * 24 * 365); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Fixes types of rows |
|
133
|
|
|
* |
|
134
|
|
|
* @param array $row |
|
135
|
|
|
*/ |
|
136
|
|
|
private function fixSubscriptionRowTyping(array &$row):void { |
|
137
|
|
|
$forceInt = [ |
|
138
|
|
|
'id', |
|
139
|
|
|
'lastmodified', |
|
140
|
|
|
RefreshWebcalService::STRIP_ALARMS, |
|
141
|
|
|
RefreshWebcalService::STRIP_ATTACHMENTS, |
|
142
|
|
|
RefreshWebcalService::STRIP_TODOS, |
|
143
|
|
|
]; |
|
144
|
|
|
|
|
145
|
|
|
foreach($forceInt as $column) { |
|
146
|
|
|
if (isset($row[$column])) { |
|
147
|
|
|
$row[$column] = (int) $row[$column]; |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|