From 29a96b3638911514c65600d6bd310918e09341d0 Mon Sep 17 00:00:00 2001 From: David Glenck Date: Sat, 17 May 2014 12:44:41 +0200 Subject: [PATCH 1/4] store and return date of last deletion --- data/upgrade/tables-0.99-postgresql.sql | 2 + data/upgrade/tables-0.99.sql | 2 + src/SemanticScuttle/Service/Bookmark.php | 71 ++++++++++++++++++++++++ www/api/posts_update.php | 26 +++++---- 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 data/upgrade/tables-0.99-postgresql.sql create mode 100644 data/upgrade/tables-0.99.sql diff --git a/data/upgrade/tables-0.99-postgresql.sql b/data/upgrade/tables-0.99-postgresql.sql new file mode 100644 index 00000000..3639fee1 --- /dev/null +++ b/data/upgrade/tables-0.99-postgresql.sql @@ -0,0 +1,2 @@ +ALTER TABLE sc_users ADD bLastDelete timestamp with time zone NOT NULL; +UPDATE sc_version SET schema_version=7; diff --git a/data/upgrade/tables-0.99.sql b/data/upgrade/tables-0.99.sql new file mode 100644 index 00000000..dc1e42b1 --- /dev/null +++ b/data/upgrade/tables-0.99.sql @@ -0,0 +1,2 @@ +ALTER TABLE `sc_users` ADD `bLastDelete` DATETIME NOT NULL; +UPDATE `sc_version` SET `schema_version`='7'; diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 13153503..faa4e3e9 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -1001,6 +1001,17 @@ public function deleteBookmark($bookmark) ); } + // update ´last_delete´ date + $userservice = SemanticScuttle_Service_Factory::get('User'); + $uId = $userservice->getCurrentUserId(); + if( !$this->updateLastDelete($uId) ) { + $this->db->sql_transaction('rollback'); + message_die( + GENERAL_ERROR, 'Could not update the time of the last deletion', + '', __LINE__, __FILE__, $query, $this->db + ); + } + $this->db->sql_transaction('commit'); return true; @@ -1027,10 +1038,70 @@ public function deleteBookmarksForUser($uId) ); } + // update ´last_delete´ date + if( !$this->updateLastDelete($uId) ) { + message_die( + GENERAL_ERROR, 'Could not update the time of the last deletion', + '', __LINE__, __FILE__, $query, $this->db + ); + } + return true; } + /** + * Update Timestamp of last deletion to current timestamp + * + * @param integer $uId User ID + * + * @return boolean true when all went well + */ + private function updateLastDelete($uId) + { + $deldatetime = gmdate('Y-m-d H:i:s', time()); + + $updates = array( + 'bLastDelete' => $deldatetime, + ); + + $query = 'UPDATE '. $GLOBALS['tableprefix'] . 'users' + . ' SET '. $this->db->sql_build_array('UPDATE', $updates) + . ' WHERE uId = ' . intval($uId); + + if (!($dbresult = $this->db->sql_query($query))) { + return false; + } + + return true; + } + + /** + * Get the timestamp of the last deletion + * Used for the ´posts_update´ API-function if last deletion is + * more recent than last modification of an existing bookmark + * + * @param integer $uId User ID + * + * @return string representing the time, parsable with strtotime() + */ + public function getLastDelete($uId) + { + $query = 'SELECT bLastDelete as lastDelete' + . ' FROM '. $GLOBALS['tableprefix'] . 'users' + . ' WHERE uId = ' . intval($uId); + + if (!($dbresult = $this->db->sql_query($query))) { + message_die( + GENERAL_ERROR, 'Could not get LastDelete', '', + __LINE__, __FILE__, $sql, $this->db + ); + } + + return $this->db->sql_fetchfield(0, 0); + } + + /** * Counts the number of bookmarks that have the same address diff --git a/www/api/posts_update.php b/www/api/posts_update.php index 0e08ee75..8d406fd0 100644 --- a/www/api/posts_update.php +++ b/www/api/posts_update.php @@ -34,25 +34,31 @@ // parameter "datemode=modified" will get last modified date // instead of last created date -$orderby = null; -$timeField = 'bDatetime'; -if (isset($_GET['datemode']) && $_GET['datemode'] == 'modified') { - $orderby = 'modified_desc'; - $timeField = 'bModified'; +$orderby = 'modified_desc'; +$timeField = 'bModified'; +if (isset($_GET['datemode']) && $_GET['datemode'] == 'created') { + $orderby = null; + $timeField = 'bDatetime'; } +$uID = $userservice->getCurrentUserId(); $bs = SemanticScuttle_Service_Factory::get('Bookmark'); -$bookmarks = $bs->getBookmarks(0, 1, $userservice->getCurrentUserId(), null, null, $orderby); +$bookmarks = $bs->getBookmarks(0, 1, $uID, null, null, $orderby); +$lastDelete = $bs->getLastDelete($uID); -// Set up the XML file and output all the tags. -echo '\r\n"; +// get time of most recent change (modification or deletion) //foreach is used in case there are no bookmarks +$time = 0; foreach ($bookmarks['bookmarks'] as $row) { + $time = max( strtotime($row[$timeField]), strtotime($lastDelete) ); +} + +// Set up the XML file and output all the tags. +echo '\r\n"; echo ''; -} ?> From c4103cf966f8d077acf6eb7907af6d2ca0d5e117 Mon Sep 17 00:00:00 2001 From: David Glenck Date: Sat, 17 May 2014 14:23:15 +0200 Subject: [PATCH 2/4] update db schema --- data/tables-postgresql.sql | 4 +++- data/tables.sql | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/data/tables-postgresql.sql b/data/tables-postgresql.sql index b0ada11a..9e8ddd1f 100644 --- a/data/tables-postgresql.sql +++ b/data/tables-postgresql.sql @@ -194,7 +194,8 @@ CREATE TABLE sc_users ( email varchar(50) NOT NULL DEFAULT '', homepage varchar(255) DEFAULT NULL, uContent text, - privateKey varchar(33) DEFAULT NULL + privateKey varchar(33) DEFAULT NULL, + bLastDelete timestamp with time zone DEFAULT now() NOT NULL ); CREATE UNIQUE INDEX privateKey on sc_users (privateKey); @@ -225,6 +226,7 @@ CREATE TABLE sc_users_sslclientcerts ( CREATE TABLE sc_version ( schema_version integer NOT NULL ); +INSERT INTO sc_version (schema_version) VALUES (7); -- -- Table structure for table "sc_votes" diff --git a/data/tables.sql b/data/tables.sql index 68d5ba97..cda2ab31 100644 --- a/data/tables.sql +++ b/data/tables.sql @@ -73,6 +73,7 @@ CREATE TABLE `sc_users` ( `homepage` varchar(255) default NULL, `uContent` text, `privateKey` varchar(33) default NULL, + `bLastDelete` datetime NOT NULL default '0000-00-00 00:00:00', PRIMARY KEY (`uId`), UNIQUE KEY `privateKey` (`privateKey`) ) CHARACTER SET utf8 COLLATE utf8_general_ci ; @@ -200,4 +201,4 @@ CREATE TABLE `sc_votes` ( CREATE TABLE `sc_version` ( `schema_version` int(11) NOT NULL ) DEFAULT CHARSET=utf8; -INSERT INTO `sc_version` (`schema_version`) VALUES ('6'); +INSERT INTO `sc_version` (`schema_version`) VALUES ('7'); From c7808ce738d0e3b1d214d4bb786a76b82cc5e82f Mon Sep 17 00:00:00 2001 From: David Glenck Date: Wed, 21 May 2014 16:50:24 +0200 Subject: [PATCH 3/4] change name and location of sql upgrade files --- .../tables-0.99-postgresql.sql => schema/7-postgresql.sql} | 0 data/{upgrade/tables-0.99.sql => schema/7.sql} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename data/{upgrade/tables-0.99-postgresql.sql => schema/7-postgresql.sql} (100%) rename data/{upgrade/tables-0.99.sql => schema/7.sql} (100%) diff --git a/data/upgrade/tables-0.99-postgresql.sql b/data/schema/7-postgresql.sql similarity index 100% rename from data/upgrade/tables-0.99-postgresql.sql rename to data/schema/7-postgresql.sql diff --git a/data/upgrade/tables-0.99.sql b/data/schema/7.sql similarity index 100% rename from data/upgrade/tables-0.99.sql rename to data/schema/7.sql From 98c8323caed3c748cd22b5897918972ce0fae2e3 Mon Sep 17 00:00:00 2001 From: David Glenck Date: Sat, 31 May 2014 17:52:23 +0200 Subject: [PATCH 4/4] add upgrade instructions --- doc/UPGRADE.txt | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/doc/UPGRADE.txt b/doc/UPGRADE.txt index 511748ee..98dcd8b5 100644 --- a/doc/UPGRADE.txt +++ b/doc/UPGRADE.txt @@ -4,6 +4,16 @@ Upgrading SemanticScuttle from a previous version .. contents:: +From version 0.98 to 0.99 +========================= +Database updates +---------------- +Apply ``data/schema/7.sql`` + + ALTER TABLE `sc_users` ADD `bLastDelete` DATETIME NOT NULL; + UPDATE `sc_version` SET `schema_version`='7'; + + From version 0.94-0.98.1 to 0.98.3 ================================== Run ``scripts/fix-unfiled-tags.php`` to fix old bookmarks that miss the