I am writing a script to update all website registered user's Username & Email address. I have more then 5000+ users in my website which causes PHP execution time out error.
So I tried to implement Batch API but I am unable to implement it. I passed whole user list to Batch API and perform operations on it. But Batch API also shows the same PHP time out error.
Can someone help me to implement Batch API?
Here is my code:
function anonymize_data_menu() {
$items = array();
$items['admin/config/content/anonymize-user-data'] = array(
'title' => 'Anonymize User Data',
'description' => 'Click this link to anonymize user data',
'type' => MENU_LOCAL_ACTION,
'page callback' => '_anonymize_users_data',
'access arguments' => array('administer user anonymize'),
'file' => 'anonymize_data.user.inc',
);
return $items;
}
function _anonymize_users_data() {
$users = entity_load('user');
$chunks = array_chunk($users, 20);
$count_user = count($chunks);
foreach ($chunks as $chunk) {
$operations[] = array('user_anonymize_op', array($chunk));
//user_anonymize_op($chunk);
//krumo($chunk);
}
$batch = array(
'operations' => $operations,
'title' => t('Import batch'),
'init_message' => t('Initializing'),
'error_message' => t('An error occurred'),
'finished' => 'user_anonymize_op_finished'
);
batch_set($batch);
batch_process('admin/people');
}
function user_anonymize_op($users, &$context) {
foreach ($users as $user) {
# code...
//krumo($user);
$context['message'] = t('processing user @user', array('@user' => $user->name));
//
if($user->uid != 0 && $user->uid != 1) {
$user->name = $i . '-' . $user->name;
$user->pass = user_hash_password('blah');
user_save($user);
}
}
}
function user_anonymize_op_finished($success, $results, $operations) {
if ($success) {
$message = count($results) . ' processed.';
}
drupal_set_message(t($message));
}
I am very confused about that how to process chunk of users.
Can someone help me it will be great.