You are here

path_redirect_generate.module in Path redirect 6

File

generate/path_redirect_generate.module
View source
<?php

/**
 * Implement hook_menu().
 */
function path_redirect_generate_menu() {
  $items['admin/generate/path-redirect'] = array(
    'title' => 'Generate redirects',
    'description' => 'Generate URL redirects.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'path_redirect_generate_form',
    ),
    'access arguments' => array(
      'administer redirects',
    ),
  );
  $items['admin/build/path-redirect/generate'] = $items['admin/generate/path-redirect'];
  $items['admin/build/path-redirect/generate']['title'] = 'Generate';
  $items['admin/build/path-redirect/generate']['type'] = MENU_LOCAL_TASK;
  return $items;
}
function path_redirect_generate_form() {
  $form['num'] = array(
    '#type' => 'textfield',
    '#title' => t('How many URL redirects would you like to generate?'),
    '#default_value' => 50,
    '#size' => 4,
  );
  $form['kill'] = array(
    '#type' => 'checkbox',
    '#title' => t('Delete all URL redirects before generating new URL redirects.'),
    '#default_value' => FALSE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Generate'),
  );
  return $form;
}
function path_redirect_generate_form_submit(&$form, &$form_state) {
  path_redirect_generate_redirects($form_state['values']['num'], $form_state['values']['kill']);
  drupal_set_message(format_plural($form_state['values']['num'], '1 URL redirect created.', '@count URL redirect created.'));
  $form_state['redirect'] = 'admin/build/path-redirect';
}
function path_redirect_generate_redirects($num, $kill) {
  module_load_include('inc', 'devel', 'devel_generate');
  module_load_include('inc', 'path_redirect', 'path_redirect.admin');

  // If not in 'safe mode', increase the maximum execution time.
  if (!ini_get('safe_mode')) {
    set_time_limit(240);
  }
  if ($kill) {
    db_query("DELETE FROM {path_redirect}");
  }
  $time = time();
  $nodes = array();
  $query = db_query(db_rewrite_sql("SELECT n.nid FROM {node} n WHERE n.status = 1"));
  while ($nid = db_result($query)) {
    $nids[] = $nid;
  }
  $types = array_keys(path_redirect_status_code_options());
  $languages = module_exists('locale') ? array_keys(locale_language_list('name')) : array();
  for ($i = 0; $i < $num; $i++) {
    $rand = mt_rand(0, 100);
    $redirect = array(
      'source' => _path_redirect_generate_url(),
      'last_used' => mt_rand($time - 31536000, $time),
    );
    if ($nids && $rand <= 75) {
      $redirect['redirect'] = 'node/' . $nids[array_rand($nids)];
    }
    else {
      $redirect['redirect'] = _path_redirect_generate_url(TRUE);
    }
    if ($rand <= 20) {
      $redirect['type'] = $types[array_rand($types)];
    }
    if ($languages && $rand <= 20) {
      $redirect['language'] = $languages[array_rand($languages)];
    }
    if ($rand <= 30) {
      $redirect['query'] = _path_redirect_generate_querystring();
    }
    if ($rand <= 30) {
      $redirect['source'] .= '?' . _path_redirect_generate_querystring();
    }
    if ($rand <= 20) {
      $redirect['fragment'] = devel_generate_word(mt_rand(4, 8));
    }
    path_redirect_save($redirect);
  }
}
function _path_redirect_generate_url($external = FALSE, $max_levels = 2) {
  $url = array();
  if ($external) {
    $tlds = array(
      'com',
      'net',
      'org',
    );
    $url[] = 'http://www.example.' . $tlds[array_rand($tlds)];
  }
  $max_levels = mt_rand($external ? 0 : 1, $max_levels);
  for ($i = 1; $i <= $max_levels; $i++) {
    $url[] = devel_generate_word(mt_rand(6 / $i, 8));
  }
  return implode('/', $url);
}
function _path_redirect_generate_querystring() {
  $query = array(
    devel_generate_word(mt_rand(1, 3)) => devel_generate_word(mt_rand(2, 4)),
  );
  return drupal_query_string_encode($query);
}