--- wordpress_import.module.txt 2008-09-03 15:32:46.000000000 -0400 +++ wordpress_import.module 2008-12-11 01:35:46.000000000 -0500 @@ -1,7 +1,9 @@ - http://www.rocq.net/yann/ This program is free software; you can redistribute it and/or modify @@ -24,15 +26,15 @@ */ function wordpress_import_help($section='') { - $output = ''; + $output = ''; - switch ($section) { - case "admin/help#wordpress_import": - $output = '

'. t("This module imports a WXR file generated by Wordpress blog software into Drupal"). '

'; - break; - } + switch ($section) { + case "admin/help#wordpress_import": + $output = '

'. t("This module imports a WXR file generated by Wordpress blog software into Drupal") .'

'; + break; + } - return $output; + return $output; } /** @@ -40,262 +42,251 @@ function wordpress_import_help($section= */ function wordpress_import_menu() { - $items = array(); - $access_config = array('administer site configuration'); - $items['admin/content/wordpress_import'] = array( - 'title' => t('Wordpress import'), - 'description' => t('Import Wordpress WXR file'), + $items = array(); + $access_config = array('administer site configuration'); + $items['admin/content/wordpress_import'] = array( + 'title' => 'Wordpress import', + 'description' => 'Import Wordpress WXR file', 'page callback' => 'drupal_get_form', 'page arguments' => array('wordpress_import_form'), 'access arguments' => $access_config - ); + ); - return $items; + return $items; } /** * Implementation of hook_form() */ -function wordpress_import_form($form_state) -{ - if (!isset($form_state['storage']['step'])) { - $form_state['storage']['step'] = 1; - } - - switch ($form_state['storage']['step']) - { - // Step 1 : Upload form +function wordpress_import_form($form_state) { + if (!isset($form_state['storage']['step'])) { + $form_state['storage']['step'] = 1; + } + + switch ($form_state['storage']['step']) { + // Step 1 : Upload form - case 1: - $form['informations'] = array( + case 1: + $form['informations'] = array( '#value' => t('This module will import a WXR file generated by Wordpress. You MUST make a backup copy of your Drupal Database before proceeding') - ); + ); - $opts = array('Server (already on saved as files/import.xml)','File upload (be aware of server file size limits)'); - // $opts = array('Server','File upload'); - $form['sourceopts'] = array( + $opts = array('Server (already on saved as files/import.xml)', 'File upload (be aware of server file size limits)'); + // $opts = array('Server', 'File upload'); + $form['sourceopts'] = array( '#type' => 'radios', '#options' => $opts, '#default_value' => '0', // 'Server' '#title' => t('Choose an upload source'), '#size' => 40 - ); - $form['upload'] = array( + ); + $form['upload'] = array( '#type' => 'file', '#title' => t('Upload your WXR file'), '#size' => 40 - ); - break; + ); + break; - // Step 2 : Options setting + // Step 2 : Options setting - case 2: - if ($form_state['values']['sourceopts']==0) { // 'Server' + case 2: + if ($form_state['values']['sourceopts']==0) { // 'Server' $fdir = file_directory_path(); - $form_state['storage']['file_path'] = $fdir . '/import.xml'; - } - $wordpress = wordpress_import_items($form_state['storage']['file_path']); - $users = wordpress_import_get_users($wordpress); + $form_state['storage']['file_path'] = $fdir .'/import.xml'; + } + $wordpress = wordpress_import_items($form_state['storage']['file_path']); + $users = wordpress_import_get_users($wordpress); - // Users mapping + // Users mapping - $form['users_map'] = array( + $form['users_map'] = array( '#title' => t('Users mapping'), - '#type' => 'fieldset', - '#description' => t('Map each wordpress user to a drupal user'), - '#collapsible' => TRUE, - '#tree' => TRUE - ); - - foreach ($users as $users_value) - { - $form['users_map'][$users_value] = array( + '#type' => 'fieldset', + '#description' => t('Map each wordpress user to a drupal user'), + '#collapsible' => TRUE, + '#tree' => TRUE + ); + + foreach ($users as $users_value) { + $form['users_map'][$users_value] = array( '#type' => 'select', '#title' => $users_value, '#options' => wordpress_import_drupal_users_list() - ); - } + ); + } - foreach (taxonomy_get_vocabularies() as $vocabulary) - { - $vocabularies[$vocabulary->id] = check_plain($vocabulary->name); - } + foreach (taxonomy_get_vocabularies() as $vocabulary) { + $vocabularies[$vocabulary->id] = check_plain($vocabulary->name); + } - // Options + // Options - $form['options'] = array( + $form['options'] = array( '#type' => 'fieldset', '#title' => t('Options'), '#collapsible' => TRUE - ); + ); - // Blog content type + // Blog content type - $node_types = node_get_types(); + $node_types = node_get_types(); - if ($node_types['blog']) - { - $form['options']['blog_type'] = array( - '#type' => 'select', - '#title' => 'Import posts in content type', - '#options' => array( + if ($node_types['blog']) { + $form['options']['blog_type'] = array( + '#type' => 'select', + '#title' => 'Import posts in content type', + '#options' => array( 'blog' => t('Blog'), - 'story' => t('Story') - ) - ); - } + 'story' => t('Story') + ) + ); + } - $form['options']['alias'] = array( + $form['options']['alias'] = array( '#type' => 'checkbox', - '#title' => 'Create path aliases', + '#title' => 'Create path aliases', '#default_value' => 'true', - '#description' => t('This option tries to preserve the path of the wordpress original posts. It is useful only if the url of your drupal site root is the same as the wordpress site (!root) and if clean urls are activated', array('!root' => $wordpress['link'])) - ); - break; - - case 3: - $form['new_users'] = array('#tree' => TRUE); - - foreach ($form_state['storage']['users_map'] as $key => $value) { - if ($value == 'create_new_user') { - $form['new_users'][$key] = array('#type' => 'fieldset', '#title' => t('Wordpress user %user', array('%user' => $key))); - $form['new_users'][$key]['name'] = array('#type' => 'textfield', '#title' => t('Username for Wordpress user %user', array('%user' => $key)), '#required' => TRUE, '#default_value' => $key); - $form['new_users'][$key]['mail'] = array('#type' => 'textfield', '#title' => t('E-mail address for Wordpress user %user', array('%user' => $key)), '#required' => TRUE); - } - } - break; - - case 4: - $form['submit'] = array('#value' => t('The Wordpress blog has been successfully imported')); - break; - } - - // Form settings - if ($form_state['storage']['step'] < 4) - { - $form['submit'] = array('#type' => 'submit', '#value' => t('Next')); - $form_state['rebuild'] = TRUE; - } - - // $form['#attributes']['enctype'] = 'multipart/form-data'; - // $form['#submit'] = array('wordpress_import_form_submit_handler'); - return $form; + '#description' => t('This option tries to preserve the path of the wordpress original posts. It is useful only if the url of your drupal site root is the same as the wordpress site (!root) and if clean urls are activated', array('!root' => $wordpress['link'])) + ); + break; + + case 3: + $form['new_users'] = array('#tree' => TRUE); + + foreach ($form_state['storage']['users_map'] as $key => $value) { + if ($value == 'create_new_user') { + $form['new_users'][$key] = array('#type' => 'fieldset', '#title' => t('Wordpress user %user', array('%user' => $key))); + $form['new_users'][$key]['name'] = array('#type' => 'textfield', '#title' => t('Username for Wordpress user %user', array('%user' => $key)), '#required' => TRUE, '#default_value' => $key); + $form['new_users'][$key]['mail'] = array('#type' => 'textfield', '#title' => t('E-mail address for Wordpress user %user', array('%user' => $key)), '#required' => TRUE); + } + } + break; + + case 4: + $form['submit'] = array('#value' => t('The Wordpress blog has been successfully imported')); + break; + } + + // Form settings + if ($form_state['storage']['step'] < 4) { + $form['submit'] = array('#type' => 'submit', '#value' => t('Next')); + $form_state['rebuild'] = TRUE; + } + + // $form['#attributes']['enctype'] = 'multipart/form-data'; + // $form['#submit'] = array('wordpress_import_form_submit_handler'); + return $form; } /** * Implementation of hook_validate() */ -function wordpress_import_form_validate($form, &$form_state) -{ - switch ($form_state['storage']['step']) { - case 1: - // TODO: rjr - I don't see a way to validate this now that file_check_upload() is rolled into file save - //if (!file_check_upload()) - //{ - // form_set_error('upload', t('You must provide a WXR file.')); - //} - if ($form_state['values']['sourceopts']==0) { // 'Server' - // check to make sure the file is really there - $fdir = file_directory_path(); - if (!file_exists($fdir . '/import.xml')) { - form_set_error('sourceopts', t('The file was not found at files/import.xml.')); - } - } - break; - case 3: - if (isset($form_state['values']['new_users'])) { // TODO: not tested since I don't need it - foreach ($form_state['values']['new_users'] as $key => $value) { - // Check user name - - if ($error = user_validate_name($value['name'])) - { - form_set_error('new_users]['.$key.'][name', $error); - } - elseif (user_load(array('name' => $value['name']))) { - form_set_error('new_users]['.$key.'][name', t('User !user already exists.', array('!user' => $value['name']))); - } - elseif (is_array($user['name']) && in_array(strtolower($value['name']), $user['name'])) { - form_set_error('new_users]['.$key.'][name', t('Two users have the same name (!user).', array('!user' => $value['name']))); - } - - // Check user mail - - if ($error = user_validate_mail($value['mail'])) - { - form_set_error('new_users]['.$key.'][mail', $error); - } - elseif (user_load(array('mail' => $value['mail']))) { - form_set_error('new_users]['.$key.'][mail', t('User with mail !mail already exists.', array('!mail' => $value['mail']))); - } - elseif (is_array($user['mail']) && in_array(strtolower($value['mail']), $user['mail'])) { - form_set_error('new_users]['.$key.'][mail', t('Two users have the same mail (!mail).', array('!mail' => $value['mail']))); - } - - $user['name'][] = strtolower($value['name']); - $user['mail'][] = strtolower($value['mail']); - } - } - break; - } +function wordpress_import_form_validate($form, &$form_state) { + switch ($form_state['storage']['step']) { + case 1: + // TODO: rjr - I don't see a way to validate this now that file_check_upload() is rolled into file save + //if (!file_check_upload()) + //{ + // form_set_error('upload', t('You must provide a WXR file.')); + //} + if ($form_state['values']['sourceopts']==0) { // 'Server' + // check to make sure the file is really there + $fdir = file_directory_path(); + if (!file_exists($fdir .'/import.xml')) { + form_set_error('sourceopts', t('The file was not found at files/import.xml.')); + } + } + break; + case 3: + if (isset($form_state['values']['new_users'])) { // TODO: not tested since I don't need it + foreach ($form_state['values']['new_users'] as $key => $value) { + // Check user name + + if ($error = user_validate_name($value['name'])) { + form_set_error('new_users]['. $key .'][name', $error); + } + elseif (user_load(array('name' => $value['name']))) { + form_set_error('new_users]['. $key .'][name', t('User !user already exists.', array('!user' => $value['name']))); + } + elseif (is_array($user['name']) && in_array(strtolower($value['name']), $user['name'])) { + form_set_error('new_users]['. $key .'][name', t('Two users have the same name (!user).', array('!user' => $value['name']))); + } + + // Check user mail + + if ($error = user_validate_mail($value['mail'])) { + form_set_error('new_users]['. $key .'][mail', $error); + } + elseif (user_load(array('mail' => $value['mail']))) { + form_set_error('new_users]['. $key .'][mail', t('User with mail !mail already exists.', array('!mail' => $value['mail']))); + } + elseif (is_array($user['mail']) && in_array(strtolower($value['mail']), $user['mail'])) { + form_set_error('new_users]['. $key .'][mail', t('Two users have the same mail (!mail).', array('!mail' => $value['mail']))); + } + + $user['name'][] = strtolower($value['name']); + $user['mail'][] = strtolower($value['mail']); + } + } + break; + } } /** * Implementation of hook_submit() */ -function wordpress_import_form_submit($form, &$form_state) -{ - switch ($form_state['storage']['step']) { - // Step 1 : upload file - case 1: - // TODO: rjr - this doesn't create the directory... it just fails if it doesn't exist - $wordpress_path = file_create_path('wordpress'); - $exists = file_check_directory($wordpress_path); - $file_info = file_save_upload('upload',array(), $wordpress_path.'/import.xml', true); - // TODO: rjr - if file_save_upload failed then $file_info is 0, handle it? - $form_state['storage']['file_path'] = $file_info->filepath; - - // TODO: kludge - upload just doesn't work. - $form_state['storage']['file_path'] = '/home/rob4/www/copper/htdocs/d6/sites/default/files/wordpress/import.xml'; - break; - - // Step 2 : import blog - - case 2: - $form_state['storage']['users_map'] = $form_state['values']['users_map']; - $form_state['storage']['alias'] = $form_state['values']['alias']; - $form_state['storage']['blog_type'] = $form_state['values']['blog_type']; - break; +function wordpress_import_form_submit($form, &$form_state) { + switch ($form_state['storage']['step']) { + // Step 1 : upload file + case 1: + // TODO: rjr - this doesn't create the directory... it just fails if it doesn't exist + $wordpress_path = file_create_path('wordpress'); + $exists = file_check_directory($wordpress_path); + $file_info = file_save_upload('upload', array(), $wordpress_path .'/import.xml', TRUE); + // TODO: rjr - if file_save_upload failed then $file_info is 0, handle it? + $form_state['storage']['file_path'] = $file_info->filepath; + + // TODO: kludge - upload just doesn't work. + $form_state['storage']['file_path'] = '/home/rob4/www/copper/htdocs/d6/sites/default/files/wordpress/import.xml'; + break; + + // Step 2 : import blog - case 3: + case 2: + $form_state['storage']['users_map'] = $form_state['values']['users_map']; + $form_state['storage']['alias'] = $form_state['values']['alias']; + $form_state['storage']['blog_type'] = $form_state['values']['blog_type']; + break; + + case 3: if (isset($form_state['values']['new_users'])) { // TODO: not tested since I don't need it - foreach ($form_state['values']['new_users'] as $key => $value) { - $user = array('mail' => $value['mail'], 'pass' => user_password(), 'name' => $value['name'], 'status' => 1); - $user = user_save('', $user); - $form_state['storage']['users_map'][$key] = $user->uid; - } - } - wordpress_import_import_blog($form_state['storage']); - break; - } - $form_state['storage']['step'] = $form_state['storage']['step'] + 1; - $form_state['rebuild'] = TRUE; + foreach ($form_state['values']['new_users'] as $key => $value) { + $user = array('mail' => $value['mail'], 'pass' => user_password(), 'name' => $value['name'], 'status' => 1); + $user = user_save('', $user); + $form_state['storage']['users_map'][$key] = $user->uid; + } + } + wordpress_import_import_blog($form_state['storage']); + break; + } + $form_state['storage']['step'] = $form_state['storage']['step'] + 1; + $form_state['rebuild'] = TRUE; } function wordpress_import_must_create_users($users_map) { - return in_array('create_new_user', $users_map); + return in_array('create_new_user', $users_map); } /** * Display Wordpress import form */ -function wordpress_import_home() -{ - $output = drupal_get_form('wordpress_import_form'); - return $output; +function wordpress_import_home() { + $output = drupal_get_form('wordpress_import_form'); + return $output; } /** @@ -303,29 +294,27 @@ function wordpress_import_home() * @param array $params parameters */ -function wordpress_import_import_blog($params) -{ - if (!$params['blog_type']) - { - $params['blog_type'] = 'story'; - } +function wordpress_import_import_blog($params) { + if (!$params['blog_type']) { + $params['blog_type'] = 'story'; + } - $tag_vocabulary = array( + $tag_vocabulary = array( 'name' => t('Wordpress tag'), 'tags' => 1, 'nodes' => array('page' => 'page', $params['blog_type'] => $params['blog_type']) - ); + ); // TODO: this function is called twice to produce the same data. I suppose it's not a big deal but still... - $wordpress = wordpress_import_items($params['file_path']); - $wpcategories = wordpress_import_import_categories($wordpress, $params); - if (is_array($wpcategories)) { - $params = array_merge($params, wordpress_import_import_categories($wordpress, $params)); - } - $params['tags_vocabulary'] = wordpress_import_create_vocabulary('tag', $tag_vocabulary); - wordpress_import_posts($wordpress, $params); + $wordpress = wordpress_import_items($params['file_path']); + $wpcategories = wordpress_import_import_categories($wordpress, $params); + if (is_array($wpcategories)) { + $params = array_merge($params, wordpress_import_import_categories($wordpress, $params)); + } + $params['tags_vocabulary'] = wordpress_import_create_vocabulary('tag', $tag_vocabulary); + wordpress_import_posts($wordpress, $params); - // Clear the cache so an anonymous poster can see the node being added or updated. - cache_clear_all(); + // Clear the cache so an anonymous poster can see the node being added or updated. + cache_clear_all(); } /** @@ -334,45 +323,44 @@ function wordpress_import_import_blog($p * @return array categories mapping and taxonomy id */ -function wordpress_import_import_categories($wordpress, $params) -{ - // Create new taxonomy for wordpress categories +function wordpress_import_import_categories($wordpress, $params) { + // Create new taxonomy for wordpress categories - $category_vocabulary_data = array( + $category_vocabulary_data = array( 'name' => t('Wordpress category'), 'hierarchy' => 2, 'multiple' => 1, 'nodes' => array('page' => 'page', $params['blog_type'] => $params['blog_type']) - ); + ); - $category_vocabulary = wordpress_import_create_vocabulary('category', $category_vocabulary_data); + $category_vocabulary = wordpress_import_create_vocabulary('category', $category_vocabulary_data); - // Import categories + // Import categories - if (is_array($wordpress['categories'])) { - foreach ($wordpress['categories'] as $key => $value) { - $category_name = wordpress_import_get_tag($value, 'wp:cat_name'); - $category_parent = wordpress_import_get_tag($value, 'wp:category_parent'); - $category_term = array( + if (is_array($wordpress['categories'])) { + foreach ($wordpress['categories'] as $key => $value) { + $category_name = wordpress_import_get_tag($value, 'wp:cat_name'); + $category_parent = wordpress_import_get_tag($value, 'wp:category_parent'); + $category_term = array( 'name' => $category_name, 'vid' => $category_vocabulary - ); - if ($category_parent !== NULL && $category_parent !== '') { - $category_term['parent'] = $category_mapping[$category_parent]; - } + ); + if ($category_parent !== NULL && $category_parent !== '') { + $category_term['parent'] = $category_mapping[$category_parent]; + } - taxonomy_save_term($category_term); + taxonomy_save_term($category_term); - // Save mapping between Wordpress and Drupal categories + // Save mapping between Wordpress and Drupal categories - $category_mapping[$category_name] = $category_term['tid']; - } + $category_mapping[$category_name] = $category_term['tid']; + } - return array ( + return array( 'categories_map' => $category_mapping, 'categories_vocabulary' => $category_vocabulary - ); - } + ); + } } /** @@ -380,26 +368,25 @@ function wordpress_import_import_categor * @return integer new taxonomy id */ -function wordpress_import_create_vocabulary($type, $vocabulary_data) -{ - // Check if a category for Wordpress already exists - - $vocabulary_id = variable_get('wordpress_import_'.$type, 0); - - if ($vocabulary_id) { - $vocabulary = (array)taxonomy_vocabulary_load($vocabulary_id); - } - - // Create vocabulary if it doesn't exist - - if (!$vocabulary) { - $vocabulary_data['module'] = 'wordpress_import'; - taxonomy_save_vocabulary($vocabulary_data); - variable_set('wordpress_import_'.$type,$vocabulary_data['vid']); - $vocabulary = $vocabulary_data; - } +function wordpress_import_create_vocabulary($type, $vocabulary_data) { + // Check if a category for Wordpress already exists + + $vocabulary_id = variable_get('wordpress_import_'. $type, 0); + + if ($vocabulary_id) { + $vocabulary = (array)taxonomy_vocabulary_load($vocabulary_id); + } + + // Create vocabulary if it doesn't exist - return $vocabulary['vid']; + if (!$vocabulary) { + $vocabulary_data['module'] = 'wordpress_import'; + taxonomy_save_vocabulary($vocabulary_data); + variable_set('wordpress_import_'. $type, $vocabulary_data['vid']); + $vocabulary = $vocabulary_data; + } + + return $vocabulary['vid']; } /** @@ -408,26 +395,24 @@ function wordpress_import_create_vocabul * @param array $params parameters */ -function wordpress_import_posts($wordpress, $params) -{ - $params['format'] = wordpress_import_get_format(); - - foreach ($wordpress['posts'] as $post_value) { - $post_info = wordpress_import_post($post_value, $params); - - if ($post_info && $params['alias']) - { - $link = wordpress_import_get_tag($post_value, 'link'); - $link = substr($link,strlen($wordpress['link'])); - // all wordpress urls end with '/', all drupal urls do not - // this line removes the trailing slash so the url will work, but - // it is a _different_ url - perhaps it's a good idea to recommend - // a module like Global Redirect or an .htaccess hack so that old - // links will still work. - $link = rtrim($link,'/'); - path_set_alias('node/'.$post_info['nid'], $link); - } - } +function wordpress_import_posts($wordpress, $params) { + $params['format'] = wordpress_import_get_format(); + + foreach ($wordpress['posts'] as $post_value) { + $post_info = wordpress_import_post($post_value, $params); + + if ($post_info && $params['alias']) { + $link = wordpress_import_get_tag($post_value, 'link'); + $link = substr($link, strlen($wordpress['link'])); + // all wordpress urls end with '/', all drupal urls do not + // this line removes the trailing slash so the url will work, but + // it is a _different_ url - perhaps it's a good idea to recommend + // a module like Global Redirect or an .htaccess hack so that old + // links will still work. + $link = rtrim($link, '/'); + path_set_alias('node/'. $post_info['nid'], $link); + } + } } /** @@ -437,120 +422,118 @@ function wordpress_import_posts($wordpre * @return array post infos */ -function wordpress_import_post($post, $params) -{ - $uid = $params['users_map'][wordpress_import_get_tag( $post, 'dc:creator' )]; - - // Don't import the post if the user hasn't been selected - - if(!ctype_digit($uid)) - return FALSE; - - // Get post type : blog entry (post) or page - // Posts are promoted in front page but not the pages - $post_type = wordpress_import_get_tag($post,'wp:post_type'); - $guess_post_type = false; - switch ($post_type) - { - case 'post': - $type = $params['blog_type']; - $promote = '1'; - break; - case 'page': - $type = 'page'; - $promote = '0'; - break; - default: - $guess_post_type = true; - break; - return; - } - if ($guess_post_type) { +function wordpress_import_post($post, $params) { + $uid = $params['users_map'][wordpress_import_get_tag( $post, 'dc:creator' )]; + + // Don't import the post if the user hasn't been selected + if (!ctype_digit($uid)) { + return FALSE; + } + + // Get post type : blog entry (post) or page + // Posts are promoted in front page but not the pages + $post_type = wordpress_import_get_tag($post, 'wp:post_type'); + $guess_post_type = FALSE; + switch ($post_type) { + case 'post': + $type = $params['blog_type']; + $promote = '1'; + break; + case 'page': + $type = 'page'; + $promote = '0'; + break; + default: + $guess_post_type = TRUE; + break; + return; + } + if ($guess_post_type) { // in my case $post_type for all posts and pages was blank, maybe a bug in export? - // alternate method of detecting $post_type - $allmeta = wordpress_import_get_tag($post,'wp:postmeta'); - $meta =''; - $tagix = 1; - // default to handling as a post - $type = $params['blog_type']; - do { - $meta = wordpress_import_get_tag($allmeta,'wp:meta_key',$tagix); - if ($meta == '_wp_page_template') { - // if there's a page template then it's a page - $type = 'page'; + // alternate method of detecting $post_type + $allmeta = wordpress_import_get_tag($post, 'wp:postmeta'); + $meta =''; + $tagix = 1; + // default to handling as a post + $type = $params['blog_type']; + do { + $meta = wordpress_import_get_tag($allmeta, 'wp:meta_key', $tagix); + if ($meta == '_wp_page_template') { + // if there's a page template then it's a page + $type = 'page'; $promote = '0'; - // if you want to add support for sorting out page templates then here you'll want to do something like - // $page_template = wordpress_import_get_tag($allmeta,'wp:meta_value',$tagix); - // and $page_template should get a value like 'my-page-template.php' - break; - } - // TODO: my posts only have one meta_key and it's the template, if there are several then this might find them + // if you want to add support for sorting out page templates then here you'll want to do something like + // $page_template = wordpress_import_get_tag($allmeta, 'wp:meta_value',$tagix); + // and $page_template should get a value like 'my-page-template.php' + break; + } + // TODO: my posts only have one meta_key and it's the template, if there are several then this might find them $tagix++; - } while ($meta !== ''); - } - // Parsing categories - - preg_match_all('|(.*?)|is', $post, $categories); - $categories = $categories[1]; - - foreach ($categories as $key => $value) { - $category = str_replace(array (''), '', $value); - $drupal_category = $params['categories_map'][$category]; - - if (!empty($drupal_category)) { - $drupal_categories[$params['categories_vocabulary']][] = $drupal_category; - } else { - $drupal_tags[] = $category; - } - } - - // Parsing tags - - preg_match_all('|(.*?)|is', $post, $tags); - - $tags=$tags[1]; - - foreach ($tags as $key => $value) { - $drupal_tags[] = str_replace(array (''), '', $value); - } - - if(is_array($drupal_tags)) - { - $drupal_categories['tags'][$params['tags_vocabulary']] = join(',', $drupal_tags); - } - - // Defining status - - switch (wordpress_import_get_tag($post,'wp:status')) - { - case 'draft': - case 'private': - $status = FALSE; - break; - default: - $status = TRUE; - } - - // Inserting node - - $timestamp = strtotime(wordpress_import_get_tag($post,'wp:post_date')); - $content = wordpress_import_get_tag($post,'content:encoded'); - // look for a tag - $teaserPos = strpos($content,''); - if ($teaserPos > 0) { - $teaser = substr($content,0,$teaserPos); - $content = $teaser . '' . substr($content,$teaserPos + 11); - } else { - $teaser = $content; + } while ($meta !== ''); + } + // Parsing categories + + preg_match_all('|(.*?)|is', $post, $categories); + $categories = $categories[1]; + + foreach ($categories as $key => $value) { + $category = str_replace(array(''), '', $value); + $drupal_category = $params['categories_map'][$category]; + + if (!empty($drupal_category)) { + $drupal_categories[$params['categories_vocabulary']][] = $drupal_category; + } + else { + $drupal_tags[] = $category; + } + } + + // Parsing tags + + preg_match_all('|(.*?)|is', $post, $tags); + + $tags=$tags[1]; + + foreach ($tags as $key => $value) { + $drupal_tags[] = str_replace(array(''), '', $value); + } + + if (is_array($drupal_tags)) { + $drupal_categories['tags'][$params['tags_vocabulary']] = join(',', $drupal_tags); + } + + // Defining status + + switch (wordpress_import_get_tag($post, 'wp:status')) { + case 'draft': + case 'private': + $status = FALSE; + break; + default: + $status = TRUE; + } + + // Inserting node + + $timestamp = strtotime(wordpress_import_get_tag($post, 'wp:post_date')); + $content = wordpress_import_get_tag($post, 'content:encoded'); + // look for a tag + $teaser_pos = strpos($content, ''); + if ($teaser_pos > 0) { + $teaser = substr($content, 0, $teaser_pos); + $content = $teaser .''. substr($content, $teaser_pos + 11); + } + else { + $teaser = $content; } // get title, handle blanks - Drupal messes up deleting blank titled stuff it seems - $title = html_entity_decode(wordpress_import_get_tag($post,'title'), ENT_COMPAT, 'UTF-8'); + $title = html_entity_decode(wordpress_import_get_tag($post, 'title'), ENT_COMPAT, 'UTF-8'); if ($title == '') { - $title = '(Untitled)'; + $title = '(Untitled)'; } - $node = array( + $node = array( 'type' => $type, 'teaser' => $teaser, 'uid' => $uid, @@ -561,73 +544,70 @@ function wordpress_import_post($post, $p 'promote' => $promote, 'created' => $timestamp, 'changed' => $timestamp, - 'comment' => wordpress_import_get_tag($post,'wp:comment_status')=='open'?COMMENT_NODE_READ_WRITE:COMMENT_NODE_READ_ONLY, - 'taxonomy'=> $drupal_categories - ); - - $node = (object)$node; - wordpress_import_node_save($node); - $return['nid'] = $node->nid; - - taxonomy_node_save($node->nid, $drupal_categories); - - // Importing comments - - $drupal_comment = array(); - preg_match_all('|(.*?)|is', $post, $comments); - $comments = $comments[1]; - - $comment_id = 0; - - foreach ($comments as $comment_key => $comment) - { - switch(wordpress_import_get_tag($comment,'wp:comment_approved')) - { - case '0': - $status = COMMENT_NOT_PUBLISHED; - break; - case '1': - $status = COMMENT_PUBLISHED; - break; - case 'spam': - continue 2; - } + 'comment' => wordpress_import_get_tag($post, 'wp:comment_status')=='open'?COMMENT_NODE_READ_WRITE:COMMENT_NODE_READ_ONLY, + 'taxonomy' => $drupal_categories + ); + + $node = (object)$node; + wordpress_import_node_save($node); + $return['nid'] = $node->nid; + + taxonomy_node_save($node->nid, $drupal_categories); + + // Importing comments + + $drupal_comment = array(); + preg_match_all('|(.*?)|is', $post, $comments); + $comments = $comments[1]; + + $comment_id = 0; + + foreach ($comments as $comment_key => $comment) { + switch (wordpress_import_get_tag($comment, 'wp:comment_approved')) { + case '0': + $status = COMMENT_NOT_PUBLISHED; + break; + case '1': + $status = COMMENT_PUBLISHED; + break; + case 'spam': + continue 2; + } - $timestamp = strtotime(wordpress_import_get_tag($comment,'wp:comment_date')); - $comment_content = wordpress_import_get_tag($comment,'wp:comment_content'); + $timestamp = strtotime(wordpress_import_get_tag($comment, 'wp:comment_date')); + $comment_content = wordpress_import_get_tag($comment, 'wp:comment_content'); - $drupal_comment[$timestamp] = array( + $drupal_comment[$timestamp] = array( 'nid' => $node->nid, 'pid' => 0, 'comment' => $comment_content, // 'subject' => trim(truncate_utf8(decode_entities(strip_tags($comment_content)), 29, TRUE)), 'subject' => '', // wordpress comments don't have subjects, so i'd rather not create them - 'name' => decode_entities(strip_tags(wordpress_import_get_tag($comment,'wp:comment_author'))), - 'mail' => wordpress_import_get_tag($comment,'wp:comment_author_email'), - 'homepage' => wordpress_import_get_tag($comment,'wp:comment_author_url'), + 'name' => decode_entities(strip_tags(wordpress_import_get_tag($comment, 'wp:comment_author'))), + 'mail' => wordpress_import_get_tag($comment, 'wp:comment_author_email'), + 'homepage' => wordpress_import_get_tag($comment, 'wp:comment_author_url'), 'timestamp' => $timestamp, - 'hostname' => wordpress_import_get_tag($comment,'wp:comment_author_IP'), + 'hostname' => wordpress_import_get_tag($comment, 'wp:comment_author_IP'), 'status' => $status, 'format' => $param['format'] - ); - } + ); + } - // Sorting comments by timestamp so the thread won't be messed up in Drupal + // Sorting comments by timestamp so the thread won't be messed up in Drupal - ksort($drupal_comment); + ksort($drupal_comment); - // Saving comments + // Saving comments - foreach ($drupal_comment as $drupal_comment_key => $drupal_comment_value) - { - $comment_id = comment_save($drupal_comment_value); + foreach ($drupal_comment as $drupal_comment_key => $drupal_comment_value) { + $comment_id = comment_save($drupal_comment_value); - // Updating fields that haven't been set by comment_save + // Updating fields that haven't been set by comment_save - db_query("UPDATE {comments} SET hostname= '%s', timestamp= '%d', status= '%d' WHERE cid= %d", $drupal_comment_value['hostname'], $drupal_comment_value['timestamp'], $drupal_comment_value['status'], $comment_id); - } + db_query("UPDATE {comments} SET hostname= '%s', timestamp= '%d', status= '%d' WHERE cid= %d", $drupal_comment_value['hostname'], $drupal_comment_value['timestamp'], $drupal_comment_value['status'], $comment_id); + } - return $return; + return $return; } // Drupal help functions @@ -636,9 +616,8 @@ function wordpress_import_post($post, $p * */ -function wordpress_import_directory() -{ - return file_create_path(file_directory_path().'/'.'wordpress'); +function wordpress_import_directory() { + return file_create_path(file_directory_path() .'/'.'wordpress'); } /** @@ -646,23 +625,20 @@ function wordpress_import_directory() * @return array users */ -function wordpress_import_drupal_users_list() -{ - $sql = "SELECT * FROM {users} ORDER BY name ASC"; - $result = db_query($sql); - - $users['create_new_user'] = t('Create new user'); - $users['do_not_import'] = t('Do not import'); - - - while ($user = db_fetch_array($result)) - { - if (!empty($user['uid'])) - { - $users[$user['uid']] = $user['name']; - } - } - return $users; +function wordpress_import_drupal_users_list() { + $sql = "SELECT * FROM {users} ORDER BY name ASC"; + $result = db_query($sql); + + $users['create_new_user'] = t('Create new user'); + $users['do_not_import'] = t('Do not import'); + + + while ($user = db_fetch_array($result)) { + if (!empty($user['uid'])) { + $users[$user['uid']] = $user['name']; + } + } + return $users; } /** @@ -670,77 +646,74 @@ function wordpress_import_drupal_users_l * @return integer format id */ -function wordpress_import_get_format() -{ - $format_id = variable_get('wordpress_import_format_id',0); - - if (!$format_id) - { - db_query("INSERT INTO {filter_formats} (name,cache) VALUES ('%s',1)", 'Wordpress format'); - $format_id = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}")); - db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format_id, 'filter', 2, 0); - variable_set('wordpress_import_format_id', $format_id); - } - - return $format_id; -} - -function wordpress_import_node_save(&$node) -{ - global $user; - - // TODO: this seems to have a bug where db_last_insert_id() returns 0 for an empty table and 0 when the last inserted node was 0, maybe a check for NULL? - $node->nid = db_last_insert_id('{node}','{node}_nid'); - $node->vid = db_last_insert_id('{node_revisions}','{node_revisions}_vid'); - - // Set some required fields: - if (empty($node->created)) { - $node->created = time(); - } - - if (empty($node->changed)) { - $node->changed = time(); - } +function wordpress_import_get_format() { + $format_id = variable_get('wordpress_import_format_id', 0); + + if (!$format_id) { + db_query("INSERT INTO {filter_formats} (name,cache) VALUES ('%s',1)", 'Wordpress format'); + $format_id = db_result(db_query("SELECT MAX(format) AS format FROM {filter_formats}")); + db_query("INSERT INTO {filters} (format, module, delta, weight) VALUES (%d, '%s', %d, %d)", $format_id, 'filter', 2, 0); + variable_set('wordpress_import_format_id', $format_id); + } + + return $format_id; +} + +function wordpress_import_node_save(&$node) { + global $user; + + // TODO: this seems to have a bug where db_last_insert_id() returns 0 for an empty table and 0 when the last inserted node was 0, maybe a check for NULL? + $node->nid = db_last_insert_id('{node}', '{node}_nid'); + $node->vid = db_last_insert_id('{node_revisions}', '{node_revisions}_vid'); + + // Set some required fields: + if (empty($node->created)) { + $node->created = time(); + } + + if (empty($node->changed)) { + $node->changed = time(); + } - // Split off revisions data to another structure - $revisions_table_values = array('nid' => $node->nid, 'vid' => $node->vid, + // Split off revisions data to another structure + $revisions_table_values = array('nid' => $node->nid, 'vid' => $node->vid, 'title' => $node->title, 'body' => $node->body, 'teaser' => $node->teaser, 'timestamp' => $node->changed, 'uid' => $user->uid, 'format' => $node->format, 'log' => t('Import from Wordpress blog')); - $revisions_table_types = array('nid' => '%d', 'vid' => '%d', + $revisions_table_types = array('nid' => '%d', 'vid' => '%d', 'title' => "'%s'", 'body' => "'%s'", 'teaser' => "'%s'", 'timestamp' => '%d', 'uid' => '%d', 'format' => '%d', 'log' => "'%s'"); - $node_table_values = array('nid' => $node->nid, 'vid' => $node->vid, + $node_table_values = array('nid' => $node->nid, 'vid' => $node->vid, 'title' => $node->title, 'type' => $node->type, 'uid' => $node->uid, 'status' => $node->status, 'created' => $node->created, 'changed' => $node->changed, 'comment' => $node->comment, 'promote' => $node->promote, 'sticky' => $node->sticky); - $node_table_types = array('nid' => '%d', 'vid' => '%d', + $node_table_types = array('nid' => '%d', 'vid' => '%d', 'title' => "'%s'", 'type' => "'%s'", 'uid' => '%d', 'status' => '%d', 'created' => '%d', 'changed' => '%d', 'comment' => '%d', 'promote' => '%d', 'sticky' => '%d'); - //Generate the node table query and the - //the node_revisions table query + //Generate the node table query and the + //the node_revisions table query - $node_query = 'INSERT INTO {node} ('. implode(', ', array_keys($node_table_types)) .') VALUES ('. implode(', ', $node_table_types) .')'; - $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')'; + $node_query = 'INSERT INTO {node} ('. implode(', ', array_keys($node_table_types)) .') VALUES ('. implode(', ', $node_table_types) .')'; + $revisions_query = 'INSERT INTO {node_revisions} ('. implode(', ', array_keys($revisions_table_types)) .') VALUES ('. implode(', ', $revisions_table_types) .')'; - // Insert the node into the database: - db_query($node_query, $node_table_values); - db_query($revisions_query, $revisions_table_values); + // Insert the node into the database: + db_query($node_query, $node_table_values); + db_query($revisions_query, $revisions_table_values); - // Call the node specific callback (if any): + // Call the node specific callback (if any): - node_invoke($node, 'insert'); - node_invoke_nodeapi($node, 'insert'); + node_invoke($node, 'insert'); + node_invoke_nodeapi($node, 'insert'); - // Update the node access table for this node. - node_access_acquire_grants($node); + // Update the node access table for this node. + node_access_acquire_grants($node); } // Simpletest hook @@ -749,10 +722,10 @@ function wordpress_import_node_save(&$no * Implementation of hook_simpletest(). */ function wordpress_import_simpletest() { - $module_name = 'wordpress_import'; - $dir = drupal_get_path('module', $module_name). '/tests'; - $tests = file_scan_directory($dir, '\.test$'); - return array_keys($tests); + $module_name = 'wordpress_import'; + $dir = drupal_get_path('module', $module_name) .'/tests'; + $tests = file_scan_directory($dir, '\.test$'); + return array_keys($tests); } // Wordpress importing functions @@ -764,55 +737,51 @@ function wordpress_import_simpletest() { * @return array wordpress data */ -function wordpress_import_items($file) -{ - $wordpress = array(); - - $fp = fopen($file, 'r'); - if ($fp) { - while ( !feof($fp) ) { - $importline = rtrim(fgets($fp)); - if ( false !== strpos($importline, '') ) { - preg_match('|(.*?)|is', $importline, $category); - $wordpress['categories'][] = $category[1]; - continue; - } - if ( false !== strpos($importline, '') ) { - preg_match('|(.*?)|is', $importline, $tag); - $wordpress['tags'][] = $tag[1]; - continue; - } - if ( false !== strpos($importline, '') ) { - $wordpress['posts'][$num] = ''; - $doing_entry = true; - continue; - } - if ( false !== strpos($importline, '') ) - { - $num++; - $doing_entry = false; - continue; - } - - if ( $doing_entry ) { - $wordpress['posts'][$num] .= $importline . "\n"; - } - else - { - if ( false !== strpos($importline, '') ) { - preg_match('|(.*?)|is', $importline, $link); - $wordpress['link'] = $link[1]; - if (substr($wordpress['link'],-1) != '/') - { - $wordpress['link'] .= '/'; - } - } - } - } - fclose($fp); - } +function wordpress_import_items($file) { + $wordpress = array(); - return $wordpress; + $fp = fopen($file, 'r'); + if ($fp) { + while ( !feof($fp) ) { + $importline = rtrim(fgets($fp)); + if ( FALSE !== strpos($importline, '') ) { + preg_match('|(.*?)|is', $importline, $category); + $wordpress['categories'][] = $category[1]; + continue; + } + if ( FALSE !== strpos($importline, '') ) { + preg_match('|(.*?)|is', $importline, $tag); + $wordpress['tags'][] = $tag[1]; + continue; + } + if ( FALSE !== strpos($importline, '') ) { + $wordpress['posts'][$num] = ''; + $doing_entry = TRUE; + continue; + } + if ( FALSE !== strpos($importline, '') ) { + $num++; + $doing_entry = FALSE; + continue; + } + + if ( $doing_entry ) { + $wordpress['posts'][$num] .= $importline ."\n"; + } + else { + if ( FALSE !== strpos($importline, '') ) { + preg_match('|(.*?)|is', $importline, $link); + $wordpress['link'] = $link[1]; + if (substr($wordpress['link'], -1) != '/') { + $wordpress['link'] .= '/'; + } + } + } + } + fclose($fp); + } + + return $wordpress; } /** @@ -823,19 +792,18 @@ function wordpress_import_items($file) */ -function wordpress_import_get_users($wordpress) -{ - $temp = array(); - - foreach ($wordpress['posts'] as $post) { - if ('' != trim($post)) { - ++ $i; - $author_name = wordpress_import_get_tag( $post, 'dc:creator' ); - $author[$author_name] = $author_name; - } - } +function wordpress_import_get_users($wordpress) { + $temp = array(); - return $author; + foreach ($wordpress['posts'] as $post) { + if ('' != trim($post)) { + ++ $i; + $author_name = wordpress_import_get_tag( $post, 'dc:creator' ); + $author[$author_name] = $author_name; + } + } + + return $author; } /** @@ -847,14 +815,13 @@ function wordpress_import_get_users($wor * @return string content of the tag */ -function wordpress_import_get_tag($string, $tag, &$match = 1) -{ - preg_match("|<$tag.*?>(.*?)|is", $string, $return); - $r = preg_replace('|^$|s', '$1', $return[$match]); - if ($r === NULL) { - $r = $return[$match]; - } - // $return = $wpdb->escape( trim( $return ) ); - // TODO : echapper le tag - return $r; +function wordpress_import_get_tag($string, $tag, &$match = 1) { + preg_match("|<$tag.*?>(.*?)|is", $string, $return); + $r = preg_replace('|^$|s', '$1', $return[$match]); + if ($r === NULL) { + $r = $return[$match]; + } + // $return = $wpdb->escape( trim( $return ) ); + // TODO : echapper le tag + return $r; } \ No newline at end of file