6 function &getInstance(&$db) {
9 $instance =& new TagService($db);
13 function TagService(&$db) {
15 $this->tablename = $GLOBALS['tableprefix'] .'tags';
18 function isNotSystemTag($var) {
19 if (utf8_substr($var, 0, 7) == 'system:')
25 function attachTags($bookmarkid, $tags, $fromApi = false, $extension = NULL, $replace = true, $fromImport = false) {
26 // Make sure that categories is an array of trimmed strings, and that if the categories are
27 // coming in from an API call to add a bookmark, that underscores are converted into strings.
28 if (!is_array($tags)) {
31 if (substr($tags, -1) == ',') {
32 $tags = substr($tags, 0, -1);
35 $tags = explode(' ', $tags);
37 $tags = explode(',', $tags);
44 $tags_count = count($tags);
45 for ($i = 0; $i < $tags_count; $i++) {
46 $tags[$i] = trim(strtolower($tags[$i]));
48 include_once(dirname(__FILE__) .'/../functions.inc.php');
49 $tags[$i] = convertTag($tags[$i], 'in');
53 if ($tags_count > 0) {
55 $tags = array_filter($tags, array($this, "isNotSystemTag"));
57 // Eliminate any duplicate categories
58 $temp = array_unique($tags);
59 $tags = array_values($temp);
62 $tags[] = 'system:unfiled';
65 // Media and file types
66 if (!is_null($extension)) {
67 include_once(dirname(__FILE__) .'/../functions.inc.php');
68 if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) {
69 $tags[] = 'system:filetype:'. $extension;
70 $tags[] = 'system:media:'. array_shift($keys);
76 $tags[] = 'system:imported';
79 $this->db->sql_transaction('begin');
82 if (!$this->deleteTagsForBookmark($bookmarkid)){
83 $this->db->sql_transaction('rollback');
84 message_die(GENERAL_ERROR, 'Could not attach tags (deleting old ones failed)', '', __LINE__, __FILE__, $sql, $this->db);
89 // Add the categories to the DB.
90 for ($i = 0; $i < count($tags); $i++) {
91 if ($tags[$i] != '') {
93 'bId' => intval($bookmarkid),
97 if (!$this->hasTag($bookmarkid, $tags[$i])) {
98 $sql = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
99 if (!($dbresult =& $this->db->sql_query($sql))) {
100 $this->db->sql_transaction('rollback');
101 message_die(GENERAL_ERROR, 'Could not attach tags', '', __LINE__, __FILE__, $sql, $this->db);
107 $this->db->sql_transaction('commit');
111 function deleteTag($tag) {
112 $userservice =& ServiceFactory::getServiceInstance('UserService');
113 $logged_on_user = $userservice->getCurrentUserId();
115 $query = 'DELETE FROM '. $this->getTableName() .' USING '. $GLOBALS['tableprefix'] .'tags, '. $GLOBALS['tableprefix'] .'bookmarks WHERE '. $GLOBALS['tableprefix'] .'tags.bId = '. $GLOBALS['tableprefix'] .'bookmarks.bId AND '. $GLOBALS['tableprefix'] .'bookmarks.uId = '. $logged_on_user .' AND '. $GLOBALS['tableprefix'] .'tags.tag = "'. $this->db->sql_escape($tag) .'"';
117 if (!($dbresult =& $this->db->sql_query($query))) {
118 message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db);
125 function deleteTagsForBookmark($bookmarkid) {
126 if (!is_int($bookmarkid)) {
127 message_die(GENERAL_ERROR, 'Could not delete tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query);
131 $query = 'DELETE FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid);
133 if (!($dbresult =& $this->db->sql_query($query))) {
134 message_die(GENERAL_ERROR, 'Could not delete tags', '', __LINE__, __FILE__, $query, $this->db);
141 function &getTagsForBookmark($bookmarkid) {
142 if (!is_int($bookmarkid)) {
143 message_die(GENERAL_ERROR, 'Could not get tags (invalid bookmarkid)', '', __LINE__, __FILE__, $query);
147 $query = 'SELECT tag FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND LEFT(tag, 7) <> "system:" ORDER BY tag';
149 if (!($dbresult =& $this->db->sql_query($query))) {
150 message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db);
155 while ($row =& $this->db->sql_fetchrow($dbresult)) {
156 $tags[] = $row['tag'];
162 function &getTags($userid = NULL) {
163 $userservice =& ServiceFactory::getServiceInstance('UserService');
164 $logged_on_user = $userservice->getCurrentUserId();
166 $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'] .'tags AS T ON B.bId = T.bId';
168 $conditions = array();
169 if (!is_null($userid)) {
170 $conditions['U.'. $userservice->getFieldName('primary')] = intval($userid);
171 if ($logged_on_user != $userid)
172 $conditions['B.bStatus'] = 0;
174 $conditions['B.bStatus'] = 0;
177 $query .= ' WHERE '. $this->db->sql_build_array('SELECT', $conditions) .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag';
179 if (!($dbresult =& $this->db->sql_query($query))) {
180 message_die(GENERAL_ERROR, 'Could not get tags', '', __LINE__, __FILE__, $query, $this->db);
183 return $this->db->sql_fetchrowset($dbresult);
187 // Returns the tags related to the specified tags; i.e. attached to the same bookmarks
188 function &getRelatedTags($tags, $for_user = NULL, $logged_on_user = NULL, $limit = 10) {
189 $conditions = array();
190 // Only count the tags that are visible to the current user.
191 if ($for_user != $logged_on_user || is_null($for_user))
192 $conditions['B.bStatus'] = 0;
194 if (!is_null($for_user))
195 $conditions['B.uId'] = $for_user;
197 // Set up the tags, if need be.
198 if (is_numeric($tags))
200 if (!is_array($tags) and !is_null($tags))
201 $tags = explode('+', trim($tags));
203 $tagcount = count($tags);
204 for ($i = 0; $i < $tagcount; $i++) {
205 $tags[$i] = trim($tags[$i]);
208 // Set up the SQL query.
209 $query_1 = 'SELECT DISTINCTROW T0.tag, COUNT(B.bId) AS bCount FROM '. $GLOBALS['tableprefix'] .'bookmarks AS B, '. $this->getTableName() .' AS T0';
211 $query_3 = ' WHERE B.bId = T0.bId ';
212 if (count($conditions) > 0)
213 $query_4 = ' AND '. $this->db->sql_build_array('SELECT', $conditions);
216 // Handle the parts of the query that depend on any tags that are present.
217 for ($i = 1; $i <= $tagcount; $i++) {
218 $query_2 .= ', '. $this->getTableName() .' AS T'. $i;
219 $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]) .'"';
221 $query_5 = ' AND LEFT(T0.tag, 7) <> "system:" GROUP BY T0.tag ORDER BY bCount DESC, T0.tag';
222 $query = $query_1 . $query_2 . $query_3 . $query_4 . $query_5;
224 if (! ($dbresult =& $this->db->sql_query_limit($query, $limit)) ){
225 message_die(GENERAL_ERROR, 'Could not get related tags', '', __LINE__, __FILE__, $query, $this->db);
228 return $this->db->sql_fetchrowset($dbresult);
231 // Returns the most popular tags used for a particular bookmark hash
232 function &getRelatedTagsByHash($hash, $limit = 20) {
233 $userservice = & ServiceFactory :: getServiceInstance('UserService');
234 $sId = $userservice->getCurrentUserId();
236 if ($userservice->isLoggedOn()) {
237 $arrWatch = $userservice->getWatchList($sId);
238 // From public bookmarks or user's own
239 $privacy = ' AND ((B.bStatus = 0) OR (B.uId = '. $sId .')';
240 // From shared bookmarks in watchlist
241 foreach ($arrWatch as $w) {
242 $privacy .= ' OR (B.uId = '. $w .' AND B.bStatus = 1)';
247 $privacy = ' AND B.bStatus = 0 ';
250 $query = 'SELECT T.tag, COUNT(T.tag) AS bCount FROM sc_bookmarks AS B LEFT JOIN sc_tags 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';
252 if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) {
253 message_die(GENERAL_ERROR, 'Could not get related tags for this hash', '', __LINE__, __FILE__, $query, $this->db);
256 return $this->db->sql_fetchrowset($dbresult);
259 function &getPopularTags($user = NULL, $limit = 30, $logged_on_user = NULL, $days = NULL) {
260 // Only count the tags that are visible to the current user.
261 if (($user != $logged_on_user) || is_null($user) || ($user === false))
262 $privacy = ' AND B.bStatus = 0';
266 if (is_null($days) || !is_int($days))
269 $span = ' AND B.bDatetime > "'. date('Y-m-d H:i:s', time() - (86400 * $days)) .'"';
271 $query = 'SELECT T.tag, COUNT(T.bId) AS bCount FROM '. $this->getTableName() .' AS T, '. $GLOBALS['tableprefix'] .'bookmarks AS B WHERE ';
272 if (is_null($user) || ($user === false)) {
273 $query .= 'B.bId = T.bId AND B.bStatus = 0';
275 $query .= 'B.uId = '. $this->db->sql_escape($user) .' AND B.bId = T.bId'. $privacy;
277 $query .= $span .' AND LEFT(T.tag, 7) <> "system:" GROUP BY T.tag ORDER BY bCount DESC, tag';
279 if (!($dbresult =& $this->db->sql_query_limit($query, $limit))) {
280 message_die(GENERAL_ERROR, 'Could not get popular tags', '', __LINE__, __FILE__, $query, $this->db);
284 return $this->db->sql_fetchrowset($dbresult);
287 function hasTag($bookmarkid, $tag) {
288 $query = 'SELECT COUNT(*) AS tCount FROM '. $this->getTableName() .' WHERE bId = '. intval($bookmarkid) .' AND tag ="'. $this->db->sql_escape($tag) .'"';
290 if (! ($dbresult =& $this->db->sql_query($query)) ) {
291 message_die(GENERAL_ERROR, 'Could not find tag', '', __LINE__, __FILE__, $query, $this->db);
295 if ($row =& $this->db->sql_fetchrow($dbresult)) {
296 if ($row['tCount'] > 0) {
303 function renameTag($userid, $old, $new, $fromApi = false) {
304 $bookmarkservice =& ServiceFactory::getServiceInstance('BookmarkService');
306 if (is_null($userid) || is_null($old) || is_null($new))
309 // Find bookmarks with old tag
310 $bookmarksInfo =& $bookmarkservice->getBookmarks(0, NULL, $userid, $old);
311 $bookmarks =& $bookmarksInfo['bookmarks'];
314 $this->deleteTag($old);
317 foreach(array_keys($bookmarks) as $key) {
318 $row =& $bookmarks[$key];
319 $this->attachTags($row['bId'], $new, $fromApi, NULL, false);
325 function &tagCloud($tags = NULL, $steps = 5, $sizemin = 90, $sizemax = 225, $sortOrder = NULL) {
327 if (is_null($tags) || count($tags) < 1) {
331 $min = $tags[count($tags) - 1]['bCount'];
332 $max = $tags[0]['bCount'];
334 for ($i = 1; $i <= $steps; $i++) {
335 $delta = ($max - $min) / (2 * $steps - $i);
336 $limit[$i] = $i * $delta + $min;
338 $sizestep = ($sizemax - $sizemin) / $steps;
339 foreach ($tags as $row) {
341 for ($i = 1; $i <= $steps; $i++) {
342 if (!$next && $row['bCount'] <= $limit[$i]) {
343 $size = $sizestep * ($i - 1) + $sizemin;
347 $tempArray = array('size' => $size .'%');
348 $row = array_merge($row, $tempArray);
352 if ($sortOrder == 'alphabet_asc') {
353 usort($output, create_function('$a,$b','return strcasecmp(utf8_deaccent($a["tag"]), utf8_deaccent($b["tag"]));'));
360 function getTableName() { return $this->tablename; }
361 function setTableName($value) { $this->tablename = $value; }