Advertisement
Guest User

Drupal batch example

a guest
Nov 7th, 2011
689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. ///// Файл МОДУЛЬ.module
  2. /**
  3.  * Обработчик формы
  4.  */
  5. function МОДУЛЬ_form_submit($form, &$form_state){
  6.  
  7.   $path = drupal_get_path('module', 'МОДУЛЬ');
  8.  
  9.   $batch = array(
  10.     'operations' => array(array('МОДУЛЬ_import_users', array())),
  11.     'finished' => 'fashion_upgrade_user_finished',
  12.     'title' => 'Импорт пользователей',
  13.     'init_message' => 'Инициализация импорта.',
  14.     'progress_message' => 'Импорт.',
  15.     'error_message' => 'Ошибка импорта пользователей',
  16.     'file' => $path.'/МОДУЛЬ_users.inc',
  17.   );
  18.  
  19.   batch_set($batch);
  20. }
  21.  
  22. //// Файл МОДУЛЬ_users.inc
  23. /**
  24.  * Batch operation
  25.  */
  26. function МОДУЛЬ_import_users(&$context){
  27.   if (!isset($context['sandbox']['progress'])) {
  28.     $context['sandbox']['progress'] = 0;
  29.     $context['sandbox']['current_id'] = 0;
  30.     db_set_active('other'); // Импорт из другой БД
  31.     $context['sandbox']['max'] = db_result(db_query('SELECT COUNT(DISTINCT id) FROM other_users'));
  32.     db_set_active();
  33.   }
  34.  
  35.   $limit = 5; // Количество записей, обрабатываемых за один раз
  36.  
  37.   db_set_active('other'); // Импорт из другой БД
  38.   $result = db_query_range(
  39.     "SELECT * FROM other_users WHERE other_users.id > %d ORDER BY other_users.id ASC",
  40.      $context['sandbox']['current_id'], 0, $limit);
  41.   db_set_active();
  42.  
  43.   while ($row = db_fetch_object($result)) {
  44.     if($row->id)
  45.       $user = МОДУЛЬ_user_process($row); // Внешняя функция обработки записи, возвращает объект юзера
  46.  
  47.     $context['results'][] = $user->uid . ' : ' . check_plain($user->name);
  48.  
  49.     // Update our progress information.
  50.     $context['sandbox']['progress']++;
  51.     $context['sandbox']['current_id'] = $row->id;
  52.     $context['message'] = check_plain($user->name);
  53.   }
  54.    
  55.   // Inform the batch engine that we are not finished,
  56.   // and provide an estimation of the completion level we reached.
  57.   if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
  58.     $context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
  59.   }
  60. }
  61.  
  62. /**
  63.  * Добавление нового пользователя
  64.  */
  65. function МОДУЛЬ_user_process($obj){
  66.   ...
  67.   return $user;
  68. }
  69.  
  70. /**
  71.  * Кнец пакетной обработки
  72.  */
  73. function МОДУЛЬ_users_finished($success, $results, $operations) {
  74.   if ($success) {
  75.     $message = count($results) . ' processed.';
  76.   }
  77.   else {
  78.     // An error occurred.
  79.     // $operations contains the operations that remained unprocessed.
  80.     $error_operation = reset($operations);
  81.     $message = 'An error occurred while processing ' . $error_operation[0] . ' with arguments :' . print_r($error_operation[0], TRUE);
  82.   }
  83.   drupal_set_message($message);
  84. }
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement