Completed
Push — master ( b2b3ad...48dda4 )
by Matias
16s queued 12s
created

PersonMapper::deleteUserModel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 9
ccs 0
cts 7
cp 0
crap 6
rs 10
c 0
b 0
f 0
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 image, takes all faces that belong to that image
147
	 * and invalidates all person that those faces belongs to.
148
	 *
149
	 * @param int $imageId ID of image for which to invalidate persons for
150
	 */
151 12
	public function invalidatePersons(int $imageId) {
152 12
		$sub = $this->db->getQueryBuilder();
153 12
		$tableNameWithPrefixWithoutQuotes = trim($sub->getTableName($this->getTableName()), '`');
154 12
		$sub->select(new Literal('1'));
155 12
		$sub->from('facerecog_images', 'i')
156 12
			->innerJoin('i', 'facerecog_faces' ,'f', $sub->expr()->eq('i.id', 'f.image'))
157 12
			->where($sub->expr()->eq($tableNameWithPrefixWithoutQuotes . '.id', 'f.person'))
158 12
			->andWhere($sub->expr()->eq('i.id', $sub->createParameter('image_id')));
159
160 12
		$qb = $this->db->getQueryBuilder();
161 12
		$qb->update($this->getTableName())
162 12
			->set("is_valid", $qb->createParameter('is_valid'))
163 12
			->where('EXISTS (' . $sub->getSQL() . ')')
164 12
			->setParameter('image_id', $imageId)
165 12
			->setParameter('is_valid', false, IQueryBuilder::PARAM_BOOL)
166 12
			->execute();
167 12
	}
168
169
	/**
170
	 * Based on current clusters and new clusters, do database reconciliation.
171
	 * It tries to do that in minimal number of SQL queries. Operation is atomic.
172
	 *
173
	 * Clusters are array, where keys are ID of persons, and values are indexed arrays
174
	 * with values that are ID of the faces for those persons.
175
	 *
176
	 * @param string $userId ID of the user that clusters belong to
177
	 * @param array $currentClusters Current clusters
178
	 * @param array $newClusters New clusters
179
	 */
180 14
	public function mergeClusterToDatabase(string $userId, $currentClusters, $newClusters) {
181 14
		$this->db->beginTransaction();
182 14
		$currentDateTime = new \DateTime();
183
184
		try {
185
			// Delete clusters that do not exist anymore
186 14
			foreach($currentClusters as $oldPerson => $oldFaces) {
187 11
				if (array_key_exists($oldPerson, $newClusters)) {
188 6
					continue;
189
				}
190
191
				// OK, we bumped into cluster that existed and now it does not exist.
192
				// We need to remove all references to it and to delete it.
193 7
				foreach ($oldFaces as $oldFace) {
194 7
					$this->updateFace($oldFace, null);
195
				}
196
197
				// todo: this is not very cool. What if user had associated linked user to this. And all lost?
198 7
				$qb = $this->db->getQueryBuilder();
199
				// todo: for extra safety, we should probably add here additional condition, where (user=$userId)
200
				$qb
201 7
					->delete($this->getTableName())
202 7
					->where($qb->expr()->eq('id', $qb->createNamedParameter($oldPerson)))
203 7
					->execute();
204
			}
205
206
			// Modify existing clusters
207 14
			foreach($newClusters as $newPerson=>$newFaces) {
208 12
				if (!array_key_exists($newPerson, $currentClusters)) {
209
					// This cluster didn't exist, there is nothing to modify
210
					// It will be processed during cluster adding operation
211 9
					continue;
212
				}
213
214 6
				$oldFaces = $currentClusters[$newPerson];
215 6
				if ($newFaces === $oldFaces) {
216
					// Set cluster as valid now
217 2
					$qb = $this->db->getQueryBuilder();
218
					$qb
219 2
						->update($this->getTableName())
220 2
						->set("is_valid", $qb->createParameter('is_valid'))
221 2
						->where($qb->expr()->eq('id', $qb->createNamedParameter($newPerson)))
222 2
						->setParameter('is_valid', true, IQueryBuilder::PARAM_BOOL)
223 2
						->execute();
224 2
					continue;
225
				}
226
227
				// OK, set of faces do differ. Now, we could potentially go into finer grain details
228
				// and add/remove each individual face, but this seems too detailed. Enough is to
229
				// reset all existing faces to null and to add new faces to new person. That should
230
				// take care of both faces that are removed from cluster, as well as for newly added
231
				// faces to this cluster.
232
233
				// First remove all old faces from any cluster (reset them to null)
234 5
				foreach ($oldFaces as $oldFace) {
235
					// Reset face to null only if it wasn't moved to other cluster!
236
					// (if face is just moved to other cluster, do not reset to null, as some other
237
					// pass for some other cluster will eventually update it to proper cluster)
238 5
					if ($this->isFaceInClusters($oldFace, $newClusters) === false) {
239 1
						$this->updateFace($oldFace, null);
240
					}
241
				}
242
243
				// Then set all new faces to belong to this cluster
244 5
				foreach ($newFaces as $newFace) {
245 5
					$this->updateFace($newFace, $newPerson);
246
				}
247
248
				// Set cluster as valid now
249 5
				$qb = $this->db->getQueryBuilder();
250
				$qb
251 5
					->update($this->getTableName())
252 5
					->set("is_valid", $qb->createParameter('is_valid'))
253 5
					->where($qb->expr()->eq('id', $qb->createNamedParameter($newPerson)))
254 5
					->setParameter('is_valid', true, IQueryBuilder::PARAM_BOOL)
255 5
					->execute();
256
			}
257
258
			// Add new clusters
259 14
			foreach($newClusters as $newPerson=>$newFaces) {
260 12
				if (array_key_exists($newPerson, $currentClusters)) {
261
					// This cluster already existed, nothing to add
262
					// It was already processed during modify cluster operation
263 6
					continue;
264
				}
265
266
				// Create new cluster and add all faces to it
267 9
				$qb = $this->db->getQueryBuilder();
268
				$qb
269 9
					->insert($this->getTableName())
270 9
					->values([
271 9
						'user' => $qb->createNamedParameter($userId),
272 9
						'name' => $qb->createNamedParameter(sprintf("New person %d", $newPerson)),
273 9
						'is_valid' => $qb->createNamedParameter(true),
274 9
						'last_generation_time' => $qb->createNamedParameter($currentDateTime, IQueryBuilder::PARAM_DATE),
275 9
						'linked_user' => $qb->createNamedParameter(null)])
276 9
					->execute();
277 9
				$insertedPersonId = $this->db->lastInsertId($this->getTableName());
278 9
				foreach ($newFaces as $newFace) {
279 9
					$this->updateFace($newFace, $insertedPersonId);
280
				}
281
			}
282
283 14
			$this->db->commit();
284
		} catch (\Exception $e) {
285
			$this->db->rollBack();
286
			throw $e;
287
		}
288 14
	}
289
290
	/**
291
	 * Deletes all persons from that user.
292
	 *
293
	 * @param string $userId User to drop persons from a table.
294
	 */
295 28
	public function deleteUserPersons(string $userId) {
296 28
		$qb = $this->db->getQueryBuilder();
297 28
		$qb->delete($this->getTableName())
298 28
			->where($qb->expr()->eq('user', $qb->createNamedParameter($userId)))
299 28
			->execute();
300 28
	}
301
302
	/**
303
	 * Deletes all persons from that user and model
304
	 *
305
	 * @param string $userId ID of user for drop from table
306
	 * @param string $modelId model for drop from table
307
	 */
308
	public function deleteUserModel(string $userId, int $modelId) {
309
		//TODO: Make it atomic
310
		$qb = $this->db->getQueryBuilder();
311
		$qb->delete($this->getTableName())
312
			->where($qb->expr()->eq('id', $qb->createParameter('person')));
313
314
		$persons = $this->findAll($userId, $modelId);
315
		foreach ($persons as $person) {
316
			$qb->setParameter('person', $person->getId())->execute();
317
		}
318
	}
319
320
	/**
321
	 * Deletes person if it is empty (have no faces associated to it)
322
	 *
323
	 * @param int $personId Person to check if it should be deleted
324
	 */
325
	public function removeIfEmpty(int $personId) {
326
		$sub = $this->db->getQueryBuilder();
327
		$sub->select(new Literal('1'));
328
		$sub->from('facerecog_faces', 'f')
329
			->where($sub->expr()->eq('f.person', $sub->createParameter('person')));
330
331
		$qb = $this->db->getQueryBuilder();
332
		$qb->delete($this->getTableName())
333
			->where($qb->expr()->eq('id', $qb->createParameter('person')))
334
			->andWhere('NOT EXISTS (' . $sub->getSQL() . ')')
335
			->setParameter('person', $personId)
336
			->execute();
337
	}
338
339
	/**
340
	 * Deletes all persons that have no faces associated to them
341
	 *
342
	 * @param string $userId ID of user for which we are deleting orphaned persons
343
	 */
344 1
	public function deleteOrphaned(string $userId): int {
345 1
		$sub = $this->db->getQueryBuilder();
346 1
		$sub->select(new Literal('1'));
347 1
		$sub->from('facerecog_faces', 'f')
348 1
			->where($sub->expr()->eq('f.person', 'p.id'));
349
350 1
		$qb = $this->db->getQueryBuilder();
351 1
		$qb->select('p.id')
352 1
			->from($this->getTableName(), 'p')
353 1
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
354 1
			->andWhere('NOT EXISTS (' . $sub->getSQL() . ')')
355 1
			->setParameter('user', $userId);
356 1
		$orphanedPersons = $this->findEntities($qb);
357
358 1
		$orphaned = 0;
359 1
		foreach ($orphanedPersons as $person) {
360
			$qb = $this->db->getQueryBuilder();
361
			$orphaned += $qb->delete($this->getTableName())
362
				->where($qb->expr()->eq('id', $qb->createNamedParameter($person->id)))
363
				->execute();
364
		}
365 1
		return $orphaned;
366
	}
367
368
	/**
369
	 * Updates one face with $faceId to database to person ID $personId.
370
	 *
371
	 * @param int $faceId ID of the face
372
	 * @param int|null $personId ID of the person
373
	 */
374 12
	private function updateFace(int $faceId, $personId) {
375 12
		$qb = $this->db->getQueryBuilder();
376 12
		$qb->update('facerecog_faces')
377 12
			->set("person", $qb->createNamedParameter($personId))
378 12
			->where($qb->expr()->eq('id', $qb->createNamedParameter($faceId)))
379 12
			->execute();
380 12
	}
381
382
	/**
383
	 * Checks if face with a given ID is in any cluster.
384
	 *
385
	 * @param int $faceId ID of the face to check
386
	 * @param array $cluster All clusters to check into
387
	 *
388
	 * @return bool True if face is found in any cluster, false otherwise.
389
	 */
390 5
	private function isFaceInClusters(int $faceId, array $clusters): bool {
391 5
		foreach ($clusters as $_=>$faces) {
392 5
			if (in_array($faceId, $faces)) {
393 5
				return true;
394
			}
395
		}
396 1
		return false;
397
	}
398
}
399