diff options
Diffstat (limited to 'src/SemanticScuttle')
-rw-r--r-- | src/SemanticScuttle/Service/Bookmark.php | 30 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Bookmark2Tag.php | 924 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Cache.php | 14 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/CommonDescription.php | 308 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Factory.php | 2 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/SearchHistory.php | 222 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Tag.php | 220 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Tag2Tag.php | 728 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/TagCache.php | 654 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/TagStat.php | 360 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Template.php | 64 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/User.php | 1276 | ||||
-rw-r--r-- | src/SemanticScuttle/Service/Vote.php | 4 |
13 files changed, 2403 insertions, 2403 deletions
diff --git a/src/SemanticScuttle/Service/Bookmark.php b/src/SemanticScuttle/Service/Bookmark.php index 3acdc73..9cf5c70 100644 --- a/src/SemanticScuttle/Service/Bookmark.php +++ b/src/SemanticScuttle/Service/Bookmark.php @@ -70,7 +70,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $b2tservice = SemanticScuttle_Service_Factory :: get('Bookmark2Tag'); $row['tags'] = $b2tservice->getTagsForBookmark($bid); } - $output = $row; + $output = $row; } else { $output = false; } @@ -109,13 +109,13 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService break; case 'shared': $sql.= ' AND bStatus = 1'; - break; + break; case 'public': default: $sql.= ' AND bStatus = 0'; break; - } - + } + if (!($dbresult = & $this->db->sql_query($sql))) { message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); } @@ -178,7 +178,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); } if($this->db->sql_fetchfield(0, 0) > 0) { - $output = true; + $output = true; } else { $output = false; } @@ -332,7 +332,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $categories, $date = NULL, $fromApi = false ) { if (!is_numeric($bId)) { - return false; + return false; } // Get the client's IP address and the date; note that the date is in GMT. @@ -345,10 +345,10 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $ip = getenv('HTTP_X_FORWARDED_FOR'); $moddatetime = gmdate('Y-m-d H:i:s', time()); - + $address = $this->normalize($address); - - //check if a new address ($address) doesn't already exist for another bookmark from the same user + + //check if a new address ($address) doesn't already exist for another bookmark from the same user $bookmark = $this->getBookmark($bId); if($bookmark['bAddress'] != $address && $this->bookmarkExists($address, $bookmark['uId'])) { message_die(GENERAL_ERROR, 'Could not update bookmark (URL already existing = '.$address.')', '', __LINE__, __FILE__); @@ -415,7 +415,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService * @param string $terms Search terms separated by spaces * @param string $sortOrder One of the following values: * "date_asc", "date_desc", - * "title_desc", "title_asc", + * "title_desc", "title_asc", * "url_desc", "url_asc" * @param boolean $watched True if only watched bookmarks * shall be returned (FIXME) @@ -540,7 +540,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService if ($terms) { // Multiple search terms okay $aTerms = explode(' ', $terms); - $aTerms = array_map('trim', $aTerms); + $aTerms = array_map('trim', $aTerms); // Search terms in tags as well when none given if (!count($tags)) { @@ -555,7 +555,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService $query_4 .= ' AND (B.bTitle LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"'; $query_4 .= ' OR B.bDescription LIKE "%'. $this->db->sql_escape($aTerms[$i]) .'%"'; $query_4 .= ' OR B.bPrivateNote LIKE "'. $this->db->sql_escape($aTerms[$i]) .'%"'; //warning : search in private notes of everybody but private notes won't appear if not allowed. - $query_4 .= ' OR U.username = "'. $this->db->sql_escape($aTerms[$i]) .'"'; //exact match for username + $query_4 .= ' OR U.username = "'. $this->db->sql_escape($aTerms[$i]) .'"'; //exact match for username if ($dotags) { $query_4 .= ' OR T.tag LIKE "'. $this->db->sql_escape($aTerms[$i]) .'%"'; } @@ -622,8 +622,8 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); return false; } - - + + $query = 'DELETE FROM '. $GLOBALS['tableprefix'] .'bookmarks2tags WHERE bId = '. intval($bookmarkid); $this->db->sql_transaction('begin'); @@ -679,7 +679,7 @@ class SemanticScuttle_Service_Bookmark extends SemanticScuttle_DbService if (!($dbresult = & $this->db->sql_query($sql))) { message_die(GENERAL_ERROR, 'Could not get vars', '', __LINE__, __FILE__, $sql, $this->db); } - + $output = $this->db->sql_fetchfield(0, 0) - 1; $this->db->sql_freeresult($dbresult); return $output; diff --git a/src/SemanticScuttle/Service/Bookmark2Tag.php b/src/SemanticScuttle/Service/Bookmark2Tag.php index 2702acb..56c85ae 100644 --- a/src/SemanticScuttle/Service/Bookmark2Tag.php +++ b/src/SemanticScuttle/Service/Bookmark2Tag.php @@ -8,475 +8,475 @@ class SemanticScuttle_Service_Bookmark2Tag extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - public function __construct($db) + public function __construct($db) { - $this->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::get('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::get('Bookmark'); - $tts =SemanticScuttle_Service_Factory::get('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( + $this->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::get('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::get('Bookmark'); + $tts =SemanticScuttle_Service_Factory::get('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::get('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::get('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 :: get('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 :: get('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 :: get('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::get('Bookmark'); - $tagservice =SemanticScuttle_Service_Factory::get('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); - } + ); + + 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::get('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::get('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 :: get('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 :: get('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 :: get('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::get('Bookmark'); + $tagservice =SemanticScuttle_Service_Factory::get('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); + } } ?> diff --git a/src/SemanticScuttle/Service/Cache.php b/src/SemanticScuttle/Service/Cache.php index 5ca2843..a5794a1 100644 --- a/src/SemanticScuttle/Service/Cache.php +++ b/src/SemanticScuttle/Service/Cache.php @@ -11,18 +11,18 @@ class SemanticScuttle_Service_Cache extends SemanticScuttle_Service * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } protected function __construct() { - $this->basedir = $GLOBALS['dir_cache']; + $this->basedir = $GLOBALS['dir_cache']; } function Start($hash, $time = 300) { @@ -37,7 +37,7 @@ class SemanticScuttle_Service_Cache extends SemanticScuttle_Service } function End($hash) { - $cachefile = $this->basedir .'/'. $hash . $this->fileextension; + $cachefile = $this->basedir .'/'. $hash . $this->fileextension; $handle = fopen($cachefile, 'w'); fwrite($handle, ob_get_contents()); fclose($handle); diff --git a/src/SemanticScuttle/Service/CommonDescription.php b/src/SemanticScuttle/Service/CommonDescription.php index 92327c2..d9aa814 100644 --- a/src/SemanticScuttle/Service/CommonDescription.php +++ b/src/SemanticScuttle/Service/CommonDescription.php @@ -8,165 +8,165 @@ class SemanticScuttle_Service_CommonDescription extends SemanticScuttle_DbServic * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - public function __construct($db) + public function __construct($db) { - $this->db = $db; - $this->tablename = $GLOBALS['tableprefix'] .'commondescription'; - } - - function addTagDescription($tag, $desc, $uId, $time) { - // Check if no modification - $lastDesc = $this->getLastTagDescription($tag); - if($lastDesc['cdDescription'] == $desc) { - return true; - } - - // If modification - $datetime = gmdate('Y-m-d H:i:s', $time); - $values = array('tag'=>$tag, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime); - $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 add tag description', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - - return true; - } - - function getLastTagDescription($tag) { - $query = "SELECT *"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag='".$tag."'"; - $query.= " ORDER BY cdDatetime DESC"; - - if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) { - message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - return $row; - } else { - return false; - } - } - - function getAllTagsDescription($tag) { - $query = "SELECT *"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag='".$tag."'"; - $query.= " ORDER BY cdDatetime DESC"; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return $this->db->sql_fetchrowset($dbresult); - - } - - function getDescriptionById($cdId) { - $query = "SELECT *"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE cdId='".$cdId."'"; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - return $row; - } else { - return false; - } - - } - - function addBookmarkDescription($bHash, $title, $desc, $uId, $time) { - // Check if no modification - $lastDesc = $this->getLastBookmarkDescription($bHash); - if($lastDesc['cdTitle'] == $title && $lastDesc['cdDescription'] == $desc) { - return true; - } - - // If modification - $datetime = gmdate('Y-m-d H:i:s', $time); - $values = array('bHash'=>$bHash, 'cdTitle'=>$title, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime); - $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 add bookmark description', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - return true; - } - - function getLastBookmarkDescription($bHash) { - $query = "SELECT *"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE bHash='".$bHash."'"; - $query.= " ORDER BY cdDatetime DESC"; - - if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) { - message_die(GENERAL_ERROR, 'Could not get bookmark description', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - return $row; - } else { - return false; - } - } - - function getAllBookmarksDescription($bHash) { - $query = "SELECT *"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE bHash='".$bHash."'"; - $query.= " ORDER BY cdDatetime DESC"; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get bookmark descriptions', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return $this->db->sql_fetchrowset($dbresult); - - } - - function deleteDescriptionsForUser($uId){ - $query = 'DELETE FROM '. $this->getTableName() . ' WHERE uId = '. intval($uId); - - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($query))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not delete user descriptions', '', - __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } - - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - } + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'commondescription'; + } + + function addTagDescription($tag, $desc, $uId, $time) { + // Check if no modification + $lastDesc = $this->getLastTagDescription($tag); + if($lastDesc['cdDescription'] == $desc) { + return true; + } + + // If modification + $datetime = gmdate('Y-m-d H:i:s', $time); + $values = array('tag'=>$tag, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime); + $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 add tag description', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + + return true; + } + + function getLastTagDescription($tag) { + $query = "SELECT *"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag='".$tag."'"; + $query.= " ORDER BY cdDatetime DESC"; + + if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) { + message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + return $row; + } else { + return false; + } + } + + function getAllTagsDescription($tag) { + $query = "SELECT *"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag='".$tag."'"; + $query.= " ORDER BY cdDatetime DESC"; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return $this->db->sql_fetchrowset($dbresult); + + } + + function getDescriptionById($cdId) { + $query = "SELECT *"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE cdId='".$cdId."'"; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tag descriptions', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + return $row; + } else { + return false; + } + + } + + function addBookmarkDescription($bHash, $title, $desc, $uId, $time) { + // Check if no modification + $lastDesc = $this->getLastBookmarkDescription($bHash); + if($lastDesc['cdTitle'] == $title && $lastDesc['cdDescription'] == $desc) { + return true; + } + + // If modification + $datetime = gmdate('Y-m-d H:i:s', $time); + $values = array('bHash'=>$bHash, 'cdTitle'=>$title, 'cdDescription'=>$desc, 'uId'=>$uId, 'cdDatetime'=>$datetime); + $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 add bookmark description', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + return true; + } + + function getLastBookmarkDescription($bHash) { + $query = "SELECT *"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE bHash='".$bHash."'"; + $query.= " ORDER BY cdDatetime DESC"; + + if (!($dbresult = & $this->db->sql_query_limit($query, 1, 0))) { + message_die(GENERAL_ERROR, 'Could not get bookmark description', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + return $row; + } else { + return false; + } + } + + function getAllBookmarksDescription($bHash) { + $query = "SELECT *"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE bHash='".$bHash."'"; + $query.= " ORDER BY cdDatetime DESC"; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get bookmark descriptions', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return $this->db->sql_fetchrowset($dbresult); + + } + + function deleteDescriptionsForUser($uId){ + $query = 'DELETE FROM '. $this->getTableName() . ' WHERE uId = '. intval($uId); + + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($query))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not delete user descriptions', '', + __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } } ?> diff --git a/src/SemanticScuttle/Service/Factory.php b/src/SemanticScuttle/Service/Factory.php index 0ea9009..2461575 100644 --- a/src/SemanticScuttle/Service/Factory.php +++ b/src/SemanticScuttle/Service/Factory.php @@ -112,7 +112,7 @@ class SemanticScuttle_Service_Factory */ protected static function loadDb() { - global $dbhost, $dbuser, $dbpass, $dbname, + global $dbhost, $dbuser, $dbpass, $dbname, $dbport, $dbpersist, $dbtype; if (self::$db !== null) { diff --git a/src/SemanticScuttle/Service/SearchHistory.php b/src/SemanticScuttle/Service/SearchHistory.php index b50d056..9874790 100644 --- a/src/SemanticScuttle/Service/SearchHistory.php +++ b/src/SemanticScuttle/Service/SearchHistory.php @@ -1,7 +1,7 @@ <?php class SemanticScuttle_Service_SearchHistory extends SemanticScuttle_DbService { - var $sizeSearchHistory; + var $sizeSearchHistory; /** * Returns the single service instance @@ -10,121 +10,121 @@ class SemanticScuttle_Service_SearchHistory extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - public function __construct($db) + public function __construct($db) { - $this->db = $db; - $this->tablename = $GLOBALS['tableprefix'] .'searchhistory'; - if(isset($GLOBALS['sizeSearchHistory'])) { - $this->sizeSearchHistory = $GLOBALS['sizeSearchHistory']; - } else { - $this->sizeSearchHistory = 10; - } - } - - function addSearch($terms, $range, $nbResults, $uId=0) { - if(strlen($terms) == 0) { - return false; - } - $datetime = gmdate('Y-m-d H:i:s', time()); - - //Insert values - $values = array('shTerms'=>$terms, 'shRange'=>$range, 'shDatetime'=>$datetime, 'shNbResults'=>$nbResults, 'uId'=>$uId); - $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'); - message_die(GENERAL_ERROR, 'Could not insert search history', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - - if($this->sizeSearchHistory != -1 && - $this->countSearches() > $this->sizeSearchHistory) { - $this->deleteOldestSearch(); - } - } - - function getAllSearches($range = NULL, $uId = NULL, $nb = NULL, $start = NULL, $distinct = false, $withResults = false) { - $sql = 'SELECT DISTINCT(shTerms), shId, shRange, shNbResults, shDatetime, uId'; - $sql.= ' FROM '. $this->getTableName(); - $sql.= ' WHERE 1=1'; - if($range != NULL) { - $sql.= ' AND shRange = "'.$range.'"'; - } else { - $sql.= ' AND shRange = "all"'; - } - if($uId != NULL) { - $sql.= ' AND uId = '.$uId; - } - if($withResults = true) { - $sql.= ' AND shNbResults > 0'; - } - if($distinct) { - $sql.= ' GROUP BY shTerms'; - } - $sql.= ' ORDER BY shId DESC'; - - if (!($dbresult = & $this->db->sql_query_limit($sql, $nb, $start))) { - message_die(GENERAL_ERROR, 'Could not get searches', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - - $searches = array(); - while ($row = & $this->db->sql_fetchrow($dbresult)) { - $searches[] = $row; - } - $this->db->sql_freeresult($dbresult); - return $searches; - } - - function countSearches() { - $sql = 'SELECT COUNT(*) AS `total` FROM '. $this->getTableName(); - if (!($dbresult = & $this->db->sql_query($sql)) || (!($row = & $this->db->sql_fetchrow($dbresult)))) { - message_die(GENERAL_ERROR, 'Could not get total searches', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - $this->db->sql_freeresult($dbresult); - return $row['total']; - } - - /* This function allows to limit the number of saved searches - by deleting the oldest one */ - function deleteOldestSearch() { - $sql = 'DELETE FROM '.$this->getTableName(); - $sql.= ' ORDER BY shId ASC LIMIT 1'; // warning: here the limit is important - - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - } - - function deleteSearchHistoryForUser($uId) { - $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete search history', '', - __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - } + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'searchhistory'; + if(isset($GLOBALS['sizeSearchHistory'])) { + $this->sizeSearchHistory = $GLOBALS['sizeSearchHistory']; + } else { + $this->sizeSearchHistory = 10; + } + } + + function addSearch($terms, $range, $nbResults, $uId=0) { + if(strlen($terms) == 0) { + return false; + } + $datetime = gmdate('Y-m-d H:i:s', time()); + + //Insert values + $values = array('shTerms'=>$terms, 'shRange'=>$range, 'shDatetime'=>$datetime, 'shNbResults'=>$nbResults, 'uId'=>$uId); + $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'); + message_die(GENERAL_ERROR, 'Could not insert search history', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + + if($this->sizeSearchHistory != -1 && + $this->countSearches() > $this->sizeSearchHistory) { + $this->deleteOldestSearch(); + } + } + + function getAllSearches($range = NULL, $uId = NULL, $nb = NULL, $start = NULL, $distinct = false, $withResults = false) { + $sql = 'SELECT DISTINCT(shTerms), shId, shRange, shNbResults, shDatetime, uId'; + $sql.= ' FROM '. $this->getTableName(); + $sql.= ' WHERE 1=1'; + if($range != NULL) { + $sql.= ' AND shRange = "'.$range.'"'; + } else { + $sql.= ' AND shRange = "all"'; + } + if($uId != NULL) { + $sql.= ' AND uId = '.$uId; + } + if($withResults = true) { + $sql.= ' AND shNbResults > 0'; + } + if($distinct) { + $sql.= ' GROUP BY shTerms'; + } + $sql.= ' ORDER BY shId DESC'; + + if (!($dbresult = & $this->db->sql_query_limit($sql, $nb, $start))) { + message_die(GENERAL_ERROR, 'Could not get searches', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + + $searches = array(); + while ($row = & $this->db->sql_fetchrow($dbresult)) { + $searches[] = $row; + } + $this->db->sql_freeresult($dbresult); + return $searches; + } + + function countSearches() { + $sql = 'SELECT COUNT(*) AS `total` FROM '. $this->getTableName(); + if (!($dbresult = & $this->db->sql_query($sql)) || (!($row = & $this->db->sql_fetchrow($dbresult)))) { + message_die(GENERAL_ERROR, 'Could not get total searches', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + $this->db->sql_freeresult($dbresult); + return $row['total']; + } + + /* This function allows to limit the number of saved searches + by deleting the oldest one */ + function deleteOldestSearch() { + $sql = 'DELETE FROM '.$this->getTableName(); + $sql.= ' ORDER BY shId ASC LIMIT 1'; // warning: here the limit is important + + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + } + + function deleteSearchHistoryForUser($uId) { + $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete search history', '', + __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } } ?> diff --git a/src/SemanticScuttle/Service/Tag.php b/src/SemanticScuttle/Service/Tag.php index c863640..93cfd45 100644 --- a/src/SemanticScuttle/Service/Tag.php +++ b/src/SemanticScuttle/Service/Tag.php @@ -8,121 +8,121 @@ class SemanticScuttle_Service_Tag extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - public function __construct($db) + public function __construct($db) { - $this->db = $db; - $this->tablename = $GLOBALS['tableprefix'] .'tags'; - } - - function getDescription($tag, $uId) { - $query = 'SELECT tag, uId, tDescription'; - $query.= ' FROM '.$this->getTableName(); - $query.= ' WHERE tag = "'.$tag.'"'; - $query.= ' AND uId = "'.$uId.'"'; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - return $row; - } else { - return array('tDescription'=>''); - } - } - - function existsDescription($tag, $uId) { - $query = 'SELECT tag, uId, tDescription'; - $query.= ' FROM '.$this->getTableName(); - $query.= ' WHERE tag = "'.$tag.'"'; - $query.= ' AND uId = "'.$uId.'"'; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - return true; - } else { - return false; - } - } - - function getAllDescriptions($tag) { - $query = 'SELECT tag, uId, tDescription'; - $query.= ' FROM '.$this->getTableName(); - $query.= ' WHERE tag = "'.$tag.'"'; - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return $this->db->sql_fetchrowset($dbresult); - } - - function updateDescription($tag, $uId, $desc) { - if($this->existsDescription($tag, $uId)) { - $query = 'UPDATE '.$this->getTableName(); - $query.= ' SET tDescription="'.$this->db->sql_escape($desc).'"'; - $query.= ' WHERE tag="'.$tag.'" AND uId="'.$uId.'"'; - } else { - $values = array('tag'=>$tag, 'uId'=>$uId, 'tDescription'=>$desc); - $query = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); - } - - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($query))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - return true; - } - - function renameTag($uId, $oldName, $newName) { - $newname = $this->normalize($newName); - - $query = 'UPDATE `'. $this->getTableName() .'`'; - $query.= ' SET tag="'.$newName.'"'; - $query.= ' WHERE tag="'.$oldName.'"'; - $query.= ' AND uId="'.$uId.'"'; - $this->db->sql_query($query); - return true; - } - - /* normalize the input tags which could be a string or an array*/ - function normalize($tags) { - //clean tags from strange characters - $tags = str_replace(array('"', '\'', '/'), "_", $tags); - - //normalize - if(!is_array($tags)) { - $tags = strtolower(trim($tags)); - } else { - for($i=0; $i<count($tags); $i++) { - $tags[$i] = strtolower(trim($tags[$i])); - } - } - return $tags; - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - } + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'tags'; + } + + function getDescription($tag, $uId) { + $query = 'SELECT tag, uId, tDescription'; + $query.= ' FROM '.$this->getTableName(); + $query.= ' WHERE tag = "'.$tag.'"'; + $query.= ' AND uId = "'.$uId.'"'; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + return $row; + } else { + return array('tDescription'=>''); + } + } + + function existsDescription($tag, $uId) { + $query = 'SELECT tag, uId, tDescription'; + $query.= ' FROM '.$this->getTableName(); + $query.= ' WHERE tag = "'.$tag.'"'; + $query.= ' AND uId = "'.$uId.'"'; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + return true; + } else { + return false; + } + } + + function getAllDescriptions($tag) { + $query = 'SELECT tag, uId, tDescription'; + $query.= ' FROM '.$this->getTableName(); + $query.= ' WHERE tag = "'.$tag.'"'; + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get tag description', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return $this->db->sql_fetchrowset($dbresult); + } + + function updateDescription($tag, $uId, $desc) { + if($this->existsDescription($tag, $uId)) { + $query = 'UPDATE '.$this->getTableName(); + $query.= ' SET tDescription="'.$this->db->sql_escape($desc).'"'; + $query.= ' WHERE tag="'.$tag.'" AND uId="'.$uId.'"'; + } else { + $values = array('tag'=>$tag, 'uId'=>$uId, 'tDescription'=>$desc); + $query = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); + } + + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($query))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not delete bookmarks', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + return true; + } + + function renameTag($uId, $oldName, $newName) { + $newname = $this->normalize($newName); + + $query = 'UPDATE `'. $this->getTableName() .'`'; + $query.= ' SET tag="'.$newName.'"'; + $query.= ' WHERE tag="'.$oldName.'"'; + $query.= ' AND uId="'.$uId.'"'; + $this->db->sql_query($query); + return true; + } + + /* normalize the input tags which could be a string or an array*/ + function normalize($tags) { + //clean tags from strange characters + $tags = str_replace(array('"', '\'', '/'), "_", $tags); + + //normalize + if(!is_array($tags)) { + $tags = strtolower(trim($tags)); + } else { + for($i=0; $i<count($tags); $i++) { + $tags[$i] = strtolower(trim($tags[$i])); + } + } + return $tags; + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } } ?> diff --git a/src/SemanticScuttle/Service/Tag2Tag.php b/src/SemanticScuttle/Service/Tag2Tag.php index 41e26bf..cc8abb8 100644 --- a/src/SemanticScuttle/Service/Tag2Tag.php +++ b/src/SemanticScuttle/Service/Tag2Tag.php @@ -8,376 +8,376 @@ class SemanticScuttle_Service_Tag2Tag extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - function __construct(&$db) + function __construct(&$db) { - $this->db =& $db; - $this->tablename = $GLOBALS['tableprefix'] .'tags2tags'; - } - - function addLinkedTags($tag1, $tag2, $relationType, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('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 :: get('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::get('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::get('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."<br>\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::get('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::get('TagStat'); - $tsts->updateStat($tag1, $relationType, $uId); - - $tcs = SemanticScuttle_Service_Factory::get('TagCache'); - $tcs->deleteByUser($uId); - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - - $tsts =SemanticScuttle_Service_Factory::get('TagStat'); - $tsts->deleteAll(); - } + $this->db =& $db; + $this->tablename = $GLOBALS['tableprefix'] .'tags2tags'; + } + + function addLinkedTags($tag1, $tag2, $relationType, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('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 :: get('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::get('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::get('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."<br>\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::get('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::get('TagStat'); + $tsts->updateStat($tag1, $relationType, $uId); + + $tcs = SemanticScuttle_Service_Factory::get('TagCache'); + $tcs->deleteByUser($uId); + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + + $tsts =SemanticScuttle_Service_Factory::get('TagStat'); + $tsts->deleteAll(); + } } ?> diff --git a/src/SemanticScuttle/Service/TagCache.php b/src/SemanticScuttle/Service/TagCache.php index 7f5a157..9e8b298 100644 --- a/src/SemanticScuttle/Service/TagCache.php +++ b/src/SemanticScuttle/Service/TagCache.php @@ -20,338 +20,338 @@ class SemanticScuttle_Service_TagCache extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - protected function __construct($db) + protected function __construct($db) { - $this->db =$db; - $this->tablename = $GLOBALS['tableprefix'] .'tagscache'; - } - - function getChildren($tag1, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag1 = $tagservice->normalize($tag1); - - if($tag1 == '') return false; - - $query = "SELECT DISTINCT tag2 as 'tag'"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE relationType = '>'"; - $query.= " AND tag1 = '".$tag1."'"; - $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) { - $output[] = $row['tag']; - } - - $this->db->sql_freeresult($dbresult); - return $output; - } - - function addChild($tag1, $tag2, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag1 = $tagservice->normalize($tag1); - $tag2 = $tagservice->normalize($tag2); - - if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 - || ($this->existsChild($tag1, $tag2, $uId))) { - return false; - } - - $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache inference', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - } - - function removeChild($tag1, $tag2, $uId) { - if(($tag1 != '' && $tag1 == $tag2) || - ($tag1 == '' && $tag2 == '' && $uId == '')) { - 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.= ' AND relationType = ">"'; - $query.= strlen($uId)>0 ? ' AND uId = "'. $uId .'"' : ''; - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - } - - function removeChildren($tag1, $uId) { - $this->removeChild($tag1, NULL, $uId); - } - - function existsChild($tag1, $tag2, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag1 = $tagservice->normalize($tag1); - $tag2 = $tagservice->normalize($tag2); - - $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND tag2 = '".$tag2."'"; - $query.= " AND relationType = '>'"; - $query.= " AND uId = '".$uId."'"; - - //echo($query."<br>\n"); - - return $this->db->sql_numrows($this->db->sql_query($query)) > 0; - - } - - /* - * Synonyms of a same concept are a group. A group has one main synonym called key - * and a list of synonyms called values. - */ - function addSynonym($tag1, $tag2, $uId) { - - if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 - || ($this->existsSynonym($tag1, $tag2, $uId))) { - return false; - } - - $case1 = '0'; // not in DB - if($this->_isSynonymKey($tag1, $uId)) { - $case1 = 'key'; - } elseif($this->_isSynonymValue($tag1, $uId)) { - $case1 = 'value'; - } - - $case2 = '0'; // not in DB - if($this->_isSynonymKey($tag2, $uId)) { - $case2 = 'key'; - } elseif($this->_isSynonymValue($tag2, $uId)) { - $case2 = 'value'; - } - $case = $case1.$case2; - - // all the possible cases - switch ($case) { - case 'keykey': - $values = $this->_getSynonymValues($tag2, $uId); - $this->removeSynonymGroup($tag2, $uId); - foreach($values as $value) { - $this->addSynonym($tag1, $value['tag'], $uId); - } - $this->addSynonym($tag1, $tag2, $uId); - break; - - case 'valuekey': - $key = $this->_getSynonymKey($tag1, $uId); - $this->addSynonym($key, $tag2, $uId); - break; - - case 'keyvalue': - $this->addSynonym($tag2, $tag1, $uId); - break; - case 'valuevalue': - $key1 = $this->_getSynonymKey($tag1, $uId); - $key2 = $this->_getSynonymKey($tag2, $uId); - $this->addSynonym($key1, $key2, $uId); - break; - case '0value': - $key = $this->_getSynonymKey($tag2, $uId); - $this->addSynonym($key, $tag1, $uId); - break; - case 'value0': - $this->addSynonym($tag2, $tag1, $uId); - break; - case '0key': - $this->addSynonym($tag2, $tag1, $uId); - break; - default: - $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache synonymy', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - break; - } - } - - function removeSynonymGroup($tag1, $uId) { - $query = 'DELETE FROM '. $this->getTableName(); - $query.= ' WHERE 1=1'; - $query.= ' AND tag1 = "'. $tag1 .'"'; - $query.= ' AND relationType = "="'; - $query.= ' AND uId = "'. $uId .'"'; - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - } - - function _isSynonymKey($tag1, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag1 = $tagservice->normalize($tag1); - - $query = "SELECT tag1 FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '='"; - $query.= " AND uId = '".$uId."'"; - - return $this->db->sql_numrows($this->db->sql_query($query)) > 0; - } - - function _isSynonymValue($tag2, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag2 = $tagservice->normalize($tag2); - - $query = "SELECT tag2 FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag2 = '" .$tag2 ."'"; - $query.= " AND relationType = '='"; - $query.= " AND uId = '".$uId."'"; - - return $this->db->sql_numrows($this->db->sql_query($query)) > 0; - } - - function getSynonyms($tag1, $uId) { - $values = array(); - if($this->_isSynonymKey($tag1, $uId)) { - $values = $this->_getSynonymValues($tag1, $uId); - } elseif($this->_isSynonymValue($tag1, $uId)) { - $key = $this->_getSynonymKey($tag1, $uId); - $values = $this->_getSynonymValues($key, $uId, $tag1); - $values[] = $key; - } - return $values; - } - - function _getSynonymKey($tag2, $uId) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag2 = $tagservice->normalize($tag2); - - if($this->_isSynonymKey($tag2)) return $tag2; - - if($tag2 == '') return false; - - $query = "SELECT DISTINCT tag1 as 'tag'"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE relationType = '='"; - $query.= " AND tag2 = '".$tag2."'"; - $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; - } - - $row = $this->db->sql_fetchrow($dbresult); - $this->db->sql_freeresult($dbresult); - return $row['tag']; - } - - /* - * Return values associated with a key. - * $tagExcepted allows to hide a value. - */ - function _getSynonymValues($tag1, $uId, $tagExcepted = NULL) { - $tagservice =SemanticScuttle_Service_Factory::get('Tag'); - $tag1 = $tagservice->normalize($tag1); - $tagExcepted = $tagservice->normalize($tagExcepted); - - if($tag1 == '') return false; - - $query = "SELECT DISTINCT tag2 as 'tag'"; - $query.= " FROM `". $this->getTableName() ."`"; - $query.= " WHERE relationType = '='"; - $query.= " AND tag1 = '".$tag1."'"; - $query.= " AND uId = '".$uId."'"; - $query.= $tagExcepted!=''?" AND tag2!='".$tagExcepted."'":""; - - 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) { - $output[] = $row['tag']; - } - - $this->db->sql_freeresult($dbresult); - return $output; - } - - function existsSynonym($tag1, $tag2, $uId) { - if($this->_getSynonymKey($tag1, $uId) == $tag2 || $this->_getSynonymKey($tag2, $uId) == $tag1) { - return true; - } else { - return false; - } - } - - - function updateTag($tag1, $relationType, $otherTags, $uId) { - if($relationType == '=') { - if($this->getSynonyms($tag1, $uId)) { // remove previous data avoiding unconstistency - $this->removeSynonymGroup($tag1, $uId); - } - - foreach($otherTags as $tag2) { - $this->addSynonym($tag1, $tag2, $uId); - } - } elseif($relationType == '>') { - if(count($this->getChildren($tag1, $uId))>0) { // remove previous data avoiding unconstistency - $this->removeChildren($tag1); - } - - foreach($otherTags as $tag2) { - $this->addChild($tag1, $tag2, $uId); - } - } - } - - function deleteByUser($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 tags cache', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - return true; - - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - } + $this->db =$db; + $this->tablename = $GLOBALS['tableprefix'] .'tagscache'; + } + + function getChildren($tag1, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag1 = $tagservice->normalize($tag1); + + if($tag1 == '') return false; + + $query = "SELECT DISTINCT tag2 as 'tag'"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE relationType = '>'"; + $query.= " AND tag1 = '".$tag1."'"; + $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) { + $output[] = $row['tag']; + } + + $this->db->sql_freeresult($dbresult); + return $output; + } + + function addChild($tag1, $tag2, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag1 = $tagservice->normalize($tag1); + $tag2 = $tagservice->normalize($tag2); + + if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 + || ($this->existsChild($tag1, $tag2, $uId))) { + return false; + } + + $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache inference', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + } + + function removeChild($tag1, $tag2, $uId) { + if(($tag1 != '' && $tag1 == $tag2) || + ($tag1 == '' && $tag2 == '' && $uId == '')) { + 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.= ' AND relationType = ">"'; + $query.= strlen($uId)>0 ? ' AND uId = "'. $uId .'"' : ''; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + } + + function removeChildren($tag1, $uId) { + $this->removeChild($tag1, NULL, $uId); + } + + function existsChild($tag1, $tag2, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag1 = $tagservice->normalize($tag1); + $tag2 = $tagservice->normalize($tag2); + + $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND tag2 = '".$tag2."'"; + $query.= " AND relationType = '>'"; + $query.= " AND uId = '".$uId."'"; + + //echo($query."<br>\n"); + + return $this->db->sql_numrows($this->db->sql_query($query)) > 0; + + } + + /* + * Synonyms of a same concept are a group. A group has one main synonym called key + * and a list of synonyms called values. + */ + function addSynonym($tag1, $tag2, $uId) { + + if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0 + || ($this->existsSynonym($tag1, $tag2, $uId))) { + return false; + } + + $case1 = '0'; // not in DB + if($this->_isSynonymKey($tag1, $uId)) { + $case1 = 'key'; + } elseif($this->_isSynonymValue($tag1, $uId)) { + $case1 = 'value'; + } + + $case2 = '0'; // not in DB + if($this->_isSynonymKey($tag2, $uId)) { + $case2 = 'key'; + } elseif($this->_isSynonymValue($tag2, $uId)) { + $case2 = 'value'; + } + $case = $case1.$case2; + + // all the possible cases + switch ($case) { + case 'keykey': + $values = $this->_getSynonymValues($tag2, $uId); + $this->removeSynonymGroup($tag2, $uId); + foreach($values as $value) { + $this->addSynonym($tag1, $value['tag'], $uId); + } + $this->addSynonym($tag1, $tag2, $uId); + break; + + case 'valuekey': + $key = $this->_getSynonymKey($tag1, $uId); + $this->addSynonym($key, $tag2, $uId); + break; + + case 'keyvalue': + $this->addSynonym($tag2, $tag1, $uId); + break; + case 'valuevalue': + $key1 = $this->_getSynonymKey($tag1, $uId); + $key2 = $this->_getSynonymKey($tag2, $uId); + $this->addSynonym($key1, $key2, $uId); + break; + case '0value': + $key = $this->_getSynonymKey($tag2, $uId); + $this->addSynonym($key, $tag1, $uId); + break; + case 'value0': + $this->addSynonym($tag2, $tag1, $uId); + break; + case '0key': + $this->addSynonym($tag2, $tag1, $uId); + break; + default: + $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache synonymy', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + break; + } + } + + function removeSynonymGroup($tag1, $uId) { + $query = 'DELETE FROM '. $this->getTableName(); + $query.= ' WHERE 1=1'; + $query.= ' AND tag1 = "'. $tag1 .'"'; + $query.= ' AND relationType = "="'; + $query.= ' AND uId = "'. $uId .'"'; + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + } + + function _isSynonymKey($tag1, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag1 = $tagservice->normalize($tag1); + + $query = "SELECT tag1 FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '='"; + $query.= " AND uId = '".$uId."'"; + + return $this->db->sql_numrows($this->db->sql_query($query)) > 0; + } + + function _isSynonymValue($tag2, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag2 = $tagservice->normalize($tag2); + + $query = "SELECT tag2 FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag2 = '" .$tag2 ."'"; + $query.= " AND relationType = '='"; + $query.= " AND uId = '".$uId."'"; + + return $this->db->sql_numrows($this->db->sql_query($query)) > 0; + } + + function getSynonyms($tag1, $uId) { + $values = array(); + if($this->_isSynonymKey($tag1, $uId)) { + $values = $this->_getSynonymValues($tag1, $uId); + } elseif($this->_isSynonymValue($tag1, $uId)) { + $key = $this->_getSynonymKey($tag1, $uId); + $values = $this->_getSynonymValues($key, $uId, $tag1); + $values[] = $key; + } + return $values; + } + + function _getSynonymKey($tag2, $uId) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag2 = $tagservice->normalize($tag2); + + if($this->_isSynonymKey($tag2)) return $tag2; + + if($tag2 == '') return false; + + $query = "SELECT DISTINCT tag1 as 'tag'"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE relationType = '='"; + $query.= " AND tag2 = '".$tag2."'"; + $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; + } + + $row = $this->db->sql_fetchrow($dbresult); + $this->db->sql_freeresult($dbresult); + return $row['tag']; + } + + /* + * Return values associated with a key. + * $tagExcepted allows to hide a value. + */ + function _getSynonymValues($tag1, $uId, $tagExcepted = NULL) { + $tagservice =SemanticScuttle_Service_Factory::get('Tag'); + $tag1 = $tagservice->normalize($tag1); + $tagExcepted = $tagservice->normalize($tagExcepted); + + if($tag1 == '') return false; + + $query = "SELECT DISTINCT tag2 as 'tag'"; + $query.= " FROM `". $this->getTableName() ."`"; + $query.= " WHERE relationType = '='"; + $query.= " AND tag1 = '".$tag1."'"; + $query.= " AND uId = '".$uId."'"; + $query.= $tagExcepted!=''?" AND tag2!='".$tagExcepted."'":""; + + 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) { + $output[] = $row['tag']; + } + + $this->db->sql_freeresult($dbresult); + return $output; + } + + function existsSynonym($tag1, $tag2, $uId) { + if($this->_getSynonymKey($tag1, $uId) == $tag2 || $this->_getSynonymKey($tag2, $uId) == $tag1) { + return true; + } else { + return false; + } + } + + + function updateTag($tag1, $relationType, $otherTags, $uId) { + if($relationType == '=') { + if($this->getSynonyms($tag1, $uId)) { // remove previous data avoiding unconstistency + $this->removeSynonymGroup($tag1, $uId); + } + + foreach($otherTags as $tag2) { + $this->addSynonym($tag1, $tag2, $uId); + } + } elseif($relationType == '>') { + if(count($this->getChildren($tag1, $uId))>0) { // remove previous data avoiding unconstistency + $this->removeChildren($tag1); + } + + foreach($otherTags as $tag2) { + $this->addChild($tag1, $tag2, $uId); + } + } + } + + function deleteByUser($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 tags cache', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + return true; + + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } } ?> diff --git a/src/SemanticScuttle/Service/TagStat.php b/src/SemanticScuttle/Service/TagStat.php index 0c0b70a..f925bfc 100644 --- a/src/SemanticScuttle/Service/TagStat.php +++ b/src/SemanticScuttle/Service/TagStat.php @@ -8,191 +8,191 @@ class SemanticScuttle_Service_TagStat extends SemanticScuttle_DbService * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - protected function __construct($db) + protected function __construct($db) { - $this->db = $db; - $this->tablename = $GLOBALS['tableprefix'] .'tagsstats'; - } - - function getNbChildren($tag1, $relationType, $uId) { - $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); - $query = "SELECT tag1, relationType, uId FROM `". $tts->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - return $this->db->sql_numrows($this->db->sql_query($query)); - } - - function getNbDescendants($tag1, $relationType, $uId) { - $query = "SELECT nb FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - $dbresults =& $this->db->sql_query($query); - $row = $this->db->sql_fetchrow($dbresults); - if($row['nb'] == null) { - return 0; - } else { - return (int) $row['nb']; - } - } - - function getMaxDepth($tag1, $relationType, $uId) { - $query = "SELECT depth FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - $dbresults =& $this->db->sql_query($query); - $row = $this->db->sql_fetchrow($dbresults); - if($row['depth'] == null) { - return 0; - } else { - return (int) $row['depth']; - }; - } - - function getNbUpdates($tag1, $relationType, $uId) { - $query = "SELECT nbupdate FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - $dbresults =& $this->db->sql_query($query); - $row = $this->db->sql_fetchrow($dbresults); - if($row['nbupdate'] == null) { - return 0; - } else { - return (int) $row['nbupdate']; - } - } - - function existStat($tag1, $relationType, $uId) { - $query = "SELECT tag1, relationType, uId FROM `". $this->getTableName() ."`"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - return $this->db->sql_numrows($this->db->sql_query($query))>0; - } - - function createStat($tag1, $relationType, $uId) { - $query = "INSERT INTO `". $this->getTableName() ."`"; - $query.= "(tag1, relationType, uId)"; - $query.= " VALUES ('".$tag1."','".$relationType."','".$uId."')"; - $this->db->sql_query($query); - } - - function updateStat($tag1, $relationType, $uId=null, $stoplist=array()) { - if(in_array($tag1, $stoplist)) { - return false; - } - - $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); - $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId); - $nbDescendants = 0; - $maxDepth = 0; - foreach($linkedTags as $linkedTag) { - $nbDescendants+= 1 + $this->getNbDescendants($linkedTag, $relationType, $uId); - $maxDepth = max($maxDepth, 1 + $this->getMaxDepth($linkedTag, $relationType, $uId)); - } - $this->setNbDescendants($tag1, $relationType, $uId, $nbDescendants); - $this->setMaxDepth($tag1, $relationType, $uId, $maxDepth); - $this->increaseNbUpdate($tag1, $relationType, $uId); - - // propagation to the precedent tags - $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId, true); - $stoplist[] = $tag1; - foreach($linkedTags as $linkedTag) { - $this->updateStat($linkedTag, $relationType, $uId, $stoplist); - } - } - - function updateAllStat() { - $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); - - $query = "SELECT tag1, uId FROM `". $tts->getTableName() ."`"; - $query.= " WHERE relationType = '>'"; - - //die($query); - - if (! ($dbresult =& $this->db->sql_query($query)) ){ - message_die(GENERAL_ERROR, 'Could not update stats', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $rowset = $this->db->sql_fetchrowset($dbresult); - foreach($rowset as $row) { - $this->updateStat($row['tag1'], '>', $row['uId']); - } - } - - function setNbDescendants($tag1, $relationType, $uId, $nb) { - if(!$this->existStat($tag1, $relationType, $uId)) { - $this->createStat($tag1, $relationType, $uId); - } - $query = "UPDATE `". $this->getTableName() ."`"; - $query.= " SET nb = ". $nb; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - $this->db->sql_query($query); - } - - function setMaxDepth($tag1, $relationType, $uId, $depth) { - if(!$this->existStat($tag1, $relationType, $uId)) { - $this->createStat($tag1, $relationType, $uId); - } - $query = "UPDATE `". $this->getTableName() ."`"; - $query.= " SET depth = ". $depth; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - $this->db->sql_query($query); - } - - function increaseNbUpdate($tag1, $relationType, $uId) { - if(!$this->existStat($tag1, $relationType, $uId)) { - $this->createStat($tag1, $relationType, $uId); - } - $query = "UPDATE `". $this->getTableName() ."`"; - $query.= " SET nbupdate = nbupdate + 1"; - $query.= " WHERE tag1 = '" .$tag1 ."'"; - $query.= " AND relationType = '". $relationType ."'"; - $query.= " AND uId = '".$uId."'"; - - //die($query); - - $this->db->sql_query($query); - } - - function deleteTagStatForUser($uId) { - $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); - - if (!($dbresult = & $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not delete tag stats', '', __LINE__, - __FILE__, $query, $this->db); - return false; - } - - return true; - } - - function deleteAll() { - $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; - $this->db->sql_query($query); - } + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'tagsstats'; + } + + function getNbChildren($tag1, $relationType, $uId) { + $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); + $query = "SELECT tag1, relationType, uId FROM `". $tts->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + return $this->db->sql_numrows($this->db->sql_query($query)); + } + + function getNbDescendants($tag1, $relationType, $uId) { + $query = "SELECT nb FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + $dbresults =& $this->db->sql_query($query); + $row = $this->db->sql_fetchrow($dbresults); + if($row['nb'] == null) { + return 0; + } else { + return (int) $row['nb']; + } + } + + function getMaxDepth($tag1, $relationType, $uId) { + $query = "SELECT depth FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + $dbresults =& $this->db->sql_query($query); + $row = $this->db->sql_fetchrow($dbresults); + if($row['depth'] == null) { + return 0; + } else { + return (int) $row['depth']; + }; + } + + function getNbUpdates($tag1, $relationType, $uId) { + $query = "SELECT nbupdate FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + $dbresults =& $this->db->sql_query($query); + $row = $this->db->sql_fetchrow($dbresults); + if($row['nbupdate'] == null) { + return 0; + } else { + return (int) $row['nbupdate']; + } + } + + function existStat($tag1, $relationType, $uId) { + $query = "SELECT tag1, relationType, uId FROM `". $this->getTableName() ."`"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + return $this->db->sql_numrows($this->db->sql_query($query))>0; + } + + function createStat($tag1, $relationType, $uId) { + $query = "INSERT INTO `". $this->getTableName() ."`"; + $query.= "(tag1, relationType, uId)"; + $query.= " VALUES ('".$tag1."','".$relationType."','".$uId."')"; + $this->db->sql_query($query); + } + + function updateStat($tag1, $relationType, $uId=null, $stoplist=array()) { + if(in_array($tag1, $stoplist)) { + return false; + } + + $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); + $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId); + $nbDescendants = 0; + $maxDepth = 0; + foreach($linkedTags as $linkedTag) { + $nbDescendants+= 1 + $this->getNbDescendants($linkedTag, $relationType, $uId); + $maxDepth = max($maxDepth, 1 + $this->getMaxDepth($linkedTag, $relationType, $uId)); + } + $this->setNbDescendants($tag1, $relationType, $uId, $nbDescendants); + $this->setMaxDepth($tag1, $relationType, $uId, $maxDepth); + $this->increaseNbUpdate($tag1, $relationType, $uId); + + // propagation to the precedent tags + $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId, true); + $stoplist[] = $tag1; + foreach($linkedTags as $linkedTag) { + $this->updateStat($linkedTag, $relationType, $uId, $stoplist); + } + } + + function updateAllStat() { + $tts =SemanticScuttle_Service_Factory::get('Tag2Tag'); + + $query = "SELECT tag1, uId FROM `". $tts->getTableName() ."`"; + $query.= " WHERE relationType = '>'"; + + //die($query); + + if (! ($dbresult =& $this->db->sql_query($query)) ){ + message_die(GENERAL_ERROR, 'Could not update stats', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $rowset = $this->db->sql_fetchrowset($dbresult); + foreach($rowset as $row) { + $this->updateStat($row['tag1'], '>', $row['uId']); + } + } + + function setNbDescendants($tag1, $relationType, $uId, $nb) { + if(!$this->existStat($tag1, $relationType, $uId)) { + $this->createStat($tag1, $relationType, $uId); + } + $query = "UPDATE `". $this->getTableName() ."`"; + $query.= " SET nb = ". $nb; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + $this->db->sql_query($query); + } + + function setMaxDepth($tag1, $relationType, $uId, $depth) { + if(!$this->existStat($tag1, $relationType, $uId)) { + $this->createStat($tag1, $relationType, $uId); + } + $query = "UPDATE `". $this->getTableName() ."`"; + $query.= " SET depth = ". $depth; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + $this->db->sql_query($query); + } + + function increaseNbUpdate($tag1, $relationType, $uId) { + if(!$this->existStat($tag1, $relationType, $uId)) { + $this->createStat($tag1, $relationType, $uId); + } + $query = "UPDATE `". $this->getTableName() ."`"; + $query.= " SET nbupdate = nbupdate + 1"; + $query.= " WHERE tag1 = '" .$tag1 ."'"; + $query.= " AND relationType = '". $relationType ."'"; + $query.= " AND uId = '".$uId."'"; + + //die($query); + + $this->db->sql_query($query); + } + + function deleteTagStatForUser($uId) { + $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId); + + if (!($dbresult = & $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not delete tag stats', '', __LINE__, + __FILE__, $query, $this->db); + return false; + } + + return true; + } + + function deleteAll() { + $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; + $this->db->sql_query($query); + } } ?> diff --git a/src/SemanticScuttle/Service/Template.php b/src/SemanticScuttle/Service/Template.php index dbe5670..110c5cf 100644 --- a/src/SemanticScuttle/Service/Template.php +++ b/src/SemanticScuttle/Service/Template.php @@ -1,7 +1,7 @@ <?php class SemanticScuttle_Service_Template extends SemanticScuttle_Service { - protected $basedir; + protected $basedir; /** * Returns the single service instance @@ -10,48 +10,48 @@ class SemanticScuttle_Service_Template extends SemanticScuttle_Service * * @return SemanticScuttle_Service */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - public function __construct() + public function __construct() { - $this->basedir = $GLOBALS['TEMPLATES_DIR']; - } + $this->basedir = $GLOBALS['TEMPLATES_DIR']; + } - function loadTemplate($template, $vars = NULL) { - if (substr($template, -4) != '.php') - $template .= '.php'; - $tpl =& new Template($this->basedir .'/'. $template, $vars, $this); - $tpl->parse(); - return $tpl; - } + function loadTemplate($template, $vars = NULL) { + if (substr($template, -4) != '.php') + $template .= '.php'; + $tpl =& new Template($this->basedir .'/'. $template, $vars, $this); + $tpl->parse(); + return $tpl; + } } class Template { - var $vars = array(); - var $file = ''; - var $templateservice; + var $vars = array(); + var $file = ''; + var $templateservice; - function Template($file, $vars = NULL, &$templateservice) { - $this->vars = $vars; - $this->file = $file; - $this->templateservice = $templateservice; - } + function Template($file, $vars = NULL, &$templateservice) { + $this->vars = $vars; + $this->file = $file; + $this->templateservice = $templateservice; + } - function parse() { - if (isset($this->vars)) - extract($this->vars); - include($this->file); - } + function parse() { + if (isset($this->vars)) + extract($this->vars); + include($this->file); + } - function includeTemplate($name) { - return $this->templateservice->loadTemplate($name, $this->vars); - } + function includeTemplate($name) { + return $this->templateservice->loadTemplate($name, $this->vars); + } } ?>
\ No newline at end of file diff --git a/src/SemanticScuttle/Service/User.php b/src/SemanticScuttle/Service/User.php index bc9ebfe..f44073d 100644 --- a/src/SemanticScuttle/Service/User.php +++ b/src/SemanticScuttle/Service/User.php @@ -1,14 +1,14 @@ <?php class SemanticScuttle_Service_User extends SemanticScuttle_DbService { - protected $fields = array( + protected $fields = array( 'primary' => 'uId', 'username' => 'username', 'password' => 'password'); - protected $profileurl; - protected $sessionkey; - protected $cookiekey; - protected $cookietime = 1209600; // 2 weeks + protected $profileurl; + protected $sessionkey; + protected $cookiekey; + protected $cookietime = 1209600; // 2 weeks /** * Returns the single service instance @@ -17,654 +17,654 @@ class SemanticScuttle_Service_User extends SemanticScuttle_DbService * * @return SemanticScuttle_Service_User */ - public static function getInstance($db) + public static function getInstance($db) { - static $instance; - if (!isset($instance)) { + static $instance; + if (!isset($instance)) { $instance = new self($db); } - return $instance; - } + return $instance; + } - protected function __construct($db) + protected function __construct($db) { - $this->db = $db; - $this->tablename = $GLOBALS['tableprefix'] .'users'; - $this->sessionkey = INSTALLATION_ID.'-currentuserid'; - $this->cookiekey = INSTALLATION_ID.'-login'; - $this->profileurl = createURL('profile', '%2$s'); - $this->updateSessionStability(); - } - - function _checkdns($host) { - if (function_exists('checkdnsrr')) { - return checkdnsrr($host); - } else { - return $this->_checkdnsrr($host); - } - } - - function _checkdnsrr($host, $type = "MX") { - if(!empty($host)) { - @exec("nslookup -type=$type $host", $output); - while(list($k, $line) = each($output)) { - if(eregi("^$host", $line)) { - return true; - } - } - return false; - } - } - - function _getuser($fieldname, $value) { - $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'; - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $row =& $this->db->sql_fetchrow($dbresult); - $this->db->sql_freeresult($dbresult); - if ($row) { - return $row; - } else { - return false; - } - } - - function & getUsers($nb=0) { - $query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; - if($nb>0) { - $query .= ' LIMIT 0, '.$nb; - } - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - while ($row = & $this->db->sql_fetchrow($dbresult)) { - $users[] = $row; - } - $this->db->sql_freeresult($dbresult); - return $users; - } - - function & getObjectUsers($nb=0) { - $query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; - if($nb>0) { - $query .= ' LIMIT 0, '.$nb; - } - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - while ($row = & $this->db->sql_fetchrow($dbresult)) { - $users[] = new User($row[$this->getFieldName('primary')], $row[$this->getFieldName('username')]); - } - $this->db->sql_freeresult($dbresult); - return $users; - } - - function _randompassword() { - $seed = (integer) md5(microtime()); - mt_srand($seed); - $password = mt_rand(1, 99999999); - $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12)); - return $password; - } - - function _updateuser($uId, $fieldname, $value) { - $updates = array ($fieldname => $value); - $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId); - - // Execute the statement. - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - - // Everything worked out, so return true. - return true; - } - - function getProfileUrl($id, $username) { - return sprintf($this->profileurl, urlencode($id), urlencode($username)); - } - - function getUserByUsername($username) { - return $this->_getuser($this->getFieldName('username'), $username); - } - - function getObjectUserByUsername($username) { - $user = $this->_getuser($this->getFieldName('username'), $username); - if($user != false) { - return new User($user[$this->getFieldName('primary')], $username); - } else { - return NULL; - } - } - - /* Takes an numerical "id" or a string "username" - and returns the numerical "id" if the user exists else returns NULL */ - function getIdFromUser($user) { - if (is_int($user)) { - return intval($user); - } else { - $objectUser = $this->getObjectUserByUsername($user); - if($objectUser != NULL) { - return $objectUser->getId(); - } - } - return NULL; - } - - function getUser($id) { - return $this->_getuser($this->getFieldName('primary'), $id); - } - - // Momentary useful in order to go to object code - function getObjectUser($id) { - $user = $this->_getuser($this->getFieldName('primary'), $id); - return new User($id, $user[$this->getFieldName('username')]); - } - - function isLoggedOn() { - return ($this->getCurrentUserId() !== false); - } - - function &getCurrentUser($refresh = FALSE, $newval = NULL) { - static $currentuser; - if (!is_null($newval)) { //internal use only: reset currentuser - $currentuser = $newval; - } else if ($refresh || !isset($currentuser)) { - if ($id = $this->getCurrentUserId()) { - $currentuser = $this->getUser($id); - } else { - $currentuser = null; - } - } - return $currentuser; - } - - // Momentary useful in order to go to object code - function getCurrentObjectUser($refresh = FALSE, $newval = NULL) { - static $currentObjectUser; - if (!is_null($newval)) { //internal use only: reset currentuser - $currentObjectUser = $newval; - } else if ($refresh || !isset($currentObjectUser)) { - if ($id = $this->getCurrentUserId()) { - $currentObjectUser = $this->getObjectUser($id); - } else { - $currentObjectUser = null; - } - } - return $currentObjectUser; - } - - function existsUserWithUsername($username) { - if($this->getUserByUsername($username) != '') { - return true; - } else { - return false; - } - } - - function existsUser($id) { - if($this->getUser($id) != '') { - return true; - } else { - return false; - } - } - - /** - * Checks if the given user is an administrator. - * Uses global admin_users property containing admin - * user names - * - * @param integer|array $user User ID or user row from DB - * - * @return boolean True if the user is admin - */ - function isAdmin($user) - { - if (is_numeric($user)) { - $user = $this->getUser($user); - } - - if (isset($GLOBALS['admin_users']) - && in_array($user['username'], $GLOBALS['admin_users']) - ) { - return true; - } else { - return false; - } - } - - /* return current user id based on session or cookie */ - function getCurrentUserId() { - if (isset($_SESSION[$this->getSessionKey()])) { - return $_SESSION[$this->getSessionKey()]; - } else if (isset($_COOKIE[$this->getCookieKey()])) { - $cook = split(':', $_COOKIE[$this->getCookieKey()]); - //cookie looks like this: 'id:md5(username+password)' - $query = 'SELECT * FROM '. $this->getTableName() . + $this->db = $db; + $this->tablename = $GLOBALS['tableprefix'] .'users'; + $this->sessionkey = INSTALLATION_ID.'-currentuserid'; + $this->cookiekey = INSTALLATION_ID.'-login'; + $this->profileurl = createURL('profile', '%2$s'); + $this->updateSessionStability(); + } + + function _checkdns($host) { + if (function_exists('checkdnsrr')) { + return checkdnsrr($host); + } else { + return $this->_checkdnsrr($host); + } + } + + function _checkdnsrr($host, $type = "MX") { + if(!empty($host)) { + @exec("nslookup -type=$type $host", $output); + while(list($k, $line) = each($output)) { + if(eregi("^$host", $line)) { + return true; + } + } + return false; + } + } + + function _getuser($fieldname, $value) { + $query = 'SELECT * FROM '. $this->getTableName() .' WHERE '. $fieldname .' = "'. $this->db->sql_escape($value) .'"'; + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $row =& $this->db->sql_fetchrow($dbresult); + $this->db->sql_freeresult($dbresult); + if ($row) { + return $row; + } else { + return false; + } + } + + function & getUsers($nb=0) { + $query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; + if($nb>0) { + $query .= ' LIMIT 0, '.$nb; + } + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + while ($row = & $this->db->sql_fetchrow($dbresult)) { + $users[] = $row; + } + $this->db->sql_freeresult($dbresult); + return $users; + } + + function & getObjectUsers($nb=0) { + $query = 'SELECT * FROM '. $this->getTableName() .' ORDER BY `uId` DESC'; + if($nb>0) { + $query .= ' LIMIT 0, '.$nb; + } + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + while ($row = & $this->db->sql_fetchrow($dbresult)) { + $users[] = new User($row[$this->getFieldName('primary')], $row[$this->getFieldName('username')]); + } + $this->db->sql_freeresult($dbresult); + return $users; + } + + function _randompassword() { + $seed = (integer) md5(microtime()); + mt_srand($seed); + $password = mt_rand(1, 99999999); + $password = substr(md5($password), mt_rand(0, 19), mt_rand(6, 12)); + return $password; + } + + function _updateuser($uId, $fieldname, $value) { + $updates = array ($fieldname => $value); + $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId); + + // Execute the statement. + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + + // Everything worked out, so return true. + return true; + } + + function getProfileUrl($id, $username) { + return sprintf($this->profileurl, urlencode($id), urlencode($username)); + } + + function getUserByUsername($username) { + return $this->_getuser($this->getFieldName('username'), $username); + } + + function getObjectUserByUsername($username) { + $user = $this->_getuser($this->getFieldName('username'), $username); + if($user != false) { + return new User($user[$this->getFieldName('primary')], $username); + } else { + return NULL; + } + } + + /* Takes an numerical "id" or a string "username" + and returns the numerical "id" if the user exists else returns NULL */ + function getIdFromUser($user) { + if (is_int($user)) { + return intval($user); + } else { + $objectUser = $this->getObjectUserByUsername($user); + if($objectUser != NULL) { + return $objectUser->getId(); + } + } + return NULL; + } + + function getUser($id) { + return $this->_getuser($this->getFieldName('primary'), $id); + } + + // Momentary useful in order to go to object code + function getObjectUser($id) { + $user = $this->_getuser($this->getFieldName('primary'), $id); + return new User($id, $user[$this->getFieldName('username')]); + } + + function isLoggedOn() { + return ($this->getCurrentUserId() !== false); + } + + function &getCurrentUser($refresh = FALSE, $newval = NULL) { + static $currentuser; + if (!is_null($newval)) { //internal use only: reset currentuser + $currentuser = $newval; + } else if ($refresh || !isset($currentuser)) { + if ($id = $this->getCurrentUserId()) { + $currentuser = $this->getUser($id); + } else { + $currentuser = null; + } + } + return $currentuser; + } + + // Momentary useful in order to go to object code + function getCurrentObjectUser($refresh = FALSE, $newval = NULL) { + static $currentObjectUser; + if (!is_null($newval)) { //internal use only: reset currentuser + $currentObjectUser = $newval; + } else if ($refresh || !isset($currentObjectUser)) { + if ($id = $this->getCurrentUserId()) { + $currentObjectUser = $this->getObjectUser($id); + } else { + $currentObjectUser = null; + } + } + return $currentObjectUser; + } + + function existsUserWithUsername($username) { + if($this->getUserByUsername($username) != '') { + return true; + } else { + return false; + } + } + + function existsUser($id) { + if($this->getUser($id) != '') { + return true; + } else { + return false; + } + } + + /** + * Checks if the given user is an administrator. + * Uses global admin_users property containing admin + * user names + * + * @param integer|array $user User ID or user row from DB + * + * @return boolean True if the user is admin + */ + function isAdmin($user) + { + if (is_numeric($user)) { + $user = $this->getUser($user); + } + + if (isset($GLOBALS['admin_users']) + && in_array($user['username'], $GLOBALS['admin_users']) + ) { + return true; + } else { + return false; + } + } + + /* return current user id based on session or cookie */ + function getCurrentUserId() { + if (isset($_SESSION[$this->getSessionKey()])) { + return $_SESSION[$this->getSessionKey()]; + } else if (isset($_COOKIE[$this->getCookieKey()])) { + $cook = split(':', $_COOKIE[$this->getCookieKey()]); + //cookie looks like this: 'id:md5(username+password)' + $query = 'SELECT * FROM '. $this->getTableName() . ' WHERE MD5(CONCAT('.$this->getFieldName('username') . ', '.$this->getFieldName('password') . ')) = \''.$this->db->sql_escape($cook[1]).'\' AND '. - $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]); - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row = $this->db->sql_fetchrow($dbresult)) { - $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')]; - $this->db->sql_freeresult($dbresult); - return $_SESSION[$this->getSessionKey()]; - } - } - return false; - } - - function login($username, $password, $remember = FALSE) { - $password = $this->sanitisePassword($password); - $query = 'SELECT '. $this->getFieldName('primary') .' FROM '. $this->getTableName() .' WHERE '. $this->getFieldName('username') .' = "'. $this->db->sql_escape($username) .'" AND '. $this->getFieldName('password') .' = "'. $this->db->sql_escape($password) .'"'; - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - if ($row =& $this->db->sql_fetchrow($dbresult)) { - $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')]; - if ($remember) { - $cookie = $id .':'. md5($username.$password); - setcookie($this->cookiekey, $cookie, time() + $this->cookietime, '/'); - } - $this->db->sql_freeresult($dbresult); - return true; - } else { - return false; - } - } - - function logout() { - @setcookie($this->getCookiekey(), '', time() - 1, '/'); - unset($_COOKIE[$this->getCookiekey()]); - session_unset(); - $this->getCurrentUser(TRUE, false); - } - - function getWatchlist($uId) { - // Gets the list of user IDs being watched by the given user. - $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId); - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $arrWatch = array(); - if ($this->db->sql_numrows($dbresult) == 0) { - $this->db->sql_freeresult($dbresult); - return $arrWatch; - } - while ($row =& $this->db->sql_fetchrow($dbresult)) { - $arrWatch[] = $row['watched']; - } - $this->db->sql_freeresult($dbresult); - return $arrWatch; - } - - function getWatchNames($uId, $watchedby = false) { - // Gets the list of user names being watched by the given user. - // - If $watchedby is false get the list of users that $uId watches - // - If $watchedby is true get the list of users that watch $uId - if ($watchedby) { - $table1 = 'b'; - $table2 = 'a'; - } else { - $table1 = 'a'; - $table2 = 'b'; - } - $query = 'SELECT '. $table1 .'.'. $this->getFieldName('username') .' FROM '. $GLOBALS['tableprefix'] .'watched AS W, '. $this->getTableName() .' AS a, '. $this->getTableName() .' AS b WHERE W.watched = a.'. $this->getFieldName('primary') .' AND W.uId = b.'. $this->getFieldName('primary') .' AND '. $table2 .'.'. $this->getFieldName('primary') .' = '. intval($uId) .' ORDER BY '. $table1 .'.'. $this->getFieldName('username'); - - if (!($dbresult =& $this->db->sql_query($query))) { - message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $arrWatch = array(); - if ($this->db->sql_numrows($dbresult) == 0) { - $this->db->sql_freeresult($dbresult); - return $arrWatch; - } - while ($row =& $this->db->sql_fetchrow($dbresult)) { - $arrWatch[] = $row[$this->getFieldName('username')]; - } - $this->db->sql_freeresult($dbresult); - return $arrWatch; - } - - function getWatchStatus($watcheduser, $currentuser) { - // Returns true if the current user is watching the given user, and false otherwise. - $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched AS W INNER JOIN '. $this->getTableName() .' AS U ON U.'. $this->getFieldName('primary') .' = W.watched WHERE U.'. $this->getFieldName('primary') .' = '. intval($watcheduser) .' AND W.uId = '. intval($currentuser); - - if (! ($dbresult =& $this->db->sql_query($query)) ) { - message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db); - return false; - } - - $arrWatch = array(); - if ($this->db->sql_numrows($dbresult) == 0) - return false; - else - return true; - } - - function setWatchStatus($subjectUserID) { - if (!is_numeric($subjectUserID)) - return false; - - $currentUserID = $this->getCurrentUserId(); - $watched = $this->getWatchStatus($subjectUserID, $currentUserID); - - if ($watched) { - $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID); - if (!($dbresult =& $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - } else { - $values = array( + $this->getFieldName('primary'). ' = '. $this->db->sql_escape($cook[0]); + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row = $this->db->sql_fetchrow($dbresult)) { + $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')]; + $this->db->sql_freeresult($dbresult); + return $_SESSION[$this->getSessionKey()]; + } + } + return false; + } + + function login($username, $password, $remember = FALSE) { + $password = $this->sanitisePassword($password); + $query = 'SELECT '. $this->getFieldName('primary') .' FROM '. $this->getTableName() .' WHERE '. $this->getFieldName('username') .' = "'. $this->db->sql_escape($username) .'" AND '. $this->getFieldName('password') .' = "'. $this->db->sql_escape($password) .'"'; + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get user', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + if ($row =& $this->db->sql_fetchrow($dbresult)) { + $id = $_SESSION[$this->getSessionKey()] = $row[$this->getFieldName('primary')]; + if ($remember) { + $cookie = $id .':'. md5($username.$password); + setcookie($this->cookiekey, $cookie, time() + $this->cookietime, '/'); + } + $this->db->sql_freeresult($dbresult); + return true; + } else { + return false; + } + } + + function logout() { + @setcookie($this->getCookiekey(), '', time() - 1, '/'); + unset($_COOKIE[$this->getCookiekey()]); + session_unset(); + $this->getCurrentUser(TRUE, false); + } + + function getWatchlist($uId) { + // Gets the list of user IDs being watched by the given user. + $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($uId); + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $arrWatch = array(); + if ($this->db->sql_numrows($dbresult) == 0) { + $this->db->sql_freeresult($dbresult); + return $arrWatch; + } + while ($row =& $this->db->sql_fetchrow($dbresult)) { + $arrWatch[] = $row['watched']; + } + $this->db->sql_freeresult($dbresult); + return $arrWatch; + } + + function getWatchNames($uId, $watchedby = false) { + // Gets the list of user names being watched by the given user. + // - If $watchedby is false get the list of users that $uId watches + // - If $watchedby is true get the list of users that watch $uId + if ($watchedby) { + $table1 = 'b'; + $table2 = 'a'; + } else { + $table1 = 'a'; + $table2 = 'b'; + } + $query = 'SELECT '. $table1 .'.'. $this->getFieldName('username') .' FROM '. $GLOBALS['tableprefix'] .'watched AS W, '. $this->getTableName() .' AS a, '. $this->getTableName() .' AS b WHERE W.watched = a.'. $this->getFieldName('primary') .' AND W.uId = b.'. $this->getFieldName('primary') .' AND '. $table2 .'.'. $this->getFieldName('primary') .' = '. intval($uId) .' ORDER BY '. $table1 .'.'. $this->getFieldName('username'); + + if (!($dbresult =& $this->db->sql_query($query))) { + message_die(GENERAL_ERROR, 'Could not get watchlist', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $arrWatch = array(); + if ($this->db->sql_numrows($dbresult) == 0) { + $this->db->sql_freeresult($dbresult); + return $arrWatch; + } + while ($row =& $this->db->sql_fetchrow($dbresult)) { + $arrWatch[] = $row[$this->getFieldName('username')]; + } + $this->db->sql_freeresult($dbresult); + return $arrWatch; + } + + function getWatchStatus($watcheduser, $currentuser) { + // Returns true if the current user is watching the given user, and false otherwise. + $query = 'SELECT watched FROM '. $GLOBALS['tableprefix'] .'watched AS W INNER JOIN '. $this->getTableName() .' AS U ON U.'. $this->getFieldName('primary') .' = W.watched WHERE U.'. $this->getFieldName('primary') .' = '. intval($watcheduser) .' AND W.uId = '. intval($currentuser); + + if (! ($dbresult =& $this->db->sql_query($query)) ) { + message_die(GENERAL_ERROR, 'Could not get watchstatus', '', __LINE__, __FILE__, $query, $this->db); + return false; + } + + $arrWatch = array(); + if ($this->db->sql_numrows($dbresult) == 0) + return false; + else + return true; + } + + function setWatchStatus($subjectUserID) { + if (!is_numeric($subjectUserID)) + return false; + + $currentUserID = $this->getCurrentUserId(); + $watched = $this->getWatchStatus($subjectUserID, $currentUserID); + + if ($watched) { + $sql = 'DELETE FROM '. $GLOBALS['tableprefix'] .'watched WHERE uId = '. intval($currentUserID) .' AND watched = '. intval($subjectUserID); + if (!($dbresult =& $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not add user to watch list', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + } else { + $values = array( 'uId' => intval($currentUserID), 'watched' => intval($subjectUserID) - ); - $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $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 add user to watch list', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - } - - $this->db->sql_transaction('commit'); - return true; - } - - function addUser($username, $password, $email) { - // Set up the SQL UPDATE statement. - $datetime = gmdate('Y-m-d H:i:s', time()); - $password = $this->sanitisePassword($password); - $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime); - $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); - - // Execute the statement. - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - - // Everything worked out, so return true. - return true; - } - - function updateUser($uId, $password, $name, $email, $homepage, $uContent) { - if (!is_numeric($uId)) - return false; - - // Set up the SQL UPDATE statement. - $moddatetime = gmdate('Y-m-d H:i:s', time()); - if ($password == '') - $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent); - else - $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent); - $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId); - - // Execute the statement. - $this->db->sql_transaction('begin'); - if (!($dbresult = & $this->db->sql_query($sql))) { - $this->db->sql_transaction('rollback'); - message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db); - return false; - } - $this->db->sql_transaction('commit'); - - // Everything worked out, so return true. - 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; - } - - // Returns an array with admin uIds - function getAdminIds() { - $admins = array(); - foreach($GLOBALS['admin_users'] as $adminName) { - if($this->getIdFromUser($adminName) != NULL) - $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 sanitisePassword($password) { - return sha1(trim($password)); - } - - function generatePassword($uId) { - if (!is_numeric($uId)) - return false; - - $password = $this->_randompassword(); - - if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password))) - return $password; - else - return false; - } - - function isReserved($username) { - if (in_array($username, $GLOBALS['reservedusers'])) { - return true; - } else { - return false; - } - } - - function isValidUsername($username) { - if (strlen($username) < 4) { - return false; - }elseif (strlen($username) > 24) { - // too long usernames are cut by database and may cause bugs when compared - return false; - } elseif (preg_match('/(\W)/', $username) > 0) { - // forbidden non-alphanumeric characters - return false; - } - return true; - } - - - - function isValidEmail($email) { - if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) { - list($emailUser, $emailDomain) = split("@", $email); - - // Check if the email domain has a DNS record - //if ($this->_checkdns($emailDomain)) { - return true; - //} - } - return false; - } - - /** - * Sets a session variable. - * Updates it when it is already set. - * This is used to detect if cookies work. - * - * @return void - * - * @see isSessionStable() - */ - function updateSessionStability() { - //find out if we have cookies enabled - if (!isset($_SESSION['sessionStable'])) { - $_SESSION['sessionStable'] = 0; - } else { - $_SESSION['sessionStable'] = 1; - } - } - - /** - * Tells you if the session is fresh or old. - * If the session is fresh, it's the first page - * call with that session id. If the session is old, - * we know that cookies (or session persistance) works - * - * @return boolean True if the - * - * @see updateSessionStability() - */ - function isSessionStable() { - return $_SESSION['sessionStable'] == 1; - } - - function getFieldName($field) { return $this->fields[$field]; } - function setFieldName($field, $value) { $this->fields[$field] = $value; } - - function getSessionKey() { return $this->sessionkey; } - function setSessionKey($value) { $this->sessionkey = $value; } - - function getCookieKey() { return $this->cookiekey; } - function setCookieKey($value) { $this->cookiekey = $value; } + ); + $sql = 'INSERT INTO '. $GLOBALS['tableprefix'] .'watched '. $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 add user to watch list', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + } + + $this->db->sql_transaction('commit'); + return true; + } + + function addUser($username, $password, $email) { + // Set up the SQL UPDATE statement. + $datetime = gmdate('Y-m-d H:i:s', time()); + $password = $this->sanitisePassword($password); + $values = array('username' => $username, 'password' => $password, 'email' => $email, 'uDatetime' => $datetime, 'uModified' => $datetime); + $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values); + + // Execute the statement. + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not insert user', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + + // Everything worked out, so return true. + return true; + } + + function updateUser($uId, $password, $name, $email, $homepage, $uContent) { + if (!is_numeric($uId)) + return false; + + // Set up the SQL UPDATE statement. + $moddatetime = gmdate('Y-m-d H:i:s', time()); + if ($password == '') + $updates = array ('uModified' => $moddatetime, 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent); + else + $updates = array ('uModified' => $moddatetime, 'password' => $this->sanitisePassword($password), 'name' => $name, 'email' => $email, 'homepage' => $homepage, 'uContent' => $uContent); + $sql = 'UPDATE '. $this->getTableName() .' SET '. $this->db->sql_build_array('UPDATE', $updates) .' WHERE '. $this->getFieldName('primary') .'='. intval($uId); + + // Execute the statement. + $this->db->sql_transaction('begin'); + if (!($dbresult = & $this->db->sql_query($sql))) { + $this->db->sql_transaction('rollback'); + message_die(GENERAL_ERROR, 'Could not update user', '', __LINE__, __FILE__, $sql, $this->db); + return false; + } + $this->db->sql_transaction('commit'); + + // Everything worked out, so return true. + 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; + } + + // Returns an array with admin uIds + function getAdminIds() { + $admins = array(); + foreach($GLOBALS['admin_users'] as $adminName) { + if($this->getIdFromUser($adminName) != NULL) + $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 sanitisePassword($password) { + return sha1(trim($password)); + } + + function generatePassword($uId) { + if (!is_numeric($uId)) + return false; + + $password = $this->_randompassword(); + + if ($this->_updateuser($uId, $this->getFieldName('password'), $this->sanitisePassword($password))) + return $password; + else + return false; + } + + function isReserved($username) { + if (in_array($username, $GLOBALS['reservedusers'])) { + return true; + } else { + return false; + } + } + + function isValidUsername($username) { + if (strlen($username) < 4) { + return false; + }elseif (strlen($username) > 24) { + // too long usernames are cut by database and may cause bugs when compared + return false; + } elseif (preg_match('/(\W)/', $username) > 0) { + // forbidden non-alphanumeric characters + return false; + } + return true; + } + + + + function isValidEmail($email) { + if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,6})$", $email)) { + list($emailUser, $emailDomain) = split("@", $email); + + // Check if the email domain has a DNS record + //if ($this->_checkdns($emailDomain)) { + return true; + //} + } + return false; + } + + /** + * Sets a session variable. + * Updates it when it is already set. + * This is used to detect if cookies work. + * + * @return void + * + * @see isSessionStable() + */ + function updateSessionStability() { + //find out if we have cookies enabled + if (!isset($_SESSION['sessionStable'])) { + $_SESSION['sessionStable'] = 0; + } else { + $_SESSION['sessionStable'] = 1; + } + } + + /** + * Tells you if the session is fresh or old. + * If the session is fresh, it's the first page + * call with that session id. If the session is old, + * we know that cookies (or session persistance) works + * + * @return boolean True if the + * + * @see updateSessionStability() + */ + function isSessionStable() { + return $_SESSION['sessionStable'] == 1; + } + + function getFieldName($field) { return $this->fields[$field]; } + function setFieldName($field, $value) { $this->fields[$field] = $value; } + + function getSessionKey() { return $this->sessionkey; } + function setSessionKey($value) { $this->sessionkey = $value; } + + function getCookieKey() { return $this->cookiekey; } + function setCookieKey($value) { $this->cookiekey = $value; } } /* Defines a user. Rare fields are filled if required. */ class User { - var $id; - var $username; - var $name; - var $email; - var $homepage; - var $content; - var $datetime; - var $isAdmin; - - function User($id, $username) { - $this->id = $id; - $this->username = $username; - } - - function getId() { - return $this->id; - } - - function getUsername() { - return $this->username; - } - - function getName() { - // Look for value only if not already set - if(!isset($this->name)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->name = $user['name']; - } - return $this->name; - } - - function getEmail() { - // Look for value only if not already set - if(!isset($this->email)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->email = $user['email']; - } - return $this->email; - } - - function getHomepage() { - // Look for value only if not already set - if(!isset($this->homepage)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->homepage = $user['homepage']; - } - return $this->homepage; - } - - function getContent() { - // Look for value only if not already set - if(!isset($this->content)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->content = $user['uContent']; - } - return $this->content; - } - - function getDatetime() { - // Look for value only if not already set - if(!isset($this->content)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $user = $userservice->getUser($this->id); - $this->datetime = $user['uDatetime']; - } - return $this->datetime; - } - - function isAdmin() { - // Look for value only if not already set - if(!isset($this->isAdmin)) { - $userservice =SemanticScuttle_Service_Factory::get('User'); - $this->isAdmin = $userservice->isAdmin($this->id); - } - return $this->isAdmin; - } - - function getNbBookmarks($range = 'public') { - $bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); - return $bookmarkservice->countBookmarks($this->getId(), $range); - } + var $id; + var $username; + var $name; + var $email; + var $homepage; + var $content; + var $datetime; + var $isAdmin; + + function User($id, $username) { + $this->id = $id; + $this->username = $username; + } + + function getId() { + return $this->id; + } + + function getUsername() { + return $this->username; + } + + function getName() { + // Look for value only if not already set + if(!isset($this->name)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $user = $userservice->getUser($this->id); + $this->name = $user['name']; + } + return $this->name; + } + + function getEmail() { + // Look for value only if not already set + if(!isset($this->email)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $user = $userservice->getUser($this->id); + $this->email = $user['email']; + } + return $this->email; + } + + function getHomepage() { + // Look for value only if not already set + if(!isset($this->homepage)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $user = $userservice->getUser($this->id); + $this->homepage = $user['homepage']; + } + return $this->homepage; + } + + function getContent() { + // Look for value only if not already set + if(!isset($this->content)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $user = $userservice->getUser($this->id); + $this->content = $user['uContent']; + } + return $this->content; + } + + function getDatetime() { + // Look for value only if not already set + if(!isset($this->content)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $user = $userservice->getUser($this->id); + $this->datetime = $user['uDatetime']; + } + return $this->datetime; + } + + function isAdmin() { + // Look for value only if not already set + if(!isset($this->isAdmin)) { + $userservice =SemanticScuttle_Service_Factory::get('User'); + $this->isAdmin = $userservice->isAdmin($this->id); + } + return $this->isAdmin; + } + + function getNbBookmarks($range = 'public') { + $bookmarkservice =SemanticScuttle_Service_Factory::get('Bookmark'); + return $bookmarkservice->countBookmarks($this->getId(), $range); + } } ?> diff --git a/src/SemanticScuttle/Service/Vote.php b/src/SemanticScuttle/Service/Vote.php index e6d021b..086b2a2 100644 --- a/src/SemanticScuttle/Service/Vote.php +++ b/src/SemanticScuttle/Service/Vote.php @@ -142,7 +142,7 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService /** - * Returns if the user has already voted for + * Returns if the user has already voted for * the given bookmark. * * @param integer $bookmark Bookmark ID @@ -220,7 +220,7 @@ class SemanticScuttle_Service_Vote extends SemanticScuttle_DbService * * @internal * We check if voting is enabled or not, - * and if the user has already voted. + * and if the user has already voted. * It is up to the calling code to make sure * the user is authorized to vote. * |