You are here

function joomla_import_content in Joomla to Drupal 7

Same name and namespace in other branches
  1. 6 joomla.module \joomla_import_content()
2 calls to joomla_import_content()
joomla_cron in ./joomla.module
joomla_import_form_submit in ./joomla.module
3 string references to 'joomla_import_content'
joomla_cron in ./joomla.module
joomla_import_form_checkboxes in ./joomla.module
These checkboxes are used on both the admin and import forms
joomla_uninstall in ./joomla.install
Implementation of hook_uninstall().

File

./joomla.module, line 702
The joomla module used for migrate Joomla to Drupal.

Code

function joomla_import_content($joomla_update_duplicate = NULL) {
  joomla_database_init();
  if ($joomla_update_duplicate === NULL) {
    $joomla_update_duplicate = variable_get('joomla_update_duplicate', JOOMLA_UPDATE_DUPLICATE);
  }
  $joomla_prefix = variable_get('joomla_prefix', JOOMLA_PREFIX);
  $content_total = 0;
  $content_updated = 0;
  $content_new = 0;
  $content_failed = 0;
  $images = array();
  db_set_active('joomla');
  $results_joomla = db_query("SELECT c.*,cf.content_id FROM {$joomla_prefix}content c LEFT JOIN {$joomla_prefix}content_frontpage cf ON cf.content_id = c.id");
  db_set_active();
  foreach ($results_joomla as $data_joomla) {
    $content_total++;
    $content_map = db_query('SELECT n.nid,jcontentid,changed FROM {joomla_content} jc JOIN {node} n ON n.nid = jc.nid WHERE jc.jcontentid = :contentid', array(
      ':contentid' => $data_joomla->id,
    ))
      ->fetch();
    if ($content_map && !$joomla_update_duplicate) {

      // Content item has already been imported and update is off
      continue;
    }

    /**
     * If the content item already exists, but has not been updated
     * since the last import, skip it
     */
    $joomla_changed = strtotime($data_joomla->modified);

    //if changed is negative drupal will throw an error so:
    if ($joomla_changed < 0) {
      $joomla_changed = 0;
    }
    if ($content_map && $joomla_changed == $content_map->changed) {
      continue;
    }
    $node = new stdClass();
    $node_revision = new stdClass();
    if ($content_map) {
      $node->nid = $content_map->nid;
      $node_revision->nid = $content_map->nid;
    }
    $author_uid = db_query('SELECT uid FROM {joomla_users} WHERE juid = :juid', array(
      ':juid' => $data_joomla->created_by,
    ))
      ->fetch();
    if ($author_uid) {
      $node->uid = intval($author_uid->uid);
    }
    $node_revision->uid = $node->uid;
    $node->title = $data_joomla->title;
    $node_revision->title = $data_joomla->title;
    $node->status = $data_joomla->state;
    $node_revision->status = $data_joomla->state;
    $node->created = strtotime($data_joomla->created);

    //if created is negative drupal will throw an error so:
    if ($node->created < 0) {
      $node->created = 0;
    }
    $node->language = 'und';
    $node->changed = $joomla_changed;
    $node_revision->timestamp = $joomla_changed;

    // Set content type
    if ($data_joomla->sectionid == 0) {
      $joomla_type = variable_get('joomla_default_static_nodetype', JOOMLA_DEFAULT_STATIC_NODETYPE);
    }
    else {
      $joomla_type = variable_get('joomla_default_blog_nodetype', JOOMLA_DEFAULT_BLOG_NODETYPE);
    }
    $node->type = $joomla_type;
    if (!empty($data_joomla->introtext)) {
      $joomla_body = $data_joomla->introtext . "<!--break-->" . $data_joomla->fulltext;
      $joomla_teaser = $data_joomla->introtext;
    }
    else {
      $joomla_body = $data_joomla->fulltext;
      $joomla_teaser = text_summary($joomla_body);
    }
    $joomla_body = str_replace("{mospagebreak}", "", $joomla_body);

    //images
    if ($data_joomla->images) {
      $joomla_teaser = joomla_replace_mos_image($data_joomla->images, $joomla_teaser);
      $joomla_body = joomla_replace_mos_image($data_joomla->images, $joomla_body);
    }
    $joomla_teaser = joomla_replace_image_link($joomla_teaser);
    $joomla_body = joomla_replace_image_link($joomla_body);

    //in d7 we need to store body in the fields table not node_revision,so:
    $body_field = new stdClass();
    $body_field->entity_type = variable_get('joomla_entity_type', JOOMLA_ENTITY_TYPE);
    $body_field->bundle = $node->type;
    $body_field->language = 'und';
    $body_field->body_value = $joomla_body;
    $body_field->body_summary = $joomla_teaser;
    $body_field->body_format = variable_get('joomla_input_format', JOOMLA_INPUT_FORMAT);
    $body_field->delta = 0;

    // This is used to make the taxonomy association
    $term_node = new stdClass();
    $term_node->tid = db_query('SELECT tid FROM {joomla_categories} WHERE jcategoryid = :jcategoryid AND jsectionid = :jsectionid', array(
      ':jcategoryid' => $data_joomla->catid,
      ':jsectionid' => $data_joomla->sectionid,
    ))
      ->fetchField();

    // Promote to front page?
    if ($data_joomla->content_id) {
      $joomla_promote = 1;
    }
    else {
      $joomla_promote = 0;
    }
    $node->promote = $joomla_promote;
    $node_revision->promote = $joomla_promote;
    $status = FALSE;
    if ($content_map) {

      // Updating an existing node
      $node_status = drupal_write_record('node', $node, 'nid');

      /*
       * Even though Joomla doesn't have revision info, no reason
       * why we can't use Drupals revision system when making an update!
       */
      $node_revision->log = 'Updated from Joomla module';
      $node_revision_status = drupal_write_record('node_revision', $node_revision);
      $node->vid = $node_revision->vid;
      db_query('UPDATE {node} SET vid = :vid WHERE nid = :nid', array(
        ':vid' => $node_revision->vid,
        ':nid' => $node->nid,
      ));
      $body_field->entity_id = $node->nid;
      $body_field->revision_id = $node_revision->vid;
      drupal_write_record('field_revision_body', $body_field);
      drupal_write_record('field_data_body', $body_field, 'entity_id');
      if ($term_node->tid) {
        $term_node->nid = $node->nid;
        drupal_write_record('taxonomy_index', $term_node);
      }
    }
    else {

      // Creating a new node
      $node_status = drupal_write_record('node', $node);
      $node_revision->nid = $node->nid;
      $node_revision->log = 'Initial creation from Joomla module';
      $node_revision_status = drupal_write_record('node_revision', $node_revision);
      $node->vid = $node_revision->vid;
      db_query('UPDATE {node} SET vid = :vid WHERE nid = :nid', array(
        ':vid' => $node_revision->vid,
        ':nid' => $node->nid,
      ));
      $body_field->entity_id = $node->nid;
      $body_field->revision_id = $node_revision->vid;
      drupal_write_record('field_data_body', $body_field);
      drupal_write_record('field_revision_body', $body_field);
      $joomla_content = new stdClass();
      $joomla_content->nid = $node->nid;
      $joomla_content->jcontentid = $data_joomla->id;
      drupal_write_record('joomla_content', $joomla_content);
      if ($term_node->tid) {
        $term_node->nid = $node->nid;
        drupal_write_record('taxonomy_index', $term_node);
      }
    }
    if ($node_status == SAVED_NEW && $node_revision_status == SAVED_NEW) {
      $content_new++;
    }
    elseif ($node_status == SAVED_UPDATED && $node_revision_status == SAVED_NEW) {
      $content_updated++;
    }
    else {
      $content_failed++;
    }

    // Hook to allow other modules to modify the node
    module_invoke_all('joomla', 'node', $node, $data_joomla->id);
    joomla_sleep($content_total);
  }

  /*
   * The node_comment_statistics table must be populated with a row for
   * all of the created nodes, even if the nodes have no comments.  This
   * query is probably only valid on MySQL, but this module can currently
   * only go from MySQL -> MySQL anyway.
   */
  db_query('INSERT IGNORE INTO {node_comment_statistics} (nid,last_comment_timestamp,last_comment_uid) SELECT n.nid, n.created,1 FROM {node} n LEFT JOIN {node_comment_statistics} ncs ON n.nid = ncs.nid WHERE ISNULL(ncs.nid)');
  drupal_set_message(t('Processed @total content items (@new new, @updated updated, @failed errors)', array(
    '@total' => $content_total,
    '@new' => $content_new,
    '@updated' => $content_updated,
    '@failed' => $content_failed,
  )));
}