'Pligg importer', 'description' => 'Pligg importer for drigg. Change $logging_is_enabled to TRUE to enable logging into /tmp/drigg_pligg_import.log', 'page callback' => 'drigg_pligg_import_pligg_importer', 'access callback' => 'user_access', 'access arguments' => array('administer site configuration'), 'type' => MENU_LOCAL_TASK, ); return $items; } ///////////////////////////////////////////////////////////////// // THE IMPORTER ///////////////////////////////////////////////////////////////// /** * The pligg importer's form. It only has a button, really. It does * check that the variable drigg_pligg_import_section_vid was set, or it will refuse * to do anything * * @param $edit * The input information * @return * The importer's form */ function drigg_pligg_import_pligg_importer($edit = array()) { if (variable_get('drigg_section_vid', '') == '' ) { return (t("Please configure the module first - the category NEEDS to be set!")); } $warning = t('WARNING: this is a DESTRUCTIVE operation. Do NOT run this on a production site, or you will lose everything. This is meant to be run on a freshly installed site, only ONCE.'); return '
'. $warning .'
'. drupal_get_form('drigg_pligg_import_pligg_importer_form', $edit); } /** * Returns the importer form * * * @param $edit * The input information * @return * The form, following Drupal's format */ function drigg_pligg_import_pligg_importer_form($edit = array()) { #$edit['host'] = 'localhost'; #$edit['login'] = 'root'; #$edit['password'] = ''; #$edit['db'] = ''; #$edit['avatar_path'] = ''; #$edit['avatar_res'] = '40'; #$edit['avatar_import'] = TRUE; $form['db_info'] = array( '#type' => 'fieldset', '#title' => t('DB information'), '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['db_info']['host'] = array( '#type' => 'textfield', '#description' => t('Host name'), '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#default_value' => $edit['host'], ); $form['db_info']['login'] = array( '#type' => 'textfield', '#description' => t('Login name'), '#size' => 60, '#maxlength' => 128, '#required' => FALSE, '#default_value' => $edit['login'], ); $form['db_info']['password'] = array( '#type' => 'textfield', '#description' => t('Password'), '#size' => 60, '#maxlength' => 128, '#required' => FALSE, '#default_value' => $edit['password'], ); $form['db_info']['db'] = array( '#type' => 'textfield', '#description' => t('Database name'), '#size' => 60, '#maxlength' => 128, '#required' => TRUE, '#default_value' => $edit['db'], ); $form['avatars'] = array( '#type' => 'fieldset', '#title' => t('Importing avatars'), '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['avatars']['avatar_import'] = array( '#type' => 'checkbox', '#description' => t('Import avatars?'), '#required' => FALSE, '#default_value' => $edit['avatar_import'], ); $form['avatars']['avatar_path'] = array( '#type' => 'textfield', '#description' => t("Path of pligg's avatars directory"), '#size' => 60, '#maxlength' => 128, '#required' => FALSE, '#default_value' => $edit['avatar_path'], ); $form['avatars']['avatar_res'] = array( '#type' => 'textfield', '#description' => t('Avatars resolution, as set in Pligg'), '#size' => 5, '#maxlength' => 6, '#required' => FALSE, '#default_value' => $edit['avatar_res'], ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Import!'), ); $form['submit'] = array( '#type' => 'submit', '#value' => t('Import!'), ); return ($form); } /** * Validate the pligg importer * * @param $form, $form_state * Usual drupal vars for hook_validate * @return * Nothing. It can call form_set_error though... */ function drigg_pligg_import_pligg_importer_form_validate($form, &$form_state) { global $drigg_pligg_import_link; $drigg_pligg_import_link = mysql_pconnect($form_state['values']['host'], $form_state['values']['login'], $form_state['values']['password']); if (!$drigg_pligg_import_link) { form_set_error('', t('Could not connect to the database!')); } else { if (!mysql_select_db($form_state['values']['db'], $drigg_pligg_import_link)) { form_set_error('db', t('Connection established, but could not find the db!')); } } if ($form_state['values']['avatar_import'] && !is_dir($form_state['values']['avatar_path'])) { form_set_error('avatar_path', t('The directory is incorrect!')); } if ($form_state['values']['avatar_import'] && $form_state['values']['avatar_res'] == 0) { form_set_error('avatar_res', t('Please specify a resolution')); } if (variable_get('drigg_section_vid', '') == 0) { form_set_error('', t("You NEED to configure a valid vocabulary for sections in Drigg's configuration first! Please configure Drigg")); } if (variable_get('drigg_tag_vid', '') == 0) { form_set_error('', t("You NEED to configure a valid vocabulary for tags in Drigg's configuration first! Please configure Drigg")); } } /** * Actually imports pligg's database. This is a highly destructive * procedure for the Drupal db it runs on. All of the users are * replaced. * * @param $form, $form_state * Usual drupal vars for hook_validate * @return * Nothing. It will call itself again, after a few drupal_set_message() */ function drigg_pligg_import_pligg_importer_form_submit($form, &$form_state) { global $drigg_pligg_import_link; set_time_limit(15000); /* db_query('DELETE FROM {users} where uid != 1'); db_query('DELETE FROM {profile_fields}'); db_query('DELETE FROM {term_data}'); db_query('DELETE FROM {term_hierarchy}'); db_query("DELETE FROM {node}"); db_query("DELETE FROM {term_node}"); db_query("DELETE FROM {node_revisions}"); db_query("DELETE FROM {drigg_pligg_import_node}"); db_query("DELETE FROM {comments}"); db_query("DELETE FROM {node_comment_statistics}"); db_query("DELETE from {simple_karma}"); */ ///////////////////////////////////////////////////////// // *** IMPORT THE USERS *** ///////////////////////////////////////////////////////// // If the user-pictures module is active, then DO import // the avatars. if (variable_get('user_pictures', 0) && $form_state['values']['avatar_import']) { $copy_pictures = TRUE; $drupal_img_path = variable_get('user_picture_path', 'pictures'); $drupal_img_path = file_create_path($drupal_img_path); } // DELETE all of the existing information (!) which will then be // reimported from pligg. Only delete the profule stuff if // the profile module is installed db_query('DELETE FROM {users} where uid != 1 AND uid != 0'); if (module_exists('profile')) { db_query('DELETE FROM {profile_fields}'); drigg_log_msg('Emptied the Drigg profile_fields table'); db_query('DELETE FROM {profile_values}'); drigg_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,'') "); db_query("INSERT INTO {profile_fields} VALUES (3,'Real name','profile_real_name','Your real name','User information','','textfield',0,0,1,3,0,'') "); db_query("INSERT INTO {profile_fields} VALUES (4,'Public email address','profile_public_email','Your public email address. Note: this will be shown to everybody!','Contact information','','textfield',0,0,1,3,0,'') "); db_query("INSERT INTO {profile_fields} VALUES (5,'AIM user name','profile_aim_user_name','Your AIM user name','Contact information','','textfield',0,0,1,3,0,'') "); db_query("INSERT INTO {profile_fields} VALUES (6,'MSN user name','profile_msn_user_name','Your MSN user name','Contact information','','textfield',0,0,1,3,0,'') "); db_query("INSERT INTO {profile_fields} VALUES (7,'Yahoo user name','profile_yahoo_user_name','Your Yahoo user name','Contact information','','textfield',0,0,1,3,0,'') "); db_query("INSERT INTO {profile_fields} VALUES (8,'GTalk user name','profile_gtalk_user_name','Your GTalk user name','Contact information','','textfield',0,0,1,3,0,'') "); 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,'') "); } drigg_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) { drigg_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); #drupal_set_message($to); @copy($from, $to); $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']); drigg_log_msg("\tCreated Pligg user_id [" . $r['user_id'] . "] in Drigg"); #drupal_set_message("Here: ". $r['user_date_t']); if (module_exists('profile')) { db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 1, $r['user_id'], $r['user_occupation']); db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 2, $r['user_id'], $r['user_url']); db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 3, $r['user_id'], $r['user_names']); db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 4, $r['user_id'], $r['user_public_email']); db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 5, $r['user_id'], $r['user_aim']); db_query("INSERT INTO {profile_values} VALUES (%d,%d,'%s') ", 6, $r['user_id'], $r['user_msn']); 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']); drigg_log_msg("\tAdded Pligg profile details to Drigg for user_id [" . $r['user_id'] . "]"); } } drigg_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); drigg_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}'); drigg_log_msg("Emptied the Drigg term_data table"); db_query('DELETE FROM {term_hierarchy}'); drigg_log_msg("Emptied the Drigg term_hierarchy table"); drigg_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_order']); db_query("INSERT INTO {term_hierarchy} VALUES (%d, 0)", $r['category__auto_id']); drigg_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}")) + 1; db_query("ALTER TABLE {term_data} AUTO_INCREMENT = %d", $max); drigg_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}"); drigg_log_msg("Emptied the Drigg node table"); db_query("DELETE FROM {term_node}"); drigg_log_msg("Emptied the Drigg term_node table"); db_query("DELETE FROM {node_revisions}"); drigg_log_msg("Emptied the Drigg node_revisions table"); db_query("DELETE FROM {drigg_node}"); drigg_log_msg("Emptied the Drigg drigg_node table"); drigg_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); while ($r = mysql_fetch_assoc($result)) { // Work out the status $s = $r['link_status']; if ($s == 'queued') { $d_promote = 0; $d_promoted_on = 0; } if ($s == 'published') { $d_promote = 1; $d_promoted_on = $r['link_published_date_t']; } $d_killed = 0; $d_comment = 2; $d_nid = $r['link_id']; $d_uid = $r['link_author']; $d_created = $r['link_date_t']; $d_changed = $r['link_modified_t']; $c_category = $r['link_category']; $d_url = $r['link_url']; $d_title = $r['link_title']; $d_title_url = $r['link_title_url']; $d_body = $r['link_content']; $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); $d_vid = db_last_insert_id('node_revisions', 'vid'); 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 drigg_log_msg("\tCreated Pligg scoop: [nid = $d_nid] $d_title"); $safe_section = $tl['lookup'][$c_category]['safe_name']; // IMPORTANT! # 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); } drigg_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)"); drigg_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}")) + 1; db_query("ALTER TABLE {node} AUTO_INCREMENT = %d", $max); drigg_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(vid) FROM {node_revisions}")) + 1; db_query("ALTER TABLE {node_revisions} AUTO_INCREMENT = %d", $max); drigg_log_msg("Corrected the AUTO_INCREMENT setting for the Drigg node_revisions table"); ///////////////////////////////////////////////////////// // *** IMPORT THE TAGS *** ///////////////////////////////////////////////////////// drigg_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']); foreach ($all_tags as $tag) { $tag = trim($tag); 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'); drigg_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'])); drigg_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); } } } drigg_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); drigg_log_msg("Corrected the AUTO_INCREMENT setting for the Drigg term_data table"); ///////////////////////////////////////////////////////// // *** IMPORT THE COMMENTS *** ///////////////////////////////////////////////////////// db_query("DELETE FROM {comments}"); drigg_log_msg('Emptied the Drigg comments table'); drigg_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) . '...';} db_query("INSERT INTO {comments} VALUES ( %d, %d, %d, %d, '%s', '%s', '%s', %d, %d, %d, '%s', '%s', '%s', '%s') ", $r['comment_id'], $r['comment_parent'], $r['comment_link_id'], $r['comment_user_id'], $subject, $r['comment_content'], '', $r['comment_date_t'], 0, 1, '', NULL, NULL, NULL); drigg_log_msg("\tCreated Pligg comment in Drigg: [comment_id = " . $r['comment_id'] . "] " . $subject); } drigg_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)"); drigg_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}")) + 1; db_query("ALTER TABLE {comments} AUTO_INCREMENT = %d", $max); drigg_log_msg("Corrected the AUTO_INCREMENT setting for the Drigg comments table"); db_query("DELETE FROM {node_comment_statistics}"); drigg_log_msg('Emptied the Drigg comment_statistics table'); drigg_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)) { db_query('INSERT INTO {node_comment_statistics} (nid, last_comment_timestamp, last_comment_name, last_comment_uid, comment_count) VALUES (%d, %d, NULL, %d, 0)', $r['link_id'], $r['link_id'], $r['link_author'] ); _comment_update_node_statistics($r['link_id']); } drigg_log_msg("\tFinished importing Pligg comment statistics"); ///////////////////////////////////////////////////////// // *** IMPORT THE VOTES *** ///////////////////////////////////////////////////////// drigg_log_msg("Importing Pligg votes..."); $query = sprintf("SELECT *,UNIX_TIMESTAMP(vote_date) as vote_date_t FROM pligg_votes where vote_user_id <> 0"); $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'; } else { $r_uid = db_result(db_query("SELECT uid FROM {comments} where cid = %d", $r['vote_link_id'])); $otype = 'c'; } $karma = 1; $v = $r['vote_value']; if ($v < 0 ) { $karma = -1; } // Give the actual vote using the Voting API - thanks Eaton! list($content_type, $value_type, $tag, $tmp) = drigg_votingapi_variables(); $new_vote = array(); $new_vote['value'] = $karma; $new_vote['value_type'] = $value_type; $new_vote['tag'] = $tag; $new_vote['uid'] = $r['vote_user_id']; $new_vote['content_type'] = $content_type; $new_vote['content_id'] = $r['vote_link_id']; votingapi_set_votes($new_vote); drigg_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']); } drigg_log_msg("\tFinished importing Pligg votes"); // That's it! drupal_set_message(t("Data imported!")); drigg_log_msg("\tFinished the Pligg"); } ///////////////////////////////////////////////////////////////// // END OF THE IMPORTER /////////////////////////////////////////////////////////////////