* Just put this in your apb root directory and call it as
* logged in and follow the instructions on your monitor.
* Works with the Netscape export file (Firefox, Mozilla)
*/
require_once ('apb.php');
if (empty ($APB_SETTINGS['auth_user_id']))
header ('location: index.php');
if (!empty ($_FILES))
{
$file = file ($_FILES['import']['tmp_name']);
$cathegoriess = array ();
$bookmarks = array ();
$private = (!empty ($_POST['private']) ? 1 : 0);
if (!empty ($_POST['dry_run']))
define ('DRY_RUN', true);
$cat_id = 0; // The current cathegory ID
$cat_level = -1; // How deep in the tree we are
$level_meaning = array (); // A small hasharray that is indexed by the level and
// gives the cat_id of that level (w00t makes this work!)
foreach ($file as $row)
{
$tmp = array ();
/* If we see any of these it means we're having a new
* cathegory comming up - so indent and put up another cat_id
*/
if (preg_match_all ('#
#', $row, $tmp))
{
$cat_parent = $cat_id;
$cat_id++;
$cat_level++;
$level_meaning[$cat_level] = $cat_id;
}
/* If we see this it means we're quitting the current cathegory
* so lets put down the level and the child
* And change the meaning of the level
*/
elseif (preg_match_all ('#
|
#', $row, $tmp))
{
$cat_parent = $cat_id -1;
$cat_level--;
if ($cat_level == 0)
$cat_parent = 0;
$level_meaning[$cat_level] = $cat_id;
}
/* This is the actual cathegory name so save it away safely */
elseif (preg_match_all ('#(.*?)#', $row, $tmp))
{
$cathegory[$cat_id] = array (
'child' => (!empty ($level_meaning[$cat_level -1]) ? $level_meaning[$cat_level -1] : 0),
'name' => $tmp['1']['0'],
);
}
/* Ze bookmark itself!
* Put it under the cathegory id of it's cathegory so we have an easy
* way of calling for it later on
*/
elseif (preg_match_all ('#(.*?)#', $row, $tmp))
{
$tmp_id = (!empty ($level_meaning[$cat_level -1]) ? $level_meaning[$cat_level -1] : 0);
$bookmarks[$tmp_id][] = array (
'url' => $tmp['1']['0'],
'title' => $tmp['3']['0'],
'desc' => '',
);
}
/* Sometimes we get a comment to a bookmark or more of a
* description, and if that occurs we want to put it where it
* belong, and no it's not up somewhere the sun doesn't shine.
*/
elseif (preg_match_all ('#(.*?)</dd>|(.*?)#i', $row, $tmp))
{
$tmp_id = (!empty ($level_meaning[$cat_level -1]) ? $level_meaning[$cat_level -1] : 0);
if (!empty ($tmp['1']['0']))
{
$count = count ($bookmarks[$tmp_id]);
$bookmarks[$tmp_id][$count -1]['desc'] = $tmp['1']['0'];
}
}
}
/* Now lets start the deal of importing these buggers.
* I'm going a perhaps awkward way to do this, but I feel that if
* you got a huge collection of bookmarks it will benefit from adding all
* of them at the same time. So what we do is that we first create the
* cathegories. Saves the ids we get when we create them in a hasharray,
* based on the internal id from earlier.
*
* Then after that we compile a huge insert with all the bookmarks
* and then, finally, we make the big insert and all our bookmarks
* are in our nifty little script!
*/
$db_id = array ();
foreach ($cathegory as $id => $cat)
{
if (!defined ('DRY_RUN'))
$child = (($cat['child'] != 0) ? $db_id[$cat['child']] : 0);
else
$child = 0;
$cat['name'] = addslashes ($cat['name']);
$query = "INSERT INTO apb_groups
(group_parent_id, group_title, user_id, group_creation_date)
VALUES ('{$child}', '{$cat['name']}', '{$APB_SETTINGS['auth_user_id']}', now())";
if (!defined ('DRY_RUN'))
{
mysql_query ($query)
or die ("Error creating groups: {$query}: ". mysql_error ());
$db_id[$id] = mysql_insert_id ();
}
else
print 'The query to add a cathegory: '. $query .'
';
}
$query = 'INSERT INTO apb_bookmarks (group_id, bookmark_title, bookmark_url, bookmark_description, bookmark_creation_date, bookmark_private, user_id) VALUES ';
foreach ($bookmarks as $id => $bookmark_row)
{
foreach ($bookmark_row as $bookmark)
{
$bookmark['url'] = html_entity_decode (addslashes ($bookmark['url']));
$bookmark['title'] = html_entity_decode (addslashes ($bookmark['title']));
$bookmark['desc'] = html_entity_decode (addslashes ($bookmark['desc']));
if (!defined ('DRY_RUN'))
$tmp = (($id != 0) ? $db_id[$id] : 0);
else
$tmp = 0;
$query .= "('{$tmp}', '{$bookmark['title']}', '{$bookmark['url']}', '{$bookmark['desc']}', now(), '{$private}', '{$APB_SETTINGS['auth_user_id']}'),";
}
}
$query = substr ($query, 0, strlen ($query) -1);
if (!defined ('DRY_RUN'))
{
mysql_query ($query)
or die ('Error adding big batch \'o bookmarks: ' . mysql_error () . '
' . $query);
die ('Everything went well! Click here to go back!');
}
else
print 'Now you have to remember that we don\'t have the relationship id\'s in controll, that we do in the real run.
'. $query;
}
?>
Choose the file you want to import: