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