tabs to spaces, document and format addBookmark()

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@397 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
cweiske 2009-10-24 07:59:31 +00:00
parent 68c01cda10
commit eb914d4a7f

View file

@ -19,13 +19,18 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $instance;
}
public function __construct($db)
{
$this->db = $db;
$this->tablename = $GLOBALS['tableprefix'] .'bookmarks';
}
function _getbookmark($fieldname, $value, $all = false) {
function _getbookmark($fieldname, $value, $all = false)
{
if (!$all) {
$userservice = SemanticScuttle_Service_Factory :: get('User');
$sId = $userservice->getCurrentUserId();
@ -50,7 +55,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $output;
}
function & getBookmark($bid, $include_tags = false) {
function getBookmark($bid, $include_tags = false)
{
if (!is_numeric($bid))
return;
@ -72,17 +80,26 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $output;
}
function getBookmarkByAddress($address) {
function getBookmarkByAddress($address)
{
$hash = md5($address);
return $this->getBookmarkByHash($hash);
}
function getBookmarkByHash($hash) {
function getBookmarkByHash($hash)
{
return $this->_getbookmark('bHash', $hash, true);
}
/* Counts bookmarks for a user. $range = {'public', 'shared', 'private', 'all'}*/
function countBookmarks($uId, $range = 'public') {
function countBookmarks($uId, $range = 'public')
{
$sql = 'SELECT COUNT(*) FROM '. $GLOBALS['tableprefix'] .'bookmarks';
$sql.= ' WHERE uId = '.$uId;
switch ($range) {
@ -107,6 +124,8 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $this->db->sql_fetchfield(0, 0);
}
/**
* Check if a bookmark may be edited by the current user
*
@ -141,7 +160,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
}
}
function bookmarkExists($address = false, $uid = NULL) {
function bookmarkExists($address = false, $uid = NULL)
{
if (!$address) {
return;
}
@ -166,9 +188,34 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $output;
}
// Adds a bookmark to the database.
// Note that date is expected to be a string that's interpretable by strtotime().
function addBookmark($address, $title, $description, $privateNote, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false, $sId = -1) {
/**
* Adds a bookmark to the database.
*
* @param string $address Full URL of the bookmark
* @param string $title Bookmark title
* @param string $description Long bookmark description
* @param string $privateNote Private note for the user.
* @param string $status Bookmark visibility:
* 0 - public
* 1 - shared
* 2 - private
* @param array $categories Array of tags
* @param string $date Date when the bookmark has been created
* originally. Used in combination with
* $fromImport. Has to be a strtotime()
* interpretable string.
* @param boolean $fromApi True when api call is responsible.
* @param boolean $fromImport True when the bookmark is from an import.
* @param integer $sId ID of user who creates the bookmark.
*
* @return integer Bookmark ID
*/
public function addBookmark(
$address, $title, $description, $privateNote, $status, $categories,
$date = null, $fromApi = false, $fromImport = false, $sId = -1
) {
if ($sId == -1) {
$userservice = SemanticScuttle_Service_Factory::get('User');
$sId = $userservice->getCurrentUserId();
@ -176,37 +223,63 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
$address = $this->normalize($address);
// Get the client's IP address and the date; note that the date is in GMT.
if (getenv('HTTP_CLIENT_IP'))
if (getenv('HTTP_CLIENT_IP')) {
$ip = getenv('HTTP_CLIENT_IP');
else
if (getenv('REMOTE_ADDR'))
} else if (getenv('REMOTE_ADDR')) {
$ip = getenv('REMOTE_ADDR');
else
} else {
$ip = getenv('HTTP_X_FORWARDED_FOR');
}
// Note that if date is NULL, then it's added with a date and time of now, and if it's present,
// it's expected to be a string that's interpretable by strtotime().
if (is_null($date) || $date == '')
/*
* Note that if date is NULL, then it's added with a date and
* time of now, and if it's present,
* it's expected to be a string that's interpretable by strtotime().
*/
if (is_null($date) || $date == '') {
$time = time();
else
} else {
$time = strtotime($date);
}
$datetime = gmdate('Y-m-d H:i:s', $time);
// Set up the SQL insert statement and execute it.
$values = array('uId' => intval($sId), 'bIp' => $ip, 'bDatetime' => $datetime, 'bModified' => $datetime, 'bTitle' => $title, 'bAddress' => $address, 'bDescription' => $description, 'bPrivateNote' => $privateNote, 'bStatus' => intval($status), 'bHash' => md5($address));
$sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
$values = array(
'uId' => intval($sId),
'bIp' => $ip,
'bDatetime' => $datetime,
'bModified' => $datetime,
'bTitle' => $title,
'bAddress' => $address,
'bDescription' => $description,
'bPrivateNote' => $privateNote,
'bStatus' => intval($status),
'bHash' => md5($address)
);
$sql = 'INSERT INTO '. $this->getTableName()
.' ' . $this->db->sql_build_array('INSERT', $values);
$this->db->sql_transaction('begin');
if (!($dbresult = & $this->db->sql_query($sql))) {
if (!($dbresult = $this->db->sql_query($sql))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
message_die(
GENERAL_ERROR,
'Could not insert bookmark',
'', __LINE__, __FILE__, $sql, $this->db
);
return false;
}
// Get the resultant row ID for the bookmark.
$bId = $this->db->sql_nextid($dbresult);
if (!isset($bId) || !is_int($bId)) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
message_die(
GENERAL_ERROR,
'Could not insert bookmark',
'', __LINE__, __FILE__, $sql, $this->db
);
return false;
}
@ -215,19 +288,54 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
unset($uriparts);
$b2tservice = SemanticScuttle_Service_Factory::get('Bookmark2Tag');
if (!$b2tservice->attachTags($bId, $categories, $fromApi, $extension, false, $fromImport)) {
$aok = !$b2tservice->attachTags(
$bId, $categories, $fromApi, $extension, false, $fromImport
);
if (!$aok) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db);
message_die(
GENERAL_ERROR,
'Could not insert bookmark',
'', __LINE__, __FILE__, $sql, $this->db
);
return false;
}
$this->db->sql_transaction('commit');
// Everything worked out, so return the new bookmark's bId.
return $bId;
}
}//public function addBookmark(..)
function updateBookmark($bId, $address, $title, $description, $privateNote, $status, $categories, $date = NULL, $fromApi = false) {
if (!is_numeric($bId))
/**
* Update an existing bookmark.
*
* @param integer $bId Bookmark ID
* @param string $address Full URL of the bookmark
* @param string $title Bookmark title
* @param string $description Long bookmark description
* @param string $privateNote Private note for the user.
* @param string $status Bookmark visibility:
* 0 - public
* 1 - shared
* 2 - private
* @param array $categories Array of tags
* @param string $date Date when the bookmark has been created
* originally. Used in combination with
* $fromImport. Has to be a strtotime()
* interpretable string.
* @param boolean $fromApi True when api call is responsible.
*
* @return boolean True if all went well, false if not.
*/
public function updateBookmark(
$bId, $address, $title, $description, $privateNote, $status,
$categories, $date = NULL, $fromApi = false
) {
if (!is_numeric($bId)) {
return false;
}
// Get the client's IP address and the date; note that the date is in GMT.
if (getenv('HTTP_CLIENT_IP'))
@ -282,7 +390,13 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return true;
}
function & getBookmarks($start = 0, $perpage = NULL, $user = NULL, $tags = NULL, $terms = NULL, $sortOrder = NULL, $watched = NULL, $startdate = NULL, $enddate = NULL, $hash = NULL) {
public function getBookmarks(
$start = 0, $perpage = NULL, $user = NULL, $tags = NULL,
$terms = NULL, $sortOrder = NULL, $watched = NULL,
$startdate = NULL, $enddate = NULL, $hash = NULL
) {
// Only get the bookmarks that are visible to the current user. Our rules:
// - if the $user is NULL, that means get bookmarks from ALL users, so we need to make
// sure to check the logged-in user's watchlist and get the contacts-only bookmarks from
@ -467,7 +581,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $output;
}
function deleteBookmark($bookmarkid) {
public function deleteBookmark($bookmarkid)
{
$query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. intval($bookmarkid);
$this->db->sql_transaction('begin');
if (!($dbresult = & $this->db->sql_query($query))) {
@ -490,7 +607,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return true;
}
function deleteBookmarksForUser($uId) {
public function deleteBookmarksForUser($uId)
{
$query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE uId = '. intval($uId);
if (!($dbresult = & $this->db->sql_query($query))) {
@ -501,7 +621,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return true;
}
function countOthers($address) {
function countOthers($address)
{
if (!$address) {
return false;
}
@ -532,7 +655,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $output;
}
function normalize($address) {
function normalize($address)
{
// If bookmark address doesn't contain ":", add "http://" to the start as a default protocol
if (strpos($address, ':') === false) {
$address = 'http://'. $address;
@ -546,17 +672,19 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_Service
return $address;
}
function deleteAll() {
function deleteAll()
{
$query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
$this->db->sql_query($query);
}
// Properties
function getTableName() { return $this->tablename; }
function setTableName($value) { $this->tablename = $value; }
}
?>