Drupal专业开发指南 第23章 一个批处理操作回调

导入了用户以后,那么将会调用importusers_optimize()。最后,当该操作也完成后,那么将会调用我们在finished键中指定的回调(importusers_finished())。下面是importusers_import()函数:

 
/**
 * Batch callback operation: Import users.
 *
 * @param $size
 * Number of users to import in each operation.
 * @param $context
 * Batch context containing state information.
 */
function importusers_import($size, &$context) {
    // Initialize sandbox the first time through.
    if (!isset($context['sandbox']['progress'])) {
        $context['sandbox']['progress'] = 0;
        $context['sandbox']['current_user_id'] = 0;
        $context['sandbox']['max'] = db_result(
            db_query('SELECT COUNT(DISTINCT user_id) FROM {old_users}'));
    }
 
    // Retrieve some users from the old_users table.
    $result = db_query_range("SELECT user_id, username AS name, email AS mail,
        pass FROM {old_users} WHERE user_id > %d ORDER BY user_id",
        $context['sandbox']['current_user_id'], 0, $size);
 
    // Transform them into Drupal users.
    while ($account = db_fetch_array($result)) {
        $new_user = user_save(array(), $account);
 
        // Update progress information.
        $context['sandbox']['progress']++;
        $context['sandbox']['current_user_id'] = $account['user_id'];
        $context['message'] = t('Importing user %username', array('%username' =>
            $new_user->name));
 
        // Store usernames in case the the 'finished' callback wants them.
        $context['results'][] = $new_user->name;
    }
 
    // Let the batch engine know how close we are to completion.
    if ($context['sandbox']['progress'] == $context['sandbox']['max']) {
        // Done!
        $context['finished'] = 1;
    }
    else {
        $context['finished'] = $context['sandbox']['progress'] /
        $context['sandbox']['max'];
    }
}
 
/**
 * Batch callback operation: Optimize users.
 * For now, this function does nothing.
 *
 * @param $context
 * Batch context containing state information.
 */
function importusers_optimize(&$context) {
    // Code would go here.
    // Inform the batch engine that we are done.
    $context['finished'] = 1;
}
 
    注意,除了你在批处理集操作数组中指出的参数以外,importusers_import()还接收了另一个名为$context的参数。$context是一个数组,它是通过引用传递的,它包含了来自于批处理引擎的关于当前批处理集的状态信息。$context的内容如下所示:
 
• sandbox:这个区域是供回调函数使用的。你可以在这里存储你需要的任何东西,并且它将会自动持久化。在我们的例子中,我们存储的信息有,要导入的用户数,当前正被导入的用户,等等。在批处理进行处理期间,使用这个来存储信息,而不是使用$_SESSION。如果你使用了$_SESSION,那么当用户打开了一个新的浏览器窗口,就可能出错。
 
• results:这个给finished回调使用的包含结果的数组。例如,如果用户想看到导入的用户名的列表时,那么就可以使用这个。
 
• message:是用来显示在进度页面的消息。
 
• finished:这是一个浮点数字,位于从0到1之间,用来指示处理了多少数据。当所有的数据处理完时,将它设置为1,来指示批处理引擎可以继续下一个操作了。
 
    下面是所有的批处理操作运行完成以后,所要调用的回调:
 
/**
 * Called when all batch operations are complete.
 */
function importusers_finished($success, $results, $operations) {
    if ($success) {
        drupal_set_message(t('User importation complete.'));
    }
    else {
        // A fatal error occurred during batch processing.
        $error_operation = reset($operations);
        $operation = array_shift($error_operation);
        $arguments = array_shift($error_operation);
        $arguments_as_string = implode(', ', $arguments);
        watchdog('importusers', "Error when calling operation '%s'('%s')",
            array($operation, $arguments_as_string));
        drupal_set_message(t('An error occurred and has been recorded
            in the system log.'), 'error');
    }
}

Drupal版本: