1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
<?php
declare(strict_types=1);
class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
#[\Override]
public static function isCompressed(): bool {
return false;
}
#[\Override]
public static function hasNativeHex(): bool {
return false;
}
#[\Override]
protected static function sqlConcat(string $s1, string $s2): string {
return $s1 . '||' . $s2;
}
#[\Override]
public static function sqlHexDecode(string $x): string {
return $x;
}
#[\Override]
public static function sqlIgnoreConflict(string $sql): string {
return str_replace('INSERT INTO ', 'INSERT OR IGNORE INTO ', $sql);
}
#[\Override]
protected static function sqlRegex(string $expression, string $regex, array &$values): string {
$values[] = $regex;
return "{$expression} REGEXP ?";
}
#[\Override]
protected function registerSqlFunctions(string $sql): void {
if (!str_contains($sql, ' REGEXP ')) {
return;
}
// https://php.net/pdo.sqlitecreatefunction
// https://www.sqlite.org/lang_expr.html#the_like_glob_regexp_match_and_extract_operators
$this->pdo->sqliteCreateFunction('regexp',
function (string $pattern, string $text): bool {
return preg_match($pattern, $text) === 1;
},
2
);
}
/** @param array{0:string,1:int,2:string} $errorInfo */
#[\Override]
protected function autoUpdateDb(array $errorInfo): bool {
if (($tableInfo = $this->pdo->query("PRAGMA table_info('entry')")) !== false) {
$columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1) ?: [];
foreach (['attributes'] as $column) {
if (!in_array($column, $columns, true)) {
return $this->addColumn($column);
}
}
}
return false;
}
#[\Override]
public function commitNewEntries(): bool {
$sql = <<<'SQL'
DROP TABLE IF EXISTS `tmp`;
CREATE TEMP TABLE `tmp` AS
SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
FROM `_entrytmp`
ORDER BY date, id;
INSERT OR IGNORE INTO `_entry`
(id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes)
SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags, attributes
FROM `tmp`
ORDER BY date, id;
DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
DROP TABLE IF EXISTS `tmp`;
SQL;
$hadTransaction = $this->pdo->inTransaction();
if (!$hadTransaction) {
$this->pdo->beginTransaction();
}
$result = $this->pdo->exec($sql) !== false;
if (!$result) {
Minz_Log::error('SQL error ' . __METHOD__ . json_encode($this->pdo->errorInfo()));
}
if (!$hadTransaction) {
$this->pdo->commit();
}
return $result;
}
/**
* Toggle the read marker on one or more article.
* Then the cache is updated.
*
* @param numeric-string|array<numeric-string> $ids
* @return int|false affected rows
*/
#[\Override]
public function markRead(array|string $ids, bool $is_read = true): int|false {
if (is_array($ids)) { //Many IDs at once (used by API)
//if (true) { //Speed heuristics //TODO: Not implemented yet for SQLite (so always call IDs one by one)
$affected = 0;
foreach ($ids as $id) {
$affected += ($this->markRead($id, $is_read) ?: 0);
}
return $affected;
//}
} else {
FreshRSS_UserDAO::touch();
$this->pdo->beginTransaction();
$sql = 'UPDATE `_entry` SET is_read=? WHERE id=? AND is_read=?';
$values = [$is_read ? 1 : 0, $ids, $is_read ? 0 : 1];
$stm = $this->pdo->prepare($sql);
if ($stm === false || !$stm->execute($values)) {
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error ' . __METHOD__ . ' A ' . json_encode($info));
$this->pdo->rollBack();
return false;
}
$affected = $stm->rowCount();
if ($affected > 0) {
$sql = 'UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
. 'WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=?)';
$values = [$ids];
$stm = $this->pdo->prepare($sql);
if ($stm === false || !$stm->execute($values)) {
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error ' . __METHOD__ . ' B ' . json_encode($info));
$this->pdo->rollBack();
return false;
}
}
$this->pdo->commit();
return $affected;
}
}
/**
* Mark all the articles in a tag as read.
* @param int $id tag ID, or empty for targeting any tag
* @param string $idMax max article ID
* @return int|false affected rows
*/
#[\Override]
public function markReadTag($id = 0, string $idMax = '0', ?FreshRSS_BooleanSearch $filters = null, int $state = 0, bool $is_read = true): int|false {
FreshRSS_UserDAO::touch();
if ($idMax == 0) {
$idMax = time() . '000000';
Minz_Log::debug('Calling markReadTag(0) is deprecated!');
}
$sql = 'UPDATE `_entry` SET is_read = ? WHERE is_read <> ? AND id <= ? AND '
. 'id IN (SELECT et.id_entry FROM `_entrytag` et '
. ($id == 0 ? '' : 'WHERE et.id_tag = ?')
. ')';
$values = [$is_read ? 1 : 0, $is_read ? 1 : 0, $idMax];
if ($id != 0) {
$values[] = $id;
}
[$searchValues, $search] = $this->sqlListEntriesWhere('e.', $filters, $state);
$stm = $this->pdo->prepare($sql . $search);
if ($stm === false || !$stm->execute(array_merge($values, $searchValues))) {
$info = $stm === false ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error ' . __METHOD__ . json_encode($info));
return false;
}
$affected = $stm->rowCount();
if (($affected > 0) && (!$this->updateCacheUnreads(null, null))) {
return false;
}
return $affected;
}
}
|