| 12 |
define('_IMPORT_TYPEPAD', 'import_typepad'); |
define('_IMPORT_TYPEPAD', 'import_typepad'); |
| 13 |
define('_ACTION_DELETE_FILE', 'Delete file from server'); |
define('_ACTION_DELETE_FILE', 'Delete file from server'); |
| 14 |
|
|
| 15 |
function import_typepad_menu() { |
function import_typepad_help($section) { |
| 16 |
$items = array(); |
switch ($section) { |
| 17 |
$items['admin/content/import_typepad'] = array( |
case 'admin/modules#description': |
| 18 |
'title' => 'Import Typepad Content', |
return t('Import nodes from a typepad file.'); |
| 19 |
'page callback' => 'import_typepad_page', |
case 'admin/help#import_typepad': |
| 20 |
'weight' => 5, |
return t(' ', array('%admin-node-import_typepad' => url('admin/node/import_typepad'), '%admin-access-permission' => url('admin/access/permission'))); |
| 21 |
'access arguments' => array('import nodes'), |
} |
| 22 |
); |
} |
| 23 |
$items['admin/content/import_typepad/preview'] = array( |
|
| 24 |
'title' => 'type pad', |
function import_typepad_menu($may_cache) { |
| 25 |
'page callback' => 'import_typepad_preview', |
$links = array(); |
| 26 |
'weight' => 5, |
$links[] = array('path' => 'admin/node/import_typepad', 'title' => t('type pad'), 'callback' => 'import_typepad_page', 'weight' => 5, 'access' => user_access('import nodes')); |
| 27 |
'access arguments' => array('import nodes'), |
$links[] = array('path' => 'admin/node/import_typepad/preview', 'title' => t('type pad'), 'callback' => 'import_typepad_preview', 'weight' => 5, 'access' => user_access('import nodes'), 'type' => MENU_CALLBACK); |
| 28 |
'type' => MENU_CALLBACK, |
return $links; |
|
); |
|
|
return $items; |
|
| 29 |
} |
} |
| 30 |
|
|
| 31 |
function import_typepad_perm() { |
function import_typepad_perm() { |
| 35 |
/** |
/** |
| 36 |
* Show the front page, with either the current working file, or a file selection form |
* Show the front page, with either the current working file, or a file selection form |
| 37 |
*/ |
*/ |
| 38 |
function import_typepad_page() { |
function import_typepad_page($form_values=NULL) { |
| 39 |
|
|
| 40 |
if ($edit[_WORKING_FILE]) { |
if ($edit[_WORKING_FILE]) { |
| 41 |
$output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file'))); |
$output .= form_item(t('File'), $edit[_WORKING_FILE] .' ('. format_size( filesize($edit[_WORKING_FILE]) ) .')<br />'. form_submit(t('Use a different file'))); |
| 42 |
} |
} |
| 43 |
|
else { |
| 44 |
|
$form['upload_file'] = array( |
| 45 |
|
'#type' => 'file', |
| 46 |
|
'#title' => t('Upload Type Pad/Movable Type file'), |
| 47 |
|
'#description' => t('The export file you wish to import')); |
| 48 |
|
$form['serverside_file'] = array( |
| 49 |
|
'#type' => 'textfield', |
| 50 |
|
'#title' => t('Server side file'), |
| 51 |
|
'#description' => t('The file located on the server you wish to import')); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
// todo add an insert/update option |
// todo add an insert/update option |
|
$output = drupal_get_form('import_typepad_form_select'); |
|
|
|
|
|
return $output; |
|
|
} |
|
|
|
|
|
function import_typepad_form_select(){ |
|
|
$form['upload_file'] = array( |
|
|
'#type' => 'file', |
|
|
'#title' => t('Upload Type Pad/Movable Type file'), |
|
|
'#description' => t('The export file you wish to import')); |
|
|
$form['serverside_file'] = array( |
|
|
'#type' => 'textfield', |
|
|
'#title' => t('Server side file'), |
|
|
'#description' => t('The file located on the server you wish to import')); |
|
| 55 |
$form['submit'] = array( |
$form['submit'] = array( |
| 56 |
'#type' => 'submit', |
'#type' => 'submit', |
| 57 |
'#value' => t('Step 2')); |
'#value' => t('Step 2')); |
| 58 |
|
|
| 59 |
$form['#attributes'] = array('enctype' => 'multipart/form-data'); |
$form['#attributes'] = array('enctype' => 'multipart/form-data'); |
| 60 |
|
|
| 61 |
|
$output = drupal_get_form('typepad_step1', $form); |
| 62 |
|
|
| 63 |
return $form; |
return $output; |
| 64 |
} |
} |
| 65 |
|
|
| 66 |
/** |
/** |
| 67 |
* Discover which file we are using, and move it into the appropriate position |
* Discover which file we are using, and move it into the appropriate position |
| 68 |
*/ |
*/ |
| 69 |
function import_typepad_form_select_validate($form, &$form_state){ |
function typepad_step1_validate($form_id, $form_values){ |
| 70 |
global $user; |
global $user; |
| 71 |
global $base_url; |
global $base_url; |
| 72 |
|
|
| 78 |
//$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath; |
//$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $file->filepath; |
| 79 |
variable_set(_WORKING_FILE, $file->filepath); |
variable_set(_WORKING_FILE, $file->filepath); |
| 80 |
variable_set(_FILE_TYPE, 'UPLOAD'); |
variable_set(_FILE_TYPE, 'UPLOAD'); |
| 81 |
} else if (strlen($form_state['values']['serverside_file'])>0 && file_exists($form_state['values']['serverside_file'])){ |
} else if (strlen($form_values['serverside_file'])>0 && file_exists($form_values['serverside_file'])){ |
| 82 |
drupal_set_message("Using server file ".$form_state['values']['serverside_file']); |
drupal_set_message("Using server file ".$form_values['serverside_file']); |
| 83 |
// See if there was a server file |
// See if there was a server file |
| 84 |
variable_set(_WORKING_FILE, $form_state['values']['serverside_file']); |
variable_set(_WORKING_FILE, $form_values['serverside_file']); |
| 85 |
variable_set(_FILE_TYPE, 'SERVER'); |
variable_set(_FILE_TYPE, 'SERVER'); |
| 86 |
//$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile']; |
//$_SESSION[_IMPORT_TYPEPAD][_WORKING_FILE] = $edit['serverfile']; |
| 87 |
} |
} |
| 94 |
/** |
/** |
| 95 |
* If the validation passed then simple redirect to the preview |
* If the validation passed then simple redirect to the preview |
| 96 |
*/ |
*/ |
| 97 |
function import_typepad_form_select_submit($form, &$form_state){ |
function typepad_step1_submit($form_id, $form_values){ |
| 98 |
$form_state['redirect'] = 'admin/content/import_typepad/preview'; |
return 'admin/node/import_typepad/preview'; |
| 99 |
} |
} |
| 100 |
|
|
| 101 |
/** |
/** |
| 104 |
* In due course we'll pass the preview count via the parameters |
* In due course we'll pass the preview count via the parameters |
| 105 |
*/ |
*/ |
| 106 |
function import_typepad_preview($previewCount = 10) { |
function import_typepad_preview($previewCount = 10) { |
|
$output = ''; |
|
| 107 |
$authors = array(); |
$authors = array(); |
| 108 |
|
$cats = array(); |
| 109 |
|
$output = ''; |
| 110 |
|
|
| 111 |
$items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], $previewCount, $authors, $cats, array()); |
$items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], $previewCount, $authors, $cats, array()); |
| 112 |
|
|
|
// Previews |
|
|
$output .= "<h3>".t("Preview Items To Import")."</h3>".$items; |
|
|
$output .= "<h3>".t("Select Import Options")."</h3>". |
|
|
drupal_get_form('import_typepad_form_process',$authors,$cats); |
|
| 113 |
|
|
| 114 |
|
// Refresh preview w/ entry count |
| 115 |
|
|
| 116 |
return $output; |
// Previews |
| 117 |
} |
$output .= $items; |
| 118 |
|
|
|
function import_typepad_form_process($form_state,$authors,$cats){ |
|
| 119 |
// Mappings and import |
// Mappings and import |
| 120 |
// Get user list |
// Get user list |
| 121 |
$users = array(); |
$users = array(); |
| 125 |
$users[$user->uid] = $user->name; |
$users[$user->uid] = $user->name; |
| 126 |
} |
} |
| 127 |
|
|
| 128 |
$import_form["author_mappings"] = array( |
$import_form["author_title"] = array( |
| 129 |
"#type" => "fieldset", |
"#type" => "markup", |
| 130 |
"#title" => t("Author mappings")); |
"#value" => t("<span>Author mappings</span>")); |
|
|
|
| 131 |
foreach ($authors as $author=>$uid){ |
foreach ($authors as $author=>$uid){ |
| 132 |
$import_form["author_mappings"][$author] = array( |
$import_form[$author] = array( |
| 133 |
"#type" => 'select', |
"#type" => 'select', |
| 134 |
"#title"=> $author, |
"#title"=> $author, |
| 135 |
'#options' => $users); |
'#options' => $users); |
| 136 |
} |
} |
| 137 |
|
|
| 138 |
$import_form["taxonomy_mappings"] = array( |
$import_form["taxonomy_title"] = array( |
| 139 |
"#type" => "fieldset", |
"#type" => "markup", |
| 140 |
"#title" => t("Taxonomy mappings")); |
"#value" => t("<span>Taxonomy mappings</span>")); |
| 141 |
|
|
| 142 |
foreach ($cats as $cat=>$count){ |
foreach ($cats as $cat=>$count){ |
| 143 |
$item = _import_typepad_taxonomy_list($cat); |
$item = _import_typepad_taxonomy_list($cat); |
| 144 |
$item['#title'] = "$cat ($count)"; |
$item['#title'] = "$cat ($count)"; |
| 145 |
$import_form["taxonomy_mappings"]['taxonomy_'.str_replace(' ','_',$cat)] = $item; |
$import_form['taxonomy_'.str_replace(' ','_',$cat)] = $item; |
| 146 |
} |
} |
| 147 |
|
|
| 148 |
$import_form["import_warning"] = array ( |
$import_form["import_warning"] = array ( |
| 149 |
"#type" => "markup", |
"#type" => "markup", |
| 150 |
"#value" => t('Importing may take a while, do not click \'Import\' more than once. To see progress, look at the <a href="?=admin/content/node">administer content</a> page in a new window.<br/><br/>')); |
"#value" => t('<span>Importing may take awhile, do not click \'Import\' more than once. To see progress, look at the <a href="?q=admin/node" target="_new">Administer -> Content</a> page in a new window.</span>') ); |
| 151 |
|
|
| 152 |
$import_form['submit'] = array('#type' => 'submit', '#value' => t('Import')); |
$import_form['submit'] = array('#type' => 'submit', '#value' => t('Import')); |
| 153 |
|
|
| 154 |
return $import_form; |
$output .= drupal_get_form('import_typepad_process', $import_form); |
| 155 |
|
|
| 156 |
|
return $output; |
| 157 |
} |
} |
| 158 |
|
|
| 159 |
/** |
/** |
| 163 |
function _import_typepad_taxonomy_list($name){ |
function _import_typepad_taxonomy_list($name){ |
| 164 |
if (user_access('access content')) { |
if (user_access('access content')) { |
| 165 |
$default = ''; |
$default = ''; |
| 166 |
$vocabs = taxonomy_get_vocabularies(); |
$vocabs = taxonomy_get_vocabularies('blog'); |
| 167 |
$items = array(0=>t("(none)")); |
$items = array(); |
| 168 |
foreach ($vocabs as $vocab) { |
foreach ($vocabs as $vocab) { |
| 169 |
$tree = taxonomy_get_tree($vocab->vid); |
$tree = taxonomy_get_tree($vocab->vid); |
| 170 |
foreach ($tree as $term) { |
foreach ($tree as $term) { |
| 171 |
$items[$term->tid] = $vocab->name." | ".$term->name; |
$items[$term->tid] = _taxonomy_depth($term->depth) .' '.$term->name; |
| 172 |
if ($name == $term->name){ |
if ($name == $term->name){ |
| 173 |
$default = $term->tid; |
$default = $term->tid; |
| 174 |
} |
} |
| 175 |
} |
} |
| 176 |
} |
} |
| 177 |
return array( |
return array( |
| 184 |
/** |
/** |
| 185 |
* Simply run the import, passing the form in for the taxonomy and author maps |
* Simply run the import, passing the form in for the taxonomy and author maps |
| 186 |
*/ |
*/ |
| 187 |
function import_typepad_form_process_submit($form, &$form_state){ |
function import_typepad_process_submit($formid, $form_elements){ |
| 188 |
$cats = array(); // unused |
$cats = array(); // unused |
| 189 |
$items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_state['values'], $cats, $form_state['values']); |
$items = _import_typepad_get_nodes( variable_get(_WORKING_FILE, null), $edit['type'], 0, $form_elements, $cats, $form_elements); |
| 190 |
if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){ |
if ( variable_get(_FILE_TYPE, null) == 'UPLOAD'){ |
| 191 |
unlink( variable_get(_WORKING_FILE, null) ); |
unlink( variable_get(_WORKING_FILE, null) ); |
| 192 |
} |
} |
| 193 |
variable_del(_WORKING_FILE); |
variable_del(_WORKING_FILE); |
| 194 |
variable_del(_FILE_TYPE); |
variable_del(_FILE_TYPE); |
| 195 |
$form_state['redirect'] = 'admin/content/import_typepad'; |
return 'admin/node/import_typepad'; |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
/** |
/** |
| 264 |
* |
* |
| 265 |
* @return preview |
* @return preview |
| 266 |
*/ |
*/ |
| 267 |
function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy, $type="blog") { |
function _import_typepad_get_nodes($path, $type, $preview, &$authors, &$cats, $taxonomy) { |
| 268 |
$handle = fopen($path, 'r'); |
$handle = fopen($path, 'r'); |
| 269 |
if ($handle == null) |
if ($handle == null) |
| 270 |
return false; |
return false; |
| 343 |
// Start new blog |
// Start new blog |
| 344 |
$blogCats = array(); |
$blogCats = array(); |
| 345 |
$currentBlog = new StdClass(); |
$currentBlog = new StdClass(); |
| 346 |
$currentBlog->type = $type; |
$currentBlog->type = 'blog'; |
| 347 |
$currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) ); |
$currentBlog->author = trim( substr($line, strpos($line,"AUTHOR:") + strlen("AUTHOR:")) ); |
| 348 |
// We generate a hash for the author name which can be used in HTML forms |
// We generate a hash for the author name which can be used in HTML forms |
| 349 |
$authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author); |
$authorHash = 'author_map_'.str_replace(" ","_",$currentBlog->author); |
| 379 |
} else if ($state == 2){ |
} else if ($state == 2){ |
| 380 |
//echo "<!-- $name $val $sep -->\n"; |
//echo "<!-- $name $val $sep -->\n"; |
| 381 |
if (strlen($name) > 0 && array_key_exists($name, $mapping)){ |
if (strlen($name) > 0 && array_key_exists($name, $mapping)){ |
| 382 |
$currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = trim($value); |
$currentBlog->comments[count($currentBlog->comments)-1]->$mapping[$name] = $value; |
| 383 |
} else { |
} else { |
| 384 |
$currentBlog->comments[count($currentBlog->comments)-1]->comment .= trim($rawLine); |
$currentBlog->comments[count($currentBlog->comments)-1]->comment .= $rawLine; |
| 385 |
} |
} |
| 386 |
} else if ($state == 3){ |
} else if ($state == 3){ |
| 387 |
// Do nothing (Skip) |
// Do nothing (Skip) |
| 423 |
} |
} |
| 424 |
*/ |
*/ |
| 425 |
if ($success) { |
if ($success) { |
| 426 |
drupal_set_message(t('Successfully imported %count.', array('%count' => $success . format_plural($success, ' node', ' nodes')))); |
drupal_set_message(t('Successfully imported %count.', array('%count' => format_plural($success, '1 node', '%count nodes')))); |
| 427 |
} |
} |
| 428 |
|
|
| 429 |
return $output; |
return $output; |
| 449 |
|
|
| 450 |
// Save taxonomy items |
// Save taxonomy items |
| 451 |
$terms = array_unique($terms); |
$terms = array_unique($terms); |
| 452 |
taxonomy_node_save($currentBlog, $terms); |
taxonomy_node_save($currentBlog->nid, $terms); |
| 453 |
|
|
| 454 |
//echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n"; |
//echo "<!-- Number of comments = ".count($currentBlog->comments)." -->\n"; |
| 455 |
foreach ($currentBlog->comments as $comment){ |
foreach ($currentBlog->comments as $comment){ |
| 456 |
// Most of the following code is ripped straight from comment.module |
// Most of the following code is ripped straight from comment.module |
| 457 |
|
$cid = db_next_id('{comments}_cid'); |
| 458 |
|
|
| 459 |
// This is a comment with no parent comment (depth 0): we start |
// This is a comment with no parent comment (depth 0): we start |
| 460 |
// by retrieving the maximum thread level. |
// by retrieving the maximum thread level. |
| 461 |
$max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid'])); |
$max = db_result(db_query('SELECT MAX(thread) FROM {comments} WHERE nid = %d', $edit['nid'])); |
| 484 |
$thread = $decimals . $units .'/'; |
$thread = $decimals . $units .'/'; |
| 485 |
|
|
| 486 |
// Type pad doesn't have threaded comments, or subject lines, so we have to improvise them |
// Type pad doesn't have threaded comments, or subject lines, so we have to improvise them |
| 487 |
db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, format, hostname, timestamp, status, thread, name, mail, homepage) VALUES (%d, %d, %d, '%s', '%s', %d, '%s', %d, %d, '%s', '%s', '%s', '%s')", $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, $thread, $comment->name, $comment->mail, $comment->homepage); |
db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, format, hostname, timestamp, status, score, users, thread, name, mail, homepage) VALUES (%d, %d, %d, %d, '%s', '%s', %d, '%s', %d, %d, %d, '%s', '%s', '%s', '%s', '%s')", |
| 488 |
|
$cid, $currentBlog->nid, 0/*pid*/, 0, "re: ".$currentBlog->title, $comment->comment, 1/*format*/, $comment->hostname, $comment->timestamp, 0/*status*/, 0/*score*/, ''/*$users*/, $thread, $comment->name, $comment->mail, $comment->homepage); |
| 489 |
db_last_insert_id('comments', 'cid'); |
_comment_update_node_statistics($nid); |
|
// _comment_update_node_statistics($nid); |
|
| 490 |
} |
} |
| 491 |
} |
} |
| 492 |
} |
} |