diff -u b/realname.drush.inc b/realname.drush.inc --- b/realname.drush.inc +++ b/realname.drush.inc @@ -1,4 +1,5 @@ "Recreate realnames on the site.", + 'description' => 'Recreate realnames on the site.', 'drupal dependencies' => array('realname'), 'arguments' => array( 'uid' => '(optional) A space-delimited list of User IDs', @@ -53,7 +54,6 @@ } } - /** * Recreates realnames for specified (or all) users. * @@ -69,40 +69,81 @@ $args = func_get_args(); - // If there are any arguments (at all) then they should all be UIDs. + $query = db_select('users'); + $query->fields('users', array('uid')); + // No point updating the anonymous user. + $query->condition('uid', 0, '!='); + + // Take into account the UIDs which were given. if (count($args)) { - $uids = array(); + $query->condition('uid', $args, 'IN'); + } + + $uids = $query->execute()->fetchCol(); + + // Display info about UIDs which aren't valid. + if ($wrong_uids = array_diff($args, $uids)) { + drush_log(dt('Users with uids !uids do not exist and can\'t be updated.', array('!uids' => implode(', ', $wrong_uids))), 'error'); + } + + // Loop complete, it's possible there are no valid UIDs to update + if (!$uids) { + return drush_set_error(dt('No valid users available to update.')); + } - // Loop over each of the UIDs and check if the user account exists. - foreach ($args as $uid) { - if ($account = user_load($uid)) { - // Account exists, add the UID to our array. - $uids[] = $uid; - } - else { - // Account does not exist, let the user know. - drush_set_error('error', dt('User with UID #!uid does not exist.', array('!uid' => $uid))); - } - } - - // Loop complete, it's possible there are no valid UIDs to update - if (empty($uids)) { - return drush_set_error(dt('No valid users available to update.')); - } - } - else { - // No UIDs defined, so we need to load all of the UIDs from {users} - $uids = db_select('users', 'u') - ->fields('u', array('uid')) - // no point updating the anonymous user. - ->condition('u.uid', 0, '!=') - ->execute() - ->fetchCol(); + // Break up all of our data so each process does not time out. + $operations = array(); + $chunks = array_chunk($uids, 100); + $count_chunks = count($chunks); + + foreach ($chunks as $i => $chunk) { + $operations[] = array('realname_recreate', array( + $chunk, + 'details'=> t('Updating chunk @chunk of @count', array( + '@chunk'=> $i + 1, + '@count'=> $count_chunks, + )), + )); } - // $uids now contains an array of users we know to exist. - realname_user_operations_realname_update($uids); + // Put all that information into our batch array. + $batch = array( + 'operations' => $operations, + 'title' => dt('Recreate realnames on the site.'), + 'init_message' => dt('Initializing'), + 'error_message' => dt('An unrecoverable error has occurred.'), + 'finished' => 'realname_recreate_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(); +} + +/** + * Update the users. + */ +function realname_recreate($chunk, $operation_details, &$context) { + if (!isset($context['results']['updated'])) { + $context['results']['updated'] = 0; + } + + realname_user_operations_realname_update($chunk); + $context['results']['updated'] += count($chunk); + + $context['message'] = $operation_details; +} + +/** + * Result message. + */ +function realname_recreate_finished($success, $results, $operations) { drush_log(dt('Updated realnames for !count users.', array( - '!count' => count($uids), + '!count' => $results['updated'], )), 'completed'); }