123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- <?php
- function batch_load($id) {
- $batch = db_query("SELECT batch FROM {batch} WHERE bid = :bid AND token = :token", array(
- ':bid' => $id,
- ':token' => drupal_get_token($id),
- ))->fetchField();
- if ($batch) {
- return unserialize($batch);
- }
- return FALSE;
- }
- function _batch_page() {
- $batch = &batch_get();
- if (!isset($_REQUEST['id'])) {
- return FALSE;
- }
-
- if (!$batch) {
- $batch = batch_load($_REQUEST['id']);
- if (!$batch) {
- drupal_set_message(t('No active batch.'), 'error');
- drupal_goto();
- }
- }
-
- drupal_register_shutdown_function('_batch_shutdown');
-
- foreach ($batch['sets'] as $batch_set) {
- if (isset($batch_set['css'])) {
- foreach ($batch_set['css'] as $css) {
- drupal_add_css($css);
- }
- }
- }
- $op = isset($_REQUEST['op']) ? $_REQUEST['op'] : '';
- $output = NULL;
- switch ($op) {
- case 'start':
- $output = _batch_start();
- break;
- case 'do':
-
- _batch_do();
- break;
- case 'do_nojs':
-
- $output = _batch_progress_page_nojs();
- break;
- case 'finished':
- $output = _batch_finished();
- break;
- }
- return $output;
- }
- function _batch_start() {
- if (isset($_COOKIE['has_js']) && $_COOKIE['has_js']) {
- return _batch_progress_page_js();
- }
- else {
- return _batch_progress_page_nojs();
- }
- }
- function _batch_progress_page_js() {
- $batch = batch_get();
- $current_set = _batch_current_set();
- drupal_set_title($current_set['title'], PASS_THROUGH);
-
-
- $batch['url_options']['query']['id'] = $batch['id'];
- $js_setting = array(
- 'batch' => array(
- 'errorMessage' => $current_set['error_message'] . '<br />' . $batch['error_message'],
- 'initMessage' => $current_set['init_message'],
- 'uri' => url($batch['url'], $batch['url_options']),
- ),
- );
- drupal_add_js($js_setting, 'setting');
- drupal_add_library('system', 'drupal.batch');
- return '<div id="progress"></div>';
- }
- function _batch_do() {
-
- if ($_SERVER['REQUEST_METHOD'] != 'POST') {
- drupal_set_message(t('HTTP POST is required.'), 'error');
- drupal_set_title(t('Error'));
- return '';
- }
-
- list($percentage, $message) = _batch_process();
- drupal_json_output(array('status' => TRUE, 'percentage' => $percentage, 'message' => $message));
- }
- function _batch_progress_page_nojs() {
- $batch = &batch_get();
- $current_set = _batch_current_set();
- drupal_set_title($current_set['title'], PASS_THROUGH);
- $new_op = 'do_nojs';
- if (!isset($batch['running'])) {
-
- $percentage = 0;
- $message = $current_set['init_message'];
- $batch['running'] = TRUE;
- }
- else {
-
-
-
-
- ob_start();
- $fallback = $current_set['error_message'] . '<br />' . $batch['error_message'];
- $fallback = theme('maintenance_page', array('content' => $fallback, 'show_messages' => FALSE));
-
-
-
-
- list($fallback) = explode('<!--partial-->', $fallback);
- print $fallback;
-
- list($percentage, $message) = _batch_process($batch);
- if ($percentage == 100) {
- $new_op = 'finished';
- }
-
- ob_end_clean();
- }
-
-
- $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));
- }
- function _batch_process() {
- $batch = &batch_get();
- $current_set = &_batch_current_set();
-
- $set_changed = TRUE;
-
-
-
-
- if ($batch['progressive']) {
- timer_start('batch_processing');
- }
- if (empty($current_set['start'])) {
- $current_set['start'] = microtime(TRUE);
- }
- $queue = _batch_queue($current_set);
- while (!$current_set['success']) {
-
-
-
- if ($set_changed && isset($current_set['file']) && is_file($current_set['file'])) {
- include_once DRUPAL_ROOT . '/' . $current_set['file'];
- }
- $task_message = '';
-
-
- $finished = 1;
- if ($item = $queue->claimItem()) {
- list($function, $args) = $item->data;
-
- $batch_context = array(
- 'sandbox' => &$current_set['sandbox'],
- 'results' => &$current_set['results'],
- 'finished' => &$finished,
- 'message' => &$task_message,
- );
- call_user_func_array($function, array_merge($args, array(&$batch_context)));
- if ($finished >= 1) {
-
- $finished = 0;
-
- $queue->deleteItem($item);
- $current_set['count']--;
- $current_set['sandbox'] = array();
- }
- }
-
-
-
-
-
-
- $set_changed = FALSE;
- $old_set = $current_set;
- while (empty($current_set['count']) && ($current_set['success'] = TRUE) && _batch_next_set()) {
- $current_set = &_batch_current_set();
- $current_set['start'] = microtime(TRUE);
- $set_changed = TRUE;
- }
-
-
- $queue = _batch_queue($current_set);
-
- if ($batch['progressive'] && timer_read('batch_processing') > 1000) {
-
- $current_set['elapsed'] = round((microtime(TRUE) - $current_set['start']) * 1000, 2);
- break;
- }
- }
- if ($batch['progressive']) {
-
-
-
-
- if ($set_changed && isset($current_set['queue'])) {
-
- $remaining = $current_set['count'];
- $total = $current_set['total'];
- $progress_message = $current_set['init_message'];
- $task_message = '';
- }
- else {
-
- $remaining = $old_set['count'];
- $total = $old_set['total'];
- $progress_message = $old_set['progress_message'];
- }
-
-
- $current = $total - $remaining + $finished;
- $percentage = _batch_api_percentage($total, $current);
- $elapsed = isset($current_set['elapsed']) ? $current_set['elapsed'] : 0;
- $values = array(
- '@remaining' => $remaining,
- '@total' => $total,
- '@current' => floor($current),
- '@percentage' => $percentage,
- '@elapsed' => format_interval($elapsed / 1000),
-
- '@estimate' => ($current > 0) ? format_interval(($elapsed * ($total - $current) / $current) / 1000) : '-',
- );
- $message = strtr($progress_message, $values);
- if (!empty($message)) {
- $message .= '<br />';
- }
- if (!empty($task_message)) {
- $message .= $task_message;
- }
- return array($percentage, $message);
- }
- else {
-
- return _batch_finished();
- }
- }
- function _batch_api_percentage($total, $current) {
- if (!$total || $total == $current) {
-
-
- $percentage = "100";
- }
- else {
-
-
- $decimal_places = max(0, floor(log10($total / 2.0)) - 1);
- do {
-
- $percentage = sprintf('%01.' . $decimal_places . 'f', round($current / $total * 100, $decimal_places));
-
-
-
-
-
- $decimal_places++;
- } while ($percentage == '100');
- }
- return $percentage;
- }
- function &_batch_current_set() {
- $batch = &batch_get();
- return $batch['sets'][$batch['current_set']];
- }
- function _batch_next_set() {
- $batch = &batch_get();
- if (isset($batch['sets'][$batch['current_set'] + 1])) {
- $batch['current_set']++;
- $current_set = &_batch_current_set();
- if (isset($current_set['form_submit']) && ($function = $current_set['form_submit']) && function_exists($function)) {
-
-
- $function($batch['form_state']['complete form'], $batch['form_state']);
- }
- return TRUE;
- }
- }
- function _batch_finished() {
- $batch = &batch_get();
-
- foreach ($batch['sets'] as $batch_set) {
- if (isset($batch_set['finished'])) {
-
- if (isset($batch_set['file']) && is_file($batch_set['file'])) {
- include_once DRUPAL_ROOT . '/' . $batch_set['file'];
- }
- if (function_exists($batch_set['finished'])) {
- $queue = _batch_queue($batch_set);
- $operations = $queue->getAllItems();
- $batch_set['finished']($batch_set['success'], $batch_set['results'], $operations, format_interval($batch_set['elapsed'] / 1000));
- }
- }
- }
-
- if ($batch['progressive']) {
- db_delete('batch')
- ->condition('bid', $batch['id'])
- ->execute();
- foreach ($batch['sets'] as $batch_set) {
- if ($queue = _batch_queue($batch_set)) {
- $queue->deleteQueue();
- }
- }
- }
- $_batch = $batch;
- $batch = NULL;
-
- if (isset($_SESSION)) {
- unset($_SESSION['batches'][$batch['id']]);
- if (empty($_SESSION['batches'])) {
- unset($_SESSION['batches']);
- }
- }
-
- if ($_batch['progressive']) {
-
- if (isset($_batch['destination'])) {
- $_GET['destination'] = $_batch['destination'];
- }
-
- if (!isset($_batch['form_state']['redirect'])) {
- if (isset($_batch['redirect'])) {
- $_batch['form_state']['redirect'] = $_batch['redirect'];
- }
- else {
- $_batch['form_state']['redirect'] = $_batch['source_url'];
- }
- }
-
- drupal_redirect_form($_batch['form_state']);
-
-
-
- if (!empty($_batch['form_state']['rebuild'])) {
- $_SESSION['batch_form_state'] = $_batch['form_state'];
- }
- $function = $_batch['redirect_callback'];
- if (function_exists($function)) {
- $function($_batch['source_url'], array('query' => array('op' => 'finish', 'id' => $_batch['id'])));
- }
- }
- }
- function _batch_shutdown() {
- if ($batch = batch_get()) {
- db_update('batch')
- ->fields(array('batch' => serialize($batch)))
- ->condition('bid', $batch['id'])
- ->execute();
- }
- }
|