Completed
Pull Request — master (#10059)
by Georg
21:09
created
apps/dav/lib/Migration/Version1006Date20180628111625.php 1 patch
Indentation   +70 added lines, -70 removed lines patch added patch discarded remove patch
@@ -31,78 +31,78 @@
 block discarded – undo
31 31
 
32 32
 class Version1006Date20180628111625 extends SimpleMigrationStep {
33 33
 
34
-	/**
35
-	 * @param IOutput $output
36
-	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
37
-	 * @param array $options
38
-	 * @return null|ISchemaWrapper
39
-	 */
40
-	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
41
-		/** @var ISchemaWrapper $schema */
42
-		$schema = $schemaClosure();
34
+    /**
35
+     * @param IOutput $output
36
+     * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
37
+     * @param array $options
38
+     * @return null|ISchemaWrapper
39
+     */
40
+    public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
41
+        /** @var ISchemaWrapper $schema */
42
+        $schema = $schemaClosure();
43 43
 
44
-		if (!$schema->hasTable('calendarsubscrobjects')) {
45
-			$table = $schema->createTable('calendarsubscrobjects');
46
-			$table->addColumn('id', Type::BIGINT, [
47
-				'autoincrement' => true,
48
-				'notnull' => true,
49
-				'length' => 11,
50
-				'unsigned' => true,
51
-			]);
52
-			$table->addColumn('calendardata', Type::BLOB, [
53
-				'notnull' => false,
54
-			]);
55
-			$table->addColumn('uri', Type::STRING, [
56
-				'notnull' => false,
57
-				'length' => 255,
58
-			]);
59
-			$table->addColumn('subscriptionid', Type::BIGINT, [
60
-				'notnull' => true,
61
-				'length' => 11,
62
-				'unsigned' => true,
63
-			]);
64
-			$table->addColumn('lastmodified', Type::INTEGER, [
65
-				'notnull' => false,
66
-				'length' => 10,
67
-				'unsigned' => true,
68
-			]);
69
-			$table->addColumn('etag', Type::STRING, [
70
-				'notnull' => false,
71
-				'length' => 32,
72
-			]);
73
-			$table->addColumn('size', Type::BIGINT, [
74
-				'notnull' => true,
75
-				'length' => 11,
76
-				'unsigned' => true,
77
-			]);
78
-			$table->addColumn('componenttype', Type::STRING, [
79
-				'notnull' => false,
80
-				'length' => 8,
81
-			]);
82
-			$table->addColumn('firstoccurence', Type::BIGINT, [
83
-				'notnull' => false,
84
-				'length' => 11,
85
-				'unsigned' => true,
86
-			]);
87
-			$table->addColumn('lastoccurence', Type::BIGINT, [
88
-				'notnull' => false,
89
-				'length' => 11,
90
-				'unsigned' => true,
91
-			]);
92
-			$table->addColumn('uid', Type::STRING, [
93
-				'notnull' => false,
94
-				'length' => 255,
95
-			]);
96
-			$table->addColumn('classification', Type::INTEGER, [
97
-				'notnull' => false,
98
-				'default' => 0,
99
-			]);
100
-			$table->setPrimaryKey(['id']);
101
-			$table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index');
102
-		}
44
+        if (!$schema->hasTable('calendarsubscrobjects')) {
45
+            $table = $schema->createTable('calendarsubscrobjects');
46
+            $table->addColumn('id', Type::BIGINT, [
47
+                'autoincrement' => true,
48
+                'notnull' => true,
49
+                'length' => 11,
50
+                'unsigned' => true,
51
+            ]);
52
+            $table->addColumn('calendardata', Type::BLOB, [
53
+                'notnull' => false,
54
+            ]);
55
+            $table->addColumn('uri', Type::STRING, [
56
+                'notnull' => false,
57
+                'length' => 255,
58
+            ]);
59
+            $table->addColumn('subscriptionid', Type::BIGINT, [
60
+                'notnull' => true,
61
+                'length' => 11,
62
+                'unsigned' => true,
63
+            ]);
64
+            $table->addColumn('lastmodified', Type::INTEGER, [
65
+                'notnull' => false,
66
+                'length' => 10,
67
+                'unsigned' => true,
68
+            ]);
69
+            $table->addColumn('etag', Type::STRING, [
70
+                'notnull' => false,
71
+                'length' => 32,
72
+            ]);
73
+            $table->addColumn('size', Type::BIGINT, [
74
+                'notnull' => true,
75
+                'length' => 11,
76
+                'unsigned' => true,
77
+            ]);
78
+            $table->addColumn('componenttype', Type::STRING, [
79
+                'notnull' => false,
80
+                'length' => 8,
81
+            ]);
82
+            $table->addColumn('firstoccurence', Type::BIGINT, [
83
+                'notnull' => false,
84
+                'length' => 11,
85
+                'unsigned' => true,
86
+            ]);
87
+            $table->addColumn('lastoccurence', Type::BIGINT, [
88
+                'notnull' => false,
89
+                'length' => 11,
90
+                'unsigned' => true,
91
+            ]);
92
+            $table->addColumn('uid', Type::STRING, [
93
+                'notnull' => false,
94
+                'length' => 255,
95
+            ]);
96
+            $table->addColumn('classification', Type::INTEGER, [
97
+                'notnull' => false,
98
+                'default' => 0,
99
+            ]);
100
+            $table->setPrimaryKey(['id']);
101
+            $table->addUniqueIndex(['subscriptionid', 'uri'], 'subobjects_index');
102
+        }
103 103
 
104
-		return $schema;
105
-	}
104
+        return $schema;
105
+    }
106 106
 }
107 107
 
108 108
 
Please login to merge, or discard this patch.
apps/dav/lib/BackgroundJob/RefreshWebcalJob.php 2 patches
Indentation   +194 added lines, -194 removed lines patch added patch discarded remove patch
@@ -37,198 +37,198 @@
 block discarded – undo
37 37
 
38 38
 class RefreshWebcalJob extends Job {
39 39
 
40
-	/** @var CalDavBackend */
41
-	private $calDavBackend;
42
-
43
-	/** @var IClientService */
44
-	private $clientService;
45
-
46
-	/** @var ILogger */
47
-	private $logger;
48
-
49
-	/** @var ITimeFactory */
50
-	private $timeFactory;
51
-
52
-	/** @var array */
53
-	private $subscription;
54
-
55
-	/**
56
-	 * RefreshWebcalJob constructor.
57
-	 *
58
-	 * @param CalDavBackend $calDavBackend
59
-	 * @param IClientService $clientService
60
-	 * @param ILogger $logger
61
-	 */
62
-	public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, ILogger $logger, ITimeFactory $timeFactory) {
63
-		$this->calDavBackend = $calDavBackend;
64
-		$this->clientService = $clientService;
65
-		$this->logger = $logger;
66
-		$this->timeFactory = $timeFactory;
67
-	}
68
-
69
-	/**
70
-	 * this function is called at most every hour
71
-	 *
72
-	 * @inheritdoc
73
-	 */
74
-	public function execute($jobList, ILogger $logger = null) {
75
-		$subscription = $this->getSubscription($this->argument['principaluri'], $this->argument['uri']);
76
-		if (!$subscription) {
77
-			return;
78
-		}
79
-
80
-		// if no refresh rate was configured, just refresh once a week
81
-		$subscriptionId = $subscription['id'];
82
-		$subscription['refreshrate'] = $subscription['refreshrate'] || 'P1W';
83
-
84
-		try {
85
-			/** @var \DateInterval $dateInterval */
86
-			$dateInterval = DateTimeParser::parseDuration($subscription['refreshrate']);
87
-		} catch(InvalidDataException $ex) {
88
-			$this->logger->logException($ex);
89
-			$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
90
-			return;
91
-		}
92
-
93
-		$interval = $this->getIntervalFromDateInterval($dateInterval);
94
-		if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
95
-			return;
96
-		}
97
-
98
-		parent::execute($jobList, $logger);
99
-	}
100
-
101
-	/**
102
-	 * @param array $argument
103
-	 */
104
-	protected function run($argument) {
105
-		$subscription = $this->getSubscription($argument['principaluri'], $argument['uri']);
106
-		if (!$subscription) {
107
-			return;
108
-		}
109
-
110
-		$webcalData = $this->queryWebcalFeed($subscription);
111
-		if (!$webcalData) {
112
-			return;
113
-		}
114
-
115
-		$stripTodos = $subscription['striptodos'];
116
-		$stripAlarms = $subscription['stripalarms'];
117
-		$stripAttachments = $subscription['stripattachments'];
118
-
119
-		try {
120
-			$splitter = new ICalendar($webcalData);
121
-
122
-			// we wait with deleting all purged events till we parsed the new ones
123
-			// in case the new calendar is broken and new ICalendar throws a ParseException
124
-			// the user will still see the old data
125
-			$this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']);
126
-
127
-			while ($vObject = $splitter->getNext()) {
128
-				/** @var Component $vObject */
129
-				$uid = null;
130
-				$compName = null;
131
-
132
-				foreach ($vObject->getComponents() as $component) {
133
-					if ($component->name === 'VTIMEZONE') {
134
-						continue;
135
-					}
136
-
137
-					$uid = $component->{'UID'}->getValue();
138
-					$compName = $component->name;
139
-
140
-					if ($stripAlarms) {
141
-						unset($component->{'VALARM'});
142
-					}
143
-					if ($stripAttachments) {
144
-						unset($component->{'ATTACH'});
145
-					}
146
-				}
147
-
148
-				if ($stripTodos && $compName === 'VTODO') {
149
-					continue;
150
-				}
151
-
152
-				$uri = $uid . '.ics';
153
-				$calendarData = $vObject->serialize();
154
-				try {
155
-					$this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData);
156
-				} catch(BadRequest $ex) {
157
-					$this->logger->logException($ex);
158
-				}
159
-			}
160
-
161
-		} catch(ParseException $ex) {
162
-			$subscriptionId = $subscription['id'];
163
-
164
-			$this->logger->logException($ex);
165
-			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error");
166
-		}
167
-	}
168
-
169
-	/**
170
-	 * @param array $subscription
171
-	 * @return null|resource
172
-	 */
173
-	private function queryWebcalFeed(array $subscription) {
174
-		$client = $this->clientService->newClient();
175
-		try {
176
-			$response = $client->get($subscription['source'], [
177
-				'stream' => true,
178
-				'allow_redirects' => [
179
-					'redirects' => 10
180
-				],
181
-			]);
182
-
183
-			return $response->getBody();
184
-		} catch(\Exception $ex) {
185
-			$subscriptionId = $subscription['id'];
186
-
187
-			$this->logger->logException($ex);
188
-			$this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error");
189
-
190
-			return null;
191
-		}
192
-	}
193
-
194
-	/**
195
-	 * @param string $principalUri
196
-	 * @param string $uri
197
-	 * @return array|null
198
-	 */
199
-	private function getSubscription($principalUri, $uri) {
200
-		$arrIndex = $principalUri . '::' . $uri;
201
-		if (isset($this->subscription[$arrIndex])) {
202
-			return $this->subscription[$arrIndex];
203
-		}
204
-
205
-		$subscriptions = array_filter(
206
-			$this->calDavBackend->getSubscriptionsForUser($principalUri),
207
-			function($sub) use ($uri) {
208
-				return $sub['uri'] === $uri;
209
-			}
210
-		);
211
-
212
-		if (\count($subscriptions) === 0) {
213
-			return null;
214
-		}
215
-
216
-		$this->subscription[$arrIndex] = $subscriptions[0];
217
-		return $this->subscription[$arrIndex];
218
-	}
219
-
220
-	/**
221
-	 * get total number of seconds from DateInterval object
222
-	 *
223
-	 * @param \DateInterval $interval
224
-	 * @return int
225
-	 */
226
-	private function getIntervalFromDateInterval(\DateInterval $interval):int {
227
-		return $interval->s
228
-			+ ($interval->i * 60)
229
-			+ ($interval->h * 60 * 60)
230
-			+ ($interval->d * 60 * 60 * 24)
231
-			+ ($interval->m * 60 * 60 * 24 * 30)
232
-			+ ($interval->y * 60 * 60 * 24 * 365);
233
-	}
40
+    /** @var CalDavBackend */
41
+    private $calDavBackend;
42
+
43
+    /** @var IClientService */
44
+    private $clientService;
45
+
46
+    /** @var ILogger */
47
+    private $logger;
48
+
49
+    /** @var ITimeFactory */
50
+    private $timeFactory;
51
+
52
+    /** @var array */
53
+    private $subscription;
54
+
55
+    /**
56
+     * RefreshWebcalJob constructor.
57
+     *
58
+     * @param CalDavBackend $calDavBackend
59
+     * @param IClientService $clientService
60
+     * @param ILogger $logger
61
+     */
62
+    public function __construct(CalDavBackend $calDavBackend, IClientService $clientService, ILogger $logger, ITimeFactory $timeFactory) {
63
+        $this->calDavBackend = $calDavBackend;
64
+        $this->clientService = $clientService;
65
+        $this->logger = $logger;
66
+        $this->timeFactory = $timeFactory;
67
+    }
68
+
69
+    /**
70
+     * this function is called at most every hour
71
+     *
72
+     * @inheritdoc
73
+     */
74
+    public function execute($jobList, ILogger $logger = null) {
75
+        $subscription = $this->getSubscription($this->argument['principaluri'], $this->argument['uri']);
76
+        if (!$subscription) {
77
+            return;
78
+        }
79
+
80
+        // if no refresh rate was configured, just refresh once a week
81
+        $subscriptionId = $subscription['id'];
82
+        $subscription['refreshrate'] = $subscription['refreshrate'] || 'P1W';
83
+
84
+        try {
85
+            /** @var \DateInterval $dateInterval */
86
+            $dateInterval = DateTimeParser::parseDuration($subscription['refreshrate']);
87
+        } catch(InvalidDataException $ex) {
88
+            $this->logger->logException($ex);
89
+            $this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
90
+            return;
91
+        }
92
+
93
+        $interval = $this->getIntervalFromDateInterval($dateInterval);
94
+        if (($this->timeFactory->getTime() - $this->lastRun) <= $interval) {
95
+            return;
96
+        }
97
+
98
+        parent::execute($jobList, $logger);
99
+    }
100
+
101
+    /**
102
+     * @param array $argument
103
+     */
104
+    protected function run($argument) {
105
+        $subscription = $this->getSubscription($argument['principaluri'], $argument['uri']);
106
+        if (!$subscription) {
107
+            return;
108
+        }
109
+
110
+        $webcalData = $this->queryWebcalFeed($subscription);
111
+        if (!$webcalData) {
112
+            return;
113
+        }
114
+
115
+        $stripTodos = $subscription['striptodos'];
116
+        $stripAlarms = $subscription['stripalarms'];
117
+        $stripAttachments = $subscription['stripattachments'];
118
+
119
+        try {
120
+            $splitter = new ICalendar($webcalData);
121
+
122
+            // we wait with deleting all purged events till we parsed the new ones
123
+            // in case the new calendar is broken and new ICalendar throws a ParseException
124
+            // the user will still see the old data
125
+            $this->calDavBackend->purgeAllCachedEventsForSubscription($subscription['id']);
126
+
127
+            while ($vObject = $splitter->getNext()) {
128
+                /** @var Component $vObject */
129
+                $uid = null;
130
+                $compName = null;
131
+
132
+                foreach ($vObject->getComponents() as $component) {
133
+                    if ($component->name === 'VTIMEZONE') {
134
+                        continue;
135
+                    }
136
+
137
+                    $uid = $component->{'UID'}->getValue();
138
+                    $compName = $component->name;
139
+
140
+                    if ($stripAlarms) {
141
+                        unset($component->{'VALARM'});
142
+                    }
143
+                    if ($stripAttachments) {
144
+                        unset($component->{'ATTACH'});
145
+                    }
146
+                }
147
+
148
+                if ($stripTodos && $compName === 'VTODO') {
149
+                    continue;
150
+                }
151
+
152
+                $uri = $uid . '.ics';
153
+                $calendarData = $vObject->serialize();
154
+                try {
155
+                    $this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData);
156
+                } catch(BadRequest $ex) {
157
+                    $this->logger->logException($ex);
158
+                }
159
+            }
160
+
161
+        } catch(ParseException $ex) {
162
+            $subscriptionId = $subscription['id'];
163
+
164
+            $this->logger->logException($ex);
165
+            $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a parsing error");
166
+        }
167
+    }
168
+
169
+    /**
170
+     * @param array $subscription
171
+     * @return null|resource
172
+     */
173
+    private function queryWebcalFeed(array $subscription) {
174
+        $client = $this->clientService->newClient();
175
+        try {
176
+            $response = $client->get($subscription['source'], [
177
+                'stream' => true,
178
+                'allow_redirects' => [
179
+                    'redirects' => 10
180
+                ],
181
+            ]);
182
+
183
+            return $response->getBody();
184
+        } catch(\Exception $ex) {
185
+            $subscriptionId = $subscription['id'];
186
+
187
+            $this->logger->logException($ex);
188
+            $this->logger->warning("Subscription $subscriptionId could not be refreshed due to a network error");
189
+
190
+            return null;
191
+        }
192
+    }
193
+
194
+    /**
195
+     * @param string $principalUri
196
+     * @param string $uri
197
+     * @return array|null
198
+     */
199
+    private function getSubscription($principalUri, $uri) {
200
+        $arrIndex = $principalUri . '::' . $uri;
201
+        if (isset($this->subscription[$arrIndex])) {
202
+            return $this->subscription[$arrIndex];
203
+        }
204
+
205
+        $subscriptions = array_filter(
206
+            $this->calDavBackend->getSubscriptionsForUser($principalUri),
207
+            function($sub) use ($uri) {
208
+                return $sub['uri'] === $uri;
209
+            }
210
+        );
211
+
212
+        if (\count($subscriptions) === 0) {
213
+            return null;
214
+        }
215
+
216
+        $this->subscription[$arrIndex] = $subscriptions[0];
217
+        return $this->subscription[$arrIndex];
218
+    }
219
+
220
+    /**
221
+     * get total number of seconds from DateInterval object
222
+     *
223
+     * @param \DateInterval $interval
224
+     * @return int
225
+     */
226
+    private function getIntervalFromDateInterval(\DateInterval $interval):int {
227
+        return $interval->s
228
+            + ($interval->i * 60)
229
+            + ($interval->h * 60 * 60)
230
+            + ($interval->d * 60 * 60 * 24)
231
+            + ($interval->m * 60 * 60 * 24 * 30)
232
+            + ($interval->y * 60 * 60 * 24 * 365);
233
+    }
234 234
 }
Please login to merge, or discard this patch.
Spacing   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -84,7 +84,7 @@  discard block
 block discarded – undo
84 84
 		try {
85 85
 			/** @var \DateInterval $dateInterval */
86 86
 			$dateInterval = DateTimeParser::parseDuration($subscription['refreshrate']);
87
-		} catch(InvalidDataException $ex) {
87
+		} catch (InvalidDataException $ex) {
88 88
 			$this->logger->logException($ex);
89 89
 			$this->logger->warning("Subscription $subscriptionId could not be refreshed, refreshrate in database is invalid");
90 90
 			return;
@@ -149,16 +149,16 @@  discard block
 block discarded – undo
149 149
 					continue;
150 150
 				}
151 151
 
152
-				$uri = $uid . '.ics';
152
+				$uri = $uid.'.ics';
153 153
 				$calendarData = $vObject->serialize();
154 154
 				try {
155 155
 					$this->calDavBackend->addCachedEvent($subscription['id'], $uri, $calendarData);
156
-				} catch(BadRequest $ex) {
156
+				} catch (BadRequest $ex) {
157 157
 					$this->logger->logException($ex);
158 158
 				}
159 159
 			}
160 160
 
161
-		} catch(ParseException $ex) {
161
+		} catch (ParseException $ex) {
162 162
 			$subscriptionId = $subscription['id'];
163 163
 
164 164
 			$this->logger->logException($ex);
@@ -181,7 +181,7 @@  discard block
 block discarded – undo
181 181
 			]);
182 182
 
183 183
 			return $response->getBody();
184
-		} catch(\Exception $ex) {
184
+		} catch (\Exception $ex) {
185 185
 			$subscriptionId = $subscription['id'];
186 186
 
187 187
 			$this->logger->logException($ex);
@@ -197,7 +197,7 @@  discard block
 block discarded – undo
197 197
 	 * @return array|null
198 198
 	 */
199 199
 	private function getSubscription($principalUri, $uri) {
200
-		$arrIndex = $principalUri . '::' . $uri;
200
+		$arrIndex = $principalUri.'::'.$uri;
201 201
 		if (isset($this->subscription[$arrIndex])) {
202 202
 			return $this->subscription[$arrIndex];
203 203
 		}
Please login to merge, or discard this patch.