Created
September 29, 2017 18:22
-
-
Save andriyun/47d50e073674075c9a05eb8a23dcbe51 to your computer and use it in GitHub Desktop.
Example of batch function to delete nodes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Callback function for delete all nodes. | |
* | |
* @param array $types_of_content | |
* Array of machine node type names to delete. | |
* | |
* @param array $except_uids | |
* Array with uids of ingnore users who created nodes. | |
*/ | |
function _delete_all_nodes($types_of_content, $except_uids = array()) { | |
if (empty($types_of_content)) { | |
drush_print('Node types not specified.'); | |
return; | |
} | |
$results = db_select('node', 'n') | |
->fields('n', array('nid')) | |
->condition('n.type', $types_of_content, 'IN'); | |
// Exclude nodes created from special users. | |
if (!empty($except_uids)) { | |
$results = $results->condition('n.uid', $except_uids, 'NOT IN'); | |
} | |
$results = $results->orderBy('n.nid', 'ASC') | |
->execute(); | |
$nids = $results->fetchCol(); | |
$num_of_nodes = count($nids); | |
if ($num_of_nodes < 1) { | |
drush_print('Not have nodes for delete.'); | |
return; | |
} | |
$chunks = array_chunk($nids, 20); | |
$operations = array(); | |
$progress = 0; | |
$limit = 20; | |
$max = $num_of_nodes; | |
// Set up our batch operations. | |
foreach ($chunks as $chunk) { | |
$operations[] = array( | |
'_delete_nodes', | |
array( | |
$chunk, $progress, $limit, $max, | |
), | |
); | |
$progress = $progress + $limit; | |
} | |
$batch = array( | |
'operations' => $operations, | |
'title' => t('Delete nodes'), | |
'init_message' => t('Initializing'), | |
'error_message' => t('An error occurred'), | |
'progress_message' => t('Processed batch #@current out of @total.'), | |
'finished' => '_common_delete_nodes_finished', | |
); | |
// Get the batch process all ready! | |
batch_set($batch); | |
$batch =& batch_get(); | |
// Because we are doing this on the back-end, we set progressive to false. | |
$batch['progressive'] = FALSE; | |
// Start processing the batch operations. | |
drush_backend_batch_process(); | |
} | |
/** | |
* Callback function for delete all nodes batch process. | |
*/ | |
function _delete_nodes($nids, $progress, $limit, $max, &$context) { | |
// Set default starting values. | |
if (empty($context['sandbox'])) { | |
$context['sandbox'] = array(); | |
$context['sandbox']['progress'] = 0; | |
$context['sandbox']['current_nid'] = 0; | |
$context['sandbox']['max'] = $limit; | |
} | |
if (!empty($nids)) { | |
node_delete_multiple($nids); | |
foreach ($nids as $nid) { | |
$path = path_load( | |
array('source' => 'node/' . $nid) | |
); | |
path_delete($path['pid']); | |
} | |
} | |
$context['results'][] = $max; | |
$context['sandbox']['progress'] += $progress; | |
$context['sandbox']['current_nid'] = array_pop($nids); | |
// Inform the batch engine that we are not finished, | |
// and provide an estimation of the completion level we reached. | |
if ($context['sandbox']['progress'] != $context['sandbox']['max']) { | |
$context['finished'] = (($context['sandbox']['max'] - $context['sandbox']['progress']) <= $limit) || ($context['sandbox']['progress'] >= $context['sandbox']['max']); | |
} | |
} | |
/** | |
* Callback function for delete all nodes batch finish process. | |
*/ | |
function _delete_nodes_finished($success, $results, $operations) { | |
if ($success) { | |
drupal_set_message(t('@count nodes was deleted.', array('@count' => array_pop($results)))); | |
} | |
else { | |
$error_operation = reset($operations); | |
drupal_set_message( | |
t('An error occurred while processing @operation with arguments : @args', | |
array( | |
'@operation' => $error_operation[0], | |
'@args' => print_r($error_operation[0], TRUE), | |
) | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment