Completed
Pull Request — master (#9773)
by Georg
35:26 queued 19:05
created
apps/dav/lib/BackgroundJob/UpdateCalendarResourcesRoomsBackgroundJob.php 1 patch
Indentation   +301 added lines, -301 removed lines patch added patch discarded remove patch
@@ -34,305 +34,305 @@
 block discarded – undo
34 34
 
35 35
 class UpdateCalendarResourcesRoomsBackgroundJob extends TimedJob {
36 36
 
37
-	/** @var IResourceManager */
38
-	private $resourceManager;
39
-
40
-	/** @var IRoomManager */
41
-	private $roomManager;
42
-
43
-	/** @var IDBConnection */
44
-	private $db;
45
-
46
-	/** @var CalDavBackend */
47
-	private $calDavBackend;
48
-
49
-	/** @var string */
50
-	private $resourceDbTable;
51
-
52
-	/** @var string */
53
-	private $resourcePrincipalUri;
54
-
55
-	/** @var string */
56
-	private $roomDbTable;
57
-
58
-	/** @var string */
59
-	private $roomPrincipalUri;
60
-
61
-	/**
62
-	 * UpdateCalendarResourcesRoomsBackgroundJob constructor.
63
-	 *
64
-	 * @param IResourceManager $resourceManager
65
-	 * @param IRoomManager $roomManager
66
-	 * @param IDBConnection $dbConnection
67
-	 * @param CalDavBackend $calDavBackend
68
-	 */
69
-	public function __construct(IResourceManager $resourceManager, IRoomManager $roomManager,
70
-								IDBConnection $dbConnection, CalDavBackend $calDavBackend) {
71
-		$this->resourceManager = $resourceManager;
72
-		$this->roomManager = $roomManager;
73
-		$this->db = $dbConnection;
74
-		$this->calDavBackend = $calDavBackend;
75
-		$this->resourceDbTable = 'calendar_resources_cache';
76
-		$this->resourcePrincipalUri = 'principals/calendar-resources';
77
-		$this->roomDbTable = 'calendar_rooms_cache';
78
-		$this->roomPrincipalUri = 'principals/calendar-rooms';
79
-
80
-		// run once an hour
81
-		$this->setInterval(60 * 60);
82
-	}
83
-
84
-	/**
85
-	 * @param $argument
86
-	 */
87
-	public function run($argument) {
88
-		$this->runResources();
89
-		$this->runRooms();
90
-	}
91
-
92
-	/**
93
-	 * run timed job for resources
94
-	 */
95
-	private function runResources() {
96
-		$resourceBackends = $this->resourceManager->getBackends();
97
-		$cachedResources = $this->getCached($this->resourceDbTable);
98
-		$cachedResourceIds = $this->getCachedResourceIds($cachedResources);
99
-
100
-		$remoteResourceIds = [];
101
-		foreach($resourceBackends as $resourceBackend) {
102
-			try {
103
-				$remoteResourceIds[$resourceBackend->getBackendIdentifier()] =
104
-					$resourceBackend->listAllResources();
105
-			} catch(BackendTemporarilyUnavailableException $ex) {
106
-				// If the backend is temporarily unavailable
107
-				// ignore this backend in this execution
108
-				unset($cachedResourceIds[$resourceBackend->getBackendIdentifier()]);
109
-			}
110
-		}
111
-
112
-		$sortedResources = $this->sortByNewDeletedExisting($cachedResourceIds, $remoteResourceIds);
113
-
114
-		foreach($sortedResources['new'] as $backendId => $newResources) {
115
-			foreach ($newResources as $newResource) {
116
-				$resource = $this->resourceManager->getBackend($backendId)
117
-					->getResource($newResource);
118
-				$this->addToCache($this->resourceDbTable, $resource);
119
-			}
120
-		}
121
-		foreach($sortedResources['deleted'] as $backendId => $deletedResources) {
122
-			foreach ($deletedResources as $deletedResource) {
123
-				$this->deleteFromCache($this->resourceDbTable,
124
-					$this->resourcePrincipalUri, $backendId, $deletedResource);
125
-			}
126
-		}
127
-		foreach($sortedResources['edited'] as $backendId => $editedResources) {
128
-			foreach ($editedResources as $editedResource) {
129
-				$resource = $this->resourceManager->getBackend($backendId)
130
-					->getResource($editedResource);
131
-				$this->updateCache($this->resourceDbTable, $resource);
132
-			}
133
-		}
134
-	}
135
-
136
-	/**
137
-	 * run timed job for rooms
138
-	 */
139
-	private function runRooms() {
140
-		$roomBackends = $this->roomManager->getBackends();
141
-		$cachedRooms = $this->getCached($this->roomDbTable);
142
-		$cachedRoomIds = $this->getCachedRoomIds($cachedRooms);
143
-
144
-		$remoteRoomIds = [];
145
-		foreach($roomBackends as $roomBackend) {
146
-			try {
147
-				$remoteRoomIds[$roomBackend->getBackendIdentifier()] =
148
-					$roomBackend->listAllRooms();
149
-			} catch(BackendTemporarilyUnavailableException $ex) {
150
-				// If the backend is temporarily unavailable
151
-				// ignore this backend in this execution
152
-				unset($cachedRoomIds[$roomBackend->getBackendIdentifier()]);
153
-			}
154
-		}
155
-
156
-		$sortedRooms = $this->sortByNewDeletedExisting($cachedRoomIds, $remoteRoomIds);
157
-
158
-		foreach($sortedRooms['new'] as $backendId => $newRooms) {
159
-			foreach ($newRooms as $newRoom) {
160
-				$resource = $this->roomManager->getBackend($backendId)
161
-					->getRoom($newRoom);
162
-				$this->addToCache($this->roomDbTable, $resource);
163
-			}
164
-		}
165
-		foreach($sortedRooms['deleted'] as $backendId => $deletedRooms) {
166
-			foreach ($deletedRooms as $deletedRoom) {
167
-				$this->deleteFromCache($this->roomDbTable,
168
-					$this->roomPrincipalUri, $backendId, $deletedRoom);
169
-			}
170
-		}
171
-		foreach($sortedRooms['edited'] as $backendId => $editedRooms) {
172
-			foreach ($editedRooms as $editedRoom) {
173
-				$resource = $this->roomManager->getBackend($backendId)
174
-					->getRoom($editedRoom);
175
-				$this->updateCache($this->roomDbTable, $resource);
176
-			}
177
-		}
178
-	}
179
-
180
-	/**
181
-	 * get cached db rows for resources / rooms
182
-	 * @param string $tableName
183
-	 * @return array
184
-	 */
185
-	private function getCached($tableName):array {
186
-		$query = $this->db->getQueryBuilder();
187
-		$query->select('*')->from($tableName);
188
-
189
-		$rows = [];
190
-		$stmt = $query->execute();
191
-		while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
192
-			$rows[] = $row;
193
-		}
194
-
195
-		return $rows;
196
-	}
197
-
198
-	/**
199
-	 * @param array $cachedResources
200
-	 * @return array
201
-	 */
202
-	private function getCachedResourceIds(array $cachedResources):array {
203
-		$cachedResourceIds = [];
204
-		foreach ($cachedResources as $cachedResource) {
205
-			if (!isset($cachedResourceIds[$cachedResource['backend_id']])) {
206
-				$cachedResourceIds[$cachedResource['backend_id']] = [];
207
-			}
208
-
209
-			$cachedResourceIds[$cachedResource['backend_id']][] =
210
-				$cachedResource['resource_id'];
211
-		}
212
-
213
-		return $cachedResourceIds;
214
-	}
215
-
216
-	/**
217
-	 * @param array $cachedRooms
218
-	 * @return array
219
-	 */
220
-	private function getCachedRoomIds(array $cachedRooms):array {
221
-		$cachedRoomIds = [];
222
-		foreach ($cachedRooms as $cachedRoom) {
223
-			if (!isset($cachedRoomIds[$cachedRoom['backend_id']])) {
224
-				$cachedRoomIds[$cachedRoom['backend_id']] = [];
225
-			}
226
-
227
-			$cachedRoomIds[$cachedRoom['backend_id']][] =
228
-				$cachedRoom['resource_id'];
229
-		}
230
-
231
-		return $cachedRoomIds;
232
-	}
233
-
234
-	/**
235
-	 * sort list of ids by whether they appear only in the backend /
236
-	 * only in the cache / in both
237
-	 *
238
-	 * @param array $cached
239
-	 * @param array $remote
240
-	 * @return array
241
-	 */
242
-	private function sortByNewDeletedExisting(array $cached, array $remote):array {
243
-		$sorted = [
244
-			'new' => [],
245
-			'deleted' => [],
246
-			'existing' => [],
247
-		];
248
-
249
-		$backendIds = array_merge(array_keys($cached), array_keys($remote));
250
-		foreach($backendIds as $backendId) {
251
-			if (!isset($cached[$backendId])) {
252
-				$sorted['new'][$backendId] = $remote[$backendId];
253
-			} elseif (!isset($remote[$backendId])) {
254
-				$sorted['deleted'][$backendId] = $remote[$backendId];
255
-			} else {
256
-				$sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]);
257
-				$sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]);
258
-				$sorted['existing'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]);
259
-			}
260
-		}
261
-
262
-		return $sorted;
263
-	}
264
-
265
-	/**
266
-	 * add entry to cache that exists remotely but not yet in cache
267
-	 *
268
-	 * @param string $table
269
-	 * @param IResource|IRoom $remote
270
-	 */
271
-	private function addToCache($table, $remote) {
272
-		$query = $this->db->getQueryBuilder();
273
-		$query->insert($table)
274
-			->values([
275
-				'backend_id' => $query->createNamedParameter($remote->getBackend()),
276
-				'resource_id' => $query->createNamedParameter($remote->getId()),
277
-				'email' => $query->createNamedParameter($remote->getEMail()),
278
-				'displayname' => $query->createNamedParameter($remote->getDisplayName()),
279
-				'group_restrictions' => $query->createNamedParameter(
280
-					$this->serializeGroupRestrictions(
281
-						$remote->getGroupRestrictions()
282
-					))
283
-			])
284
-			->execute();
285
-	}
286
-
287
-	/**
288
-	 * delete entry from cache that does not exist anymore remotely
289
-	 *
290
-	 * @param string $table
291
-	 * @param string $principalUri
292
-	 * @param string $backendId
293
-	 * @param string $resourceId
294
-	 */
295
-	private function deleteFromCache($table, $principalUri, $backendId, $resourceId) {
296
-		$query = $this->db->getQueryBuilder();
297
-		$query->delete($table)
298
-			->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
299
-			->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)))
300
-			->execute();
301
-
302
-		try {
303
-			$calendar = $this->calDavBackend->getCalendarByUri($principalUri, implode('-', [$backendId, $resourceId]));
304
-			$this->calDavBackend->deleteCalendar($calendar['id']);
305
-		} catch() {
306
-
307
-		}
308
-		// todo - delete corresponding calendar incl. calendar objects
309
-	}
310
-
311
-	/**
312
-	 * update an existing entry in cache
313
-	 *
314
-	 * @param string $table
315
-	 * @param IResource|IRoom $remote
316
-	 */
317
-	private function updateCache($table, $remote) {
318
-		$query = $this->db->getQueryBuilder();
319
-		$query->update($table)
320
-			->set('email', $query->createNamedParameter($remote->getEMail()))
321
-			->set('displayname', $query->createNamedParameter($remote->getDisplayName()))
322
-			->set('group_restrictions', $query->createNamedParameter(
323
-				$this->serializeGroupRestrictions(
324
-					$remote->getGroupRestrictions()
325
-				)))
326
-			->execute();
327
-	}
328
-
329
-	/**
330
-	 * serialize array of group restrictions to store them in database
331
-	 *
332
-	 * @param array $groups
333
-	 * @return string
334
-	 */
335
-	private function serializeGroupRestrictions(array $groups):string {
336
-		return \json_encode($groups);
337
-	}
37
+    /** @var IResourceManager */
38
+    private $resourceManager;
39
+
40
+    /** @var IRoomManager */
41
+    private $roomManager;
42
+
43
+    /** @var IDBConnection */
44
+    private $db;
45
+
46
+    /** @var CalDavBackend */
47
+    private $calDavBackend;
48
+
49
+    /** @var string */
50
+    private $resourceDbTable;
51
+
52
+    /** @var string */
53
+    private $resourcePrincipalUri;
54
+
55
+    /** @var string */
56
+    private $roomDbTable;
57
+
58
+    /** @var string */
59
+    private $roomPrincipalUri;
60
+
61
+    /**
62
+     * UpdateCalendarResourcesRoomsBackgroundJob constructor.
63
+     *
64
+     * @param IResourceManager $resourceManager
65
+     * @param IRoomManager $roomManager
66
+     * @param IDBConnection $dbConnection
67
+     * @param CalDavBackend $calDavBackend
68
+     */
69
+    public function __construct(IResourceManager $resourceManager, IRoomManager $roomManager,
70
+                                IDBConnection $dbConnection, CalDavBackend $calDavBackend) {
71
+        $this->resourceManager = $resourceManager;
72
+        $this->roomManager = $roomManager;
73
+        $this->db = $dbConnection;
74
+        $this->calDavBackend = $calDavBackend;
75
+        $this->resourceDbTable = 'calendar_resources_cache';
76
+        $this->resourcePrincipalUri = 'principals/calendar-resources';
77
+        $this->roomDbTable = 'calendar_rooms_cache';
78
+        $this->roomPrincipalUri = 'principals/calendar-rooms';
79
+
80
+        // run once an hour
81
+        $this->setInterval(60 * 60);
82
+    }
83
+
84
+    /**
85
+     * @param $argument
86
+     */
87
+    public function run($argument) {
88
+        $this->runResources();
89
+        $this->runRooms();
90
+    }
91
+
92
+    /**
93
+     * run timed job for resources
94
+     */
95
+    private function runResources() {
96
+        $resourceBackends = $this->resourceManager->getBackends();
97
+        $cachedResources = $this->getCached($this->resourceDbTable);
98
+        $cachedResourceIds = $this->getCachedResourceIds($cachedResources);
99
+
100
+        $remoteResourceIds = [];
101
+        foreach($resourceBackends as $resourceBackend) {
102
+            try {
103
+                $remoteResourceIds[$resourceBackend->getBackendIdentifier()] =
104
+                    $resourceBackend->listAllResources();
105
+            } catch(BackendTemporarilyUnavailableException $ex) {
106
+                // If the backend is temporarily unavailable
107
+                // ignore this backend in this execution
108
+                unset($cachedResourceIds[$resourceBackend->getBackendIdentifier()]);
109
+            }
110
+        }
111
+
112
+        $sortedResources = $this->sortByNewDeletedExisting($cachedResourceIds, $remoteResourceIds);
113
+
114
+        foreach($sortedResources['new'] as $backendId => $newResources) {
115
+            foreach ($newResources as $newResource) {
116
+                $resource = $this->resourceManager->getBackend($backendId)
117
+                    ->getResource($newResource);
118
+                $this->addToCache($this->resourceDbTable, $resource);
119
+            }
120
+        }
121
+        foreach($sortedResources['deleted'] as $backendId => $deletedResources) {
122
+            foreach ($deletedResources as $deletedResource) {
123
+                $this->deleteFromCache($this->resourceDbTable,
124
+                    $this->resourcePrincipalUri, $backendId, $deletedResource);
125
+            }
126
+        }
127
+        foreach($sortedResources['edited'] as $backendId => $editedResources) {
128
+            foreach ($editedResources as $editedResource) {
129
+                $resource = $this->resourceManager->getBackend($backendId)
130
+                    ->getResource($editedResource);
131
+                $this->updateCache($this->resourceDbTable, $resource);
132
+            }
133
+        }
134
+    }
135
+
136
+    /**
137
+     * run timed job for rooms
138
+     */
139
+    private function runRooms() {
140
+        $roomBackends = $this->roomManager->getBackends();
141
+        $cachedRooms = $this->getCached($this->roomDbTable);
142
+        $cachedRoomIds = $this->getCachedRoomIds($cachedRooms);
143
+
144
+        $remoteRoomIds = [];
145
+        foreach($roomBackends as $roomBackend) {
146
+            try {
147
+                $remoteRoomIds[$roomBackend->getBackendIdentifier()] =
148
+                    $roomBackend->listAllRooms();
149
+            } catch(BackendTemporarilyUnavailableException $ex) {
150
+                // If the backend is temporarily unavailable
151
+                // ignore this backend in this execution
152
+                unset($cachedRoomIds[$roomBackend->getBackendIdentifier()]);
153
+            }
154
+        }
155
+
156
+        $sortedRooms = $this->sortByNewDeletedExisting($cachedRoomIds, $remoteRoomIds);
157
+
158
+        foreach($sortedRooms['new'] as $backendId => $newRooms) {
159
+            foreach ($newRooms as $newRoom) {
160
+                $resource = $this->roomManager->getBackend($backendId)
161
+                    ->getRoom($newRoom);
162
+                $this->addToCache($this->roomDbTable, $resource);
163
+            }
164
+        }
165
+        foreach($sortedRooms['deleted'] as $backendId => $deletedRooms) {
166
+            foreach ($deletedRooms as $deletedRoom) {
167
+                $this->deleteFromCache($this->roomDbTable,
168
+                    $this->roomPrincipalUri, $backendId, $deletedRoom);
169
+            }
170
+        }
171
+        foreach($sortedRooms['edited'] as $backendId => $editedRooms) {
172
+            foreach ($editedRooms as $editedRoom) {
173
+                $resource = $this->roomManager->getBackend($backendId)
174
+                    ->getRoom($editedRoom);
175
+                $this->updateCache($this->roomDbTable, $resource);
176
+            }
177
+        }
178
+    }
179
+
180
+    /**
181
+     * get cached db rows for resources / rooms
182
+     * @param string $tableName
183
+     * @return array
184
+     */
185
+    private function getCached($tableName):array {
186
+        $query = $this->db->getQueryBuilder();
187
+        $query->select('*')->from($tableName);
188
+
189
+        $rows = [];
190
+        $stmt = $query->execute();
191
+        while($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
192
+            $rows[] = $row;
193
+        }
194
+
195
+        return $rows;
196
+    }
197
+
198
+    /**
199
+     * @param array $cachedResources
200
+     * @return array
201
+     */
202
+    private function getCachedResourceIds(array $cachedResources):array {
203
+        $cachedResourceIds = [];
204
+        foreach ($cachedResources as $cachedResource) {
205
+            if (!isset($cachedResourceIds[$cachedResource['backend_id']])) {
206
+                $cachedResourceIds[$cachedResource['backend_id']] = [];
207
+            }
208
+
209
+            $cachedResourceIds[$cachedResource['backend_id']][] =
210
+                $cachedResource['resource_id'];
211
+        }
212
+
213
+        return $cachedResourceIds;
214
+    }
215
+
216
+    /**
217
+     * @param array $cachedRooms
218
+     * @return array
219
+     */
220
+    private function getCachedRoomIds(array $cachedRooms):array {
221
+        $cachedRoomIds = [];
222
+        foreach ($cachedRooms as $cachedRoom) {
223
+            if (!isset($cachedRoomIds[$cachedRoom['backend_id']])) {
224
+                $cachedRoomIds[$cachedRoom['backend_id']] = [];
225
+            }
226
+
227
+            $cachedRoomIds[$cachedRoom['backend_id']][] =
228
+                $cachedRoom['resource_id'];
229
+        }
230
+
231
+        return $cachedRoomIds;
232
+    }
233
+
234
+    /**
235
+     * sort list of ids by whether they appear only in the backend /
236
+     * only in the cache / in both
237
+     *
238
+     * @param array $cached
239
+     * @param array $remote
240
+     * @return array
241
+     */
242
+    private function sortByNewDeletedExisting(array $cached, array $remote):array {
243
+        $sorted = [
244
+            'new' => [],
245
+            'deleted' => [],
246
+            'existing' => [],
247
+        ];
248
+
249
+        $backendIds = array_merge(array_keys($cached), array_keys($remote));
250
+        foreach($backendIds as $backendId) {
251
+            if (!isset($cached[$backendId])) {
252
+                $sorted['new'][$backendId] = $remote[$backendId];
253
+            } elseif (!isset($remote[$backendId])) {
254
+                $sorted['deleted'][$backendId] = $remote[$backendId];
255
+            } else {
256
+                $sorted['new'][$backendId] = array_diff($remote[$backendId], $cached[$backendId]);
257
+                $sorted['deleted'][$backendId] = array_diff($cached[$backendId], $remote[$backendId]);
258
+                $sorted['existing'][$backendId] = array_intersect($remote[$backendId], $cached[$backendId]);
259
+            }
260
+        }
261
+
262
+        return $sorted;
263
+    }
264
+
265
+    /**
266
+     * add entry to cache that exists remotely but not yet in cache
267
+     *
268
+     * @param string $table
269
+     * @param IResource|IRoom $remote
270
+     */
271
+    private function addToCache($table, $remote) {
272
+        $query = $this->db->getQueryBuilder();
273
+        $query->insert($table)
274
+            ->values([
275
+                'backend_id' => $query->createNamedParameter($remote->getBackend()),
276
+                'resource_id' => $query->createNamedParameter($remote->getId()),
277
+                'email' => $query->createNamedParameter($remote->getEMail()),
278
+                'displayname' => $query->createNamedParameter($remote->getDisplayName()),
279
+                'group_restrictions' => $query->createNamedParameter(
280
+                    $this->serializeGroupRestrictions(
281
+                        $remote->getGroupRestrictions()
282
+                    ))
283
+            ])
284
+            ->execute();
285
+    }
286
+
287
+    /**
288
+     * delete entry from cache that does not exist anymore remotely
289
+     *
290
+     * @param string $table
291
+     * @param string $principalUri
292
+     * @param string $backendId
293
+     * @param string $resourceId
294
+     */
295
+    private function deleteFromCache($table, $principalUri, $backendId, $resourceId) {
296
+        $query = $this->db->getQueryBuilder();
297
+        $query->delete($table)
298
+            ->where($query->expr()->eq('backend_id', $query->createNamedParameter($backendId)))
299
+            ->andWhere($query->expr()->eq('resource_id', $query->createNamedParameter($resourceId)))
300
+            ->execute();
301
+
302
+        try {
303
+            $calendar = $this->calDavBackend->getCalendarByUri($principalUri, implode('-', [$backendId, $resourceId]));
304
+            $this->calDavBackend->deleteCalendar($calendar['id']);
305
+        } catch() {
306
+
307
+        }
308
+        // todo - delete corresponding calendar incl. calendar objects
309
+    }
310
+
311
+    /**
312
+     * update an existing entry in cache
313
+     *
314
+     * @param string $table
315
+     * @param IResource|IRoom $remote
316
+     */
317
+    private function updateCache($table, $remote) {
318
+        $query = $this->db->getQueryBuilder();
319
+        $query->update($table)
320
+            ->set('email', $query->createNamedParameter($remote->getEMail()))
321
+            ->set('displayname', $query->createNamedParameter($remote->getDisplayName()))
322
+            ->set('group_restrictions', $query->createNamedParameter(
323
+                $this->serializeGroupRestrictions(
324
+                    $remote->getGroupRestrictions()
325
+                )))
326
+            ->execute();
327
+    }
328
+
329
+    /**
330
+     * serialize array of group restrictions to store them in database
331
+     *
332
+     * @param array $groups
333
+     * @return string
334
+     */
335
+    private function serializeGroupRestrictions(array $groups):string {
336
+        return \json_encode($groups);
337
+    }
338 338
 }
Please login to merge, or discard this patch.