Completed
Push — master ( 060b04...657701 )
by
unknown
04:35
created

FeedMapper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 3.9%
Metric Value
wmc 10
lcom 0
cbo 1
dl 0
loc 146
ccs 3
cts 77
cp 0.039
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A find() 0 18 1
A findAllFromUser() 0 23 1
A findAll() 0 21 1
A findByUrlHash() 0 18 1
A delete() 0 9 1
A getToDelete() 0 19 3
A deleteUser() 0 4 1
1
<?php
2
/**
3
 * ownCloud - News
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Alessandro Cosentino <[email protected]>
9
 * @author Bernhard Posselt <[email protected]>
10
 * @copyright Alessandro Cosentino 2012
11
 * @copyright Bernhard Posselt 2012, 2014
12
 */
13
14
namespace OCA\News\Db;
15
16
use OCP\IDBConnection;
17
use OCP\AppFramework\Db\Entity;
18
19
20
class FeedMapper extends NewsMapper {
21
22
23 16
    public function __construct(IDBConnection $db) {
24 16
        parent::__construct($db, 'news_feeds', Feed::class);
25 16
    }
26
27
28
    public function find($id, $userId){
29
        $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' .
30
            'FROM `*PREFIX*news_feeds` `feeds` ' .
31
            'LEFT JOIN `*PREFIX*news_items` `items` ' .
32
                'ON `feeds`.`id` = `items`.`feed_id` ' .
33
                // WARNING: this is a desperate attempt at making this query
34
                // work because prepared statements dont work. This is a
35
                // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
36
                // think twice when changing this
37
                'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' .
38
                StatusFlag::UNREAD . ' ' .
39
            'WHERE `feeds`.`id` = ? ' .
40
                'AND `feeds`.`user_id` = ? ' .
41
            'GROUP BY `feeds`.`id`';
42
        $params = [$id, $userId];
43
44
        return $this->findEntity($sql, $params);
45
    }
46
47
48
    public function findAllFromUser($userId){
49
        $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' .
50
            'FROM `*PREFIX*news_feeds` `feeds` ' .
51
            'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '.
52
                'ON `feeds`.`folder_id` = `folders`.`id` ' .
53
            'LEFT JOIN `*PREFIX*news_items` `items` ' .
54
                'ON `feeds`.`id` = `items`.`feed_id` ' .
55
                // WARNING: this is a desperate attempt at making this query
56
                // work because prepared statements dont work. This is a
57
                // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
58
                // think twice when changing this
59
                'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' .
60
                StatusFlag::UNREAD . ' ' .
61
            'WHERE `feeds`.`user_id` = ? ' .
62
            'AND (`feeds`.`folder_id` = 0 ' .
63
                'OR `folders`.`deleted_at` = 0' .
64
            ')' .
65
            'AND `feeds`.`deleted_at` = 0 ' .
66
            'GROUP BY `feeds`.`id`';
67
        $params = [$userId];
68
69
        return $this->findEntities($sql, $params);
70
    }
71
72
73
    public function findAll(){
74
        $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' .
75
            'FROM `*PREFIX*news_feeds` `feeds` ' .
76
            'LEFT OUTER JOIN `*PREFIX*news_folders` `folders` '.
77
                'ON `feeds`.`folder_id` = `folders`.`id` ' .
78
            'LEFT JOIN `*PREFIX*news_items` `items` ' .
79
                'ON `feeds`.`id` = `items`.`feed_id` ' .
80
                // WARNING: this is a desperate attempt at making this query
81
                // work because prepared statements dont work. This is a
82
                // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
83
                // think twice when changing this
84
                'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' .
85
                StatusFlag::UNREAD . ' ' .
86
            'WHERE (`feeds`.`folder_id` = 0 ' .
87
                'OR `folders`.`deleted_at` = 0' .
88
            ')' .
89
            'AND `feeds`.`deleted_at` = 0 ' .
90
            'GROUP BY `feeds`.`id`';
91
92
        return $this->findEntities($sql);
93
    }
94
95
96
    public function findByUrlHash($hash, $userId){
97
        $sql = 'SELECT `feeds`.*, COUNT(`items`.`id`) AS `unread_count` ' .
98
            'FROM `*PREFIX*news_feeds` `feeds` ' .
99
            'LEFT JOIN `*PREFIX*news_items` `items` ' .
100
                'ON `feeds`.`id` = `items`.`feed_id` ' .
101
                // WARNING: this is a desperate attempt at making this query
102
                // work because prepared statements dont work. This is a
103
                // POSSIBLE SQL INJECTION RISK WHEN MODIFIED WITHOUT THOUGHT.
104
                // think twice when changing this
105
                'AND (`items`.`status` & ' . StatusFlag::UNREAD . ') = ' .
106
                StatusFlag::UNREAD . ' ' .
107
            'WHERE `feeds`.`url_hash` = ? ' .
108
                'AND `feeds`.`user_id` = ? ' .
109
            'GROUP BY `feeds`.`id`';
110
        $params = [$hash, $userId];
111
112
        return $this->findEntity($sql, $params);
113
    }
114
115
116
    public function delete(Entity $entity){
117
        parent::delete($entity);
118
119
        // someone please slap me for doing this manually :P
120
        // we needz CASCADE + FKs please
121
        $sql = 'DELETE FROM `*PREFIX*news_items` WHERE `feed_id` = ?';
122
        $params = [$entity->getId()];
123
        $this->execute($sql, $params);
124
    }
125
126
127
    /**
128
     * @param int $deleteOlderThan if given gets all entries with a delete date
129
     * older than that timestamp
130
     * @param string $userId if given returns only entries from the given user
131
     * @return array with the database rows
132
     */
133
    public function getToDelete($deleteOlderThan=null, $userId=null) {
134
        $sql = 'SELECT * FROM `*PREFIX*news_feeds` ' .
135
            'WHERE `deleted_at` > 0 ';
136
        $params = [];
137
138
        // sometimes we want to delete all entries
139
        if ($deleteOlderThan !== null) {
140
            $sql .= 'AND `deleted_at` < ? ';
141
            $params[] = $deleteOlderThan;
142
        }
143
144
        // we need to sometimes only delete feeds of a user
145
        if($userId !== null) {
146
            $sql .= 'AND `user_id` = ?';
147
            $params[] = $userId;
148
        }
149
150
        return $this->findEntities($sql, $params);
151
    }
152
153
154
    /**
155
     * Deletes all feeds of a user, delete items first since the user_id
156
     * is not defined in there
157
     * @param string $userId the name of the user
158
     */
159
    public function deleteUser($userId) {
160
        $sql = 'DELETE FROM `*PREFIX*news_feeds` WHERE `user_id` = ?';
161
        $this->execute($sql, [$userId]);
162
    }
163
164
165
}
166