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

[WIP] API Update #9

Open
wants to merge 3 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
11 changes: 9 additions & 2 deletions src/SemanticScuttle/Service/Bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ public function updateBookmark(
* @param integer $enddate Filter for creation date.
* SQL-DateTime value
* "YYYY-MM-DD hh:ii:ss'
* @param string $hash Filter by URL hash
* @param mixed $hash Filter by URL hash, may be an array of hashes or a single hash string
*
* @return array Array with two keys: 'bookmarks' and 'total'.
* First contains an array of bookmarks, 'total'
Expand Down Expand Up @@ -896,7 +896,14 @@ public function getBookmarks(

// Hash
if ($hash) {
$query_4 .= ' AND B.bHash = "'. $hash .'"';
if (!is_array($hash)) {
$hash = array($hash);
}
$hash_query = array();
foreach ($hash as $h) {
$hash_query[] = ' B.bHash = "'. $h .'" ';
}
$query_4 .= ' AND ('. implode($hash_query, ' OR ') .') ';
}


Expand Down
12 changes: 6 additions & 6 deletions www/api/posts_add.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
* @param string $url URL of the bookmark (required)
* @param string $description Bookmark title (required)
* @param string $extended Extended bookmark description (optional)
* @param string $tags Space-separated list of tags (optional)
* @param string $dt Date and time of bookmark creation (optional)
* @param string $tags comma delimited list of tags (optional)
* @param string $dt UTC Date and time of bookmark creation (optional)
* Must be of format YYYY-MM-DDTHH:II:SSZ
* @param integer $status Visibility status (optional):
* - 2 or 'private': Bookmark is totally private
Expand All @@ -20,7 +20,6 @@
* same URL (optional)
*
* Notes:
* - tags cannot have spaces
* - URL and description (title) are mandatory
* - delicious "description" is the "title" in SemanticScuttle
* - delicious "extended" is the "description" in SemanticScuttle
Expand Down Expand Up @@ -74,7 +73,8 @@
}

if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) {
$dt = trim($_REQUEST['dt']);
$date = new DateTime( trim($_REQUEST['dt']) , new DateTimeZone('UTC')); //adjust to UTC
$dt = date('Y-m-d\TH:i:s\Z', $date->getTimestamp());
} else {
$dt = null;
}
Expand Down Expand Up @@ -134,7 +134,7 @@

if (!$exists) {
$added = $bs->addBookmark(
$url, $description, $extended, '', $status, $tags, null, $dt, true
$url, $description, $extended, '', $status, $tags, null, $dt, false //false, because new API uses comma separated tags
);
$msg = 'done';
}
Expand All @@ -143,4 +143,4 @@
// Set up the XML file and output the result.
echo '<?xml version="1.0" standalone="yes" ?' . ">\r\n";
echo '<result code="' . $msg .'" />';
?>
?>
116 changes: 108 additions & 8 deletions www/api/posts_all.php
Original file line number Diff line number Diff line change
@@ -1,32 +1,126 @@
<?php
// Implements the del.icio.us API request for all a user's posts, optionally filtered by tag.
// Implements the del.icio.us API request for all a user's posts.

// del.icio.us behavior:
// - doesn't include the filtered tag as an attribute on the root element (we do)
// Arguments: (copied from: https://github.com/SciDevs/delicious-api/blob/master/api/posts.md#v1postsallhashes on 2014-05-18)
/*
- `&tag_separator=comma` (optional) - (Recommended) Returns tags separated by a comma, instead of a space character. A space separator is currently used by default to avoid breaking existing clients - these default may change in future API revisions.
- `&tag={TAG}` (optional) — Filter by this tag.
- `&start={xx}` (optional) — Start returning posts this many results into the set.
- `&results={xx}` (optional) — Return up to this many results. By default, up to 1000 bookmarks are returned. (no limits in scuttles)
- `&fromdt={CCYY-MM-DDThh:mm:ssZ}` (optional) — Filter for posts on this date or later.
- `&todt={CCYY-MM-DDThh:mm:ssZ}` (optional) — Filter for posts on this date or earlier.
- `&meta=yes` (optional) — Include change detection signatures on each item in a ‘meta’ attribute. Clients wishing to maintain a synchronized local store of bookmarks should retain the value of this attribute - its value will change when any significant field of the bookmark changes.
*/

// Scuttle behavior:
// - returns privacy status of each bookmark.
// - There is no upper limit for the 'results' argument

// fail, will be called, if somethink "goes wrong" to copy delicious behaviour
function fail() {
echo '<result code="something went wrong"/>';
exit();
}

// no_bookmarks: indicate, that there are no bookmarks using these filters
function no_bookmarks() {
echo '<result code="no bookmarks"/>';
exit();
}

// Force HTTP authentication first!
$httpContentType = 'text/xml';
require_once 'httpauth.inc.php';

// xml header
echo '<?xml version="1.0" encoding="UTF-8" ?'.">\r\n";

/* Service creation: only useful services are created */
$bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark');

// special case - hashes: only return url hash and meta (change indicator)
if ( isset($_REQUEST['hashes']) ) {
// get all bookmarks
$bookmarks = $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId());

// output:
// Set up the XML file and output all the posts.
echo "<posts>\r\n";
foreach($bookmarks['bookmarks'] as $row) {
$url = md5($row['bAddress']);
//create a meta, which changes, when the bookmark changes in the form of a md5 hash (as delicious does it).
$meta = md5($row['bModified']);

echo "\t<post meta=\"". $meta .'" url="'. $url ."\"/>\r\n";
}

echo '</posts>';
exit();
}

// Check to see if a tag was specified.
if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != ''))
$tag = trim($_REQUEST['tag']);
else
$tag = NULL;

// 'tag separator' option
if (isset($_REQUEST['tag_separator']) && (trim($_REQUEST['tag_separator']) == 'comma'))
$tag_separator = ',';
else
$tag_separator = ' ';

// 'start' option
if (isset($_REQUEST['start']) && (intval($_REQUEST['start']) > 0))
$start = intval($_REQUEST['start']);
else
$start = 0; //default in delicious api

// 'results' option
// upper limit of delicious api is 100000. There is no upper limit here. TODO: implement upper limit?
if (isset($_REQUEST['results'])) {
if( $_REQUEST['results'] < 0 )
fail(); //like delicious
elseif( $_REQUEST['results'] == 0 )
no_bookmarks();
else
$results = intval($_REQUEST['results']);
} else {
$results = 1000; //default, as in delicious api
}

// 'fromdt' option: filter result by date
if (isset($_REQUEST['fromdt'])) {
$date = new DateTime( $_REQUEST['fromdt'] , new DateTimeZone('UTC')); //adjust to UTC
$fromdt = date('Y-m-d\TH:i:s\Z', $date->getTimestamp());
} else {
$fromdt = NULL;
}

// 'todt' option: filter result by date
if (isset($_REQUEST['todt'])) {
$date = new DateTime( $_REQUEST['todt'] , new DateTimeZone('UTC')); //adjust to UTC
$todt = date('Y-m-d\TH:i:s\Z', $date->getTimestamp());
} else {
$todt = NULL;
}

// 'meta' option: get meta (change indicator)
if (isset($_REQUEST['meta']) && (trim($_REQUEST['meta']) == 'yes'))
$meta = true;
else
$meta = false;

// Get the posts relevant to the passed-in variables.
$bookmarks = $bookmarkservice->getBookmarks(0, NULL, $userservice->getCurrentUserId(), $tag);
$bookmarks = $bookmarkservice->getBookmarks($start, $results, $userservice->getCurrentUserId(), $tag, NULL, NULL, NULL, $fromdt, $todt);

// check for empty result
if (count($bookmarks['bookmarks'])==0) {
no_bookmarks();
}

// Set up the XML file and output all the posts.
echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
echo '<posts update="'. gmdate('Y-m-d\TH:i:s\Z') .'" user="'. htmlspecialchars($currentUser->getUsername()) .'"'. (is_null($tag) ? '' : ' tag="'. htmlspecialchars($tag) .'"') .">\r\n";
echo '<posts update="'. gmdate('Y-m-d\TH:i:s\Z') .'" user="'. htmlspecialchars($currentUser->getUsername()) .'" total="'. count($bookmarks['bookmarks']) .'" '. (is_null($tag) ? '' : ' tag="'. htmlspecialchars($tag) .'"') .">\r\n";

foreach($bookmarks['bookmarks'] as $row) {
if (is_null($row['bDescription']) || (trim($row['bDescription']) == ''))
Expand All @@ -37,13 +131,19 @@
$taglist = '';
if (count($row['tags']) > 0) {
foreach($row['tags'] as $tag)
$taglist .= convertTag($tag) .' ';
$taglist .= convertTag($tag) . $tag_separator;
$taglist = substr($taglist, 0, -1);
} else {
$taglist = 'system:unfiled';
}

echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. md5($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) . '" status="'. filter($row['bStatus'], 'xml') ."\" />\r\n";
if( $meta ) {
$meta_print = ' meta="'. md5($row['bModified']) .'" ';
} else {
$meta_print = '';
}

echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. md5($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) . '" '. $meta_print .' status="'. filter($row['bStatus'], 'xml') ."\" />\r\n";
}

echo '</posts>';
Expand Down
76 changes: 57 additions & 19 deletions www/api/posts_get.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,32 +32,70 @@
/* Service creation: only useful services are created */
$bookmarkservice = SemanticScuttle_Service_Factory::get('Bookmark');

//// 'meta' argument
$includeMeta = (isset($_REQUEST['meta']) && (trim($_REQUEST['meta']) == 'yes'));

// Check to see if a tag was specified.
if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) {
$tag = trim($_REQUEST['tag']);
//// 'tag_separator' argument
if (isset($_REQUEST['tag_separator']) && (trim($_REQUEST['tag_separator']) == 'comma')) {
$tag_separator = ',';
} else {
$tag = null;
$tag_separator = ' ';
}

// Check to see if a date was specified; the format should be YYYY-MM-DD
if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) {
$dtstart = trim($_REQUEST['dt']);
} else {
$dtstart = date('Y-m-d H:i:s');
//// 'url' argument
if (isset($_REQUEST['url']) && (trim($_REQUEST['url']) != '')) {
$_REQUEST['hashes'] = md5($_REQUEST['url']);
}
$dtend = date('Y-m-d H:i:s', strtotime($dtstart .'+1 day'));

// Get the posts relevant to the passed-in variables.
$bookmarks = $bookmarkservice->getBookmarks(
0, null, $userservice->getCurrentUserId(), $tag,
null, null, null, $dtstart, $dtend
);
//// 'hashes' argument
if (isset($_REQUEST['hashes']) && (trim($_REQUEST['hashes']) != '')) {
$hashes = explode(' ', trim($_REQUEST['hashes']) );
// directly get the bookmarks for these hashes
//TODO: getBookmarks can't handle multiple hashes
$bookmarks = $bookmarkservice->getBookmarks(
0, null, $userservice->getCurrentUserId(), null,
null, null, null, null, null, $hashes
);
} else {

//// 'tag' argument
// Check to see if a tag was specified.
if (isset($_REQUEST['tag']) && (trim($_REQUEST['tag']) != '')) {
// convert spaces back to '+' and explode
$tag = str_replace(' ', '+', trim($_REQUEST['tag']));
$tag = explode('+', $tag);
} else {
$tag = null;
}

//// 'dt' argument
// Check to see if a date was specified; the format should be YYYY-MM-DD in GMT/UTC
if (isset($_REQUEST['dt']) && (trim($_REQUEST['dt']) != '')) {
$dtstart = trim($_REQUEST['dt']);
} else {
$dtstart = gmdate('Y-m-d') . ' 00:00:00'; //Default: Today midnight (UTC)
}
//adjust from UTC to server time
$date = new DateTime( $dtstart , new DateTimeZone('UTC'));
$dtstart = date('Y-m-d H:i:s', $date->getTimestamp());
$dtstart_day = date('Y-m-d', $date->getTimestamp());

$dtend = date('Y-m-d H:i:s', strtotime($dtstart .'+1 day'));

//

// Get the posts relevant to the passed-in variables.
$bookmarks = $bookmarkservice->getBookmarks(
0, null, $userservice->getCurrentUserId(), $tag,
null, null, null, $dtstart, $dtend
);

}


// Set up the XML file and output all the tags.
echo '<?xml version="1.0" standalone="yes" ?'.">\r\n";
echo '<posts'. (is_null($dtstart) ? '' : ' dt="'. $dtstart .'"') .' tag="'. (is_null($tag) ? '' : filter($tag, 'xml')) .'" user="'. filter($currentUser->getUsername(), 'xml') ."\">\r\n";
echo '<?xml version="1.0" encoding="UTF-8" ?'.">\r\n";
echo '<posts'. (is_null($dtstart_day) ? '' : ' dt="'. $dtstart_day .'"') .' tags="'. (is_null($tag) ? '' : filter(implode($tag, $tag_separator), 'xml')) .'" user="'. filter($currentUser->getUsername(), 'xml') ."\">\r\n";

foreach ($bookmarks['bookmarks'] as $row) {
if (is_null($row['bDescription']) || (trim($row['bDescription']) == '')) {
Expand All @@ -69,14 +107,14 @@
$taglist = '';
if (count($row['tags']) > 0) {
foreach ($row['tags'] as $tag) {
$taglist .= convertTag($tag) . ' ';
$taglist .= convertTag($tag) . $tag_separator;
}
$taglist = substr($taglist, 0, -1);
} else {
$taglist = 'system:unfiled';
}

echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. $row['bHash'] .'" others="'. $bookmarkservice->countOthers($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) . '" status="'. filter($row['bStatus'], 'xml') ."\" />\r\n";
echo "\t<post href=\"". filter($row['bAddress'], 'xml') .'" description="'. filter($row['bTitle'], 'xml') .'" '. $description .'hash="'. $row['bHash'] .'" '. ($includeMeta?'meta="'. md5($row['bModified']) .'"':'') .' others="'. $bookmarkservice->countOthers($row['bAddress']) .'" tag="'. filter($taglist, 'xml') .'" time="'. gmdate('Y-m-d\TH:i:s\Z', strtotime($row['bDatetime'])) . '" private="'. ($row['bStatus']==2?'yes':'no') .'" shared="'. ($row['bStatus']==2?'no':'yes') .'" status="'. filter($row['bStatus'], 'xml') ."\" />\r\n";
}

echo '</posts>';
Expand Down