From d5f09b7e4d802cdc2d5f3f876c48ea918c961488 Mon Sep 17 00:00:00 2001 From: cweiske Date: Sat, 3 Oct 2009 21:52:30 +0000 Subject: make the application work again git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@388 b3834d28-1941-0410-a4f8-b48e95affb8f --- src/SemanticScuttle/Service/Bookmark.php | 62 +-- src/SemanticScuttle/Service/Bookmark2Tag.php | 489 ++++++++++++++++++++++ src/SemanticScuttle/Service/Bookmark2tag.php | 478 --------------------- src/SemanticScuttle/Service/Cache.php | 29 +- src/SemanticScuttle/Service/CommonDescription.php | 24 +- src/SemanticScuttle/Service/Factory.php | 20 +- src/SemanticScuttle/Service/SearchHistory.php | 24 +- src/SemanticScuttle/Service/Tag.php | 24 +- src/SemanticScuttle/Service/Tag2Tag.php | 388 +++++++++++++++++ src/SemanticScuttle/Service/Tag2tag.php | 377 ----------------- src/SemanticScuttle/Service/TagCache.php | 54 ++- src/SemanticScuttle/Service/TagStat.php | 31 +- src/SemanticScuttle/Service/Template.php | 23 +- src/SemanticScuttle/Service/User.php | 111 ++--- 14 files changed, 1128 insertions(+), 1006 deletions(-) create mode 100644 src/SemanticScuttle/Service/Bookmark2Tag.php delete mode 100644 src/SemanticScuttle/Service/Bookmark2tag.php create mode 100644 src/SemanticScuttle/Service/Tag2Tag.php delete mode 100644 src/SemanticScuttle/Service/Tag2tag.php (limited to 'src/SemanticScuttle/Service') diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index f119593..6075a0d 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -1,23 +1,33 @@ db = & $db; + public function __construct($db) + { + $this->db = $db; $this->tablename = $GLOBALS['tableprefix'] .'bookmarks'; } function _getbookmark($fieldname, $value, $all = false) { if (!$all) { - $userservice = & ServiceFactory :: getServiceInstance('UserService'); + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); $sId = $userservice->getCurrentUserId(); $range = ' AND uId = '. $sId; } else { @@ -51,7 +61,7 @@ class BookmarkService { if ($row = & $this->db->sql_fetchrow($dbresult)) { if ($include_tags) { - $b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService'); + $b2tservice = SemanticScuttle_Service_Factory :: getServiceInstance('Bookmark2Tag'); $row['tags'] = $b2tservice->getTagsForBookmark($bid); } $output = $row; @@ -118,7 +128,7 @@ class BookmarkService { return false; } - $userservice = & ServiceFactory::getServiceInstance('UserService'); + $userservice = SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getCurrentUser(); //user has to be either admin, or owner @@ -160,7 +170,7 @@ class BookmarkService { // 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) { if($sId == -1) { - $userservice = & ServiceFactory :: getServiceInstance('UserService'); + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); $sId = $userservice->getCurrentUserId(); } @@ -204,7 +214,7 @@ class BookmarkService { $extension = end($uriparts); unset($uriparts); - $b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService'); + $b2tservice = SemanticScuttle_Service_Factory :: getServiceInstance('Bookmark2Tag'); if (!$b2tservice->attachTags($bId, $categories, $fromApi, $extension, false, $fromImport)) { $this->db->sql_transaction('rollback'); message_die(GENERAL_ERROR, 'Could not insert bookmark', '', __LINE__, __FILE__, $sql, $this->db); @@ -260,7 +270,7 @@ class BookmarkService { $extension = end($uriparts); unset($uriparts); - $b2tservice = & ServiceFactory :: getServiceInstance('Bookmark2TagService'); + $b2tservice = SemanticScuttle_Service_Factory :: getServiceInstance('Bookmark2Tag'); if (!$b2tservice->attachTags($bId, $categories, $fromApi, $extension)) { $this->db->sql_transaction('rollback'); message_die(GENERAL_ERROR, 'Could not update bookmark', '', __LINE__, __FILE__, $sql, $this->db); @@ -282,9 +292,9 @@ class BookmarkService { // bookmarks; otherwise, just get the public bookmarks. // - if the $user is set and IS the logged-in user, then get all bookmarks. - $userservice =& ServiceFactory::getServiceInstance('UserService'); - $b2tservice =& ServiceFactory::getServiceInstance('Bookmark2TagService'); - $tag2tagservice =& ServiceFactory::getServiceInstance('Tag2TagService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); + $b2tservice =SemanticScuttle_Service_Factory::getServiceInstance('Bookmark2Tag'); + $tag2tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag2Tag'); $sId = $userservice->getCurrentUserId(); if ($userservice->isLoggedOn()) { @@ -480,15 +490,15 @@ class BookmarkService { return true; } - function deleteBookmarksForUser($uId) { - $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE uId = '. intval($uId); - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; + function deleteBookmarksForUser($uId) { + $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks WHERE uId = '. intval($uId); + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; } function countOthers($address) { @@ -496,7 +506,7 @@ class BookmarkService { return false; } - $userservice = & ServiceFactory :: getServiceInstance('UserService'); + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); $sId = $userservice->getCurrentUserId(); if ($userservice->isLoggedOn()) { diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php new file mode 100644 index 0000000..bd3f3af --- /dev/null +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -0,0 +1,489 @@ +db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'bookmarks2tags'; + } + + function isNotSystemTag($var) { + if (utf8_substr($var, 0, 7) == 'system:') + return false; + else + return true; + } + + function attachTags($bookmarkid, $tags, $fromApi = false, $extension = NULL, $replace = true, $fromImport = false) { + // Make sure that categories is an array of trimmed strings, and that if the categories are + // coming in from an API call to add a bookmark, that underscores are converted into strings. + + if (!is_array($tags)) { + $tags = trim($tags); + if ($tags != '') { + if (substr($tags, -1) == ',') { + $tags = substr($tags, 0, -1); + } + if ($fromApi) { + $tags = explode(' ', $tags); + } else { + $tags = explode(',', $tags); + } + } else { + $tags = null; + } + } + + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); + $tags = $tagservice->normalize($tags); + + + $tags_count = is_array($tags)?count($tags):0; + + for ($i = 0; $i < $tags_count; $i++) { + $tags[$i] = trim(strtolower($tags[$i])); + if ($fromApi) { + include_once 'SemanticScuttle/functions.php'; + $tags[$i] = convertTag($tags[$i], 'in'); + } + } + + if ($tags_count > 0) { + // Remove system tags + $tags = array_filter($tags, array($this, "isNotSystemTag")); + + // Eliminate any duplicate categories + $temp = array_unique($tags); + $tags = array_values($temp); + } else { + // Unfiled + $tags[] = 'system:unfiled'; + } + + // Media and file types + if (!is_null($extension)) { + include_once 'SemanticScuttle/functions.php'; + + if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) { + $tags[] = 'system:filetype:'. $extension; + $tags[] = 'system:media:'. array_shift($keys); + } + } + + // Imported + if ($fromImport) { + $tags[] = 'system:imported'; + } + + $this->db->sql_transaction('begin'); + + if ($replace) { + if (!$this->deleteTagsForBookmark($bookmarkid)){ + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not attach tags (deleting old ones failed)', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + } + + $bs =SemanticScuttle_Service_Factory::getServiceInstance('Bookmark'); + $tts =SemanticScuttle_Service_Factory::getServiceInstance('Tag2Tag'); + + // Create links between tags + foreach($tags as $key => $tag) { + if(strpos($tag, '=')) { + // case "=" + $pieces = explode('=', $tag); + $nbPieces = count($pieces); + if($nbPieces > 1) { + for($i = 0; $i < $nbPieces-1; $i++) { + $bookmark = $bs->getBookmark($bookmarkid); + $uId = $bookmark['uId']; + $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId); + } + $tags[$key] = $pieces[0]; // Attach just the last tag to the bookmark + } + } else { + // case ">" + $pieces = explode('>', $tag); + $nbPieces = count($pieces); + if($nbPieces > 1) { + for($i = 0; $i < $nbPieces-1; $i++) { + $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] != '') { + $values = array( + 'bId' => intval($bookmarkid), + 'tag' => $tags[$i] + ); + + if (!$this->hasTag($bookmarkid, $tags[$i])) { + $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); + if (!($dbresult =& $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + } + } + } + $this->db->sql_transaction('commit'); + return true; + } + + function deleteTag($uId, $tag) { + $bs =SemanticScuttle_Service_Factory::getServiceInstance('Bookmark'); + + $query = 'DELETE FROM '. $this->getTableName(); + $query.= ' USING '. $this->getTableName() .', '. $bs->getTableName(); + $query.= ' WHERE '. $this->getTableName() .'.bId = '. $bs->getTableName() .'.bId'; + $query.= ' AND '. $bs->getTableName() .'.uId = '. $uId; + $query.= ' AND '. $this->getTableName() .'.tag = "'. $this->db->sql_escape($tag) .'"'; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + function deleteTagsForBookmark($bookmarkid) { + if (!is_int($bookmarkid)) { + message_die(GENERAL_ERROR, 'Could not delete tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query); + return false; + } + + $query = 'DELETE FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid); + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + /* Allow deletion in admin page */ + function deleteTagsForUser($uId) { + $qmask = 'DELETE FROM %s USING %s, %s WHERE %s.bId = %s.bId AND %s.uId = %d'; + $query = sprintf($qmask, + $this->getTableName(), + $this->getTableName(), + $GLOBALS['tableprefix'].'bookmarks', + $this->getTableName(), + $GLOBALS['tableprefix'].'bookmarks', + $GLOBALS['tableprefix'].'bookmarks', + $uId); + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + function &getTagsForBookmark($bookmarkid) { + if (!is_numeric($bookmarkid)) { + message_die(GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query); + return false; + } + + $query = 'SELECT tag FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND LEFT(tag, 7) <> "system:" ORDER BY id ASC'; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $tags = array(); + while ($row =& $this->db->sql_fetchrow($dbresult)) { + $tags[] = $row['tag']; + } + $this->db->sql_freeresult($dbresult); + return $tags; + } + + function &getTags($userid = NULL) { + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); + $logged_on_user = $userservice->getCurrentUserId(); + + $query = 'SELECT T.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B INNER JOIN '. $userservice->getTableName() .' AS U ON B.uId = U.'. $userservice->getFieldName('primary') .' INNER JOIN '. $GLOBALS['tableprefix'] .'bookmarks2tags AS T ON B.bId = T.bId'; + + $conditions = array(); + if (!is_null($userid)) { + $conditions['U.'. $userservice->getFieldName('primary')] = intval($userid); + if ($logged_on_user != $userid) + $conditions['B.bStatus'] = 0; + } else { + $conditions['B.bStatus'] = 0; + } + + $query .= ' WHERE '. $this->db->sql_build_array('SELECT', $conditions) .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + + + // Returns the tags related to the specified tags; i.e. attached to the same bookmarks + function &getRelatedTags($tags, $for_user = NULL, $logged_on_user = NULL, $limit = 10) { + $conditions = array(); + // Only count the tags that are visible to the current user. + if ($for_user != $logged_on_user || is_null($for_user)) + $conditions['B.bStatus'] = 0; + + if (!is_null($for_user)) + $conditions['B.uId'] = $for_user; + + // Set up the tags, if need be. + if (is_numeric($tags)) + $tags = NULL; + if (!is_array($tags) and !is_null($tags)) + $tags = explode('+', trim($tags)); + + $tagcount = count($tags); + for ($i = 0; $i < $tagcount; $i++) { + $tags[$i] = trim($tags[$i]); + } + + // Set up the SQL query. + $query_1 = 'SELECT DISTINCTROW T0.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B, '. $this->getTableName() .' AS T0'; + $query_2 = ''; + $query_3 = ' WHERE B.bId = T0.bId '; + if (count($conditions) > 0) + $query_4 = ' AND '. $this->db->sql_build_array('SELECT', $conditions); + else + $query_4 = ''; + // Handle the parts of the query that depend on any tags that are present. + for ($i = 1; $i <= $tagcount; $i++) { + $query_2 .= ', '. $this->getTableName() .' AS T'. $i; + $query_4 .= ' AND T'. $i .'.bId = B.bId AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i - 1]) .'" AND T0.tag <> "'. $this->db->sql_escape($tags[$i - 1]) .'"'; + } + $query_5 = ' AND LEFT(T0.tag, 7) <> "system:" GROUP BY T0.tag ORDER BY bCount DESC, T0.tag'; + $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5; + + 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; + } + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + + // Returns the most popular tags used for a particular bookmark hash + function &getRelatedTagsByHash($hash, $limit = 20) { + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); + $sId = $userservice->getCurrentUserId(); + // Logged in + if ($userservice->isLoggedOn()) { + $arrWatch = $userservice->getWatchList($sId); + // From public bookmarks or user's own + $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')'; + // From shared bookmarks in watchlist + foreach ($arrWatch as $w) { + $privacy .= ' OR (B.uId = '. $w .' AND B.bStatus = 1)'; + } + $privacy .= ') '; + // Not logged in + } else { + $privacy = ' AND B.bStatus = 0 '; + } + + $query = 'SELECT T.tag, COUNT(T.tag) AS bCount FROM '.$GLOBALS['tableprefix'].'bookmarks AS B LEFT JOIN '.$GLOBALS['tableprefix'].'bookmarks2tags AS T ON B.bId = T.bId WHERE B.bHash = "'. $hash .'" '. $privacy .'AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC'; + + if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { + message_die(GENERAL_ERROR, 'Could not get related tags for this hash', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + + function &getAdminTags($limit = 30, $logged_on_user = NULL, $days = NULL) { + // look for admin ids + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); + $adminIds = $userservice->getAdminIds(); + + // ask for their tags + return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); + } + + function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { + // look for contact ids + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); + $contacts = $userservice->getWatchlist($user); + + // add the user (to show him/her also his/her tags) + if(!is_null($logged_on_user)) { + $contacts[] = $logged_on_user; + } + + // ask for their tags + return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); + } + + // $users can be {NULL, an id, an array of id} + function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) { + // Only count the tags that are visible to the current user. + if (($user != $logged_on_user) || is_null($user) || ($user === false)) + $privacy = ' AND B.bStatus = 0'; + else + $privacy = ''; + + if (is_null($days) || !is_int($days)) + $span = ''; + else + $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"'; + + $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE '; + if (is_null($user) || ($user === false)) { + $query .= 'B.bId = T.bId AND B.bStatus = 0'; + } elseif(is_array($user)) { + $query .= ' (1 = 0'; //tricks + foreach($user as $u) { + $query .= ' OR B.uId = '. $this->db->sql_escape($u) .' AND B.bId = T.bId'; + } + $query .= ' )'; + } else { + $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy; + } + $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; + + if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { + message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + + function hasTag($bookmarkid, $tag) { + $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"'; + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not find tag', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + if ($row['tCount'] > 0) { + $output = true; + } + } + $output = false; + $this->db->sql_freeresult($dbresult); + return $output; + } + + function renameTag($userid, $old, $new, $fromApi = false) { + $bookmarkservice =SemanticScuttle_Service_Factory::getServiceInstance('Bookmark'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); + + if (is_null($userid) || is_null($old) || is_null($new)) + return false; + + // Find bookmarks with old tag + $bookmarksInfo =& $bookmarkservice->getBookmarks(0, NULL, $userid, $old); + $bookmarks =& $bookmarksInfo['bookmarks']; + + // Delete old tag + $this->deleteTag($userid, $old); + + // Attach new tags + $new = $tagservice->normalize($new); + + foreach(array_keys($bookmarks) as $key) { + $row =& $bookmarks[$key]; + $this->attachTags($row['bId'], $new, $fromApi, NULL, false); + } + + return true; + } + + function &tagCloud($tags = NULL, $steps = 5, $sizemin = 90, $sizemax = 225, $sortOrder = NULL) { + + if (is_null($tags) || count($tags) < 1) { + $output = false; + return $output; + } + + $min = $tags[count($tags) - 1]['bCount']; + $max = $tags[0]['bCount']; + + for ($i = 1; $i <= $steps; $i++) { + $delta = ($max - $min) / (2 * $steps - $i); + $limit[$i] = $i * $delta + $min; + } + $sizestep = ($sizemax - $sizemin) / $steps; + foreach ($tags as $row) { + $next = false; + for ($i = 1; $i <= $steps; $i++) { + if (!$next && $row['bCount'] <= $limit[$i]) { + $size = $sizestep * ($i - 1) + $sizemin; + $next = true; + } + } + $tempArray = array('size' => $size .'%'); + $row = array_merge($row, $tempArray); + $output[] = $row; + } + + if ($sortOrder == 'alphabet_asc') { + usort($output, create_function('$a,$b','return strcasecmp(utf8_deaccent($a["tag"]), utf8_deaccent($b["tag"]));')); + } + + 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; } +} +?> diff --git a/src/SemanticScuttle/Service/Bookmark2tag.php b/src/SemanticScuttle/Service/Bookmark2tag.php deleted file mode 100644 index 918fb5b..0000000 --- a/src/SemanticScuttle/Service/Bookmark2tag.php +++ /dev/null @@ -1,478 +0,0 @@ -db =& $db; - $this->tablename = $GLOBALS['tableprefix'] .'bookmarks2tags'; - } - - function isNotSystemTag($var) { - if (utf8_substr($var, 0, 7) == 'system:') - return false; - else - return true; - } - - function attachTags($bookmarkid, $tags, $fromApi = false, $extension = NULL, $replace = true, $fromImport = false) { - // Make sure that categories is an array of trimmed strings, and that if the categories are - // coming in from an API call to add a bookmark, that underscores are converted into strings. - - if (!is_array($tags)) { - $tags = trim($tags); - if ($tags != '') { - if (substr($tags, -1) == ',') { - $tags = substr($tags, 0, -1); - } - if ($fromApi) { - $tags = explode(' ', $tags); - } else { - $tags = explode(',', $tags); - } - } else { - $tags = null; - } - } - - $tagservice =& ServiceFactory::getServiceInstance('TagService'); - $tags = $tagservice->normalize($tags); - - - $tags_count = is_array($tags)?count($tags):0; - - for ($i = 0; $i < $tags_count; $i++) { - $tags[$i] = trim(strtolower($tags[$i])); - if ($fromApi) { - include_once(dirname(__FILE__) .'/../functions.inc.php'); - $tags[$i] = convertTag($tags[$i], 'in'); - } - } - - if ($tags_count > 0) { - // Remove system tags - $tags = array_filter($tags, array($this, "isNotSystemTag")); - - // Eliminate any duplicate categories - $temp = array_unique($tags); - $tags = array_values($temp); - } else { - // Unfiled - $tags[] = 'system:unfiled'; - } - - // Media and file types - if (!is_null($extension)) { - include_once(dirname(__FILE__) .'/../functions.inc.php'); - - if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) { - $tags[] = 'system:filetype:'. $extension; - $tags[] = 'system:media:'. array_shift($keys); - } - } - - // Imported - if ($fromImport) { - $tags[] = 'system:imported'; - } - - $this->db->sql_transaction('begin'); - - if ($replace) { - if (!$this->deleteTagsForBookmark($bookmarkid)){ - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not attach tags (deleting old ones failed)', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - } - - $bs =& ServiceFactory::getServiceInstance('BookmarkService'); - $tts =& ServiceFactory::getServiceInstance('Tag2TagService'); - - // Create links between tags - foreach($tags as $key => $tag) { - if(strpos($tag, '=')) { - // case "=" - $pieces = explode('=', $tag); - $nbPieces = count($pieces); - if($nbPieces > 1) { - for($i = 0; $i < $nbPieces-1; $i++) { - $bookmark = $bs->getBookmark($bookmarkid); - $uId = $bookmark['uId']; - $tts->addLinkedTags($pieces[$i], $pieces[$i+1], '=', $uId); - } - $tags[$key] = $pieces[0]; // Attach just the last tag to the bookmark - } - } else { - // case ">" - $pieces = explode('>', $tag); - $nbPieces = count($pieces); - if($nbPieces > 1) { - for($i = 0; $i < $nbPieces-1; $i++) { - $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] != '') { - $values = array( - 'bId' => intval($bookmarkid), - 'tag' => $tags[$i] - ); - - if (!$this->hasTag($bookmarkid, $tags[$i])) { - $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); - if (!($dbresult =& $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - } - } - } - $this->db->sql_transaction('commit'); - return true; - } - - function deleteTag($uId, $tag) { - $bs =& ServiceFactory::getServiceInstance('BookmarkService'); - - $query = 'DELETE FROM '. $this->getTableName(); - $query.= ' USING '. $this->getTableName() .', '. $bs->getTableName(); - $query.= ' WHERE '. $this->getTableName() .'.bId = '. $bs->getTableName() .'.bId'; - $query.= ' AND '. $bs->getTableName() .'.uId = '. $uId; - $query.= ' AND '. $this->getTableName() .'.tag = "'. $this->db->sql_escape($tag) .'"'; - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } - - function deleteTagsForBookmark($bookmarkid) { - if (!is_int($bookmarkid)) { - message_die(GENERAL_ERROR, 'Could not delete tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query); - return false; - } - - $query = 'DELETE FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid); - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } - - /* Allow deletion in admin page */ - function deleteTagsForUser($uId) { - $qmask = 'DELETE FROM %s USING %s, %s WHERE %s.bId = %s.bId AND %s.uId = %d'; - $query = sprintf($qmask, - $this->getTableName(), - $this->getTableName(), - $GLOBALS['tableprefix'].'bookmarks', - $this->getTableName(), - $GLOBALS['tableprefix'].'bookmarks', - $GLOBALS['tableprefix'].'bookmarks', - $uId); - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } - - function &getTagsForBookmark($bookmarkid) { - if (!is_numeric($bookmarkid)) { - message_die(GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query); - return false; - } - - $query = 'SELECT tag FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND LEFT(tag, 7) <> "system:" ORDER BY id ASC'; - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $tags = array(); - while ($row =& $this->db->sql_fetchrow($dbresult)) { - $tags[] = $row['tag']; - } - $this->db->sql_freeresult($dbresult); - return $tags; - } - - function &getTags($userid = NULL) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); - $logged_on_user = $userservice->getCurrentUserId(); - - $query = 'SELECT T.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B INNER JOIN '. $userservice->getTableName() .' AS U ON B.uId = U.'. $userservice->getFieldName('primary') .' INNER JOIN '. $GLOBALS['tableprefix'] .'bookmarks2tags AS T ON B.bId = T.bId'; - - $conditions = array(); - if (!is_null($userid)) { - $conditions['U.'. $userservice->getFieldName('primary')] = intval($userid); - if ($logged_on_user != $userid) - $conditions['B.bStatus'] = 0; - } else { - $conditions['B.bStatus'] = 0; - } - - $query .= ' WHERE '. $this->db->sql_build_array('SELECT', $conditions) .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - - - // Returns the tags related to the specified tags; i.e. attached to the same bookmarks - function &getRelatedTags($tags, $for_user = NULL, $logged_on_user = NULL, $limit = 10) { - $conditions = array(); - // Only count the tags that are visible to the current user. - if ($for_user != $logged_on_user || is_null($for_user)) - $conditions['B.bStatus'] = 0; - - if (!is_null($for_user)) - $conditions['B.uId'] = $for_user; - - // Set up the tags, if need be. - if (is_numeric($tags)) - $tags = NULL; - if (!is_array($tags) and !is_null($tags)) - $tags = explode('+', trim($tags)); - - $tagcount = count($tags); - for ($i = 0; $i < $tagcount; $i++) { - $tags[$i] = trim($tags[$i]); - } - - // Set up the SQL query. - $query_1 = 'SELECT DISTINCTROW T0.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B, '. $this->getTableName() .' AS T0'; - $query_2 = ''; - $query_3 = ' WHERE B.bId = T0.bId '; - if (count($conditions) > 0) - $query_4 = ' AND '. $this->db->sql_build_array('SELECT', $conditions); - else - $query_4 = ''; - // Handle the parts of the query that depend on any tags that are present. - for ($i = 1; $i <= $tagcount; $i++) { - $query_2 .= ', '. $this->getTableName() .' AS T'. $i; - $query_4 .= ' AND T'. $i .'.bId = B.bId AND T'. $i .'.tag = "'. $this->db->sql_escape($tags[$i - 1]) .'" AND T0.tag <> "'. $this->db->sql_escape($tags[$i - 1]) .'"'; - } - $query_5 = ' AND LEFT(T0.tag, 7) <> "system:" GROUP BY T0.tag ORDER BY bCount DESC, T0.tag'; - $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5; - - 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; - } - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - - // Returns the most popular tags used for a particular bookmark hash - function &getRelatedTagsByHash($hash, $limit = 20) { - $userservice = & ServiceFactory :: getServiceInstance('UserService'); - $sId = $userservice->getCurrentUserId(); - // Logged in - if ($userservice->isLoggedOn()) { - $arrWatch = $userservice->getWatchList($sId); - // From public bookmarks or user's own - $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')'; - // From shared bookmarks in watchlist - foreach ($arrWatch as $w) { - $privacy .= ' OR (B.uId = '. $w .' AND B.bStatus = 1)'; - } - $privacy .= ') '; - // Not logged in - } else { - $privacy = ' AND B.bStatus = 0 '; - } - - $query = 'SELECT T.tag, COUNT(T.tag) AS bCount FROM '.$GLOBALS['tableprefix'].'bookmarks AS B LEFT JOIN '.$GLOBALS['tableprefix'].'bookmarks2tags AS T ON B.bId = T.bId WHERE B.bHash = "'. $hash .'" '. $privacy .'AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC'; - - if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { - message_die(GENERAL_ERROR, 'Could not get related tags for this hash', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - - function &getAdminTags($limit = 30, $logged_on_user = NULL, $days = NULL) { - // look for admin ids - $userservice = & ServiceFactory :: getServiceInstance('UserService'); - $adminIds = $userservice->getAdminIds(); - - // ask for their tags - return $this->getPopularTags($adminIds, $limit, $logged_on_user, $days); - } - - function &getContactTags($user, $limit = 30, $logged_on_user = NULL, $days = NULL) { - // look for contact ids - $userservice = & ServiceFactory :: getServiceInstance('UserService'); - $contacts = $userservice->getWatchlist($user); - - // add the user (to show him/her also his/her tags) - if(!is_null($logged_on_user)) { - $contacts[] = $logged_on_user; - } - - // ask for their tags - return $this->getPopularTags($contacts, $limit, $logged_on_user, $days); - } - - // $users can be {NULL, an id, an array of id} - function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) { - // Only count the tags that are visible to the current user. - if (($user != $logged_on_user) || is_null($user) || ($user === false)) - $privacy = ' AND B.bStatus = 0'; - else - $privacy = ''; - - if (is_null($days) || !is_int($days)) - $span = ''; - else - $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"'; - - $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE '; - if (is_null($user) || ($user === false)) { - $query .= 'B.bId = T.bId AND B.bStatus = 0'; - } elseif(is_array($user)) { - $query .= ' (1 = 0'; //tricks - foreach($user as $u) { - $query .= ' OR B.uId = '. $this->db->sql_escape($u) .' AND B.bId = T.bId'; - } - $query .= ' )'; - } else { - $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy; - } - $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag'; - - if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) { - message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - - function hasTag($bookmarkid, $tag) { - $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"'; - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not find tag', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - if ($row['tCount'] > 0) { - $output = true; - } - } - $output = false; - $this->db->sql_freeresult($dbresult); - return $output; - } - - function renameTag($userid, $old, $new, $fromApi = false) { - $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService'); - $tagservice =& ServiceFactory::getServiceInstance('TagService'); - - if (is_null($userid) || is_null($old) || is_null($new)) - return false; - - // Find bookmarks with old tag - $bookmarksInfo =& $bookmarkservice->getBookmarks(0, NULL, $userid, $old); - $bookmarks =& $bookmarksInfo['bookmarks']; - - // Delete old tag - $this->deleteTag($userid, $old); - - // Attach new tags - $new = $tagservice->normalize($new); - - foreach(array_keys($bookmarks) as $key) { - $row =& $bookmarks[$key]; - $this->attachTags($row['bId'], $new, $fromApi, NULL, false); - } - - return true; - } - - function &tagCloud($tags = NULL, $steps = 5, $sizemin = 90, $sizemax = 225, $sortOrder = NULL) { - - if (is_null($tags) || count($tags) < 1) { - $output = false; - return $output; - } - - $min = $tags[count($tags) - 1]['bCount']; - $max = $tags[0]['bCount']; - - for ($i = 1; $i <= $steps; $i++) { - $delta = ($max - $min) / (2 * $steps - $i); - $limit[$i] = $i * $delta + $min; - } - $sizestep = ($sizemax - $sizemin) / $steps; - foreach ($tags as $row) { - $next = false; - for ($i = 1; $i <= $steps; $i++) { - if (!$next && $row['bCount'] <= $limit[$i]) { - $size = $sizestep * ($i - 1) + $sizemin; - $next = true; - } - } - $tempArray = array('size' => $size .'%'); - $row = array_merge($row, $tempArray); - $output[] = $row; - } - - if ($sortOrder == 'alphabet_asc') { - usort($output, create_function('$a,$b','return strcasecmp(utf8_deaccent($a["tag"]), utf8_deaccent($b["tag"]));')); - } - - 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; } -} -?> diff --git a/src/SemanticScuttle/Service/Cache.php b/src/SemanticScuttle/Service/Cache.php index fe66d38..5ca2843 100644 --- a/src/SemanticScuttle/Service/Cache.php +++ b/src/SemanticScuttle/Service/Cache.php @@ -1,18 +1,27 @@ basedir = $GLOBALS['dir_cache']; } diff --git a/src/SemanticScuttle/Service/CommonDescription.php b/src/SemanticScuttle/Service/CommonDescription.php index 86e0c0f..ed1ffdd 100644 --- a/src/SemanticScuttle/Service/CommonDescription.php +++ b/src/SemanticScuttle/Service/CommonDescription.php @@ -1,17 +1,27 @@ db =& $db; + public function __construct($db) + { + $this->db = $db; $this->tablename = $GLOBALS['tableprefix'] .'commondescription'; } diff --git a/src/SemanticScuttle/Service/Factory.php b/src/SemanticScuttle/Service/Factory.php index b5215e3..b4ba28e 100644 --- a/src/SemanticScuttle/Service/Factory.php +++ b/src/SemanticScuttle/Service/Factory.php @@ -1,16 +1,19 @@ sql_connect($dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist); if(!$db->db_connect_id) { @@ -25,12 +28,15 @@ class ServiceFactory { } if (!class_exists($name)) { if (!isset($servicedir)) { - $servicedir = dirname(__FILE__) .'/'; + $servicedir = 'SemanticScuttle/Service/'; } - require_once($servicedir . strtolower($name) . '.php'); + require_once $servicedir . $name . '.php'; } - $instances[$name] = call_user_func(array($name, 'getInstance'), $db); + $instances[$name] = call_user_func( + array('SemanticScuttle_Service_' . $name, 'getInstance'), + $db + ); } return $instances[$name]; } diff --git a/src/SemanticScuttle/Service/SearchHistory.php b/src/SemanticScuttle/Service/SearchHistory.php index 91457e8..7cffa83 100644 --- a/src/SemanticScuttle/Service/SearchHistory.php +++ b/src/SemanticScuttle/Service/SearchHistory.php @@ -1,18 +1,28 @@ db =& $db; + public function __construct($db) + { + $this->db = $db; $this->tablename = $GLOBALS['tableprefix'] .'searchhistory'; if(isset($GLOBALS['sizeSearchHistory'])) { $this->sizeSearchHistory = $GLOBALS['sizeSearchHistory']; diff --git a/src/SemanticScuttle/Service/Tag.php b/src/SemanticScuttle/Service/Tag.php index fc44a99..2a70948 100644 --- a/src/SemanticScuttle/Service/Tag.php +++ b/src/SemanticScuttle/Service/Tag.php @@ -1,17 +1,27 @@ db =& $db; + public function __construct($db) + { + $this->db = $db; $this->tablename = $GLOBALS['tableprefix'] .'tags'; } diff --git a/src/SemanticScuttle/Service/Tag2Tag.php b/src/SemanticScuttle/Service/Tag2Tag.php new file mode 100644 index 0000000..b209d60 --- /dev/null +++ b/src/SemanticScuttle/Service/Tag2Tag.php @@ -0,0 +1,388 @@ +db =& $db; + $this->tablename = $GLOBALS['tableprefix'] .'tags2tags'; + } + + function addLinkedTags($tag1, $tag2, $relationType, $uId) { + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); + $tag1 = $tagservice->normalize($tag1); + $tag2 = $tagservice->normalize($tag2); + + if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 + || ($relationType != ">" && $relationType != "=") + || !is_numeric($uId) || $uId<=0 + || ($this->existsLinkedTags($tag1, $tag2, $relationType, $uId))) { + 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'); + + // Update stats and cache + $this->update($tag1, $tag2, $relationType, $uId); + + return true; + } + + // Return linked tags just for admin users + function getAdminLinkedTags($tag, $relationType, $inverseRelation = false, $stopList = array()) { + // look for admin ids + $userservice = SemanticScuttle_Service_Factory :: getServiceInstance('User'); + $adminIds = $userservice->getAdminIds(); + + //ask for their linked tags + return $this->getLinkedTags($tag, $relationType, $adminIds, $inverseRelation, $stopList); + } + + // Return the target linked tags. If inverseRelation is true, return the source linked tags. + function getLinkedTags($tag, $relationType, $uId = null, $inverseRelation = false, $stopList = array()) { + // Set up the SQL query. + if($inverseRelation) { + $queriedTag = "tag1"; + $givenTag = "tag2"; + } else { + $queriedTag = "tag2"; + $givenTag = "tag1"; + } + + $query = "SELECT DISTINCT ". $queriedTag ." as 'tag'"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE 1=1"; + if($tag !=null) { + $query.= " AND ". $givenTag ." = '". $tag ."'"; + } + if($relationType) { + $query.= " AND relationType = '". $relationType ."'"; + } + if(is_array($uId)) { + $query.= " AND ( 1=0 "; //tricks always false + foreach($uId as $u) { + $query.= " OR uId = '".$u."'"; + } + $query.= " ) "; + } elseif($uId != null) { + $query.= " AND uId = '".$uId."'"; + } + //die($query); + if (! ($dbresult =& $this->db->sql_query($query)) ){ + 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) { + if(!in_array($row['tag'], $stopList)) { + $output[] = $row['tag']; + } + } + + //bijective case for '=' + if($relationType == '=' && $inverseRelation == false) { + //$stopList[] = $tag; + $bijectiveOutput = $this->getLinkedTags($tag, $relationType, $uId, true, $stopList); + $output = array_merge($output, $bijectiveOutput); + //$output = array_unique($output); // remove duplication + } + + $this->db->sql_freeresult($dbresult); + return $output; + } + + /* + * Returns all linked tags (all descendants if relation is >, + * all synonyms if relation is = ) + * $stopList allows to avoid cycle (a > b > a) between tags + */ + function getAllLinkedTags($tag1, $relationType, $uId, $stopList=array()) { + if(in_array($tag1, $stopList) || $tag1 == '') { + return array(); + } + + // try to find data in cache + $tcs = SemanticScuttle_Service_Factory::getServiceInstance('TagCache'); + if(count($stopList) == 0) { + $activatedCache = true; + } else { + $activatedCache = false; + } + + // look for existing links + $stopList[] = $tag1; + $linkedTags = $this->getLinkedTags($tag1, $relationType, $uId, false, $stopList); + if($relationType != '=') { + $linkedTags = array_merge($linkedTags, $this->getLinkedTags($tag1, '=', $uId, false, $stopList)); + } + + if(count($linkedTags) == 0) { + return array(); + + } else { + // use cache if possible + if($activatedCache) { + if($relationType == '>') { + $output = $tcs->getChildren($tag1, $uId); + } elseif($relationType == '=') { + $output = $tcs->getSynonyms($tag1, $uId); + } + if(count($output)>0) { + return $output; + } + } + + // else compute the links + $output = array(); + + foreach($linkedTags as $linkedTag) { + $allLinkedTags = $this->getAllLinkedTags($linkedTag, $relationType, $uId, $stopList); + $output[] = $linkedTag; + if(is_array($allLinkedTags)) { + $output = array_merge($output, $allLinkedTags); + } else { + $output[] = $allLinkedTags; + } + } + + // and save in cache + if($activatedCache == true && $uId>0) { + $tcs->updateTag($tag1, $relationType, $output, $uId); + } + + //$output = array_unique($output); // remove duplication + return $output; + + } + } + + function getOrphewTags($relationType, $uId = 0, $limit = null, $orderBy = null) { + $query = "SELECT DISTINCT tts.tag1 as tag"; + $query.= " FROM `". $this->getTableName() ."` tts"; + if($orderBy != null) { + $tsts =SemanticScuttle_Service_Factory::getServiceInstance('TagStat'); + $query.= ", ".$tsts->getTableName() ." tsts"; + } + $query.= " WHERE tts.tag1 <> ALL"; + $query.= " (SELECT DISTINCT tag2 FROM `". $this->getTableName() ."`"; + $query.= " WHERE relationType = '".$relationType."'"; + if($uId > 0) { + $query.= " AND uId = '".$uId."'"; + } + $query.= ")"; + if($uId > 0) { + $query.= " AND tts.uId = '".$uId."'"; + } + + switch($orderBy) { + case "nb": + $query.= " AND tts.tag1 = tsts.tag1"; + $query.= " AND tsts.relationType = '".$relationType."'"; + if($uId > 0) { + $query.= " AND tsts.uId = ".$uId; + } + $query.= " ORDER BY tsts.nb DESC"; + break; + case "depth": // by nb of descendants + $query.= " AND tts.tag1 = tsts.tag1"; + $query.= " AND tsts.relationType = '".$relationType."'"; + if($uId > 0) { + $query.= " AND tsts.uId = ".$uId; + } + $query.= " ORDER BY tsts.depth DESC"; + break; + case "nbupdate": + $query.= " AND tts.tag1 = tsts.tag1"; + $query.= " AND tsts.relationType = '".$relationType."'"; + if($uId > 0) { + $query.= " AND tsts.uId = ".$uId; + } + $query.= " ORDER BY tsts.nbupdate DESC"; + break; + } + + if($limit != null) { + $query.= " LIMIT 0,".$limit; + } + + if (! ($dbresult =& $this->db->sql_query($query)) ){ + message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + + function getMenuTags($uId) { + if(strlen($GLOBALS['menuTag']) < 1) { + return array(); + } else { + // we don't use the getAllLinkedTags function in order to improve performance + $query = "SELECT tag2 as 'tag', COUNT(tag2) as 'count'"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '".$GLOBALS['menuTag']."'"; + $query.= " AND relationType = '>'"; + if($uId > 0) { + $query.= " AND uId = '".$uId."'"; + } + $query.= " GROUP BY tag2"; + $query.= " ORDER BY count DESC"; + $query.= " LIMIT 0, ".$GLOBALS['maxSizeMenuBlock']; + + if (! ($dbresult =& $this->db->sql_query($query)) ){ + message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $output = $this->db->sql_fetchrowset($dbresult); + $this->db->sql_freeresult($dbresult); + return $output; + } + } + + + function existsLinkedTags($tag1, $tag2, $relationType, $uId) { + + //$tag1 = mysql_real_escape_string($tag1); + //$tag2 = mysql_real_escape_string($tag2); + + $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."'"; + + //echo($query."
\n"); + + return $this->db->sql_numrows($this->db->sql_query($query)) > 0; + } + + function getLinks($uId) { + $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`"; + $query.= " WHERE 1=1"; + if($uId > 0) { + $query.= " AND uId = '".$uId."'"; + } + + return $this->db->sql_fetchrowset($this->db->sql_query($query)); + } + + function removeLinkedTags($tag1, $tag2, $relationType, $uId) { + if(($tag1 != '' && $tag1 == $tag2) || + ($relationType != ">" && $relationType != "=" && $relationType != "") || + ($tag1 == '' && $tag2 == '')) { + return false; + } + $query = 'DELETE FROM '. $this->getTableName(); + $query.= ' WHERE 1=1'; + $query.= strlen($tag1)>0 ? ' AND tag1 = "'. $tag1 .'"' : ''; + $query.= strlen($tag2)>0 ? ' AND tag2 = "'. $tag2 .'"' : ''; + $query.= strlen($relationType)>0 ? ' AND relationType = "'. $relationType .'"' : ''; + $query.= strlen($uId)>0 ? ' 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; + } + + + // Update stats and cache + $this->update($tag1, $tag2, $relationType, $uId); + + $this->db->sql_freeresult($dbresult); + return true; + } + + function removeLinkedTagsForUser($uId) { + $query = 'DELETE FROM '. $this->getTableName(); + $query.= ' WHERE 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; + } + + + // Update stats and cache + $this->update('', '', '', $uId); + + $this->db->sql_freeresult($dbresult); + return true; + } + + function renameTag($uId, $oldName, $newName) { + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); + $newName = $tagservice->normalize($newName); + + $query = 'UPDATE `'. $this->getTableName() .'`'; + $query.= ' SET tag1="'.$newName.'"'; + $query.= ' WHERE tag1="'.$oldName.'"'; + $query.= ' AND uId="'.$uId.'"'; + $this->db->sql_query($query); + + $query = 'UPDATE `'. $this->getTableName() .'`'; + $query.= ' SET tag2="'.$newName.'"'; + $query.= ' WHERE tag2="'.$oldName.'"'; + $query.= ' AND uId="'.$uId.'"'; + $this->db->sql_query($query); + + + // Update stats and cache + $this->update($oldName, NULL, '=', $uId); + $this->update($oldName, NULL, '>', $uId); + $this->update($newName, NULL, '=', $uId); + $this->update($newName, NULL, '>', $uId); + + return true; + + } + + function update($tag1, $tag2, $relationType, $uId) { + $tsts =SemanticScuttle_Service_Factory::getServiceInstance('TagStat'); + $tsts->updateStat($tag1, $relationType, $uId); + + $tcs = SemanticScuttle_Service_Factory::getServiceInstance('TagCache'); + $tcs->deleteByUser($uId); + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + + $tsts =SemanticScuttle_Service_Factory::getServiceInstance('TagStat'); + $tsts->deleteAll(); + } + + // Properties + function getTableName() { return $this->tablename; } + function setTableName($value) { $this->tablename = $value; } +} +?> diff --git a/src/SemanticScuttle/Service/Tag2tag.php b/src/SemanticScuttle/Service/Tag2tag.php deleted file mode 100644 index 956fd49..0000000 --- a/src/SemanticScuttle/Service/Tag2tag.php +++ /dev/null @@ -1,377 +0,0 @@ -db =& $db; - $this->tablename = $GLOBALS['tableprefix'] .'tags2tags'; - } - - function addLinkedTags($tag1, $tag2, $relationType, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); - $tag1 = $tagservice->normalize($tag1); - $tag2 = $tagservice->normalize($tag2); - - if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 - || ($relationType != ">" && $relationType != "=") - || !is_numeric($uId) || $uId<=0 - || ($this->existsLinkedTags($tag1, $tag2, $relationType, $uId))) { - 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'); - - // Update stats and cache - $this->update($tag1, $tag2, $relationType, $uId); - - return true; - } - - // Return linked tags just for admin users - function getAdminLinkedTags($tag, $relationType, $inverseRelation = false, $stopList = array()) { - // look for admin ids - $userservice = & ServiceFactory :: getServiceInstance('UserService'); - $adminIds = $userservice->getAdminIds(); - - //ask for their linked tags - return $this->getLinkedTags($tag, $relationType, $adminIds, $inverseRelation, $stopList); - } - - // Return the target linked tags. If inverseRelation is true, return the source linked tags. - function getLinkedTags($tag, $relationType, $uId = null, $inverseRelation = false, $stopList = array()) { - // Set up the SQL query. - if($inverseRelation) { - $queriedTag = "tag1"; - $givenTag = "tag2"; - } else { - $queriedTag = "tag2"; - $givenTag = "tag1"; - } - - $query = "SELECT DISTINCT ". $queriedTag ." as 'tag'"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE 1=1"; - if($tag !=null) { - $query.= " AND ". $givenTag ." = '". $tag ."'"; - } - if($relationType) { - $query.= " AND relationType = '". $relationType ."'"; - } - if(is_array($uId)) { - $query.= " AND ( 1=0 "; //tricks always false - foreach($uId as $u) { - $query.= " OR uId = '".$u."'"; - } - $query.= " ) "; - } elseif($uId != null) { - $query.= " AND uId = '".$uId."'"; - } - //die($query); - if (! ($dbresult =& $this->db->sql_query($query)) ){ - 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) { - if(!in_array($row['tag'], $stopList)) { - $output[] = $row['tag']; - } - } - - //bijective case for '=' - if($relationType == '=' && $inverseRelation == false) { - //$stopList[] = $tag; - $bijectiveOutput = $this->getLinkedTags($tag, $relationType, $uId, true, $stopList); - $output = array_merge($output, $bijectiveOutput); - //$output = array_unique($output); // remove duplication - } - - $this->db->sql_freeresult($dbresult); - return $output; - } - - /* - * Returns all linked tags (all descendants if relation is >, - * all synonyms if relation is = ) - * $stopList allows to avoid cycle (a > b > a) between tags - */ - function getAllLinkedTags($tag1, $relationType, $uId, $stopList=array()) { - if(in_array($tag1, $stopList) || $tag1 == '') { - return array(); - } - - // try to find data in cache - $tcs = & ServiceFactory::getServiceInstance('TagCacheService'); - if(count($stopList) == 0) { - $activatedCache = true; - } else { - $activatedCache = false; - } - - // look for existing links - $stopList[] = $tag1; - $linkedTags = $this->getLinkedTags($tag1, $relationType, $uId, false, $stopList); - if($relationType != '=') { - $linkedTags = array_merge($linkedTags, $this->getLinkedTags($tag1, '=', $uId, false, $stopList)); - } - - if(count($linkedTags) == 0) { - return array(); - - } else { - // use cache if possible - if($activatedCache) { - if($relationType == '>') { - $output = $tcs->getChildren($tag1, $uId); - } elseif($relationType == '=') { - $output = $tcs->getSynonyms($tag1, $uId); - } - if(count($output)>0) { - return $output; - } - } - - // else compute the links - $output = array(); - - foreach($linkedTags as $linkedTag) { - $allLinkedTags = $this->getAllLinkedTags($linkedTag, $relationType, $uId, $stopList); - $output[] = $linkedTag; - if(is_array($allLinkedTags)) { - $output = array_merge($output, $allLinkedTags); - } else { - $output[] = $allLinkedTags; - } - } - - // and save in cache - if($activatedCache == true && $uId>0) { - $tcs->updateTag($tag1, $relationType, $output, $uId); - } - - //$output = array_unique($output); // remove duplication - return $output; - - } - } - - function getOrphewTags($relationType, $uId = 0, $limit = null, $orderBy = null) { - $query = "SELECT DISTINCT tts.tag1 as tag"; - $query.= " FROM `". $this->getTableName() ."` tts"; - if($orderBy != null) { - $tsts =& ServiceFactory::getServiceInstance('TagStatService'); - $query.= ", ".$tsts->getTableName() ." tsts"; - } - $query.= " WHERE tts.tag1 <> ALL"; - $query.= " (SELECT DISTINCT tag2 FROM `". $this->getTableName() ."`"; - $query.= " WHERE relationType = '".$relationType."'"; - if($uId > 0) { - $query.= " AND uId = '".$uId."'"; - } - $query.= ")"; - if($uId > 0) { - $query.= " AND tts.uId = '".$uId."'"; - } - - switch($orderBy) { - case "nb": - $query.= " AND tts.tag1 = tsts.tag1"; - $query.= " AND tsts.relationType = '".$relationType."'"; - if($uId > 0) { - $query.= " AND tsts.uId = ".$uId; - } - $query.= " ORDER BY tsts.nb DESC"; - break; - case "depth": // by nb of descendants - $query.= " AND tts.tag1 = tsts.tag1"; - $query.= " AND tsts.relationType = '".$relationType."'"; - if($uId > 0) { - $query.= " AND tsts.uId = ".$uId; - } - $query.= " ORDER BY tsts.depth DESC"; - break; - case "nbupdate": - $query.= " AND tts.tag1 = tsts.tag1"; - $query.= " AND tsts.relationType = '".$relationType."'"; - if($uId > 0) { - $query.= " AND tsts.uId = ".$uId; - } - $query.= " ORDER BY tsts.nbupdate DESC"; - break; - } - - if($limit != null) { - $query.= " LIMIT 0,".$limit; - } - - if (! ($dbresult =& $this->db->sql_query($query)) ){ - message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - - function getMenuTags($uId) { - if(strlen($GLOBALS['menuTag']) < 1) { - return array(); - } else { - // we don't use the getAllLinkedTags function in order to improve performance - $query = "SELECT tag2 as 'tag', COUNT(tag2) as 'count'"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '".$GLOBALS['menuTag']."'"; - $query.= " AND relationType = '>'"; - if($uId > 0) { - $query.= " AND uId = '".$uId."'"; - } - $query.= " GROUP BY tag2"; - $query.= " ORDER BY count DESC"; - $query.= " LIMIT 0, ".$GLOBALS['maxSizeMenuBlock']; - - if (! ($dbresult =& $this->db->sql_query($query)) ){ - message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $output = $this->db->sql_fetchrowset($dbresult); - $this->db->sql_freeresult($dbresult); - return $output; - } - } - - - function existsLinkedTags($tag1, $tag2, $relationType, $uId) { - - //$tag1 = mysql_real_escape_string($tag1); - //$tag2 = mysql_real_escape_string($tag2); - - $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."'"; - - //echo($query."
\n"); - - return $this->db->sql_numrows($this->db->sql_query($query)) > 0; - } - - function getLinks($uId) { - $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`"; - $query.= " WHERE 1=1"; - if($uId > 0) { - $query.= " AND uId = '".$uId."'"; - } - - return $this->db->sql_fetchrowset($this->db->sql_query($query)); - } - - function removeLinkedTags($tag1, $tag2, $relationType, $uId) { - if(($tag1 != '' && $tag1 == $tag2) || - ($relationType != ">" && $relationType != "=" && $relationType != "") || - ($tag1 == '' && $tag2 == '')) { - return false; - } - $query = 'DELETE FROM '. $this->getTableName(); - $query.= ' WHERE 1=1'; - $query.= strlen($tag1)>0 ? ' AND tag1 = "'. $tag1 .'"' : ''; - $query.= strlen($tag2)>0 ? ' AND tag2 = "'. $tag2 .'"' : ''; - $query.= strlen($relationType)>0 ? ' AND relationType = "'. $relationType .'"' : ''; - $query.= strlen($uId)>0 ? ' 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; - } - - - // Update stats and cache - $this->update($tag1, $tag2, $relationType, $uId); - - $this->db->sql_freeresult($dbresult); - return true; - } - - function removeLinkedTagsForUser($uId) { - $query = 'DELETE FROM '. $this->getTableName(); - $query.= ' WHERE 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; - } - - - // Update stats and cache - $this->update('', '', '', $uId); - - $this->db->sql_freeresult($dbresult); - return true; - } - - function renameTag($uId, $oldName, $newName) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); - $newName = $tagservice->normalize($newName); - - $query = 'UPDATE `'. $this->getTableName() .'`'; - $query.= ' SET tag1="'.$newName.'"'; - $query.= ' WHERE tag1="'.$oldName.'"'; - $query.= ' AND uId="'.$uId.'"'; - $this->db->sql_query($query); - - $query = 'UPDATE `'. $this->getTableName() .'`'; - $query.= ' SET tag2="'.$newName.'"'; - $query.= ' WHERE tag2="'.$oldName.'"'; - $query.= ' AND uId="'.$uId.'"'; - $this->db->sql_query($query); - - - // Update stats and cache - $this->update($oldName, NULL, '=', $uId); - $this->update($oldName, NULL, '>', $uId); - $this->update($newName, NULL, '=', $uId); - $this->update($newName, NULL, '>', $uId); - - return true; - - } - - function update($tag1, $tag2, $relationType, $uId) { - $tsts =& ServiceFactory::getServiceInstance('TagStatService'); - $tsts->updateStat($tag1, $relationType, $uId); - - $tcs = & ServiceFactory::getServiceInstance('TagCacheService'); - $tcs->deleteByUser($uId); - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - - $tsts =& ServiceFactory::getServiceInstance('TagStatService'); - $tsts->deleteAll(); - } - - // Properties - function getTableName() { return $this->tablename; } - function setTableName($value) { $this->tablename = $value; } -} -?> diff --git a/src/SemanticScuttle/Service/TagCache.php b/src/SemanticScuttle/Service/TagCache.php index ed2eefc..30f9ebd 100644 --- a/src/SemanticScuttle/Service/TagCache.php +++ b/src/SemanticScuttle/Service/TagCache.php @@ -1,31 +1,43 @@ tag2>tag3, the system can infer that tag is included into tag1. - * Instead of computing this relation several times, it is saved into this current table. +/** + * This class infers on relation between tags by storing all + * the including tags or synonymous tag. + * For example, if the user creates: tag1>tag2>tag3, the system + * can infer that tag is included into tag1. + * Instead of computing this relation several times, it is saved + * into this current table. * For synonymy, this table stores also the group of synonymous tags. - * The table must be updated for each modification of the relations between tags. + * The table must be updated for each modification of + * the relations between tags. */ - -class TagCacheService { - var $db; +class SemanticScuttle_Service_TagCache extends SemanticScuttle_Service +{ var $tablename; - function &getInstance(&$db) { + /** + * Returns the single service instance + * + * @param DB $db Database object + * + * @return SemanticScuttle_Service + */ + public static function getInstance($db) + { static $instance; - if (!isset($instance)) - $instance =& new TagCacheService($db); + if (!isset($instance)) { + $instance = new self($db); + } return $instance; } - function TagCacheService(&$db) { - $this->db =& $db; + protected function __construct($db) + { + $this->db =$db; $this->tablename = $GLOBALS['tableprefix'] .'tagscache'; } function getChildren($tag1, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag1 = $tagservice->normalize($tag1); if($tag1 == '') return false; @@ -54,7 +66,7 @@ class TagCacheService { } function addChild($tag1, $tag2, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag1 = $tagservice->normalize($tag1); $tag2 = $tagservice->normalize($tag2); @@ -98,7 +110,7 @@ class TagCacheService { } function existsChild($tag1, $tag2, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag1 = $tagservice->normalize($tag1); $tag2 = $tagservice->normalize($tag2); @@ -202,7 +214,7 @@ class TagCacheService { } function _isSynonymKey($tag1, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag1 = $tagservice->normalize($tag1); $query = "SELECT tag1 FROM `". $this->getTableName() ."`"; @@ -214,7 +226,7 @@ class TagCacheService { } function _isSynonymValue($tag2, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag2 = $tagservice->normalize($tag2); $query = "SELECT tag2 FROM `". $this->getTableName() ."`"; @@ -238,7 +250,7 @@ class TagCacheService { } function _getSynonymKey($tag2, $uId) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag2 = $tagservice->normalize($tag2); if($this->_isSynonymKey($tag2)) return $tag2; @@ -267,7 +279,7 @@ class TagCacheService { * $tagExcepted allows to hide a value. */ function _getSynonymValues($tag1, $uId, $tagExcepted = NULL) { - $tagservice =& ServiceFactory::getServiceInstance('TagService'); + $tagservice =SemanticScuttle_Service_Factory::getServiceInstance('Tag'); $tag1 = $tagservice->normalize($tag1); $tagExcepted = $tagservice->normalize($tagExcepted); diff --git a/src/SemanticScuttle/Service/TagStat.php b/src/SemanticScuttle/Service/TagStat.php index 9d3ca5d..c54dcb7 100644 --- a/src/SemanticScuttle/Service/TagStat.php +++ b/src/SemanticScuttle/Service/TagStat.php @@ -1,22 +1,33 @@ db =& $db; + protected function __construct($db) + { + $this->db = $db; $this->tablename = $GLOBALS['tableprefix'] .'tagsstats'; } function getNbChildren($tag1, $relationType, $uId) { - $tts =& ServiceFactory::getServiceInstance('Tag2TagService'); + $tts =SemanticScuttle_Service_Factory::getServiceInstance('Tag2Tag'); $query = "SELECT tag1, relationType, uId FROM `". $tts->getTableName() ."`"; $query.= " WHERE tag1 = '" .$tag1 ."'"; $query.= " AND relationType = '". $relationType ."'"; @@ -91,7 +102,7 @@ class TagStatService { return false; } - $tts =& ServiceFactory::getServiceInstance('Tag2TagService'); + $tts =SemanticScuttle_Service_Factory::getServiceInstance('Tag2Tag'); $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId); $nbDescendants = 0; $maxDepth = 0; @@ -112,7 +123,7 @@ class TagStatService { } function updateAllStat() { - $tts =& ServiceFactory::getServiceInstance('Tag2TagService'); + $tts =SemanticScuttle_Service_Factory::getServiceInstance('Tag2Tag'); $query = "SELECT tag1, uId FROM `". $tts->getTableName() ."`"; $query.= " WHERE relationType = '>'"; diff --git a/src/SemanticScuttle/Service/Template.php b/src/SemanticScuttle/Service/Template.php index 05e494c..dbe5670 100644 --- a/src/SemanticScuttle/Service/Template.php +++ b/src/SemanticScuttle/Service/Template.php @@ -1,15 +1,26 @@ basedir = $GLOBALS['TEMPLATES_DIR']; } diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index 407632b..bc88c0b 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -1,28 +1,39 @@ 'uId', 'username' => 'username', 'password' => 'password'); - var $profileurl; - var $tablename; - var $sessionkey; - var $cookiekey; - var $cookietime = 1209600; // 2 weeks - - function &getInstance(&$db) { + protected $profileurl; + protected $tablename; + protected $sessionkey; + protected $cookiekey; + protected $cookietime = 1209600; // 2 weeks + + /** + * Returns the single service instance + * + * @param DB $db Database object + * + * @return SemanticScuttle_Service + */ + public static function getInstance($db) + { static $instance; - if (!isset($instance)) - $instance =& new UserService($db); + if (!isset($instance)) { + $instance = new self($db); + } return $instance; } - function UserService(& $db) { - $this->db =& $db; - $this->tablename = $GLOBALS['tableprefix'] .'users'; + protected function __construct($db) + { + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'users'; $this->sessionkey = INSTALLATION_ID.'-currentuserid'; - $this->cookiekey = INSTALLATION_ID.'-login'; + $this->cookiekey = INSTALLATION_ID.'-login'; $this->profileurl = createURL('profile', '%2$s'); $this->updateSessionStability(); } @@ -436,21 +447,21 @@ class UserService { return true; } - function getAllUsers ( ) { - $query = 'SELECT * FROM '. $this->getTableName(); - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get users', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $rows = array(); - - while ( $row = $this->db->sql_fetchrow($dbresult) ) { - $rows[] = $row; - } - $this->db->sql_freeresult($dbresult); - return $rows; + function getAllUsers ( ) { + $query = 'SELECT * FROM '. $this->getTableName(); + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get users', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $rows = array(); + + while ( $row = $this->db->sql_fetchrow($dbresult) ) { + $rows[] = $row; + } + $this->db->sql_freeresult($dbresult); + return $rows; } // Returns an array with admin uIds @@ -461,18 +472,18 @@ class UserService { $admins[] = $this->getIdFromUser($adminName); } return $admins; - } - - function deleteUser($uId) { - $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } + } + + function deleteUser($uId) { + $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } function sanitisePassword($password) { @@ -601,7 +612,7 @@ class User { function getName() { // Look for value only if not already set if(!isset($this->name)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getUser($this->id); $this->name = $user['name']; } @@ -611,7 +622,7 @@ class User { function getEmail() { // Look for value only if not already set if(!isset($this->email)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getUser($this->id); $this->email = $user['email']; } @@ -621,7 +632,7 @@ class User { function getHomepage() { // Look for value only if not already set if(!isset($this->homepage)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getUser($this->id); $this->homepage = $user['homepage']; } @@ -631,7 +642,7 @@ class User { function getContent() { // Look for value only if not already set if(!isset($this->content)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getUser($this->id); $this->content = $user['uContent']; } @@ -641,7 +652,7 @@ class User { function getDatetime() { // Look for value only if not already set if(!isset($this->content)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $user = $userservice->getUser($this->id); $this->datetime = $user['uDatetime']; } @@ -651,14 +662,14 @@ class User { function isAdmin() { // Look for value only if not already set if(!isset($this->isAdmin)) { - $userservice =& ServiceFactory::getServiceInstance('UserService'); + $userservice =SemanticScuttle_Service_Factory::getServiceInstance('User'); $this->isAdmin = $userservice->isAdmin($this->id); } return $this->isAdmin; } function getNbBookmarks($range = 'public') { - $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService'); + $bookmarkservice =SemanticScuttle_Service_Factory::getServiceInstance('Bookmark'); return $bookmarkservice->countBookmarks($this->getId(), $range); } } -- cgit v1.2.3