You are here

function redirect_generate_batch_generate in Redirect 8

Same name and namespace in other branches
  1. 7.2 redirect.generate.inc \redirect_generate_batch_generate()
  2. 7 redirect.generate.inc \redirect_generate_batch_generate()
1 string reference to 'redirect_generate_batch_generate'
redirect_generate_redirects_batch_info in ./redirect.generate.inc

File

./redirect.generate.inc, line 85
Generate callbacks for the redirect module.

Code

function redirect_generate_batch_generate($num, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = [];
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $num;
    $query = \Drupal::database()
      ->select('node', 'n');
    $query
      ->addField('n', 'nid');
    $query
      ->condition('n.status', NodeInterface::PUBLISHED);
    $query
      ->addTag('node_access');
    $context['sandbox']['nids'] = $query
      ->execute()
      ->fetchAllKeyed(0, 0);
  }
  module_load_include('inc', 'devel_generate');
  $limit = 20;
  $types = array_keys(redirect_status_code_options());
  $languages = \Drupal::moduleHandler()
    ->moduleExists('locale') ? array_keys(\Drupal::languageManager()
    ->getLanguages()) : [];
  for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
    $rand = mt_rand(0, 100);
    $redirect = Redirect::create();
    $source = _redirect_generate_url();
    $source_options = [];
    $redirect_options = [];
    if ($context['sandbox']['nids'] && $rand >= 40) {
      $redirect_target = 'node/' . array_rand($context['sandbox']['nids']);
    }
    else {
      $redirect_target = _redirect_generate_url(TRUE);
      if ($rand <= 20) {
        $redirect_options['query'] = _redirect_generate_querystring();
      }
      if ($rand <= 5) {
        $redirect_options['fragment'] = DevelGenerateBase::generateWord(mt_rand(4, 8));
      }
    }
    if ($rand <= 20) {
      $redirect
        ->setStatusCode($types[array_rand($types)]);
    }
    if ($languages && $rand <= 20) {
      $redirect
        ->setLanguage($languages[array_rand($languages)]);
    }
    $query = [];
    if ($rand <= 30) {
      $query = _redirect_generate_querystring();
    }
    $redirect
      ->setSource($source, $query);
    $redirect
      ->setRedirect($redirect_target);
    $redirect
      ->save();
    if (mt_rand(0, 1)) {
      $query = \Drupal::database();
      $query
        ->update('redirect')
        ->fields([
        'count' => mt_rand(1, 500),
        'access' => mt_rand(Drupal::time()
          ->getRequestTime() - 31536000, Drupal::time()
          ->getRequestTime()),
      ])
        ->condition('rid', $redirect
        ->id())
        ->execute();
    }
    $context['results'][] = $redirect
      ->id();
  }

  // Update our progress information.
  $context['sandbox']['progress'] += $limit;

  //$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));

  // Inform the batch engine that we are not finished,
  // and provide an estimation of the completion level we reached.
  if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
    $context['finished'] = $context['sandbox']['progress'] >= $context['sandbox']['max'];
  }
}