You are here

function remove_duplicates_batch_operation in Remove Duplicates 7

Operation for batch_set().

Parameters

string $node_type_machine_name: The node type to fetch.

string $node_field_machine_name: The {field} used to group nodes and therefore create sets of duplicate nodes.

bool $prioritize_published_nodes: If TRUE, the last published node in a set of duplicate nodes will be kept. Otherwise, the first node in a set of duplicate nodes will be kept.

bool $case_sensitive: If TRUE, duplicates detection is case sensitive Otherwise, duplicates detection is case insensitive.

array $nodes_marked_as_removable: [Optional] An array of nids to remove. Provided when using custom tableselect output.

1 string reference to 'remove_duplicates_batch_operation'
remove_duplicates_confirm_settings_form_submit_process in ./remove_duplicates.module
Processing the settings form (Processing step 2/2).

File

./remove_duplicates.module, line 426
Remove duplicate nodes according to node fields or Custom fields.

Code

function remove_duplicates_batch_operation($node_type_machine_name, $node_field_machine_name, $prioritize_published_nodes, $case_sensitive, $nodes_marked_as_removable, &$context) {
  if (isset($context['sandbox']) && isset($context['sandbox']['nodes_to_remove'])) {
    $nodes_to_remove = $context['sandbox']['nodes_to_remove'];
  }
  else {
    $nodes_to_remove = _remove_duplicates_get_nodes_ids_to_remove($node_type_machine_name, $node_field_machine_name, $prioritize_published_nodes, $case_sensitive, $nodes_marked_as_removable);
  }
  if (empty($context['sandbox'])) {
    $context['sandbox']['current'] = 0;
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = count($nodes_to_remove);
    $context['sandbox']['nodes_to_remove'] = $nodes_to_remove;
  }
  if (empty($nodes_to_remove)) {
    $context['finished'] = 1;
    if (isset($context['sandbox']) && isset($context['sandbox']['nodes_to_remove'])) {
      unset($context['sandbox']['nodes_to_remove']);
    }
  }
  else {
    $limit = 5;
    if (count($nodes_to_remove) < $limit) {
      $limit = count($nodes_to_remove);
    }
    $preserve_keys = TRUE;
    $nodes_to_remove = array_slice($nodes_to_remove, 0, $limit, $preserve_keys);
    if (count($nodes_to_remove)) {
      $nodes_to_remove_nids = array();
      foreach ($nodes_to_remove as $node_to_remove) {
        if (is_object($node_to_remove)) {
          if ($node_to_remove->nid) {
            $nodes_to_remove_nids[$node_to_remove->nid] = $node_to_remove->nid;
          }
          watchdog('remove_duplicates', 'Batch - Duplicate node @nid deleted : @title | updated on @changed | created on @created.', array(
            '@nid' => isset($node_to_remove->nid) ? $node_to_remove->nid : NULL,
            '@title' => isset($node_to_remove->title) ? $node_to_remove->title : NULL,
            '@changed' => isset($node_to_remove->changed) ? format_date($node_to_remove->changed) : NULL,
            '@created' => isset($node_to_remove->created) ? format_date($node_to_remove->created) : NULL,
          ), WATCHDOG_DEBUG);
        }
      }
      node_delete_multiple($nodes_to_remove_nids);
      foreach ($nodes_to_remove_nids as $nid) {
        if (isset($context['sandbox']['nodes_to_remove']) && isset($context['sandbox']['nodes_to_remove'][$nid])) {
          unset($context['sandbox']['nodes_to_remove'][$nid]);
        }
      }
    }
    $context['sandbox']['progress'] = $context['sandbox']['progress'] + $limit;
    if ($context['sandbox']['progress'] != $context['sandbox']['max'] && $context['sandbox']['max'] != 0) {
      $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
    }
    else {
      $context['finished'] = 0.99;
    }
  }
  $context['results']['processed'] = $context['sandbox']['progress'];
}