diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/bookmarkservice.php | 55 | ||||
-rw-r--r-- | services/servicefactory.php | 2 | ||||
-rw-r--r-- | services/tag2tagservice.php | 143 | ||||
-rw-r--r-- | services/tagservice.php | 27 | ||||
-rw-r--r-- | services/userservice.php | 2 |
5 files changed, 215 insertions, 14 deletions
diff --git a/services/bookmarkservice.php b/services/bookmarkservice.php index afc7179..fdb49a0 100644 --- a/services/bookmarkservice.php +++ b/services/bookmarkservice.php @@ -1,6 +1,7 @@ <?php class BookmarkService { var $db; + var $tablename; function & getInstance(& $db) { static $instance; @@ -11,6 +12,7 @@ class BookmarkService { function BookmarkService(& $db) { $this->db = & $db; + $this->tablename = $GLOBALS['tableprefix'] .'bookmarks'; } function _getbookmark($fieldname, $value, $all = false) { @@ -20,7 +22,7 @@ class BookmarkService { $range = ' AND uId = '. $sId; } - $query = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'. $range; + $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'. $range; if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) { message_die(GENERAL_ERROR, 'Could not get bookmark', '', __LINE__, __FILE__, $query, $this->db); @@ -38,7 +40,7 @@ class BookmarkService { if (!is_numeric($bid)) return; - $sql = 'SELECT * FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE bId = '. $this->db->sql_escape($bid); + $sql = 'SELECT * FROM '. $this->getTableName() .' WHERE bId = '. $this->db->sql_escape($bid); if (!($dbresult = & $this->db->sql_query($sql))) message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); @@ -103,9 +105,11 @@ class BookmarkService { // 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, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false) { - $userservice = & ServiceFactory :: getServiceInstance('UserService'); - $sId = $userservice->getCurrentUserId(); + function addBookmark($address, $title, $description, $status, $categories, $date = NULL, $fromApi = false, $fromImport = false, $sId = -1) { + if($sId == -1) { + $userservice = & ServiceFactory :: getServiceInstance('UserService'); + $sId = $userservice->getCurrentUserId(); + } // If bookmark address doesn't contain ":", add "http://" to the start as a default protocol if (strpos($address, ':') === false) { @@ -131,7 +135,7 @@ class BookmarkService { // 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, 'bStatus' => intval($status), 'bHash' => md5($address)); - $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'bookmarks '. $this->db->sql_build_array('INSERT', $values); + $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); $this->db->sql_transaction('begin'); if (!($dbresult = & $this->db->sql_query($sql))) { $this->db->sql_transaction('rollback'); @@ -220,6 +224,7 @@ class BookmarkService { // - if the $user is set and IS the logged-in user, then get all bookmarks. $userservice =& ServiceFactory::getServiceInstance('UserService'); $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tag2tagservice =& ServiceFactory::getServiceInstance('Tag2TagService'); $sId = $userservice->getCurrentUserId(); if ($userservice->isLoggedOn()) { @@ -252,7 +257,7 @@ class BookmarkService { } $query_1 .= 'B.*, U.'. $userservice->getFieldName('username'); - $query_2 = ' FROM '. $userservice->getTableName() .' AS U, '. $GLOBALS['tableprefix'] .'bookmarks AS B'; + $query_2 = ' FROM '. $userservice->getTableName() .' AS U, '. $this->getTableName() .' AS B'; $query_3 = ' WHERE B.uId = U.'. $userservice->getFieldName('primary') . $privacy; if (is_null($watched)) { @@ -295,8 +300,23 @@ class BookmarkService { // Handle the parts of the query that depend on any tags that are present. $query_4 = ''; for ($i = 0; $i < $tagcount; $i ++) { - $query_2 .= ', '. $GLOBALS['tableprefix'] .'tags AS T'. $i; - $query_4 .= ' AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i]) .'" AND T'. $i .'.bId = B.bId'; + $query_2 .= ', '. $tagservice->getTableName() .' AS T'. $i; + $query_4 .= ' AND ('; + + $allLinkedTags = $tag2tagservice->getAllLinkedTags($this->db->sql_escape($tags[$i]), '>', $user); + while (count($allLinkedTags)>1) { + $query_4 .= ' T'. $i .'.tag = "'. array_pop($allLinkedTags) .'"'; + $query_4 .= ' OR'; + } + if(is_array($allLinkedTags)) { + $query_4 .= ' T'. $i .'.tag = "'. array_pop($allLinkedTags) .'"'; + } else { + $query_4 .= ' T'. $i .'.tag = "'. $allLinkedTags .'"'; + } + + + $query_4 .= ') AND T'. $i .'.bId = B.bId'; +//die($query_4); } // Search terms @@ -307,7 +327,7 @@ class BookmarkService { // Search terms in tags as well when none given if (!count($tags)) { - $query_2 .= ' LEFT JOIN '. $GLOBALS['tableprefix'] .'tags AS T ON B.bId = T.bId'; + $query_2 .= ' LEFT JOIN '. $tagservice->getTableName() .' AS T ON B.bId = T.bId'; $dotags = true; } else { $dotags = false; @@ -336,8 +356,8 @@ class BookmarkService { if ($hash) { $query_4 .= ' AND B.bHash = "'. $hash .'"'; } - $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5; +//die($query); if (!($dbresult = & $this->db->sql_query_limit($query, intval($perpage), intval($start)))) { message_die(GENERAL_ERROR, 'Could not get bookmarks', '', __LINE__, __FILE__, $query, $this->db); return false; @@ -412,5 +432,18 @@ class BookmarkService { } return $this->db->sql_fetchfield(0, 0) - 1; } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } + + // Properties + function getTableName() { return $this->tablename; } + function setTableName($value) { $this->tablename = $value; } + } + + + ?> diff --git a/services/servicefactory.php b/services/servicefactory.php index ba2d6d7..9d50841 100644 --- a/services/servicefactory.php +++ b/services/servicefactory.php @@ -30,4 +30,4 @@ class ServiceFactory { return $instances[$name]; } } -?>
\ No newline at end of file +?> diff --git a/services/tag2tagservice.php b/services/tag2tagservice.php new file mode 100644 index 0000000..0b53d64 --- /dev/null +++ b/services/tag2tagservice.php @@ -0,0 +1,143 @@ +<?php +class Tag2TagService { + var $db; + var $tablename; + + function &getInstance(&$db) { + static $instance; + if (!isset($instance)) + $instance =& new Tag2TagService($db); + return $instance; + } + + function Tag2TagService(&$db) { + $this->db =& $db; + $this->tablename = $GLOBALS['tableprefix'] .'tags2tags'; + } + + function addLinkedTags($tag1, $tag2, $relationType, $uId) { + if($tag1 == $tag2) { + return false; + } + $values = array('tag1' => $tag1, 'tag2' => $tag2, 'relationType'=> $relationType, 'uId'=> $uId); + $query = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); +//die($query); + if (!($dbresult =& $this->db->sql_query($query))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not attach tag to tag', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + return true; + } + + function getLinkedTags($tag1, $relationType, $uId) { + // Set up the SQL query. + $query = "SELECT DISTINCT tag2 as 'tag' FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + if($relationType) { + $query.= " AND relationType = '". $relationType ."'"; + } + if($uId) { + $query.= " AND uId = '".$uId."'"; + } + + if (! ($dbresult =& $this->db->sql_query_limit($query, $limit)) ){ + message_die(GENERAL_ERROR, 'Could not get related tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $rowset = $this->db->sql_fetchrowset($dbresult); + $output = array(); + foreach($rowset as $row) { + $output[] = $row['tag']; + } + return $output; + } + + /* TODO: clean the outputs to obtain homogenous ones*/ + function getAllLinkedTags($tag1, $relationType, $uId, $asFlatList=true, $stopList=array()) { + if(in_array($tag1, $stopList)) { + return $tag1; + } + $linkedTags = $this->getLinkedTags($tag1, $relationType, $uId); + if(count($linkedTags) == 0) { + return $tag1; + } else { + $output = array(); + if($asFlatList == true) { + $output[$tag1] = $tag1; + } else { + $output = array('node'=>$tag1); + } + + $stopList[] = $tag1; + foreach($linkedTags as $linkedTag) { + $allLinkedTags = $this->getAllLinkedTags($linkedTag, $relationType, $uId, $asFlatList, $stopList); + if($asFlatList == true) { + if(is_array($allLinkedTags)) { + $output = array_merge($output, $allLinkedTags); + } else { + $output[$allLinkedTags] = $allLinkedTags; + } + } else { + $output[] = $allLinkedTags; + } + } + } + return $output; + } + + function getOrphewTags($relationType, $uId) { + $query = "SELECT DISTINCT tag1 as tag FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 <> ALL"; + $query.= " (SELECT DISTINCT tag2 FROM `". $this->getTableName() ."`"; + $query.= " WHERE relationType = '".$relationType."'"; + $query.= " AND uId = '".$uId."'"; + $query.= ")"; + $query.= " AND uId = '".$uId."'"; + + //die($query); + + if (! ($dbresult =& $this->db->sql_query_limit($query, $limit)) ){ + message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + return $this->db->sql_fetchrowset($dbresult); + } + + function existsLinkedTags($tag1, $tag2, $relationType, $uId) { + $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND tag2 = '".$tag2."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + return $this->db->sql_numrows($dbresult) > 0; + } + + function removeLinkedTags($tag1, $tag2, $relationType, $uId) { + $query = 'DELETE FROM '. $this->getTableName(); + $query.= ' WHERE tag1 = "'. $tag1 .'"'; + $query.= ' AND tag2 = "'. $tag2 .'"'; + $query.= ' AND relationType = "'. $relationType .'"'; + $query.= ' AND uId = "'. $uId .'"'; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not remove tag relation', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } + + // Properties + function getTableName() { return $this->tablename; } + function setTableName($value) { $this->tablename = $value; } +} +?> diff --git a/services/tagservice.php b/services/tagservice.php index 6bfbf15..509e575 100644 --- a/services/tagservice.php +++ b/services/tagservice.php @@ -86,6 +86,25 @@ class TagService { } } + // Create links between tags + foreach($tags as $key => $tag) { + // case ">" + $pieces = explode('>', $tag); + $nbPieces = count($pieces); + if($nbPieces > 1) { + for($i = 0; $i < $nbPieces-1; $i++) { + $bs =& ServiceFactory::getServiceInstance('BookmarkService'); + $tts =& ServiceFactory::getServiceInstance('Tag2TagService'); + + $bookmark = $bs->getBookmark($bookmarkid); + $uId = $bookmark['uId']; + $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '>', $uId); + } + $tags[$key] = $pieces[$nbPieces-1]; // Attach just the last tag to the bookmark + } + + } + // Add the categories to the DB. for ($i = 0; $i < count($tags); $i++) { if ($tags[$i] != '') { @@ -356,8 +375,14 @@ class TagService { return $output; } + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } + + // Properties function getTableName() { return $this->tablename; } function setTableName($value) { $this->tablename = $value; } } -?>
\ No newline at end of file +?> diff --git a/services/userservice.php b/services/userservice.php index 1e7ed46..b585388 100644 --- a/services/userservice.php +++ b/services/userservice.php @@ -20,7 +20,7 @@ class UserService { var $cookiekey; var $cookietime = 1209600; // 2 weeks - function UserService(&$db) { + function UserService(& $db) { $this->db =& $db; $this->tablename = $GLOBALS['tableprefix'] .'users'; $this->sessionkey = $GLOBALS['cookieprefix'] .'-currentuserid'; |