|
1
|
|
|
<?php |
|
2
|
|
|
namespace OCA\FaceRecognition\Db; |
|
3
|
|
|
|
|
4
|
|
|
use OCP\IDBConnection; |
|
5
|
|
|
use OCP\AppFramework\Db\Mapper; |
|
6
|
|
|
use OCP\AppFramework\Db\DoesNotExistException; |
|
7
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
8
|
|
|
|
|
9
|
|
|
class FaceNewMapper extends Mapper { |
|
10
|
|
|
public function __construct(IDBConnection $db) { |
|
11
|
|
|
parent::__construct($db, 'face_recognition_faces', '\OCA\FaceRecognition\Db\FaceNew'); |
|
12
|
|
|
} |
|
13
|
|
|
|
|
14
|
|
|
public function countFaces(string $userId, $model): int { |
|
15
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
16
|
|
|
$query = $qb |
|
17
|
|
|
->select($qb->createFunction('COUNT(' . $qb->getColumnName('f.id') . ')')) |
|
18
|
|
|
->from('face_recognition_faces', 'f') |
|
19
|
|
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
20
|
|
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
21
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
22
|
|
|
->setParameter('user', $userId) |
|
23
|
|
|
->setParameter('model', $model); |
|
24
|
|
|
$resultStatement = $query->execute(); |
|
25
|
|
|
$data = $resultStatement->fetch(\PDO::FETCH_NUM); |
|
26
|
|
|
$resultStatement->closeCursor(); |
|
27
|
|
|
|
|
28
|
|
|
return (int)$data[0]; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public function getFaces(string $userId, $model): array { |
|
32
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
33
|
|
|
$query = $qb |
|
|
|
|
|
|
34
|
|
|
->select('f.id', 'f.person', 'f.descriptor') |
|
35
|
|
|
->from('face_recognition_faces', 'f') |
|
36
|
|
|
->innerJoin('f', 'face_recognition_images' ,'i', $qb->expr()->eq('f.image', 'i.id')) |
|
37
|
|
|
->where($qb->expr()->eq('user', $qb->createParameter('user'))) |
|
38
|
|
|
->andWhere($qb->expr()->eq('model', $qb->createParameter('model'))) |
|
39
|
|
|
->setParameter('user', $userId) |
|
40
|
|
|
->setParameter('model', $model); |
|
41
|
|
|
$faces = $this->frFindEntities($qb); |
|
42
|
|
|
return $faces; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param int $imageId Image for which to delete faces for |
|
47
|
|
|
*/ |
|
48
|
|
|
public function removeFaces(int $imageId) { |
|
49
|
|
|
$qb = $this->db->getQueryBuilder(); |
|
50
|
|
|
$qb->delete($this->getTableName()) |
|
51
|
|
|
->where($qb->expr()->eq('image', $qb->createNamedParameter($imageId))) |
|
52
|
|
|
->execute(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Runs a sql query and returns an array of entities |
|
57
|
|
|
* |
|
58
|
|
|
* todo: stolen from QBMapper. However, this class is in use from 14.0 only. |
|
59
|
|
|
* If we use it, we are "locked" ourselves to versions >= 14.0 |
|
60
|
|
|
* |
|
61
|
|
|
* @param IQueryBuilder $query |
|
62
|
|
|
* @return Entity[] all fetched entities |
|
63
|
|
|
* @since 14.0.0 |
|
64
|
|
|
*/ |
|
65
|
|
|
protected function frFindEntities(IQueryBuilder $query): array { |
|
66
|
|
|
$cursor = $query->execute(); |
|
67
|
|
|
|
|
68
|
|
|
$entities = []; |
|
69
|
|
|
|
|
70
|
|
|
while($row = $cursor->fetch()){ |
|
71
|
|
|
$entities[] = $this->mapRowToEntity($row); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
$cursor->closeCursor(); |
|
75
|
|
|
|
|
76
|
|
|
return $entities; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.