ItemMapper::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 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\Mysql;
15
16
use OCP\IDBConnection;
17
18
use OCA\News\Db\StatusFlag;
19
20
21
class ItemMapper extends \OCA\News\Db\ItemMapper {
22
23
    public function __construct(IDBConnection $db){
24
        parent::__construct($db);
25
    }
26
27
28
    /**
29
     * Delete all items for feeds that have over $threshold unread and not
30
     * starred items
31
	 * @param int $threshold the number of items that should be deleted
32
     */
33
    public function deleteReadOlderThanThreshold($threshold){
34
        $status = StatusFlag::STARRED | StatusFlag::UNREAD;
35
        $sql = 'SELECT (COUNT(*) - `feeds`.`articles_per_update`) AS `size`, ' .
36
        '`feeds`.`id` AS `feed_id`, `feeds`.`articles_per_update` ' .
37
            'FROM `*PREFIX*news_items` `items` ' .
38
            'JOIN `*PREFIX*news_feeds` `feeds` ' .
39
                'ON `feeds`.`id` = `items`.`feed_id` ' .
40
                'AND NOT ((`items`.`status` & ?) > 0) ' .
41
            'GROUP BY `feeds`.`id`, `feeds`.`articles_per_update` ' .
42
            'HAVING COUNT(*) > ?';
43
        $params = [$status, $threshold];
44
        $result = $this->execute($sql, $params);
45
46
        while($row = $result->fetch()) {
47
48
            $size = (int) $row['size'];
49
            $limit = $size - $threshold;
50
51
            if($limit > 0) {
52
                $params = [$status, $row['feed_id'], $limit];
53
54
                $sql = 'DELETE FROM `*PREFIX*news_items` ' .
55
                    'WHERE NOT ((`status` & ?) > 0) ' .
56
                    'AND `feed_id` = ? ' .
57
                    'ORDER BY `id` ASC ' .
58
                    'LIMIT ?';
59
60
                $this->execute($sql, $params);
61
            }
62
        }
63
64
    }
65
66
    public function readItem($itemId, $isRead, $lastModified, $userId) {
67
        $item = $this->find($itemId, $userId);
68
69
        if ($isRead) {
70
            $sql = 'UPDATE `*PREFIX*news_items` `items`
71
                JOIN `*PREFIX*news_feeds` `feeds`
72
                    ON `feeds`.`id` = `items`.`feed_id`
73
                SET `items`.`status` = `items`.`status` & ?,
74
                    `items`.`last_modified` = ?
75
                WHERE `items`.`fingerprint` = ?
76
                    AND `feeds`.`user_id` = ?';
77
            $params = [~StatusFlag::UNREAD, $lastModified,
78
                       $item->getFingerprint(), $userId];
79
            $this->execute($sql, $params);
80
        } else {
81
            $item->setLastModified($lastModified);
82
            $item->setUnread();
83
            $this->update($item);
84
        }
85
    }
86
87
}
88