Minor fix: improve import from Netscape files (now takes description into account)

git-svn-id: https://semanticscuttle.svn.sourceforge.net/svnroot/semanticscuttle/trunk@177 b3834d28-1941-0410-a4f8-b48e95affb8f
This commit is contained in:
mensonge 2008-11-27 10:13:29 +00:00
parent 1958ddd8e1
commit ed4760018f
4 changed files with 42 additions and 22 deletions

View file

@ -34,6 +34,8 @@ isset($_POST['status']) ? define('POST_STATUS', $_POST['status']): define('POST_
$tplVars = array(); $tplVars = array();
$countImportedBookmarks = 0;
$tplVars['msg'] = '';
if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) { if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['size'] > 0) {
$userinfo = $userservice->getCurrentObjectUser(); $userinfo = $userservice->getCurrentObjectUser();
@ -48,16 +50,22 @@ if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['si
$html = file_get_contents($_FILES['userfile']['tmp_name']); $html = file_get_contents($_FILES['userfile']['tmp_name']);
// Create link array // Create link array
preg_match_all('/<a\s+(.*?)\s*\/*>([^<]*)/si', $html, $matches); //preg_match_all('/<a\s+(.*?)\s*\/*>([^<]*)/si', $html, $matches);
preg_match_all('/<a\s+(.*?)>([^<]*?)<\/a>.*?(<dd>([^<]*)|<dt>)/si', $html, $matches);
//var_dump($matches);die();
$links = $matches[1]; $links = $matches[1];
$titles = $matches[2]; $titles = $matches[2];
$descriptions = $matches[4];
$size = count($links); $size = count($links);
for ($i = 0; $i < $size; $i++) { for ($i = 0; $i < $size; $i++) {
// echo "<hr/>"; // echo "<hr/>";
// echo $links[$i]."<br/>"; // echo $links[$i]."<br/>";
preg_match_all('/(\w*\s*=\s*"[^"]*")/', $links[$i], $attributes); preg_match_all('/(\w*\s*=\s*"[^"]*")/', $links[$i], $attributes);
//$attributes = $attributes[0]; // we keep just one row //$attributes = $attributes[0]; // we keep just one row
@ -82,28 +90,35 @@ if ($userservice->isLoggedOn() && sizeof($_FILES) > 0 && $_FILES['userfile']['si
break; break;
} }
} }
$bTitle = eregi_replace('"', '&quot;', trim($titles[$i])); $bTitle = trim($titles[$i]);
$bDescription = trim($descriptions[$i]);
if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) { if ($bookmarkservice->bookmarkExists($bAddress, $userservice->getCurrentUserId())) {
$tplVars['error'] = T_('You have already submitted this bookmark.'); $tplVars['error'] = T_('You have already submitted some of these bookmarks.');
} else { } else {
// If bookmark is local (like javascript: or place: in Firefox3), do nothing // If bookmark is local (like javascript: or place: in Firefox3), do nothing
if(substr($bAddress, 0, 7) == "http://") { if(substr($bAddress, 0, 7) == "http://" || substr($bAddress, 0, 8) == "https://") {
// If bookmark claims to be from the future, set it to be now instead // If bookmark claims to be from the future, set it to be now instead
if (strtotime($bDatetime) > time()) { if (strtotime($bDatetime) > time()) {
$bDatetime = gmdate('Y-m-d H:i:s'); $bDatetime = gmdate('Y-m-d H:i:s');
} }
if ($bookmarkservice->addBookmark($bAddress, $bTitle, NULL, $status, $bCategories, $bDatetime, false, true)) { if ($bookmarkservice->addBookmark($bAddress, $bTitle, $bDescription, $status, $bCategories, $bDatetime, false, true)) {
$tplVars['msg'] = T_('Bookmark imported.'); $countImportedBookmarks++;
} else { } else {
$tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.'); $tplVars['error'] = T_('There was an error saving your bookmark. Please try again or contact the administrator.');
} }
} }
} }
} }
header('Location: '. createURL('bookmarks', $userinfo->getUsername())); //header('Location: '. createURL('bookmarks', $userinfo->getUsername()));
$templatename = 'importNetscape.tpl';
$tplVars['msg'].= T_('Bookmarks found: ').$size.' ';
$tplVars['msg'].= T_('Bookmarks imported: ').' '.$countImportedBookmarks;
$tplVars['subtitle'] = T_('Import Bookmarks from Browser File');
$tplVars['formaction'] = createURL('importNetscape');
$templateservice->loadTemplate($templatename, $tplVars);
} else { } else {
$templatename = 'importNetscape.tpl'; $templatename = 'importNetscape.tpl';
$tplVars['subtitle'] = T_('Import Bookmarks from Browser File'); $tplVars['subtitle'] = T_('Import Bookmarks from Browser File');

View file

@ -42,7 +42,7 @@ class Bookmark2TagService {
} }
//clean tags from strange characters //clean tags from strange characters
$tags = str_replace(array('"', '\''), "_", $tags); $tags = str_replace(array('"', '\'', '/'), "_", $tags);
$tags_count = is_array($tags)?count($tags):0; $tags_count = is_array($tags)?count($tags):0;

View file

@ -92,10 +92,7 @@ class BookmarkService {
return; return;
} }
// If address doesn't contain ":", add "http://" as the default protocol $address = $this->normalize($address);
if (strpos($address, ':') === false) {
$address = 'http://'. $address;
}
$crit = array ('bHash' => md5($address)); $crit = array ('bHash' => md5($address));
if (isset ($uid)) { if (isset ($uid)) {
@ -117,13 +114,7 @@ class BookmarkService {
$sId = $userservice->getCurrentUserId(); $sId = $userservice->getCurrentUserId();
} }
// If bookmark address doesn't contain ":", add "http://" to the start as a default protocol $address = $this->normalize($address);
if (strpos($address, ':') === false) {
$address = 'http://'. $address;
}
if (substr($address, -1) == '/') {
$address = substr($address, 0, count($address)-2);
}
// Get the client's IP address and the date; note that the date is in GMT. // Get the client's IP address and the date; note that the date is in GMT.
if (getenv('HTTP_CLIENT_IP')) if (getenv('HTTP_CLIENT_IP'))
@ -462,6 +453,20 @@ class BookmarkService {
} }
return $this->db->sql_fetchfield(0, 0) - 1; return $this->db->sql_fetchfield(0, 0) - 1;
} }
function normalize($address) {
// If bookmark address doesn't contain ":", add "http://" to the start as a default protocol
if (strpos($address, ':') === false) {
$address = 'http://'. $address;
}
// Delete final /
if (substr($address, -1) == '/') {
$address = substr($address, 0, count($address)-2);
}
return $address;
}
function deleteAll() { function deleteAll() {
$query = 'TRUNCATE TABLE `'. $this->getTableName() .'`'; $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';

View file

@ -63,10 +63,10 @@ if(!isset($_GET['popup'])) {
if (isset($subtitle)) { if (isset($subtitle)) {
echo '<h2>'. $subtitle ."</h2>\n"; echo '<h2>'. $subtitle ."</h2>\n";
} }
if (isset($error)) { if (isset($error) && $msg!='') {
echo '<p class="error">'. $error ."</p>\n"; echo '<p class="error">'. $error ."</p>\n";
} }
if (isset($msg)) { if (isset($msg) && $msg!='') {
echo '<p class="success">'. $msg ."</p>\n"; echo '<p class="success">'. $msg ."</p>\n";
} }
?> ?>