Skip to content
This repository was archived by the owner on Dec 16, 2019. It is now read-only.

Fix: Detect deletion of bookmark via api/posts_update.php #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions data/schema/7-postgresql.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE sc_users ADD bLastDelete timestamp with time zone NOT NULL;
UPDATE sc_version SET schema_version=7;
2 changes: 2 additions & 0 deletions data/schema/7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `sc_users` ADD `bLastDelete` DATETIME NOT NULL;
UPDATE `sc_version` SET `schema_version`='7';
4 changes: 3 additions & 1 deletion data/tables-postgresql.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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"
Expand Down
3 changes: 2 additions & 1 deletion data/tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 ;
Expand Down Expand Up @@ -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');
10 changes: 10 additions & 0 deletions doc/UPGRADE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
71 changes: 71 additions & 0 deletions src/SemanticScuttle/Service/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
26 changes: 16 additions & 10 deletions www/api/posts_update.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '<?xml version="1.0" standalone="yes" ?' . ">\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 '<?xml version="1.0" standalone="yes" ?' . ">\r\n";
echo '<update time="'
. gmdate('Y-m-d\TH:i:s\Z', strtotime($row[$timeField]))
. gmdate('Y-m-d\TH:i:s\Z', $time)
. '"'
. ' inboxnew="0"'
. ' />';
}
?>