Я немного просмотрел код, но мне все еще неясно, что происходит за кулисами.
Может ли кто-нибудь, знакомый с процессом, описать, как он работает?
Что происходит, чтобы избежать тайм-аутов PHP, браузер периодически пропускает через AJAX 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;
}
In _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);
});
};
Опрос летучей мышиch 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 включен в Drupal 7 .Drupal 7 также устанавливает атрибуты WAI-ARIA для совместимости с программами чтения с экрана;это также происходит в HTML-коде, добавленном из кода JavaScript, например, приведенного ниже в файле 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"> </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();
}
}