--- drigg_pligg_import.module	2009-06-19 15:18:54.000000000 -0400
+++ drigg_pligg_import.module.new	2009-06-19 15:18:50.000000000 -0400
@@ -6,6 +6,30 @@
 * @file
 */
 
+function log_msg($msg) {
+  $logging_is_enabled = false;
+  $log_file           = '/tmp/drigg_pligg_import.log';
+
+  if ($logging_is_enabled !== true)
+    {return;}
+
+  $formatted_msg = $msg . "\n";
+
+  try {
+    $file = @fopen($log_file, 'a');
+    if ($file == false)
+      {throw new Exception("ERROR: Failed to open log file [" . $log_file . "]");}
+
+    if (fwrite($file, $formatted_msg) == false)
+      {throw new Exception("ERROR: Failed to write to log file [" . $log_file . "]");}
+
+    if (fclose($file) == false)
+      {throw new Exception("ERROR: Failed to close log file [" . $log_file . "]");}
+  }
+  catch(Exception $exception)
+    { /* Nothing's done about this. */ }
+}
+
 /**
  * Implementation of hook_help().
  */
@@ -267,7 +291,9 @@ function drigg_pligg_import_pligg_import
 
   if (module_exists('profile')) {
     db_query('DELETE FROM {profile_fields}');
+    log_msg('Emptied the Drigg profile_fields table');
     db_query('DELETE FROM {profile_values}');
+    log_msg('Emptied the Drigg profile_values table');
 
     db_query("INSERT INTO {profile_fields} VALUES (1,'Occupation','profile_occupation','Your occupation','User information','','textfield',0,0,1,3,0,'')");
     db_query("INSERT INTO {profile_fields} VALUES (2,'Home page','profile_home_page','Your home page','User information','','textfield',0,0,1,3,0,'') ");
@@ -280,12 +306,14 @@ function drigg_pligg_import_pligg_import
     db_query("INSERT INTO {profile_fields} VALUES (9,'Skype user name','profile_skype_user_name','Your Skype user name','Contact information','','textfield',0,0,1,3,0,'') ");
   }
 
+  log_msg("Importing Pligg users...");
   $query = sprintf("SELECT *,UNIX_TIMESTAMP(user_lastlogin) as user_lastlogin_t , UNIX_TIMESTAMP(user_date) as user_date_t FROM pligg_users where user_id != 1");
   $result = mysql_query($query, $drigg_pligg_import_link);
   while ($r = mysql_fetch_assoc($result)) {
 
     $drupal_pic_path = '';
     if ($r['user_avatar_source'] == 'useruploaded' && $copy_pictures) {
+      log_msg("\tCopying avatar for Pligg user_id [" . $r['user_id'] . "]");
       $from = $form_state['values']['avatar_path'] .'/'. $r['user_login'] .'_'. $form_state['values']['avatar_res'] .'.jpg';
       $to = "$drupal_img_path/picture-". $r['user_id'] .'.jpg';
       #drupal_set_message($from);
@@ -294,6 +322,7 @@ function drigg_pligg_import_pligg_import
       $drupal_pic_path = $to;
     }
     db_query("INSERT INTO {users} VALUES (%d, '%s', '%s', '%s', 0, 0, 0, '', '', %d, %d, %d, 1, NULL, '', '%s', '%s', NULL ) ", $r['user_id'], $r['user_login'], $r['user_pass'], $r['user_email'], $r['user_date_t'], $r['user_lastlogin_t'], $r['user_date_t'], $drupal_pic_path, $r['user_email']);
+    log_msg("\tCreated Pligg user_id [" . $r['user_id'] . "] in Drigg");
 
 
     #drupal_set_message("Here: ". $r['user_date_t']);
@@ -307,47 +336,59 @@ function drigg_pligg_import_pligg_import
       db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 7, $r['user_id'], $r['user_yahoo']);
       db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 8, $r['user_id'], $r['user_gtalk']);
       db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 9, $r['user_id'], $r['user_skype']);
+      log_msg("\tAdded Pligg profile details to Drigg for user_id [" . $r['user_id'] . "]");
     }
   }
+  log_msg("\tFinished importing Pligg users");
 
   $max_uid = db_result(db_query("SELECT max(uid) FROM {users}"));
   $max_uid ++;
   db_query("ALTER TABLE {users} AUTO_INCREMENT = %d", $max_uid);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg users table");
+
 
   /////////////////////////////////////////////////////////
   //          *** IMPORT THE TERMS ***
   /////////////////////////////////////////////////////////
 
-
   $vid = variable_get('drigg_section_vid', '');
   db_query('DELETE FROM {term_data}');
+  log_msg("Emptied the Drigg term_data table");
   db_query('DELETE FROM {term_hierarchy}');
+  log_msg("Emptied the Drigg term_hierarchy table");
 
+  log_msg('Importing Pligg categories...');
   $query = sprintf("SELECT * FROM pligg_categories where category_name <> 'All'");
   $result = mysql_query($query, $drigg_pligg_import_link);
   while ($r = mysql_fetch_assoc($result)) {
-    db_query("INSERT INTO {term_data} VALUES (%d, %d, '%s', '%s', %d) ", $r['category__auto_id'], $vid, $r['category_name'], $r['category_name'], $r['category_order']);
+    db_query("INSERT INTO {term_data} VALUES (%d, %d, '%s', '%s', %d) ", $r['category__auto_id'], $vid, $r['category_name'], '', $r['category_order']);
     db_query("INSERT INTO {term_hierarchy} VALUES (%d, 0)", $r['category__auto_id']);
+    log_msg("\tAdded category [" . $r['category_name'] . "] to Drigg");
   } 
 
   // Reset the auto_increment so that it doesn't go bananas
-  $max = db_result(db_query("SELECT max(tid) FROM {term_data}"));
-  $max ++;
+  $max = db_result(db_query("SELECT max(tid) FROM {term_data}")) + 1;
   db_query("ALTER TABLE {term_data} AUTO_INCREMENT = %d", $max);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg term_data table");
   
   // This is important: the sections have changed!
   variable_set("drigg_list", drigg_section_list());
 
+  
   /////////////////////////////////////////////////////////
   //          *** IMPORT THE NODES ***
   /////////////////////////////////////////////////////////
   
-  
   db_query("DELETE FROM {node}");
+  log_msg("Emptied the Drigg node table");
   db_query("DELETE FROM {term_node}");
+  log_msg("Emptied the Drigg term_node table");
   db_query("DELETE FROM {node_revisions}");
+  log_msg("Emptied the Drigg node_revisions table");
   db_query("DELETE FROM {drigg_node}");
+  log_msg("Emptied the Drigg drigg_node table");
 
+  log_msg('Importing queued and published Pligg scoops...');
   $tl = drigg_section_list();
   $query = sprintf("SELECT *,UNIX_TIMESTAMP(link_published_date) as link_published_date_t, UNIX_TIMESTAMP(link_date) as link_date_t, UNIX_TIMESTAMP(link_modified) as link_modified_t FROM pligg_links where (link_status = 'queued' OR link_status = 'published' )");
   $result = mysql_query($query, $drigg_pligg_import_link);
@@ -381,98 +422,105 @@ function drigg_pligg_import_pligg_import
 
     $d_teaser = node_teaser($d_body);
 
-    db_query("INSERT INTO {node_revisions}(nid, uid, title, body, teaser, log, timestamp, format) VALUES (%d, %d, '%s', '%s', '%s', '', 0, 1) ", $d_nid, $d_uid, $d_title, $d_body, $d_teaser);
+    db_query("INSERT INTO {node_revisions}
+      (nid,   uid,    title,    body,     teaser,     log,  timestamp,  format) VALUES
+      (%d,    %d,     '%s',     '%s',     '%s',       '',   0,          1) ",
+      $d_nid, $d_uid, $d_title, $d_body,  $d_teaser);
 
     $d_vid = db_last_insert_id('node_revisions', 'vid'); 
 		  
-    db_query("INSERT INTO {node} VALUES (%d, %d, '%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d) ", $d_nid, $d_vid, 'drigg', $d_title, $d_uid, 1, $d_created, $d_changed, $d_comment, $d_promote, 0, 0);
+    db_query("INSERT INTO {node} VALUES (
+      %d,       %d,       '%s',     '%s',       '%s',       %d,       %d,       %d,           %d,           %d,           %d,           %d,         %d,       %d,     %d)",
+      $d_nid,   $d_vid,   'drigg',  '',         $d_title,   $d_uid,   1,        $d_created,   $d_changed,   $d_comment,   $d_promote,   0,          0,        0,      0);
+#     nid,      vid,      type,     language,   title,      uid,      status,   created,      changed,      comment,      promote,      moderate,   sticky,   tnid,   translate
+
+    log_msg("\tCreated Pligg scoop: [nid = $d_nid] $d_title");
 
     $safe_section = $tl['lookup'][$c_category]['safe_name']; // IMPORTANT!
 
-    // This is because Pligg has a "1" in the URL for "Editorial" pieces.
-    // THis means that broken links will be created! 
-    if ($d_url === '1') {
-      $d_url = rand(0, 2000);
-    }
+    # Pligg sets the URL to "1" if the scoop is an editorial, and to "http://" if URLs aren't required.
+    # If either of these occurs, the Drigg node's URL needs to be corrected.
+    if (($d_url == '1') or ($d_url == 'http://'))
+      {$d_url = "node/$d_nid";}
+
     db_query("INSERT INTO {drigg_node} VALUES (%d, %d, %d, '%s', '%s', '%s', '%s', '%s', %d) ", $d_nid, $d_promoted_on, $d_killed, $d_url, $d_title_url, $safe_section, '', '', 0);
     db_query("INSERT INTO {term_node} VALUES (%d, %d, %d) ", $d_nid, $d_vid, $c_category);
   }
+  log_msg("\tFinished importing Pligg scoops");
 
   // Clean up the nodes that went wrong
   db_query("DELETE FROM {node} WHERE nid NOT IN (SELECT dnid FROM {drigg_node} WHERE dnid = nid)");
+  log_msg('Cleaned up the "nodes that went wrong"');
 
   // Reset the auto_increment for node so that it doesn't go bananas
-  $max = db_result(db_query("SELECT max(nid) FROM {node}"));
-  $max ++;
+  $max = db_result(db_query("SELECT max(nid) FROM {node}")) + 1;
   db_query("ALTER TABLE {node} AUTO_INCREMENT = %d", $max);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg node table");
   
   // Reset the auto_increment for node_revision so that it doesn't go bananas
-  $max = db_result(db_query("SELECT max(nid) FROM {node_revisions}"));
-  $max ++;
+  $max = db_result(db_query("SELECT max(vid) FROM {node_revisions}")) + 1;
   db_query("ALTER TABLE {node_revisions} AUTO_INCREMENT = %d", $max);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg node_revisions table");
+
 
   /////////////////////////////////////////////////////////
   //          *** IMPORT THE TAGS ***
   /////////////////////////////////////////////////////////
 
+  log_msg('Importing Pligg tags...');
+
   $query = sprintf("SELECT * FROM pligg_links where (link_status = 'queued' OR link_status = 'published' )");
   $result = mysql_query($query, $drigg_pligg_import_link);
   while ($r = mysql_fetch_assoc($result)) {
+    $all_tags = explode(',', $r['link_tags']);
 
-    $tags_here = explode(',', $r['link_tags']);
-   
-
-    foreach ($tags_here as $tag) {
+    foreach ($all_tags as $tag) {
       $tag = trim($tag);
 
-      if ($tag <> '') {
-        $new_tid = db_result(db_query("SELECT * from {term_data} where name = '%s' and vid = %d", $tag,  variable_get('drigg_tag_vid', '')));
-        if (!$new_tid) {
-          
-          db_query("INSERT INTO {term_data} VALUES (%d, '%s', '', 0) ", variable_get('drigg_tag_vid', ''), $tag);
-		  $new_tid = db_last_insert_id('term_data', 'tid');
-          db_query("INSERT INTO {term_hierarchy} VALUES (%d, 0)", $new_tid);
+      if ($tag != '') {
+        $tag_id = db_result(db_query("SELECT tid from {term_data} where name = '%s' and vid = %d", $tag,  variable_get('drigg_tag_vid', '')));
+
+        if (!$tag_id) {
+          db_query("INSERT INTO {term_data} VALUES (%d, %d, '%s', '', 0) ", $tag_id, variable_get('drigg_tag_vid', ''), $tag);
+     		  $tag_id = db_last_insert_id('term_data', 'tid');
+
+          log_msg("\tAdding tag [$tag] with id [$tag_id] to Drigg");
+          db_query("INSERT INTO {term_hierarchy} VALUES (%d, 0)", $tag_id);
         }
+
+        $link_vid = db_result(db_query('SELECT vid FROM node WHERE nid = %d', $r['link_id']));
     
-        db_query("INSERT INTO {term_node} VALUES (%d, %d, %d) ", $r['link_id'], $r['link_id'], $new_tid);
+        log_msg("\tAssociating Pligg link ID [" . $r['link_id'] . "] with tag ID [$tag_id]...");
+        db_query("INSERT INTO {term_node} VALUES (%d, %d, %d) ", $r['link_id'], $link_vid, $tag_id);
       }
     }
+  }
+  log_msg("\tFinished importing Pligg tags");
+  
+  // Reset the auto_increment for term_node so that it doesn't go bananas
+  $max = db_result(db_query("SELECT max(tid) FROM {term_data}")) + 1;
+  db_query("ALTER TABLE {term_data} AUTO_INCREMENT = %d", $max);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg term_data table");
 
- }
 
   /////////////////////////////////////////////////////////
   //          *** IMPORT THE COMMENTS ***
   /////////////////////////////////////////////////////////
 
   db_query("DELETE FROM {comments}");
+  log_msg('Emptied the Drigg comments table');
+
+  log_msg("Importing Pligg scoop comments...");
   $query = sprintf("SELECT *,UNIX_TIMESTAMP(comment_date) as comment_date_t FROM pligg_comments");
   $result = mysql_query($query, $drigg_pligg_import_link);
   while ($r = mysql_fetch_assoc($result)) {
 
+  # Pligg doesn't have "subjects" or "summaries" for scoops, but Drigg does. Thus, we need to use the beginning of a scoop's content as its subject.
+  # If the comment is too long to be used as a comment's subject, truncate the subject, and append "..." to imply that there's more content.
+  $subject = $r['comment_content'];
+  if (strlen($subject) > 35)
+    {$subject = substr($subject, 0, 32) . '...';}
 
-    // Trim the body. This could be done A MILLION times more elegantly,
-    // but I just can't be asked. This whole import will be obsolete very
-    // very soon anyway...
-    $b = $r['comment_content'];
-    #drupal_set_message("A: $b");
-    if ((strlen($b) > 35) && (strlen($b) > 1)) {
-      #drupal_set_message("MEH");
-      $w = strpos($b, " ", 30);
-      #drupal_set_message("W is $w");
-      if ($w < 45) {    
-        $b = substr($b, 0, $w);
-      } 
-      else {
-        $b = substr($b, 0, 35) .' [...]';
-      }
-    }
-
-    #drupal_set_message("B: $b");
-    #if ($ccc++ == 20) {
-    #  return;
-    #}
-
-    
     db_query("INSERT INTO {comments} VALUES (
       %d, %d, %d, %d,
       '%s', '%s', '%s', 
@@ -481,20 +529,27 @@ function drigg_pligg_import_pligg_import
       '%s', '%s', '%s', '%s') ", 
 
       $r['comment_id'], $r['comment_parent'], $r['comment_link_id'], $r['comment_user_id'], 
-      $b, $r['comment_content'], '', 
+      $subject, $r['comment_content'], '', 
       $r['comment_date_t'], 
       0, 1,
       '', NULL, NULL, NULL);
-  } 
+    log_msg("\tCreated Pligg comment in Drigg: [comment_id = " . $r['comment_id'] . "] " . $subject);
+  }
+  log_msg("\tFinished importing Pligg comments");
 
   // Clean up after pligg
   db_query("DELETE FROM {comments} WHERE {comments}.nid NOT in (SELECT nid FROM {node} n WHERE n.nid = {comments}.nid)");
+  log_msg('Cleaned up the "comments that went wrong"');
+
   // Reset the auto_increment for node_revision so that it doesn't go bananas
-  $max = db_result(db_query("SELECT max(cid) FROM {comments}"));
-  $max ++;
+  $max = db_result(db_query("SELECT max(cid) FROM {comments}")) + 1;
   db_query("ALTER TABLE {comments} AUTO_INCREMENT = %d", $max);
+  log_msg("Corrected the AUTO_INCREMENT setting for the Drigg comments table");
 
   db_query("DELETE FROM {node_comment_statistics}");
+  log_msg('Emptied the Drigg comment_statistics table');
+
+  log_msg("Importing Pligg comment statistics...");
   $query = sprintf("SELECT *,UNIX_TIMESTAMP(link_published_date) as link_published_date_t, UNIX_TIMESTAMP(link_date) as link_date_t, UNIX_TIMESTAMP(link_modified) as link_modified_t FROM pligg_links where (link_status = 'queued' OR link_status = 'published' )");
   $result = mysql_query($query, $drigg_pligg_import_link);
   while ($r = mysql_fetch_assoc($result)) {
@@ -503,18 +558,18 @@ function drigg_pligg_import_pligg_import
       
     _comment_update_node_statistics($r['link_id']);
   }
+  log_msg("\tFinished importing Pligg comment statistics");
 
+ 
   /////////////////////////////////////////////////////////
   //          *** IMPORT THE VOTES ***
   /////////////////////////////////////////////////////////
 
- 
-
+  log_msg("Importing Pligg votes...");
   $query = sprintf("SELECT *,UNIX_TIMESTAMP(vote_date) as vote_date_t FROM pligg_votes where vote_user_id <> 0");
-  #$query = sprintf("SELECT *,UNIX_TIMESTAMP(vote_date) as vote_date_t FROM pligg_votes where vote_user_id <> 0 ORDER BY vote_id DESC LIMIT 1000"); # FOR TESTING
   $result = mysql_query($query, $drigg_pligg_import_link);
+
   while ($r = mysql_fetch_assoc($result)) {
-   
     if ($r['vote_type'] == 'links') {
       $r_uid = db_result(db_query("SELECT uid FROM {node} where nid = %d", $r['vote_link_id']));
       $otype = 'n';
@@ -541,18 +596,17 @@ function drigg_pligg_import_pligg_import
     $new_vote['content_id'] = $r['vote_link_id'];
 
     votingapi_set_votes($new_vote);
+    log_msg("\tuser_id [" . var_export($new_vote['uid'], true) . '] voted for content_id [' . var_export($new_vote['content_id'], true) . ']');
 
     // A bit of a hack to change the timestamp of the cast vote
     db_query("UPDATE {votingapi_vote} SET timestamp = %d WHERE content_type = '%s' AND content_id = %d AND value_type = '%s' AND tag='%s' AND uid=%d",  $r['vote_date_t'], $content_type, $r['vote_link_id'], $value_type , $tag , $r['vote_user_id'] );
-
     #db_query("INSERT INTO {simple_karma} VALUES (%d,%d,'%s', %d,  %d, %d)", $r['vote_user_id'], $r['vote_link_id'], $otype, $r_uid, $karma, $r['vote_date_t']);
-
   }
-
+  log_msg("\tFinished importing Pligg votes");
 
   // That's it!
   drupal_set_message(t("Data imported!"));
-
+  log_msg("\tFinished the Pligg");
 }
 
 /////////////////////////////////////////////////////////////////
