Passed
Push — restore-progress ( c8e1da...d00b2a )
by Matias
03:19
created

ImageMapper::resetImage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 10
cp 0
crap 2
rs 9.9666
c 0
b 0
f 0
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 OCP\IDBConnection;
27
use OCP\IUser;
28
29
use OCP\AppFramework\Db\Mapper;
30
use OCP\AppFramework\Db\DoesNotExistException;
31
use OCP\DB\QueryBuilder\IQueryBuilder;
32
33
class ImageMapper extends Mapper {
34
35 4
	public function __construct(IDBConnection $db) {
36 4
		parent::__construct($db, 'face_recognition_images', '\OCA\FaceRecognition\Db\Image');
37 4
	}
38
39
	public function find (string $userId, int $imageId): Image {
40
		$qb = $this->db->getQueryBuilder();
41
		$qb->select('id', 'file')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'file'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
		$qb->/** @scrutinizer ignore-call */ 
42
       select('id', 'file')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
42
			->from('face_recognition_images', 'i')
43
			->where($qb->expr()->eq('user', $qb->createParameter('user_id')))
44
			->andWhere($qb->expr()->eq('id', $qb->createParameter('image_id')));
45
		$params = array();
46
		$params['user_id'] = $userId;
47
		$params['image_id'] = $imageId;
48
		$image = $this->findEntity($qb->getSQL(), $params);
49
		return $image;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $image returns the type OCP\AppFramework\Db\Entity which includes types incompatible with the type-hinted return OCA\FaceRecognition\Db\Image.
Loading history...
50
	}
51
52
	public function findFromFile(string $userId, int $fileId) {
53
		$qb = $this->db->getQueryBuilder();
54
		$qb->select('id', 'is_processed', 'error')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'is_processed'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
		$qb->/** @scrutinizer ignore-call */ 
55
       select('id', 'is_processed', 'error')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
55
		   ->from('face_recognition_images', 'i')
56
		   ->where($qb->expr()->eq('user', $qb->createParameter('user')))
57
		   ->andWhere($qb->expr()->eq('file', $qb->createParameter('file_id')));
58
59
		$params = array();
60
		$params['user'] = $userId;
61
		$params['file_id'] = $fileId;
62
63
		try {
64
			return $this->findEntity($qb->getSQL(), $params);
65
		} catch (DoesNotExistException $e) {
66
			return null;
67
		}
68
	}
69
70 1
	public function imageExists(Image $image) {
71 1
		$qb = $this->db->getQueryBuilder();
72
		$query = $qb
73 1
			->select(['id'])
74 1
			->from('face_recognition_images')
75 1
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
76 1
			->andWhere($qb->expr()->eq('file', $qb->createParameter('file')))
77 1
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
78 1
			->setParameter('user', $image->getUser())
79 1
			->setParameter('file', $image->getFile())
80 1
			->setParameter('model', $image->getModel());
81 1
		$resultStatement = $query->execute();
82 1
		$row = $resultStatement->fetch();
83 1
		$resultStatement->closeCursor();
84 1
		return $row ? (int)$row['id'] : null;
85
	}
86
87
	public function countImages(int $model): int {
88
		$qb = $this->db->getQueryBuilder();
89
		$query = $qb
90
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
91
			->from('face_recognition_images')
92
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
93
			->setParameter('model', $model);
94
		$resultStatement = $query->execute();
95
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
96
		$resultStatement->closeCursor();
97
98
		return (int)$data[0];
99
	}
100
101
	public function countProcessedImages(int $model): int {
102
		$qb = $this->db->getQueryBuilder();
103
		$query = $qb
104
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
105
			->from('face_recognition_images')
106
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
107
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
108
			->setParameter('model', $model)
109
			->setParameter('is_processed', True);
110
		$resultStatement = $query->execute();
111
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
112
		$resultStatement->closeCursor();
113
114
		return (int)$data[0];
115
	}
116
117
	public function avgProcessingDuration(int $model): int {
118
		$qb = $this->db->getQueryBuilder();
119
		$query = $qb
120
			->select($qb->createFunction('AVG(' . $qb->getColumnName('processing_duration') . ')'))
121
			->from('face_recognition_images')
122
			->where($qb->expr()->eq('model', $qb->createParameter('model')))
123
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
124
			->setParameter('model', $model)
125
			->setParameter('is_processed', True);
126
		$resultStatement = $query->execute();
127
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
128
		$resultStatement->closeCursor();
129
130
		return (int)$data[0];
131
	}
132
133
	public function countUserImages(string $userId, $model): int {
134
		$qb = $this->db->getQueryBuilder();
135
		$query = $qb
136
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
137
			->from('face_recognition_images')
138
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
139
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
140
			->setParameter('user', $userId)
141
			->setParameter('model', $model);
142
		$resultStatement = $query->execute();
143
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
144
		$resultStatement->closeCursor();
145
146
		return (int)$data[0];
147
	}
148
149
	public function countUserProcessedImages(string $userId, $model): int {
150
		$qb = $this->db->getQueryBuilder();
151
		$query = $qb
152
			->select($qb->createFunction('COUNT(' . $qb->getColumnName('id') . ')'))
153
			->from('face_recognition_images')
154
			->where($qb->expr()->eq('user', $qb->createParameter('user')))
155
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
156
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
157
			->setParameter('user', $userId)
158
			->setParameter('model', $model)
159
			->setParameter('is_processed', True);
160
		$resultStatement = $query->execute();
161
		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
162
		$resultStatement->closeCursor();
163
164
		return (int)$data[0];
165
	}
166
167
	/**
168
	 * @param IUser|null $user User for which to get images for. If not given, all images from instance are returned.
169
	 */
170 3
	public function findImagesWithoutFaces(IUser $user = null) {
171 3
		$qb = $this->db->getQueryBuilder();
172 3
		$params = array();
173
174
		$query = $qb
175 3
			->select(['id', 'user', 'file', 'model'])
176 3
			->from('face_recognition_images')
177 3
			->where($qb->expr()->eq('is_processed',  $qb->createParameter('is_processed')));
178 3
			$params['is_processed'] = False;
179 3
		if (!is_null($user)) {
180 3
			$query->andWhere($qb->expr()->eq('user', $qb->createParameter('user')));
181 3
			$params['user'] = $user->getUID();
182
		}
183
184 3
		$images = $this->findEntities($qb->getSQL(), $params);
185 3
		return $images;
186
	}
187
188
189
	public function findImagesFromPerson(string $userId, string $name, int $model): array {
190
		$qb = $this->db->getQueryBuilder();
191
		$qb->select('i.id', 'i.file')
0 ignored issues
show
Unused Code introduced by
The call to OCP\DB\QueryBuilder\IQueryBuilder::select() has too many arguments starting with 'i.file'. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

191
		$qb->/** @scrutinizer ignore-call */ 
192
       select('i.id', 'i.file')

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
192
			->from('face_recognition_images', 'i')
193
			->innerJoin('i', 'face_recognition_faces', 'f', $qb->expr()->eq('f.image', 'i.id'))
194
			->innerJoin('i', 'face_recognition_persons', 'p', $qb->expr()->eq('f.person', 'p.id'))
195
			->where($qb->expr()->eq('p.user', $qb->createParameter('user')))
196
			->andWhere($qb->expr()->eq('model', $qb->createParameter('model')))
197
			->andWhere($qb->expr()->eq('is_processed', $qb->createParameter('is_processed')))
198
			->andWhere($qb->expr()->like($qb->func()->lower('p.name'), $qb->createParameter('query')));
0 ignored issues
show
Bug introduced by
The method lower() does not exist on OCP\DB\QueryBuilder\IFunctionBuilder. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

198
			->andWhere($qb->expr()->like($qb->func()->/** @scrutinizer ignore-call */ lower('p.name'), $qb->createParameter('query')));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
199
200
		$params = array();
201
		$params['user'] = $userId;
202
		$params['model'] = $model;
203
		$params['is_processed'] = True;
204
		$params['query'] = '%' . $this->db->escapeLikeParameter(strtolower($name)) . '%';
205
		$images = $this->findEntities($qb->getSQL(), $params);
206
		return $images;
207
	}
208
209
	/**
210
	 * Writes to DB that image has been processed. Previously found faces are deleted and new ones are inserted.
211
	 * If there is exception, its stack trace is also updated.
212
	 *
213
	 * @param Image $image Image to be updated
214
	 * @param Face[] $faces Faces to insert
215
	 * @param int $duration Processing time, in milliseconds
216
	 * @param \Exception|null $e Any exception that happened during image processing
217
	 */
218
	public function imageProcessed(Image $image, array $faces, int $duration, \Exception $e = null) {
219
		$this->db->beginTransaction();
220
		try {
221
			// Update image itself
222
			//
223
			$error = null;
224
			if ($e !== null) {
225
				$error = substr($e->getMessage(), 0, 1024);
226
			}
227
228
			$qb = $this->db->getQueryBuilder();
229
			$qb->update('face_recognition_images')
230
				->set("is_processed", $qb->createNamedParameter(true, IQueryBuilder::PARAM_BOOL))
231
				->set("error", $qb->createNamedParameter($error))
232
				->set("last_processed_time", $qb->createNamedParameter(new \DateTime(), IQueryBuilder::PARAM_DATE))
233
				->set("processing_duration", $qb->createNamedParameter($duration))
234
				->where($qb->expr()->eq('id', $qb->createNamedParameter($image->id)))
235
				->execute();
236
237
			// Delete all previous faces
238
			//
239
			$qb = $this->db->getQueryBuilder();
240
			$qb->delete('face_recognition_faces')
241
				->where($qb->expr()->eq('image', $qb->createNamedParameter($image->id)))
242
				->execute();
243
244
			// Insert all faces
245
			//
246
			foreach ($faces as $face) {
247
				// Simple INSERT will close cursor and we want to be in transaction, so use hard way
248
				// todo: should we move this to FaceMapper (don't forget to hand over connection though)
249
				$qb = $this->db->getQueryBuilder();
250
				$qb->insert('face_recognition_faces')
251
					->values([
252
						'image' => $qb->createNamedParameter($image->id),
253
						'person' => $qb->createNamedParameter(null),
254
						'left' => $qb->createNamedParameter($face->left),
255
						'right' => $qb->createNamedParameter($face->right),
256
						'top' => $qb->createNamedParameter($face->top),
257
						'bottom' => $qb->createNamedParameter($face->bottom),
258
						'descriptor' => $qb->createNamedParameter(json_encode($face->descriptor)),
259
						'creation_time' => $qb->createNamedParameter($face->creationTime, IQueryBuilder::PARAM_DATE),
260
					])
261
					->execute();
262
			}
263
264
			$this->db->commit();
265
		} catch (\Exception $e) {
266
			$this->db->rollBack();
267
			throw $e;
268
		}
269
	}
270
271
	/**
272
	 * Resets image by deleting all associated faces and prepares it to be processed again
273
	 *
274
	 * @param Image $image Image to reset
275
	 */
276
	public function resetImage(Image $image) {
277
		$qb = $this->db->getQueryBuilder();
278
		$qb->update($this->getTableName())
279
			->set("is_processed", $qb->createNamedParameter(false, IQueryBuilder::PARAM_BOOL))
280
			->set("error", $qb->createNamedParameter(null))
281
			->set("last_processed_time", $qb->createNamedParameter(null))
282
			->where($qb->expr()->eq('user', $qb->createNamedParameter($image->getUser())))
283
			->andWhere($qb->expr()->eq('file', $qb->createNamedParameter($image->getFile())))
284
			->andWhere($qb->expr()->eq('model', $qb->createNamedParameter($image->getModel())))
285
			->execute();
286
	}
287
}
288