Skip to content

Commit 259fd53

Browse files
committed
Merge pull request #532 from yDelouis/restUpdatedItems
REST API : Get only items updated since given time
2 parents af32244 + b257414 commit 259fd53

File tree

5 files changed

+138
-13
lines changed

5 files changed

+138
-13
lines changed

daos/mysql/Database.php

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function __construct() {
4242
foreach($table as $key=>$value)
4343
$tables[] = $value;
4444

45-
if(!in_array(\F3::get('db_prefix').'items', $tables))
45+
if(!in_array(\F3::get('db_prefix').'items', $tables)) {
4646
\F3::get('db')->exec('
4747
CREATE TABLE '.\F3::get('db_prefix').'items (
4848
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
@@ -56,9 +56,25 @@ public function __construct() {
5656
source INT NOT NULL ,
5757
uid VARCHAR(255) NOT NULL,
5858
link TEXT NOT NULL,
59+
updatetime DATETIME NOT NULL,
5960
INDEX (source)
6061
) ENGINE = MYISAM DEFAULT CHARSET=utf8;
6162
');
63+
\F3::get('db')->exec('
64+
CREATE TRIGGER insert_updatetime_trigger
65+
BEFORE INSERT ON items FOR EACH ROW
66+
BEGIN
67+
SET NEW.updatetime = NOW();
68+
END;
69+
');
70+
\F3::get('db')->exec('
71+
CREATE TRIGGER update_updatetime_trigger
72+
BEFORE UPDATE ON items FOR EACH ROW
73+
BEGIN
74+
SET NEW.updatetime = NOW();
75+
END;
76+
');
77+
}
6278

6379
$isNewestSourcesTable = false;
6480
if(!in_array(\F3::get('db_prefix').'sources', $tables)) {
@@ -85,7 +101,7 @@ public function __construct() {
85101
');
86102

87103
\F3::get('db')->exec('
88-
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (3);
104+
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (4);
89105
');
90106

91107
\F3::get('db')->exec('
@@ -105,14 +121,36 @@ public function __construct() {
105121
$version = @\F3::get('db')->exec('SELECT version FROM '.\F3::get('db_prefix').'version ORDER BY version DESC LIMIT 0, 1');
106122
$version = $version[0]['version'];
107123

108-
if($version == "2"){
124+
if(strnatcmp($version, "3") < 0){
109125
\F3::get('db')->exec('
110126
ALTER TABLE '.\F3::get('db_prefix').'sources ADD lastupdate INT;
111127
');
112128
\F3::get('db')->exec('
113129
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (3);
114130
');
115131
}
132+
if(strnatcmp($version, "4") < 0){
133+
\F3::get('db')->exec('
134+
ALTER TABLE '.\F3::get('db_prefix').'items ADD updatetime DATETIME;
135+
');
136+
\F3::get('db')->exec('
137+
CREATE TRIGGER insert_updatetime_trigger
138+
BEFORE INSERT ON items FOR EACH ROW
139+
BEGIN
140+
SET NEW.updatetime = NOW();
141+
END;
142+
');
143+
\F3::get('db')->exec('
144+
CREATE TRIGGER update_updatetime_trigger
145+
BEFORE UPDATE ON items FOR EACH ROW
146+
BEGIN
147+
SET NEW.updatetime = NOW();
148+
END;
149+
');
150+
\F3::get('db')->exec('
151+
INSERT INTO '.\F3::get('db_prefix').'version (version) VALUES (4);
152+
');
153+
}
116154
}
117155

118156
// just initialize once

daos/mysql/Items.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,13 @@ public function get($options = array()) {
220220
$params[':source'] = array($options['source'], \PDO::PARAM_INT);
221221
$where .= " AND items.source=:source ";
222222
}
223-
223+
224+
// update time filter
225+
if(isset($options['updatedsince']) && strlen($options['updatedsince'])>0) {
226+
$params[':updatedsince'] = array($options['updatedsince'], \PDO::PARAM_STR);
227+
$where .= " AND items.updatetime > :updatedsince ";
228+
}
229+
224230
// set limit
225231
if(!is_numeric($options['items']) || $options['items']>200)
226232
$options['items'] = \F3::get('items_perpage');
@@ -238,7 +244,7 @@ public function get($options = array()) {
238244

239245
// get items from database
240246
return \F3::get('db')->exec('SELECT
241-
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, sources.title as sourcetitle, sources.tags as tags
247+
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, sources.title as sourcetitle, sources.tags as tags
242248
FROM '.\F3::get('db_prefix').'items AS items, '.\F3::get('db_prefix').'sources AS sources
243249
WHERE items.source=sources.id '.$where.'
244250
ORDER BY items.datetime '.$order.'

daos/pgsql/Database.php

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,29 @@ public function __construct() {
6262
starred BOOLEAN NOT NULL,
6363
source INTEGER NOT NULL,
6464
uid TEXT NOT NULL,
65-
link TEXT NOT NULL
65+
link TEXT NOT NULL,
66+
updatetime TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
6667
);
6768
');
68-
6969
\F3::get('db')->exec('
7070
CREATE INDEX source ON items (
7171
source
7272
);
7373
');
74+
\F3::get('db')->exec('
75+
CREATE OR REPLACE FUNCTION update_updatetime_procedure()
76+
RETURNS TRIGGER AS $$
77+
BEGIN
78+
NEW.updatetime = NOW();
79+
RETURN NEW;
80+
END;
81+
$$ LANGUAGE "plpgsql";
82+
');
83+
\F3::get('db')->exec('
84+
CREATE TRIGGER update_updatetime_trigger
85+
BEFORE UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE
86+
update_updatetime_procedure();
87+
');
7488
}
7589

7690
$isNewestSourcesTable = false;
@@ -98,7 +112,7 @@ public function __construct() {
98112
');
99113

100114
\F3::get('db')->exec('
101-
INSERT INTO version (version) VALUES (3);
115+
INSERT INTO version (version) VALUES (4);
102116
');
103117

104118
\F3::get('db')->exec('
@@ -118,14 +132,39 @@ public function __construct() {
118132
$version = @\F3::get('db')->exec('SELECT version FROM version ORDER BY version DESC LIMIT 1');
119133
$version = $version[0]['version'];
120134

121-
if($version == "2"){
135+
if(strnatcmp($version, "3") < 0){
122136
\F3::get('db')->exec('
123137
ALTER TABLE sources ADD lastupdate INT;
124138
');
125139
\F3::get('db')->exec('
126140
INSERT INTO version (version) VALUES (3);
127141
');
128142
}
143+
if(strnatcmp($version, "4") < 0){
144+
\F3::get('db')->exec('
145+
ALTER TABLE items ADD updatetime TIMESTAMP WITH TIME ZONE;
146+
');
147+
\F3::get('db')->exec('
148+
ALTER TABLE items ALTER COLUMN updatetime SET DEFAULT CURRENT_TIMESTAMP;
149+
');
150+
\F3::get('db')->exec('
151+
CREATE OR REPLACE FUNCTION update_updatetime_procedure()
152+
RETURNS TRIGGER AS $$
153+
BEGIN
154+
NEW.updatetime = NOW();
155+
RETURN NEW;
156+
END;
157+
$$ LANGUAGE "plpgsql";
158+
');
159+
\F3::get('db')->exec('
160+
CREATE TRIGGER update_updatetime_trigger
161+
BEFORE UPDATE ON items FOR EACH ROW EXECUTE PROCEDURE
162+
update_updatetime_procedure();
163+
');
164+
\F3::get('db')->exec('
165+
INSERT INTO version (version) VALUES (4);
166+
');
167+
}
129168
}
130169

131170
// just initialize once

daos/pgsql/Items.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ public function get($options = array()) {
5151
$params[':source'] = array($options['source'], \PDO::PARAM_INT);
5252
$where .= " AND items.source=:source ";
5353
}
54+
55+
// update time filter
56+
if(isset($options['updatedsince']) && strlen($options['updatedsince'])>0) {
57+
$params[':updatedsince'] = array($options['updatedsince'], \PDO::PARAM_STR);
58+
$where .= " AND items.updatetime > :updatedsince ";
59+
}
5460

5561
// set limit
5662
if(!is_numeric($options['items']) || $options['items']>200)
@@ -65,7 +71,7 @@ public function get($options = array()) {
6571

6672
// get items from database
6773
return \F3::get('db')->exec('SELECT
68-
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, sources.title as sourcetitle, sources.tags as tags
74+
items.id, datetime, items.title AS title, content, unread, starred, source, thumbnail, icon, uid, link, updatetime, sources.title as sourcetitle, sources.tags as tags
6975
FROM items, sources
7076
WHERE items.source=sources.id '.$where.'
7177
ORDER BY items.datetime '.$order.'

daos/sqlite/Database.php

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ public function __construct() {
6161
starred BOOL NOT NULL,
6262
source INT NOT NULL,
6363
uid VARCHAR(255) NOT NULL,
64-
link TEXT NOT NULL
64+
link TEXT NOT NULL,
65+
updatetime DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
6566
);
6667
');
6768

@@ -70,6 +71,15 @@ public function __construct() {
7071
source
7172
);
7273
');
74+
\F3::get('db')->exec('
75+
CREATE TRIGGER update_updatetime_trigger
76+
AFTER UPDATE ON items FOR EACH ROW
77+
BEGIN
78+
UPDATE items
79+
SET updatetime = CURRENT_TIMESTAMP
80+
WHERE id = NEW.id;
81+
END;
82+
');
7383
}
7484

7585
$isNewestSourcesTable = false;
@@ -97,7 +107,7 @@ public function __construct() {
97107
');
98108

99109
\F3::get('db')->exec('
100-
INSERT INTO version (version) VALUES (3);
110+
INSERT INTO version (version) VALUES (4);
101111
');
102112

103113
\F3::get('db')->exec('
@@ -117,14 +127,40 @@ public function __construct() {
117127
$version = @\F3::get('db')->exec('SELECT version FROM version ORDER BY version DESC LIMIT 0, 1');
118128
$version = $version[0]['version'];
119129

120-
if($version == "2"){
130+
if(strnatcmp($version, "3") < 0){
121131
\F3::get('db')->exec('
122132
ALTER TABLE sources ADD lastupdate INT;
123133
');
124134
\F3::get('db')->exec('
125135
INSERT INTO version (version) VALUES (3);
126136
');
127137
}
138+
if(strnatcmp($version, "4") < 0){
139+
\F3::get('db')->exec('
140+
ALTER TABLE items ADD updatetime DATETIME;
141+
');
142+
\F3::get('db')->exec('
143+
CREATE TRIGGER insert_updatetime_trigger
144+
AFTER INSERT ON items FOR EACH ROW
145+
BEGIN
146+
UPDATE items
147+
SET updatetime = CURRENT_TIMESTAMP
148+
WHERE id = NEW.id;
149+
END;
150+
');
151+
\F3::get('db')->exec('
152+
CREATE TRIGGER update_updatetime_trigger
153+
AFTER UPDATE ON items FOR EACH ROW
154+
BEGIN
155+
UPDATE items
156+
SET updatetime = CURRENT_TIMESTAMP
157+
WHERE id = NEW.id;
158+
END;
159+
');
160+
\F3::get('db')->exec('
161+
INSERT INTO version (version) VALUES (4);
162+
');
163+
}
128164
}
129165

130166
// just initialize once

0 commit comments

Comments
 (0)