gpt4 book ai didi

drupal - Drupal 6 Batch API 如何工作?

转载 作者:行者123 更新时间:2023-12-05 00:40:50 25 4
gpt4 key购买 nike

我一直在成功地使用 Batch API 来执行通常会导致 PHP 超时或内存不足错误的处理,并且它一直运行良好。

我已经稍微浏览了代码,但我仍然不清楚幕后发生了什么。

熟悉这个过程的人可以描述它是如何工作的吗?

最佳答案

I've looked through the code a little, but I'm still unclear about what's happening behind the scenes.

Could someone familiar with the process describe how it works?



发生的情况是,为了避免 PHP 超时,浏览器会定期通过 AJAX ping 导致执行批处理操作的 URL ( http://example.com/batch?id= $id)。
_batch_page() ,这是 system_batch_page()调用的函数,“批处理”路径的菜单回调。
function _batch_page() {
$batch = &batch_get();

// Retrieve the current state of batch from db.
if (isset($_REQUEST['id']) && $data = db_result(db_query("SELECT batch FROM {batch} WHERE bid = %d AND token = '%s'", $_REQUEST['id'], drupal_get_token($_REQUEST['id'])))) {
$batch = unserialize($data);
}
else {
return FALSE;
}

// Register database update for end of processing.
register_shutdown_function('_batch_shutdown');

$op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
$output = NULL;
switch ($op) {
case 'start':
$output = _batch_start();
break;

case 'do':
// JS-version AJAX callback.
_batch_do();
break;

case 'do_nojs':
// Non-JS progress page.
$output = _batch_progress_page_nojs();
break;

case 'finished':
$output = _batch_finished();
break;
}

return $output;
}

_batch_progress_page_nojs() ,你会注意到下面的代码。
  $url = url($batch['url'], array('query' => array('id' => $batch['id'], 'op' => $new_op)));
drupal_set_html_head('<meta http-equiv="Refresh" content="0; URL=' . $url . '">');
$output = theme('progress_bar', $percentage, $message);
return $output;

设置“刷新”元标记将导致页面刷新。

Drupal 7 中有类似的代码;不同的是代码已经移植了,它使用了Drupal 7实现的新功能。
  // Merge required query parameters for batch processing into those provided by
// batch_set() or hook_batch_alter().
$batch['url_options']['query']['id'] = $batch['id'];
$batch['url_options']['query']['op'] = $new_op;

$url = url($batch['url'], $batch['url_options']);
$element = array(
'#tag' => 'meta',
'#attributes' => array(
'http-equiv' => 'Refresh',
'content' => '0; URL=' . $url,
),
);
drupal_add_html_head($element, 'batch_progress_meta_refresh');

return theme('progress_bar', array('percent' => $percentage, 'message' => $message));

启用 JavaScript 后,完成所有工作的代码位于 batch.js 中。文件。
/**
* Attaches the batch behavior to progress bars.
*/
Drupal.behaviors.batch = function (context) {
// This behavior attaches by ID, so is only valid once on a page.
if ($('#progress.batch-processed').size()) {
return;
}
$('#progress', context).addClass('batch-processed').each(function () {
var holder = this;
var uri = Drupal.settings.batch.uri;
var initMessage = Drupal.settings.batch.initMessage;
var errorMessage = Drupal.settings.batch.errorMessage;

// Success: redirect to the summary.
var updateCallback = function (progress, status, pb) {
if (progress == 100) {
pb.stopMonitoring();
window.location = uri+'&op=finished';
}
};

var errorCallback = function (pb) {
var div = document.createElement('p');
div.className = 'error';
$(div).html(errorMessage);
$(holder).prepend(div);
$('#wait').hide();
};

var progress = new Drupal.progressBar('updateprogress', updateCallback, "POST", errorCallback);
progress.setProgress(-1, initMessage);
$(holder).append(progress.element);
progress.startMonitoring(uri+'&op=do', 10);
});
};

批量 URL 的轮询以 progress.startMonitoring(uri+'&op=do', 10) 开头. batch.js 文件依赖于 Drupal.progressBar 中公开的功能,在 progress.js 中定义文件。

Drupal 7 中使用了类似的代码,它使用了一个略有不同版本的 batch.js , 和 progress.js文件。
(function ($) {

/**
* Attaches the batch behavior to progress bars.
*/
Drupal.behaviors.batch = {
attach: function (context, settings) {
$('#progress', context).once('batch', function () {
var holder = $(this);

// Success: redirect to the summary.
var updateCallback = function (progress, status, pb) {
if (progress == 100) {
pb.stopMonitoring();
window.location = settings.batch.uri + '&op=finished';
}
};

var errorCallback = function (pb) {
holder.prepend($('<p class="error"></p>').html(settings.batch.errorMessage));
$('#wait').hide();
};

var progress = new Drupal.progressBar('updateprogress', updateCallback, 'POST', errorCallback);
progress.setProgress(-1, settings.batch.initMessage);
holder.append(progress.element);
progress.startMonitoring(settings.batch.uri + '&op=do', 10);
});
}
};

})(jQuery);

不同之处在于,从 Drupal 7 开始,所有 jQuery 代码都包含在 (function ($) { })(jQuery); 中。 ,还有 jQuery Once plugin is included with Drupal 7 . Drupal 7 还设置了 WAI-ARIA 属性以与屏幕阅读器兼容;这也发生在从 JavaScript 代码添加的 HTML 中,如下所示,在 progress.js 文件中找到。
  // The WAI-ARIA setting aria-live="polite" will announce changes after users
// have completed their current activity and not interrupt the screen reader.
this.element = $('<div class="progress" aria-live="polite"></div>').attr('id', id);
this.element.html('<div class="bar"><div class="filled"></div></div>' +
'<div class="percentage"></div>' +
'<div class="message">&nbsp;</div>');

在提供批处理页面时,Drupal 设置 _batch_shutdown()作为关闭回调;当 PHP 由于超时而关闭时,该函数会更新数据库中的批处理数组。
// Drupal 6.
function _batch_shutdown() {
if ($batch = batch_get()) {
db_query("UPDATE {batch} SET batch = '%s' WHERE bid = %d", serialize($batch), $batch['id']);
}
}
// Drupal 7.
function _batch_shutdown() {
if ($batch = batch_get()) {
db_update('batch')
->fields(array('batch' => serialize($batch)))
->condition('bid', $batch['id'])
->execute();
}
}

关于drupal - Drupal 6 Batch API 如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3403599/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com