You are here

function redirect_generate_batch_generate in Redirect 7

Same name and namespace in other branches
  1. 8 redirect.generate.inc \redirect_generate_batch_generate()
  2. 7.2 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 72
Devel generate integration for the redirect module.

Code

function redirect_generate_batch_generate($num, array &$context) {
  if (empty($context['sandbox'])) {
    $context['sandbox'] = array();
    $context['sandbox']['progress'] = 0;
    $context['sandbox']['max'] = $num;
    $query = db_select('node', 'n');
    $query
      ->addField('n', 'nid');
    $query
      ->condition('n.status', NODE_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 = module_exists('locale') ? array_keys(locale_language_list('name')) : array();
  for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
    $rand = mt_rand(0, 100);
    $redirect = new stdClass();
    redirect_object_prepare($redirect);
    $redirect->source = _redirect_generate_url();
    $redirect->devel_generate = TRUE;
    if ($context['sandbox']['nids'] && $rand >= 40) {
      $redirect->redirect = 'node/' . array_rand($context['sandbox']['nids']);
    }
    else {
      $redirect->redirect = _redirect_generate_url(TRUE);
      if ($rand <= 20) {
        $redirect->redirect_options['query'] = _redirect_generate_querystring();
      }
      if ($rand <= 5) {
        $redirect->redirect_options['fragment'] = devel_generate_word(mt_rand(4, 8));
      }
    }
    if ($rand <= 20) {
      $redirect->status_code = $types[array_rand($types)];
    }
    if ($languages && $rand <= 20) {
      $redirect->language = $languages[array_rand($languages)];
    }
    if ($rand <= 30) {
      $redirect->source_options['query'] = _redirect_generate_querystring();
    }
    redirect_save($redirect);
    if (mt_rand(0, 1)) {
      db_update('redirect')
        ->fields(array(
        'count' => mt_rand(1, 500),
        'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
      ))
        ->condition('rid', $redirect->rid)
        ->execute();
    }
    $context['results'][] = $redirect->rid;
  }

  // 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'];
  }
}