<% include_once( "../modules/utils.inc.php" );
$u = $sessionUserId = $_COOKIE["c_uid"];
$tok = $authenticated = $_COOKIE["c_pass_token"];
if( $tok && $tok == md5( getPasswdForUser( $u ))) {
include_once( "$modules/header.inc.php" );
%>
<br />
<img src="/img/import-header.gif" />
<%
# 6. using the prep files, generate the proper SQL commands to
# INSERT link, tag, and link/tag rows. need to do some heavy state
# caching.
shout( "Processing...", "We're loading your $num_links links and $num_tags tags... <h1>COOL LOADING ANIMATION HERE...</h1>" );
$links = open_user_file_for_read( $config->get_deduped_links_cache_file_name() );
# note that we don't use the tags file anymore; any tag not used by the links is irrelevant, so we ignore it.
$import_tag_name = $config->get_import_tag_name();
$user = urlencode( $u );
$delimeter = $config->get_delimeter();
$inserted_tags = array();
$con_bulk_import = mysql_connect();
mysql_selectdb( getDBName() );
$db_errors = 0;
$imported_folder_ID = "";
# A. the links
##############################################
for( $i = 0; $i < sizeof( $links ); $i++ ) {
$line = rtrim( $links[$i] );
if( substr( $line, 0, 2 ) != "X " ) { # skip dupes
#e( "$i ::" );
$l = preg_split( "/$delimeter/", $line );
$url = urlencode( $l[0] );
$name = urlencode( $l[1] );
$taglist = array_slice( $l, 2, sizeof( $l ));
$qs = "INSERT INTO links VALUES( NULL, '$url', '$name', NULL, NULL, '$user', NULL, 1 );\n";
$q = mysql_query( $qs );
if( !$q ) {
++$db_errors;
l( "INSERTING LINK FAILED: $qs\n" );
}
# B. the tag / link associations
##############################################
$current_link_ID = mysql_insert_id();
for( $j = 0; $j < sizeof( $taglist ); $j++ ) {
$tag = urlencode( rtrim( $taglist[$j] ) );
# e( "ON tag $tag" );
# C. the tags
##############################################
$run_query = true;
# FIRST WE CHECK THE LOCAL CACHE
if( ! $inserted_tags[$tag] ) {
# THEN WE CHECK THE DATABASE
$tmp_ID = getIdForFolder( $taglist[$j], $u );
if( $tmp_ID ) {
$inserted_tags[$tag] = $tmp_ID;
$current_tag_ID = $tmp_ID;
$run_query = false; # if we have seen this before, dont try to insert it again
# IF THIS WAS THE FIRST TIME WE SAW $imported_tag_name ("imported links"), SAVE IT FOR LINK-OUTS
if( $taglist[$j] == $import_tag_name && !$imported_folder_ID )
$imported_folder_ID = $tmp_ID;
}
# FINALLY, WITH NO RECOURSE LEFT TO US, WE INSERT THE NEW TAG IN THE DB
if( $run_query ) {
$qs = "INSERT INTO linksGroups VALUES( NULL, '$user', '$tag', '' );\n";
$q = mysql_query( $qs );
if( !$q ) {
++$db_errors;
l( "INSERTING TAG FAILED: $qs\n" );
} else {
# FIRST, WE SAVE THE NEW TAG ID FOR INSERTING IN THE XREF TABLE
$current_tag_ID = mysql_insert_id();
# THEN, WE CACHE IT IN CASE WE SEE THIS TAG AGAIN:
$inserted_tags[$tag] = $current_tag_ID;
# FINALLY, IF WE JUST LOOKED UP $imported_tag_name ("imported links") SAVE IT FOR LINK-OUTS
if( $taglist[$j] == $import_tag_name && ! $imported_folder_ID )
$imported_folder_ID = $current_tag_ID;
}
}
} else {
$current_tag_ID = $inserted_tags[$tag];
}
if( $current_link_ID && $current_tag_ID ) {
$qs = "INSERT INTO linksCategoriesXRef VALUES( $current_link_ID, $current_tag_ID );\n";
$q = mysql_query( $qs );
if( !$q ) {
++$db_errors;
l( "INSERTING LINK-TAG XREF FAILED: $qs\n" );
}
} else {
++$db_errors;
l( "current_link_ID and/or current_tag_ID were 0, meaning a database INSERT failed on import" );
}
}
}
}
conClose( $con_bulk_import );
echo bounceTo( "/import/complete?imported_folder_ID=$imported_folder_ID&num_issues=$db_errors" );
include_once( "$modules/footer.inc.php" );
} else {
warn( "Please log in first..." );
echo bounceTo( $site . "/login?bounceToPage=import/" );
}
%>