Passed
Push — settings-service ( a793f8...527322 )
by Matias
04:01
created

PersonMapper::mergeClusterToDatabase()   C

Complexity

Conditions 14
Paths 216

Size

Total Lines 107
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 51
CRAP Score 14.0336

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 14
eloc 58
c 3
b 0
f 0
nc 216
nop 3
dl 0
loc 107
ccs 51
cts 54
cp 0.9444
crap 14.0336
rs 5.2333

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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