/** * Attach tags to a bookmark. * * Make sure that categories is an array of trimmed strings. * If the categories are coming in from an API call, be sure * that underscores are converted into strings. * * @param integer $bookmarkid ID of the bookmark * @param array $tags Array of tags (strings, trimmed) * @param boolean $fromApi If this is from an API call * @param string $extension File extension (i.e. 'pdf') * @param boolean $replace If existing tags for this bookmark * are to be replaced * @param boolean $fromImport If this is from a file import * * @return boolean True if all went well */ public function attachTags($bookmarkid, $tags, $fromApi = false, $extension = null, $replace = true, $fromImport = false) { 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(utf8_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) { continue; } for ($i = 0; $i < $nbPieces - 1; $i++) { $bookmark = $bs->getBookmark($bookmarkid); $uId = $bookmark['uId']; $tts->addLinkedTags($pieces[$i], $pieces[$i + 1], '=', $uId); } // Attach just the last tag to the bookmark $tags[$key] = $pieces[0]; } else { // case ">" $pieces = explode('>', $tag); $nbPieces = count($pieces); if ($nbPieces <= 1) { continue; } for ($i = 0; $i < $nbPieces - 1; $i++) { $bookmark = $bs->getBookmark($bookmarkid); $uId = $bookmark['uId']; $tts->addLinkedTags($pieces[$i], $pieces[$i + 1], '>', $uId); } // Attach just the last tag to the bookmark $tags[$key] = $pieces[$nbPieces - 1]; } } //after exploding, there may be duplicate keys //since we are in a transaction, hasTag() may // not return true for newly added duplicate tags $tags = array_unique($tags); // Add the tags to the DB. foreach ($tags as $tag) { if ($tag == '') { continue; } if ($this->hasTag($bookmarkid, $tag)) { continue; } $values = array('bId' => intval($bookmarkid), 'tag' => $tag); $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; }
$tag = str_replace(' ', '+', trim($_REQUEST['tag'])); } else { $tag = null; } // Get the posts relevant to the passed-in variables. $bookmarks = $bookmarkservice->getBookmarks(0, null, $userservice->getCurrentUserId(), $tag, null, getSortOrder()); // Set up the XML file and output all the posts. echo '<!DOCTYPE NETSCAPE-Bookmark-file-1>' . "\r\n"; echo '<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8" />'; echo '<!-- This is an automatically generated file. -->' . "\r\n"; echo '<TITLE>Bookmarks</TITLE>' . "\r\n"; echo '<H1 LAST_MODIFIED="' . date('U') . '">Bookmarks for ' . htmlspecialchars($currentUser->getUsername()) . '' . (is_null($tag) ? '' : ' tag="' . htmlspecialchars($tag) . '"') . " from " . $sitename . "</H1>\r\n"; echo '<DL>' . "\r\n"; foreach ($bookmarks['bookmarks'] as $row) { if (is_null($row['bDescription']) || trim($row['bDescription']) == '') { $description = ''; } else { $description = 'description="' . filter($row['bDescription'], 'xml') . '" '; } $taglist = ''; if (count($row['tags']) > 0) { foreach ($row['tags'] as $tag) { $taglist .= convertTag($tag) . ','; } $taglist = substr($taglist, 0, -1); } else { $taglist = 'system:unfiled'; } echo "\t<DT><A HREF=\"" . filter($row['bAddress'], 'xml') . '" ' . $description . ' hash="' . md5($row['bAddress']) . '" tags="' . filter($taglist, 'xml') . '" ADD_DATE="' . date('U', strtotime($row['bDatetime'])) . "\" >" . filter($row['bTitle'], 'xml') . "</a>\r\n"; } echo '</DL>';
<?php // Implements the del.icio.us API request for all a user's tags. // del.icio.us behavior: // - tags can't have spaces // Force HTTP authentication first! require_once 'httpauth.inc.php'; require_once '../header.inc.php'; $tagservice =& ServiceFactory::getServiceInstance('TagService'); $userservice =& ServiceFactory::getServiceInstance('UserService'); // Get the tags relevant to the passed-in variables. $tags =& $tagservice->getTags($userservice->getCurrentUserId()); // Set up the XML file and output all the tags. header('Content-Type: text/xml'); echo '<?xml version="1.0" standalone="yes" ?' . ">\r\n"; echo "<tags>\r\n"; foreach ($tags as $row) { echo "\t<tag count=\"" . $row['bCount'] . '" tag="' . filter(convertTag($row['tag'], 'out'), 'xml') . "\" />\r\n"; } echo "</tags>";
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; } } $tags_count = count($tags); for ($i = 0; $i < $tags_count; $i++) { $tags[$i] = trim(strtolower($tags[$i])); if ($fromApi) { include_once dirname(__FILE__) . '/../functions.inc.php'; $tags[$i] = convertTag($tags[$i], 'in'); } } if ($tags_count > 0) { // Remove system tags $tags = array_filter($tags, array($this, "isNotSystemTag")); // Eliminate any duplicate categories $temp = array_unique($tags); $tags = array_values($temp); } else { // Unfiled $tags[] = 'system:unfiled'; } // Media and file types if (!is_null($extension)) { include_once dirname(__FILE__) . '/../functions.inc.php'; if ($keys = multi_array_search($extension, $GLOBALS['filetypes'])) { $tags[] = 'system:filetype:' . $extension; $tags[] = 'system:media:' . array_shift($keys); } } // Imported if ($fromImport) { $tags[] = 'system:imported'; } $this->db->sql_transaction('begin'); if ($replace) { if (!$this->deleteTagsForBookmark($bookmarkid)) { $this->db->sql_transaction('rollback'); message_die(GENERAL_ERROR, 'Could not attach tags (deleting old ones failed)', '', __LINE__, __FILE__, $sql, $this->db); return false; } } // 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; }