Passed
Pull Request — master (#232)
by Matias
05:40 queued 04:10
created

PersonMapper   A

Complexity

Total Complexity 30

Size/Duplication

Total Lines 366
Duplicated Lines 0 %

Test Coverage

Coverage 75.13%

Importance

Changes 15
Bugs 0 Features 0
Metric Value
eloc 182
c 15
b 0
f 0
dl 0
loc 366
ccs 145
cts 193
cp 0.7513
rs 10
wmc 30

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 2 1
A deleteUserPersons() 0 5 1
A find() 0 7 1
A deleteOrphaned() 0 22 2
A findByName() 0 19 1
A findAll() 0 17 1
A countPersons() 0 29 2
C mergeClusterToDatabase() 0 107 14
A isFaceInClusters() 0 7 3
A removeIfEmpty() 0 12 1
A updateFace() 0 6 1
A invalidatePersons() 0 16 1
A findFromFile() 0 10 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2018-2020, Matias De lellis <[email protected]>
4
 * @copyright Copyright (c) 2018-2019, Branko Kokanovic <[email protected]>
5
 *
6
 * @author Branko Kokanovic <[email protected]>
7
 *
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
namespace OCA\FaceRecognition\Db;
25
26
use OC\DB\QueryBuilder\Literal;
0 ignored issues
show
Bug introduced by
The type OC\DB\QueryBuilder\Literal was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
28
use OCP\IDBConnection;
29
use OCP\IUser;
30
31
use OCP\AppFramework\Db\QBMapper;
32
use OCP\AppFramework\Db\DoesNotExistException;
33
use OCP\DB\QueryBuilder\IQueryBuilder;
34
35
class PersonMapper extends QBMapper {
36
37 1
	public function __construct(IDBConnection $db) {
38 1
		parent::__construct($db, 'facerecog_persons', '\OCA\FaceRecognition\Db\Person');
39 1
	}
40
41
	/**
42
	 * @param string $userId ID of the user
43
	 * @param int $personId ID of the person
44
	 */
45 8
	public function find(string $userId, int $personId) {
46 8
		$qb = $this->db->getQueryBuilder();
47 8
		$qb->select('id', 'name')
48 8
			->from($this->getTableName(), 'p')
49 8
			->where($qb->expr()->eq('id', $qb->createNamedParameter($personId)))
50 8
			->andWhere($qb->expr()->eq('user', $qb->createNamedParameter($userId)));
51 8
		return $this->findEntity($qb);
52
	}
53
54
	/**
55
	 * @param string $userId ID of the user
56
	 * @param int $modelId ID of the model
57
	 * @param string $personName name of the person to find
58
	 * @return Person[]
59
	 */
60
	public function findByName(string $userId, int $modelId, string $personName): array {
61
		$sub = $this->db->getQueryBuilder();
62
		$sub->select(new Literal('1'))
63
			->from('facerecog_faces', 'f')
64
			->innerJoin('f', 'facerecog_images' ,'i', $sub->expr()->eq('f.image', 'i.id'))
65
			->where($sub->expr()->eq('p.id', 'f.person'))
66
			->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user_id')))
67
			->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model_id')))
68
			->andwhere($sub->expr()->eq('p.name', $sub->createParameter('person_name')));
69
70
		$qb = $this->db->getQueryBuilder();
71
		$qb->select('id', 'name', 'is_valid')
72
			->from($this->getTableName(), 'p')
73
			->where('EXISTS (' . $sub->getSQL() . ')')
74
			->setParameter('user_id', $userId)
75
			->setParameter('model_id', $modelId)
76
			->setParameter('person_name', $personName);
77
78
		return $this->findEntities($qb);
79
	}
80
81
	/**
82
	 * @param string $userId ID of the user
83
	 * @param int $modelId ID of the model
84
	 * @return Person[]
85
	 */
86 13
	public function findAll(string $userId, int $modelId): array {
87 13
		$sub = $this->db->getQueryBuilder();
88 13
		$sub->select(new Literal('1'))
89 13
			->from('facerecog_faces', 'f')
90 13
			->innerJoin('f', 'facerecog_images' ,'i', $sub->expr()->eq('f.image', 'i.id'))
91 13
			->where($sub->expr()->eq('p.id', 'f.person'))
92 13
			->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user_id')))
93 13
			->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model_id')));
94
95 13
		$qb = $this->db->getQueryBuilder();
96 13
		$qb->select('id', 'name', 'is_valid')
97 13
			->from($this->getTableName(), 'p')
98 13
			->where('EXISTS (' . $sub->getSQL() . ')')
99 13
			->setParameter('user_id', $userId)
100 13
			->setParameter('model_id', $modelId);
101
102 13
		return $this->findEntities($qb);
103
	}
104
105
	/**
106
	 * Returns count of persons (clusters) found for a given user.
107
	 *
108
	 * @param string $userId ID of the user
109
	 * @param int $modelId ID of the model
110
	 * @param bool $onlyInvalid True if client wants count of invalid persons only,
111
	 *  false if client want count of all persons
112
	 * @return int Count of persons
113
	 */
114 15
	public function countPersons(string $userId, int $modelId, bool $onlyInvalid=false): int {
115 15
		$sub = $this->db->getQueryBuilder();
116 15
		$sub->select(new Literal('1'))
117 15
			->from('facerecog_faces', 'f')
118 15
			->innerJoin('f', 'facerecog_images' ,'i', $sub->expr()->eq('f.image', 'i.id'))
119 15
			->where($sub->expr()->eq('p.id', 'f.person'))
120 15
			->andWhere($sub->expr()->eq('i.user', $sub->createParameter('user_id')))
121 15
			->andWhere($sub->expr()->eq('i.model', $sub->createParameter('model_id')));
122
123 15
		$qb = $this->db->getQueryBuilder();
124 15
		$qb->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
125 15
			->from($this->getTableName(), 'p')
126 15
			->where('EXISTS (' . $sub->getSQL() . ')');
127
128 15
		if ($onlyInvalid) {
129
			$qb = $qb
130
				->andWhere($qb->expr()->eq('is_valid', $qb->createParameter('is_valid')))
131
				->setParameter('is_valid', false, IQueryBuilder::PARAM_BOOL);
132
		}
133
134
		$qb = $qb
135 15
			->setParameter('user_id', $userId)
136 15
			->setParameter('model_id', $modelId);
137
138 15
		$resultStatement = $qb->execute();
139 15
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
140 15
		$resultStatement->closeCursor();
141
142 15
		return (int)$data[0];
143
	}
144
145
	/**
146
	 * Based on a given fileId, takes all person that belong to that image
147
	 * and return an array with that.
148
	 *
149
	 * @param string $userId ID of the user that clusters belong to
150
	 * @param int $modelId ID of the model that clusters belgon to
151
	 * @param int $fileId ID of file image for which to searh persons.
152
	 *
153
	 * @return Person[] Array of persons on that file
154
	 */
155
	public function findFromFile(string $userId, int $modelId, int $fileId): array {
156
		$qb = $this->db->getQueryBuilder();
157
		$qb->select('p.id', 'name');
158
		$qb->from($this->getTableName(), 'p')
159
			->innerJoin('p', 'facerecog_faces' ,'f', $qb->expr()->eq('p.id', 'f.person'))
160
			->innerJoin('p', 'facerecog_images' ,'i', $qb->expr()->eq('i.id', 'f.image'))
161
			->where($qb->expr()->eq('p.user', $qb->createNamedParameter($userId)))
162
			->andWhere($qb->expr()->eq('i.model', $qb->createNamedParameter($modelId)))
163
			->andWhere($qb->expr()->eq('i.file', $qb->createNamedParameter($fileId)));
164
		return $this->findEntities($qb);
165
	}
166
167
	/**
168
	 * Based on a given image, takes all faces that belong to that image
169
	 * and invalidates all person that those faces belongs to.
170
	 *
171
	 * @param int $imageId ID of image for which to invalidate persons for
172
	 */
173 12
	public function invalidatePersons(int $imageId) {
174 12
		$sub = $this->db->getQueryBuilder();
175 12
		$tableNameWithPrefixWithoutQuotes = trim($sub->getTableName($this->getTableName()), '`');
176 12
		$sub->select(new Literal('1'));
177 12
		$sub->from('facerecog_images', 'i')
178 12
			->innerJoin('i', 'facerecog_faces' ,'f', $sub->expr()->eq('i.id', 'f.image'))
179 12
			->where($sub->expr()->eq($tableNameWithPrefixWithoutQuotes . '.id', 'f.person'))
180 12
			->andWhere($sub->expr()->eq('i.id', $sub->createParameter('image_id')));
181
182 12
		$qb = $this->db->getQueryBuilder();
183 12
		$qb->update($this->getTableName())
184 12
			->set("is_valid", $qb->createParameter('is_valid'))
185 12
			->where('EXISTS (' . $sub->getSQL() . ')')
186 12
			->setParameter('image_id', $imageId)
187 12
			->setParameter('is_valid', false, IQueryBuilder::PARAM_BOOL)
188 12
			->execute();
189 12
	}
190
191
	/**
192
	 * Based on current clusters and new clusters, do database reconciliation.
193
	 * It tries to do that in minimal number of SQL queries. Operation is atomic.
194
	 *
195
	 * Clusters are array, where keys are ID of persons, and values are indexed arrays
196
	 * with values that are ID of the faces for those persons.
197
	 *
198
	 * @param string $userId ID of the user that clusters belong to
199
	 * @param array $currentClusters Current clusters
200
	 * @param array $newClusters New clusters
201
	 */
202 14
	public function mergeClusterToDatabase(string $userId, $currentClusters, $newClusters) {
203 14
		$this->db->beginTransaction();
204 14
		$currentDateTime = new \DateTime();
205
206
		try {
207
			// Delete clusters that do not exist anymore
208 14
			foreach($currentClusters as $oldPerson => $oldFaces) {
209 11
				if (array_key_exists($oldPerson, $newClusters)) {
210 6
					continue;
211
				}
212
213
				// OK, we bumped into cluster that existed and now it does not exist.
214
				// We need to remove all references to it and to delete it.
215 7
				foreach ($oldFaces as $oldFace) {
216 7
					$this->updateFace($oldFace, null);
217
				}
218
219
				// todo: this is not very cool. What if user had associated linked user to this. And all lost?
220 7
				$qb = $this->db->getQueryBuilder();
221
				// todo: for extra safety, we should probably add here additional condition, where (user=$userId)
222
				$qb
223 7
					->delete($this->getTableName())
224 7
					->where($qb->expr()->eq('id', $qb->createNamedParameter($oldPerson)))
225 7
					->execute();
226
			}
227
228
			// Modify existing clusters
229 14
			foreach($newClusters as $newPerson=>$newFaces) {
230 12
				if (!array_key_exists($newPerson, $currentClusters)) {
231
					// This cluster didn't exist, there is nothing to modify
232
					// It will be processed during cluster adding operation
233 9
					continue;
234
				}
235
236 6
				$oldFaces = $currentClusters[$newPerson];
237 6
				if ($newFaces === $oldFaces) {
238
					// Set cluster as valid now
239 2
					$qb = $this->db->getQueryBuilder();
240
					$qb
241 2
						->update($this->getTableName())
242 2
						->set("is_valid", $qb->createParameter('is_valid'))
243 2
						->where($qb->expr()->eq('id', $qb->createNamedParameter($newPerson)))
244 2
						->setParameter('is_valid', true, IQueryBuilder::PARAM_BOOL)
245 2
						->execute();
246 2
					continue;
247
				}
248
249
				// OK, set of faces do differ. Now, we could potentially go into finer grain details
250
				// and add/remove each individual face, but this seems too detailed. Enough is to
251
				// reset all existing faces to null and to add new faces to new person. That should
252
				// take care of both faces that are removed from cluster, as well as for newly added
253
				// faces to this cluster.
254
255
				// First remove all old faces from any cluster (reset them to null)
256 5
				foreach ($oldFaces as $oldFace) {
257
					// Reset face to null only if it wasn't moved to other cluster!
258
					// (if face is just moved to other cluster, do not reset to null, as some other
259
					// pass for some other cluster will eventually update it to proper cluster)
260 5
					if ($this->isFaceInClusters($oldFace, $newClusters) === false) {
261 5
						$this->updateFace($oldFace, null);
262
					}
263
				}
264
265
				// Then set all new faces to belong to this cluster
266 5
				foreach ($newFaces as $newFace) {
267 5
					$this->updateFace($newFace, $newPerson);
268
				}
269
270
				// Set cluster as valid now
271 5
				$qb = $this->db->getQueryBuilder();
272
				$qb
273 5
					->update($this->getTableName())
274 5
					->set("is_valid", $qb->createParameter('is_valid'))
275 5
					->where($qb->expr()->eq('id', $qb->createNamedParameter($newPerson)))
276 5
					->setParameter('is_valid', true, IQueryBuilder::PARAM_BOOL)
277 5
					->execute();
278
			}
279
280
			// Add new clusters
281 14
			foreach($newClusters as $newPerson=>$newFaces) {
282 12
				if (array_key_exists($newPerson, $currentClusters)) {
283
					// This cluster already existed, nothing to add
284
					// It was already processed during modify cluster operation
285 6
					continue;
286
				}
287
288
				// Create new cluster and add all faces to it
289 9
				$qb = $this->db->getQueryBuilder();
290
				$qb
291 9
					->insert($this->getTableName())
292 9
					->values([
293 9
						'user' => $qb->createNamedParameter($userId),
294 9
						'name' => $qb->createNamedParameter(sprintf("New person %d", $newPerson)),
295 9
						'is_valid' => $qb->createNamedParameter(true),
296 9
						'last_generation_time' => $qb->createNamedParameter($currentDateTime, IQueryBuilder::PARAM_DATE),
297 9
						'linked_user' => $qb->createNamedParameter(null)])
298 9
					->execute();
299 9
				$insertedPersonId = $this->db->lastInsertId($this->getTableName());
300 9
				foreach ($newFaces as $newFace) {
301 9
					$this->updateFace($newFace, $insertedPersonId);
302
				}
303
			}
304
305 14
			$this->db->commit();
306
		} catch (\Exception $e) {
307
			$this->db->rollBack();
308
			throw $e;
309
		}
310 14
	}
311
312
	/**
313
	 * Deletes all persons from that user.
314
	 *
315
	 * @param string $userId User to drop persons from a table.
316
	 */
317 28
	public function deleteUserPersons(string $userId) {
318 28
		$qb = $this->db->getQueryBuilder();
319 28
		$qb->delete($this->getTableName())
320 28
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
321 28
			->execute();
322 28
	}
323
324
	/**
325
	 * Deletes person if it is empty (have no faces associated to it)
326
	 *
327
	 * @param int $personId Person to check if it should be deleted
328
	 */
329
	public function removeIfEmpty(int $personId) {
330
		$sub = $this->db->getQueryBuilder();
331
		$sub->select(new Literal('1'));
332
		$sub->from('facerecog_faces', 'f')
333
			->where($sub->expr()->eq('f.person', $sub->createParameter('person')));
334
335
		$qb = $this->db->getQueryBuilder();
336
		$qb->delete($this->getTableName())
337
			->where($qb->expr()->eq('id', $qb->createParameter('person')))
338
			->andWhere('NOT EXISTS (' . $sub->getSQL() . ')')
339
			->setParameter('person', $personId)
340
			->execute();
341
	}
342
343
	/**
344
	 * Deletes all persons that have no faces associated to them
345
	 *
346
	 * @param string $userId ID of user for which we are deleting orphaned persons
347
	 */
348 1
	public function deleteOrphaned(string $userId): int {
349 1
		$sub = $this->db->getQueryBuilder();
350 1
		$sub->select(new Literal('1'));
351 1
		$sub->from('facerecog_faces', 'f')
352 1
			->where($sub->expr()->eq('f.person', 'p.id'));
353
354 1
		$qb = $this->db->getQueryBuilder();
355 1
		$qb->select('p.id')
356 1
			->from($this->getTableName(), 'p')
357 1
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
358 1
			->andWhere('NOT EXISTS (' . $sub->getSQL() . ')')
359 1
			->setParameter('user', $userId);
360 1
		$orphanedPersons = $this->findEntities($qb);
361
362 1
		$orphaned = 0;
363 1
		foreach ($orphanedPersons as $person) {
364
			$qb = $this->db->getQueryBuilder();
365
			$orphaned += $qb->delete($this->getTableName())
366
				->where($qb->expr()->eq('id', $qb->createNamedParameter($person->id)))
367
				->execute();
368
		}
369 1
		return $orphaned;
370
	}
371
372
	/**
373
	 * Updates one face with $faceId to database to person ID $personId.
374
	 *
375
	 * @param int $faceId ID of the face
376
	 * @param int|null $personId ID of the person
377
	 */
378 12
	private function updateFace(int $faceId, $personId) {
379 12
		$qb = $this->db->getQueryBuilder();
380 12
		$qb->update('facerecog_faces')
381 12
			->set("person", $qb->createNamedParameter($personId))
382 12
			->where($qb->expr()->eq('id', $qb->createNamedParameter($faceId)))
383 12
			->execute();
384 12
	}
385
386
	/**
387
	 * Checks if face with a given ID is in any cluster.
388
	 *
389
	 * @param int $faceId ID of the face to check
390
	 * @param array $cluster All clusters to check into
391
	 *
392
	 * @return bool True if face is found in any cluster, false otherwise.
393
	 */
394 5
	private function isFaceInClusters(int $faceId, array $clusters): bool {
395 5
		foreach ($clusters as $_=>$faces) {
396 5
			if (in_array($faceId, $faces)) {
397 5
				return true;
398
			}
399
		}
400 1
		return false;
401
	}
402
}
403