From 8152b96a3c73f618f2caf0a759859f90eb0518ff Mon Sep 17 00:00:00 2001
From: aegir <aegir@750Whisperlynx64>
Date: Mon, 28 Feb 2011 02:13:15 +0100
Subject: [PATCH] Issue #748374 changed file upload validation and changed file_directory_path now imports posts from local filesystem when comments and images are disabled. is still missing body field

---
 wordpress_import.info   |    4 +-
 wordpress_import.module |  237 ++++++++++++++++++++++++++--------------------
 2 files changed, 137 insertions(+), 104 deletions(-)

diff --git a/wordpress_import.info b/wordpress_import.info
index 938be14..e29aca3 100755
--- a/wordpress_import.info
+++ b/wordpress_import.info
@@ -1,6 +1,8 @@
 name = Wordpress import
 description = "Import WXR files created by Wordpress blog tool."
-core=6.x
+core=7.x
 package = "Importing"
 dependencies[] = ctools
 php = 5.0
+files[] = wordpress_import.module
+
diff --git a/wordpress_import.module b/wordpress_import.module
index b172716..4df5c25 100755
--- a/wordpress_import.module
+++ b/wordpress_import.module
@@ -27,14 +27,14 @@
   */
 
 /**
- * Implementation of hook_help()
+ * Implements hook_help().
  */
 function wordpress_import_help($path, $arg) {
   $output = '';
 
   switch ($path) {
     case "admin/help#wordpress_import":
-    $output = '<p>'.  t("This module imports a WXR file generated by Wordpress blog software into Drupal") .'</p>';
+    $output = '<p>' . t("This module imports a WXR file generated by Wordpress blog software into Drupal") . '</p>';
     break;
   }
 
@@ -43,7 +43,7 @@ function wordpress_import_help($path, $arg) {
 
 
 /**
- * Implementation of hook_menu()
+ * Implements hook_menu().
  */
 function wordpress_import_menu() {
   $items = array();
@@ -53,24 +53,30 @@ function wordpress_import_menu() {
     'description' => 'Import Wordpress WXR file',
     'page callback' => 'wordpress_import_home',
     'access arguments' => array('import wordpress blog'),
-    );
+    'type' => MENU_LOCAL_TASK | MENU_NORMAL_ITEM,
+  );
 
   return $items;
 }
 
 
 /**
- * hook_perm
+ * Implements hook_permission().
  *
- * @return array perms
+ * @return array permissions
  * @author Yann Rocq
- **/
-function wordpress_import_perm() {
-  return array('import wordpress blog');
+ */
+function wordpress_import_permission() {
+  return array(
+    'import wordpress blog' => array(
+      'title' => t('Import WordPress blog'),
+      'description' => t('Perform the WordPress import from a WXR file.'),
+    ),
+  );
 }
 
 /**
- * Module entry point
+ * Module entry point.
  */
 function wordpress_import_home() {
 
@@ -79,7 +85,6 @@ function wordpress_import_home() {
     drupal_set_message(t('<strong>XMLReader PHP extension not found!</strong>  The XMLReader extension is available in PECL as of PHP 5.0.0 and is included and enabled as of PHP 5.1.0 by default. It can be enabled by adding the argument --enable-xmlreader  (or --with-xmlreader before 5.1.0) to your configure line. The libxml  extension is required. '), 'error');
     return '';
   }
-
   return wordpress_import_wizard();
 }
 
@@ -87,6 +92,7 @@ function wordpress_import_home() {
  * Create and manage the multistep form
  */
 function wordpress_import_wizard() {
+
   $step = arg(3);
 
   ctools_include('wizard');
@@ -139,19 +145,21 @@ function wordpress_import_wizard() {
 
   // *** SETTING THE FORM UP FOR MULTISTEP *** //
   $form_state = array(
-    'cache name' => NULL,
+    'cache name' => '',
   );
 
-  $wordpress_import = wordpress_import_form_get_page_cache(NULL);
+  $wordpress_import = wordpress_import_form_get_page_cache($form_state['cache name']);
 
   if (!$wordpress_import && $step != '') {
+    drupal_set_message(t('An illegal value has been chosen.'));
     drupal_goto('admin/content/wordpress_import');
     return;
   }
 
-  if (!$wordpress_import) {
-    drupal_set_message($step);
+  if (!$step) {
     $step = current(array_keys($form_info['order']));
+  }
+  if (!$wordpress_import) {
     $wordpress_import = new stdClass();
     ctools_object_cache_set('wordpress_import', $form_state['cache name'], $wordpress_import);
   }
@@ -167,13 +175,13 @@ function wordpress_import_wizard() {
   if ($step == 'newusers' && !array_search('newuser', $wordpress_import->usermap)) {
     drupal_goto('admin/content/wordpress_import/ready');
   }
-
   $output = ctools_wizard_multistep_form($form_info, $step, $form_state);
+
   return $output;
 }
 
 
-/*---------------- CTools cache handling functions ------------ */
+/* ---------------- CTools cache handling functions ------------ */
 
 function wordpress_import_form_clear_page_cache($name) {
   ctools_object_cache_clear('wordpress_import', $name);
@@ -186,12 +194,12 @@ function wordpress_import_form_get_page_cache($name) {
 }
 
 
-/*-------------------------- Form Steps  ---------------------- */
+/* -------------------------- Form Steps  ---------------------- */
 
-function wordpress_import_form_file(&$form, &$form_state) {
+function wordpress_import_form_file($form, &$form_state) {
   global $base_path;
 
-  $wordpress_import = &$form_state['wordpress_import_obj'];
+  $wordpress_import = $form_state['wordpress_import_obj'];
 
   $form['informations'] = array(
    '#value' => t('This module will import a Wordpress eXtended RSS (WXR) file generated by Wordpress. If you need instructions on creating this file, please read the  <a href="@codex">documentation</a>. <em>You must make a backup copy of your Drupal database before proceeding!</em>', array('@codex' => url('http://codex.wordpress.org/Tools_Export_SubPanel')))
@@ -202,24 +210,25 @@ function wordpress_import_form_file(&$form, &$form_state) {
     '#type' => 'file',
     '#title' => t('Upload your WXR file'),
     '#size' => 40,
+    '#attributes' => array('accept' => 'xml'),
     '#description' => t('The file can\'t exceed %maximum megabytes', array('%maximum' => $base_path . file_upload_max_size()/1024/1024)),
   );
 
   // Create and scan module directory
-  $wordpress_import_path = file_create_path('wordpress');
-  file_check_directory($wordpress_import_path, TRUE);
-  $files = file_scan_directory($wordpress_import_path, '.*\.xml');
+  $path = file_default_scheme() . '://wordpress';
+  file_prepare_directory($path, FILE_CREATE_DIRECTORY);
+  $files = file_scan_directory($path, '/.*\.xml/');
 
   if (count($files) > 0) {
-    foreach (file_scan_directory($wordpress_import_path, '.*\.xml') as $file) {
-      $options[$file->basename] = $file->basename;
+    foreach (file_scan_directory($path, '/.*\.xml/') as $file) {
+      $options[basename($file->filename)] = basename($file->filename);
     }
     ksort($options);
     $description = '';
   }
   else {
     $options = array();
-    $description = t('No files found in %directory', array('%directory' => file_create_path(file_directory_path() .'/wordpress')));
+    $description = t('No files found in %directory', array('%directory' => drupal_realpath($path)));
   }
 
   // Local file selector element
@@ -233,6 +242,7 @@ function wordpress_import_form_file(&$form, &$form_state) {
   $form['#attributes'] = array('enctype' => "multipart/form-data");
 
   $form_state['no buttons'] = TRUE;
+  return $form;
 }
 
 
@@ -241,15 +251,17 @@ function wordpress_import_form_file_validate(&$form, &$form_state) {
   if ($form_state['clicked_button']['#wizard type'] == 'cancel') {
     return;
   }
-
+$validators = array(
+'file_validate_extensions' => array()
+);
   // Process uploaded file (if any), and set file path
-  $upload = file_save_upload('upload');
+  $upload = file_save_upload('upload',$validators);
   if ($upload) {
     $form_state['values']['upload'] = $upload->filename;
     $filepath = $upload->filepath;
   }
   else {
-    $filepath = file_directory_path() . '/wordpress/' . $form_state['values']['local'];
+    $filepath = file_default_scheme() . ':/' . '/wordpress/' . $form_state['values']['local'];
   }
 
   // Make sure a file was uploaded or a local file was selected
@@ -291,7 +303,7 @@ function wordpress_import_form_file_submit(&$form, &$form_state) {
 }
 
 
-function wordpress_import_form_options(&$form, &$form_state) {
+function wordpress_import_form_options($form, &$form_state) {
   $wordpress_import = &$form_state['wordpress_import_obj'];
 
   $form['basic_options'] = array(
@@ -302,7 +314,7 @@ function wordpress_import_form_options(&$form, &$form_state) {
   );
 
   // Post content type
-  $node_types = node_get_types();
+  $node_types = node_type_get_types();
   $content_types = array('' => '', 'noimport' => t('Don\'t import'));
   foreach ($node_types as $k => $v) {
       if (!module_exists('og') || (module_exists('og') && !og_is_group_type($k))) {
@@ -312,14 +324,14 @@ function wordpress_import_form_options(&$form, &$form_state) {
   $form['basic_options']['nodetype_post'] = array(
     '#type' => 'select',
     '#title' => t('Import posts in content type'),
-    '#description' => t('All imported posts will be assigned the selected content type. If you are unsure, select "Story"'),
+    '#description' => t('All imported posts will be assigned the selected content type. If you are unsure, select "Article"'),
     '#default_value' => '',
     '#options' => $content_types,
     );
   $form['basic_options']['nodetype_page'] = array(
     '#type' => 'select',
     '#title' => t('Import pages in content type'),
-    '#description' => t('All imported pages will be assigned the selected content type. If you are unsure, select "Page"'),
+    '#description' => t('All imported pages will be assigned the selected content type. If you are unsure, select "Basic page"'),
     '#default_value' => '',
     '#options' => $content_types,
     );
@@ -394,7 +406,7 @@ function wordpress_import_form_options(&$form, &$form_state) {
     '#type' => 'textfield',
     '#title' => t('Images directory'),
     '#default_value' => '/images',
-    '#description' => t('Subdirectory of <strong>@file_directory_path</strong> in which to place files, must start with /', array('@file_directory_path' => file_directory_path()))
+    '#description' => t('Subdirectory of <strong>@file_directory_path</strong> in which to place files, must start with /', array('@file_directory_path' => file_default_scheme() . ':/'))
   );
 
   // Warn if some key modules are not found
@@ -405,13 +417,13 @@ function wordpress_import_form_options(&$form, &$form_state) {
       $form['advanced_options']['#collapsed'] = FALSE;
   }
   if (!module_exists('comment')) {
-      drupal_set_message(t('Could not find module "comment". <strong>Comments and tackbacks will not be imported.</strong>'), 'error');
+      drupal_set_message(t('Could not find module "comment". <strong>Comments and trackbacks will not be imported.</strong>'), 'error');
       $form['advanced_options']['disable_comments']['#default_value'] = TRUE;
       $form['advanced_options']['disable_comments']['#disabled'] = TRUE;
       $form['advanced_options']['#collapsed'] = FALSE;
   }
   if (!module_exists('trackback')) {
-      drupal_set_message(t('Could not find module "trackback". <strong>Trackbacks will not be imported.</strong>'), 'error');
+      drupal_set_message(t('Trackback module has not been ported to Drupal 7. <strong>Trackbacks will not be imported.</strong> See <a href="http://drupal.org/node/977686">http://drupal.org/node/977686</a> for more details.'), 'warning');
       $form['advanced_options']['disable_trackbacks']['#default_value'] = TRUE;
       $form['advanced_options']['disable_trackbacks']['#disabled'] = TRUE;
       $form['advanced_options']['#collapsed'] = FALSE;
@@ -426,6 +438,7 @@ function wordpress_import_form_options(&$form, &$form_state) {
       $form['advanced_options']['images']['#default_value'] = FALSE;
       $form['advanced_options']['images']['#disabled'] = TRUE;
   }
+  return $form;
 }
 
 
@@ -449,7 +462,7 @@ function wordpress_import_form_options_validate(&$form, &$form_state) {
 
   // Test if images directory is valid
   if ($form_state['values']['advanced_options']['images']) {
-    $dir = file_directory_path() . rtrim($form_state['values']['advanced_options']['images_dir'], '/\\');
+    $dir = file_default_scheme() . ':/' . rtrim($form_state['values']['advanced_options']['images_dir'], '/\\');
     if (!is_dir($dir)) {
       if (!@mkdir($dir)) {
         form_set_error('advanced_options][images_dir', t('Invalid images directory specified.'));
@@ -468,7 +481,7 @@ function wordpress_import_form_options_submit(&$form, &$form_state) {
   // Clear messages
   drupal_get_messages();
 
-  $form_state['values']['advanced_options']['images_dir'] = file_directory_path() . rtrim($form_state['values']['advanced_options']['images_dir'], '/\\');
+  $form_state['values']['advanced_options']['images_dir'] = file_default_scheme() . ':/' . rtrim($form_state['values']['advanced_options']['images_dir'], '/\\');
 
   $options = $form_state['values']['basic_options'] + $form_state['values']['advanced_options'];
   $form_state['wordpress_import_obj']->options = $options;
@@ -485,7 +498,7 @@ function wordpress_import_form_options_submit(&$form, &$form_state) {
 }
 
 
-function wordpress_import_form_og(&$form, &$form_state) {
+function wordpress_import_form_og($form, &$form_state) {
   $wordpress_import = &$form_state['wordpress_import_obj'];
 
   $form['informations'] = array(
@@ -507,6 +520,7 @@ function wordpress_import_form_og(&$form, &$form_state) {
     '#description' => t('Show this post to everyone, or only to members of the groups checked above. Posts without any groups are always <em>public</em>.'),
     );
   }
+  return $form;
 }
 
 
@@ -516,16 +530,16 @@ function wordpress_import_form_og_submit(&$form, &$form_state) {
 }
 
 
-function wordpress_import_form_mapusers(&$form, &$form_state) {
+function wordpress_import_form_mapusers($form, &$form_state) {
   $wordpress_import = &$form_state['wordpress_import_obj'];
 
   // Get Drupal users
   $sql = "SELECT * FROM {users} ORDER BY name ASC";
   $result =  db_query($sql);
 
-  while ($drupal_user = db_fetch_array($result)) {
-    if (!empty($drupal_user['uid'])) {
-      $drupal_users[$drupal_user['uid']] = $drupal_user['name'];
+  foreach ($result as $drupal_user) {
+    if (!empty($drupal_user->uid)) {
+      $drupal_users[$drupal_user->uid] = $drupal_user->name;
     }
   }
 
@@ -562,6 +576,7 @@ function wordpress_import_form_mapusers(&$form, &$form_state) {
     }
 
   }
+  return $form;
 }
 
 
@@ -604,7 +619,7 @@ function wordpress_import_form_mapusers_submit(&$form, &$form_state) {
 }
 
 
-function wordpress_import_form_newusers(&$form, &$form_state) {
+function wordpress_import_form_newusers($form, &$form_state) {
   $wordpress_import = &$form_state['wordpress_import_obj'];
 
   $form['newuser'] = array('#tree' => TRUE);
@@ -627,6 +642,7 @@ function wordpress_import_form_newusers(&$form, &$form_state) {
     }
 
   }
+  return $form;
 }
 
 
@@ -643,10 +659,10 @@ function wordpress_import_form_newusers_validate(&$form, &$form_state) {
     if ($error = user_validate_name($value['name'])) {
       form_set_error('newuser]['. $key .'][name', $error);
     }
-    elseif (user_load(array('name' => $value['name']))) {
+    elseif (user_load_by_name($value['name'])) {
       form_set_error('newuser]['. $key .'][name', t('User !user already exists.', array('!user' => $value['name'])));
     }
-    elseif (is_array($user['name']) && in_array(strtolower($value['name']), $user['name'])) {
+    elseif (is_array($user['name']) && in_array(drupal_strtolower($value['name']), $user['name'])) {
       form_set_error('newuser]['. $key .'][name', t('Two users have the same name (!user).', array('!user' => $value['name'])));
     }
 
@@ -654,15 +670,15 @@ function wordpress_import_form_newusers_validate(&$form, &$form_state) {
     if ($error = user_validate_mail($value['mail'])) {
       form_set_error('newuser]['. $key .'][mail', $error);
     }
-    elseif (user_load(array('mail' => $value['mail']))) {
+    elseif (user_load_by_mail($value['mail'])) {
       form_set_error('newuser]['. $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'])) {
+    elseif (is_array($user['mail']) && in_array(drupal_strtolower($value['mail']), $user['mail'])) {
       form_set_error('newuser]['. $key .'][mail', t('Two users have the same mail (!mail).', array('!mail' => $value['mail'])));
     }
 
-    $user['name'][] = strtolower($value['name']);
-    $user['mail'][] = strtolower($value['mail']);
+    $user['name'][] = drupal_strtolower($value['name']);
+    $user['mail'][] = drupal_strtolower($value['mail']);
   }
 }
 
@@ -672,9 +688,9 @@ function wordpress_import_form_newusers_submit(&$form, &$form_state) {
 }
 
 
-function wordpress_import_form_ready(&$form, &$form_state) {
+function wordpress_import_form_ready($form, &$form_state) {
   $wordpress_import = &$form_state['wordpress_import_obj'];
-  
+
   if (function_exists('dpm')) {
     dpm($wordpress_import);
   }
@@ -682,6 +698,7 @@ function wordpress_import_form_ready(&$form, &$form_state) {
   $form['informations'] = array(
    '#value' => t('Wordpress Import configuration is complete. Click "Finish" to launch the import process.'),
   );
+  return $form;
 }
 
 
@@ -697,7 +714,7 @@ function wordpress_import_form_subtask_cancel(&$form_state) {
   ctools_object_cache_clear('wordpress_import', $form_state['cache name']);
   $form_state['redirect'] = 'admin/content/wordpress_import';
   drupal_get_messages();
-  drupal_set_message('Import operation cancelled.');
+  drupal_set_message(t('Import operation cancelled.'));
 }
 
 
@@ -707,7 +724,7 @@ function wordpress_import_form_subtask_finish(&$form_state) {
 
   // Prepare images directory
   if ($wordpress_import->options['images'] && !empty($wordpress_import->options['images_dir'])) {
-    file_check_directory($wordpress_import->options['images_dir'], TRUE);
+    file_prepare_directory($wordpress_import->options['images_dir'], TRUE);
   }
 
   // Prepare batch operations
@@ -788,9 +805,25 @@ function wordpress_import_process_blog($wordpress_import, &$context) {
   if (!$context['sandbox']['format_done']) {
     $wordpress_import->format = variable_get('wordpress_import_format_id', 0);
     if (!$wordpress_import->format || !filter_formats($wordpress_import->format)) {
-      db_query("INSERT INTO {filter_formats} (name,cache) VALUES ('%s',1)", 'Wordpress format');
-      $wordpress_import->format = 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)", $wordpress_import->format, 'filter', 2, 0);
+      $wordpress_format = array(
+        'format' => 'wordpress_format',
+        'name' => 'Wordpress format',
+        'cache' => 1,
+      );
+      $wordpress_format = (object) $wordpress_format;
+      filter_format_save($wordpress_format);
+      $wordpress_import->format = $wordpress_format->format;
+      // TODO: Ensure the following {filters} table insert is no longer needed.
+      /*
+      db_insert('filters')
+        ->fields(array(
+          'format' => $wordpress_import->format,
+          'module' => 'filter',
+          'delta' => 2,
+          'weight' => 0
+        ))
+        ->execute();
+      */
       variable_set('wordpress_import_format_id', $wordpress_import->format);
     }
     $context['sandbox']['format_done'] = TRUE;
@@ -861,7 +894,7 @@ function wordpress_import_process_categories_tags(&$wordpress_import, &$context)
           $term['parent'] = $wordpress_import->categories_map[$category['category_parent']];
         }
 
-        taxonomy_save_term($term);
+        taxonomy_term_save((object) $term);
         $wordpress_import->categories_map[$category['cat_name']] = $term['tid'];
 
         $context['results']['created_terms']++;
@@ -892,7 +925,7 @@ function wordpress_import_process_categories_tags(&$wordpress_import, &$context)
           'vid' => $wordpress_import->tags_vocabulary,
           );
 
-        taxonomy_save_term($term);
+        taxonomy_term_save((object) $term);
         $wordpress_import->tags_map[$tag] = $term['tid'];
 
         $context['results']['created_terms']++;
@@ -1033,18 +1066,15 @@ function wordpress_import_process_post(&$wordpress_import, &$context) {
   $node['body'] = str_replace('<!--more-->', '<!--break-->', $post['content']);
 
   // Node format
-  $node['format'] = $wordpress_import->format;
+  $node['body']['format'] = $wordpress_import->format;
 
-  // Node teaser
-  if (empty($post['excerpt'])) {
-    $node['teaser'] = node_teaser($node['body'], $node['format']);
-  }
-  else {
-    $node['teaser'] = $post['excerpt'];
+    // Node teaser
+  if (!empty($post['excerpt'])) {
+    $node['body']['summary']  = $post['excerpt'];
   }
 
   // Node language
-  $node['language'] = (isset($wordpress_import->options['language']) ? $wordpress_import->options['language'] : '');
+  $node['language'] = (isset($wordpress_import->options['language']) ? $wordpress_import->options['language'] : 'en');
 
   // Node timestamp
   $node['created'] = strtotime($post['date']);
@@ -1075,10 +1105,10 @@ function wordpress_import_process_post(&$wordpress_import, &$context) {
   }
 
   // Node comment status
-  $node['comment'] = $post['comment_status']=='open'?COMMENT_NODE_READ_WRITE:COMMENT_NODE_READ_ONLY;
+  $node['comment'] = $post['comment_status']=='open'?COMMENT_NODE_OPEN:COMMENT_NODE_CLOSED;
 
   // Check if node wasn't already imported
-  $context['sandbox']['node_duplicate'] = node_load(array('created' => $node['created'], 'title' => $node['title'], 'type' => $node['type']));
+  $context['sandbox']['node_duplicate'] = node_load_multiple(FALSE, array('created' => $node['created'], 'title' => $node['title'], 'type' => $node['type']));
   if ($context['sandbox']['node_duplicate']->nid) {
     if (module_exists('comment') && !empty($post['comments'])) {
       wordpress_import_process_post_comments($post['comments'], $context['sandbox']['node_duplicate']->nid, $wordpress_import->options['disable_trackbacks'], $context);
@@ -1150,7 +1180,7 @@ function wordpress_import_process_post(&$wordpress_import, &$context) {
 function wordpress_import_process_post_link($link, $baseurl) {
 
   $path = $link;
-  $path = substr($path, strlen($baseurl));
+  $path = drupal_substr($path, drupal_strlen($baseurl));
   $path = rtrim($path, '/');
 
   // Check for Wordpress ugly permalinks and replace with proper Drupal-style aliases
@@ -1232,7 +1262,11 @@ function wordpress_import_process_post_comments($comments, $nid, $disable_trackb
             );
           // Check if duplicate
           if ($node_duplicate) {
-            $tr = db_query("SELECT tr.trid FROM {trackback_received} tr WHERE tr.nid = %d AND tr.created = %d", $node_duplicate->nid, $node_trackback['created']);
+            $tr = db_query("SELECT tr.trid FROM {trackback_received} tr WHERE tr.nid = :nid AND tr.created = :created",
+              array(
+                ':nid' => $node_duplicate->nid,
+                ':created' => $node_trackback['created'],
+            ));
             if ($tr->trid && $tr->excerpt == $node_trackback['excerpt']) {
               continue 2;
             }
@@ -1246,19 +1280,27 @@ function wordpress_import_process_post_comments($comments, $nid, $disable_trackb
         $node_comment = array(
           'nid' => $nid,
           'pid' => 0,
-          'comment' => $comment['content'],
-          'subject' => trim(truncate_utf8(decode_entities(strip_tags($comment['content'])), 29, TRUE)),
+          'subject' => truncate_utf8(trim(decode_entities(strip_tags($comment['content']))), 29, TRUE),
           'name' => decode_entities(strip_tags($comment['author'])),
           'mail' => $comment['author_email'],
           'homepage' => $comment['author_url'],
-          'timestamp' => strtotime($comment['date']),
+          'created' => strtotime($comment['date']),
           'hostname' => $comment['author_IP'],
           'status' => $status,
-          'format' => $wordpress_import->format
+          'comment_body' => array(
+            LANGUAGE_NONE => array(
+              array(
+                'value' => $comment['content'],
+                'format' => $wordpress_import->format,
+            ))),
           );
         // Check if duplicate
         if ($node_duplicate) {
-          $c = db_query("SELECT c.cid, c.comment FROM {comments} c WHERE c.nid = %d AND c.timestamp = %d", $node_duplicate->nid, $node_comment['timestamp']);
+          $c = db_query("SELECT c.cid, c.comment FROM {comments} c WHERE c.nid = :nid AND c.timestamp = :time",
+            array(
+              ':nid' => $node_duplicate->nid,
+              ':time' => $node_comment['timestamp'],
+          ));
           if ($c->cid && $c->comment == $node_comment['comment']) {
             continue 2;
           }
@@ -1276,16 +1318,7 @@ function wordpress_import_process_post_comments($comments, $nid, $disable_trackb
 
   // Save trackbacks
   foreach ($node_trackbacks as $node_trackback) {
-    db_query("INSERT INTO {trackback_received} (nid, created, site, name, subject, url, excerpt, status) VALUES (%d, %d, '%s', '%s', '%s', '%s', '%s', %d)",
-      $node_trackback['nid'],
-      $node_trackback['created'],
-      $node_trackback['site'],
-      $node_trackback['name'],
-      $node_trackback['subject'],
-      $node_trackback['url'],
-      $node_trackback['excerpt'],
-      $node_trackback['status']
-      );
+    db_insert('trackback_received')->fields($node_trackback)->execute();
     $context['results']['created_trackbacks']++;
   }
 
@@ -1293,12 +1326,10 @@ function wordpress_import_process_post_comments($comments, $nid, $disable_trackb
   foreach ($node_comments as $node_comment) {
     $comment_id = comment_save($node_comment);
     // Update fields that haven't been set by comment_save
-    db_query("UPDATE {comments} SET hostname= '%s', timestamp= %d, status= %d WHERE cid= %d",
-      $node_comment['hostname'],
-      $node_comment['timestamp'],
-      $node_comment['status'],
-      $comment_id
-      );
+    db_update('comment')->fields(array(
+      'hostname' => $node_comment['hostname'],
+      'status' => $node_comment['status'],
+      ))->condition('cid', $comment_id)->execute();
     $context['results']['created_comments']++;
   }
 }
@@ -1378,7 +1409,7 @@ function wordpress_import_batch_finished($success, $results, $operations) {
 
   if ($success) {
 
-    $message = t('Import successful : ');
+    $message = t('Import successful') . ' : ';
 
     $msg_results = array();
     $msg_results[] = '<li>' . t('@created_nodes nodes', array('@created_nodes' => $results['created_nodes'])) . '</li>';
@@ -1389,7 +1420,7 @@ function wordpress_import_batch_finished($success, $results, $operations) {
     $msg_results[] = '<li>' . t('@downloaded_images/@total_images images', array('@downloaded_images' => count($results['downloaded_images']), '@total_images' => count($results['downloaded_images'])+count($results['error_images']))) . '</li>';
 
     drupal_set_message($message . '<ul>' . join("\n", $msg_results) . '</ul>');
-    
+
     if (function_exists('dpm')) {
       dpm($results['error_images']);
     }
@@ -1400,9 +1431,9 @@ function wordpress_import_batch_finished($success, $results, $operations) {
       foreach ($results['error_images'] as $error_image) {
         $errors .= '<li>' . l($error_image['title'], 'node/' . $error_image['nid']) . "</li>\n";
       }
-      drupal_set_message(t('Some images could not be transferred for these nodes :') . '<ul>' . $errors . '</ul>', 'warning');
+      drupal_set_message(filter_xss(t('Some images could not be transferred for these nodes :') . '<ul>' . $errors . '</ul>'), 'warning');
     }
-    
+
     if ($results['password_protected'] > 0) {
       drupal_set_message(t('Encountered @password_protected password-protected posts. These were imported as unpublished nodes.', array('@password_protected' => $results['password_protected'])), 'warning');
     }
@@ -1425,7 +1456,7 @@ function wordpress_import_batch_finished($success, $results, $operations) {
 function wordpress_import_read_wxr(&$wordpress_import) {
 
   $is_wxr_file = FALSE;
-  $is_valid_xml = FALSE;
+  $is_valid_xml = TRUE;
 
   $wordpress_import->data['categories'] = array();
   $wordpress_import->data['tags'] = array();
@@ -1452,7 +1483,7 @@ function wordpress_import_read_wxr(&$wordpress_import) {
       if ($reader->name == 'wp:base_blog_url') {
         $reader->read();
         $wordpress_import->data['baseurl'] = $reader->value;
-        if (substr($wordpress_import->data['baseurl'], -1) != '/') {
+        if (drupal_substr($wordpress_import->data['baseurl'], -1) != '/') {
           $wordpress_import->data['baseurl'] .= '/';
         }
       }
@@ -1519,7 +1550,7 @@ function wordpress_import_create_vocabulary($type, $vocabulary_data) {
   $vocabulary = FALSE;
 
   // Check if a category for Wordpress already exists
-  $vocabulary_id = variable_get('wordpress_import_'. $type, 0);
+  $vocabulary_id = variable_get('wordpress_import_' . $type, 0);
 
   if ($vocabulary_id) {
     $vocabulary = taxonomy_vocabulary_load($vocabulary_id);
@@ -1529,8 +1560,8 @@ function wordpress_import_create_vocabulary($type, $vocabulary_data) {
     return $vocabulary->vid;
   }
   else {
-    taxonomy_save_vocabulary($vocabulary_data);
-    variable_set('wordpress_import_'. $type, $vocabulary_data['vid']);
+    taxonomy_vocabulary_save((object) $vocabulary_data);
+    variable_set('wordpress_import_' . $type, $vocabulary_data['vid']);
     return $vocabulary_data['vid'];
   }
 }
@@ -1539,10 +1570,10 @@ function wordpress_import_create_vocabulary($type, $vocabulary_data) {
  * Return an array of taxonomy terms belonging to a vocabulary
  */
 function wordpress_import_get_terms($vid) {
-  $db_result = db_query("SELECT t.tid, t.* FROM {term_data} t WHERE t.vid = %d", $vid);
+  $db_result = db_query("SELECT t.tid, t.* FROM {taxonomy_term_data} t WHERE t.vid = :vid", array(':vid' => $vid));
 
   $result = array();
-  while ($term = db_fetch_object($db_result)) {
+  foreach ($db_result as $term) {
     $result[$term->name] = $term->tid;
   }
 
-- 
1.7.4.1

