You are here

function oa_discussion_update_7104 in Open Atrium Discussion 7.2

Convert discussion replies to comments. This is batched and may take a while, and will run twice per reply.

File

./oa_discussion.install, line 62
oa_discussion.install

Code

function oa_discussion_update_7104(&$sandbox) {

  // Only run if currently enabled.
  if (!module_exists('oa_discussion')) {
    return;
  }
  if (!isset($sandbox['progress'])) {
    cache_clear_all('ctools_plugin_files:', 'cache', TRUE);
    $enable = array(
      'oa_comment',
    );
    module_enable($enable);
    if (!module_exists('oa_comment')) {
      drupal_set_message(t('Unable to enable Open Atrium comments, please enable manually then rerun this update.'), 'error');
      return;
    }
    $sandbox['progress'] = 0;

    // We'll -1 to disregard the uid 0...
    $sandbox['max'] = db_query('SELECT COUNT(DISTINCT uid) FROM {users}')
      ->fetchField() - 1;
    $query = new EntityFieldQuery();
    $result = $query
      ->entityCondition('entity_type', 'node')
      ->entityCondition('bundle', 'oa_discussion_post')
      ->fieldCondition('oa_parent', 'target_id', '0', '>')
      ->propertyOrderBy('created', 'ASC')
      ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
      ->count()
      ->execute();
    $sandbox['max'] = $result * 2;
    $sandbox['updated'] = array();
  }
  if (function_exists('oa_notification_skip')) {
    oa_notification_skip(TRUE);
  }
  $query = new EntityFieldQuery();
  $result = $query
    ->entityCondition('entity_type', 'node')
    ->entityCondition('bundle', 'oa_discussion_post')
    ->fieldCondition('oa_parent', 'target_id', 'NULL', '!=')
    ->propertyOrderBy('created', 'ASC')
    ->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
    ->range(0, 20);
  if ($sandbox['updated']) {
    $query
      ->entityCondition('entity_id', array_keys($sandbox['updated']), 'NOT IN');
  }
  $result = $query
    ->execute();
  $nids = !empty($result['node']) ? array_keys($result['node']) : array();
  $migrated = 0;
  $deleted = 0;
  for ($i = 0; $i < 20; $i++) {
    if ($nids && ($nid = array_shift($nids)) && ($node = node_load($nid))) {

      // Find the highest up reply that hasn't been processed so we can find
      // parent comment up correctly.
      $items = field_get_items('node', $node, 'oa_parent');
      $parent_node = NULL;
      $pid = 0;
      do {
        if ($parent_node && empty($sandbox['updated'][$parent_node->nid])) {
          $node = $parent_node;
        }
        elseif (!$pid && !empty($sandbox['updated'][$items[0]['target_id']])) {
          $pid = $sandbox['updated'][$items[0]['target_id']];
        }

        // Parent node will eventually end up being the discussion node.
        $parent_node = node_load($items[0]['target_id']);
      } while ($parent_node && ($items = field_get_items('node', $parent_node, 'oa_parent')) && !empty($items[0]['target_id']));

      // Enable comments on parent node.
      if (!$pid && $parent_node && $parent_node->comment != COMMENT_NODE_OPEN) {
        $parent_node->comment = COMMENT_NODE_OPEN;
        if (function_exists('oa_messages_skip')) {
          oa_messages_skip(TRUE);
        }
        node_save($parent_node);
        if (function_exists('oa_messages_skip')) {
          oa_messages_skip(FALSE);
        }
      }
      $comment = array(
        'status' => $node->status ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED,
        'name' => $node->name,
        'uid' => $node->uid,
        'comment_body' => $node->body,
        // Comment subject is short.
        'subject' => mb_substr($node->title, 0, 64),
        'nid' => $parent_node->nid,
        'pid' => $pid,
        'cid' => NULL,
        'created' => $node->created,
        'changed' => $node->changed,
      );

      // Preverse any same named fields as is.
      foreach (array_keys(field_info_instances('comment', 'comment_node_oa_discussion_post')) as $field_name) {
        if (!empty($node->{$field_name})) {
          $comment[$field_name] = $node->{$field_name};
        }
      }

      // Add attachments as paragraphs.
      if ($attachments = field_get_items('node', $node, 'field_oa_media')) {
        $item['entity'] = entity_create('paragraphs_item', array(
          'bundle' => 'paragraph_media',
          'field_name' => 'field_oa_related',
        ));
        $item['entity']->field_oa_media[LANGUAGE_NONE] = $attachments;
        $comment['field_oa_related'][LANGUAGE_NONE][] = $item;
      }
      $comment = (object) $comment;
      comment_save($comment);
      $migrated++;
      $sandbox['updated'][$node->nid] = $comment->cid;
    }
    elseif (!$nids && !empty($sandbox['updated'])) {
      $nid = key($sandbox['updated']);
      unset($sandbox['updated'][$nid]);
      if (function_exists('oa_messages_skip')) {
        oa_messages_skip(TRUE);
      }
      node_delete($nid);
      $deleted++;
    }
    else {
      break;
    }
  }
  if ($migrated && !$deleted) {
    drupal_set_message(t('Migrated @migrated replies (@count/@max)', array(
      '@migrated' => $migrated,
      '@count' => count($sandbox['updated']),
      '@max' => $sandbox['max'],
    )));
  }
  if ($deleted) {
    drupal_set_message(t('Deleted @deleted migrated replies (@count/@max)', array(
      '@deleted' => $deleted,
      '@count' => $sandbox['max'] - count($sandbox['updated']),
      '@max' => $sandbox['max'],
    )));
  }
  $sandbox['#finished'] = !$nids && empty($sandbox['updated']) ? 1 : (!$nids ? ($sandbox['max'] - count($sandbox['updated'])) / $sandbox['max'] : count($sandbox['updated']) / $sandbox['max']);

  // Delete the field no longer in use.
  if ($sandbox['#finished'] == 1) {
    field_delete_field('oa_parent');
  }
}