path_redirect_generate.module

Tracking master branch
  1. drupal
    1. 6 contributions/path_redirect/generate/path_redirect_generate.module
    2. 7 contributions/path_redirect/generate/path_redirect_generate.module

Functions & methods

NameDescription
path_redirect_generate_form
path_redirect_generate_form_submit
path_redirect_generate_menuImplement hook_menu().
path_redirect_generate_redirects
_path_redirect_generate_querystring
_path_redirect_generate_url

File

View source
  1. <?php
  2. /**
  3. * Implement hook_menu().
  4. */
  5. function path_redirect_generate_menu() {
  6. $items['admin/config/development/generate/path-redirect'] = array(
  7. 'title' => 'Generate redirects',
  8. 'description' => 'Generate URL redirects.',
  9. 'page callback' => 'drupal_get_form',
  10. 'page arguments' => array('path_redirect_generate_form'),
  11. 'access arguments' => array('administer redirects'),
  12. );
  13. $items['admin/config/search/path-redirect/generate'] = $items['admin/config/development/generate/path-redirect'];
  14. $items['admin/config/search/path-redirect/generate']['title'] = 'Generate';
  15. $items['admin/config/search/path-redirect/generate']['type'] = MENU_LOCAL_TASK;
  16. return $items;
  17. }
  18. function path_redirect_generate_form() {
  19. $form['num'] = array(
  20. '#type' => 'textfield',
  21. '#title' => t('How many URL redirects would you like to generate?'),
  22. '#default_value' => 50,
  23. '#size' => 4,
  24. );
  25. $form['kill'] = array(
  26. '#type' => 'checkbox',
  27. '#title' => t('Delete all URL redirects before generating new URL redirects.'),
  28. '#default_value' => FALSE,
  29. );
  30. $form['submit'] = array(
  31. '#type' => 'submit',
  32. '#value' => t('Generate'),
  33. );
  34. return $form;
  35. }
  36. function path_redirect_generate_form_submit(&$form, &$form_state) {
  37. path_redirect_generate_redirects($form_state['values']['num'], $form_state['values']['kill']);
  38. drupal_set_message(format_plural($form_state['values']['num'], '1 URL redirect created.', '@count URL redirect created.'));
  39. $form_state['redirect'] = 'admin/config/search/path-redirect';
  40. }
  41. function path_redirect_generate_redirects($num, $kill) {
  42. module_load_include('inc', 'devel', 'devel_generate');
  43. module_load_include('inc', 'path_redirect', 'path_redirect.admin');
  44. // Try to allocate enough time to run all the generations.
  45. drupal_set_time_limit(240);
  46. if ($kill) {
  47. db_truncate('path_redirect')->execute();
  48. }
  49. $query = db_select('node', 'n');
  50. $query->addField('n', 'nid');
  51. $query->addTag('nodeaccess');
  52. $query->condition('n.status', NODE_PUBLISHED);
  53. $nids = $query->execute()->fetchCol();
  54. $types = array_keys(path_redirect_status_code_options());
  55. $languages = module_exists('locale') ? array_keys(locale_language_list('name')) : array();
  56. for ($i = 0; $i < $num; $i++) {
  57. $rand = mt_rand(0, 100);
  58. $redirect = array(
  59. 'source' => _path_redirect_generate_url(),
  60. 'last_used' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
  61. );
  62. if ($nids && $rand <= 75) {
  63. $redirect['redirect'] = 'node/'. $nids[array_rand($nids)];
  64. }
  65. else {
  66. $redirect['redirect'] = _path_redirect_generate_url(TRUE);
  67. }
  68. if ($rand <= 20) {
  69. $redirect['type'] = $types[array_rand($types)];
  70. }
  71. if ($languages && $rand <= 20) {
  72. $redirect['language'] = $languages[array_rand($languages)];
  73. }
  74. if ($rand <= 30) {
  75. $redirect['query'] = _path_redirect_generate_querystring();
  76. }
  77. if ($rand <= 30) {
  78. $redirect['source'] .= '?'. _path_redirect_generate_querystring();
  79. }
  80. if ($rand <= 20) {
  81. $redirect['fragment'] = devel_generate_word(mt_rand(4, 8));
  82. }
  83. path_redirect_save($redirect);
  84. }
  85. }
  86. function _path_redirect_generate_url($external = FALSE, $max_levels = 2) {
  87. $url = array();
  88. if ($external) {
  89. $tlds = array('com', 'net', 'org');
  90. $url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
  91. }
  92. $max_levels = mt_rand($external ? 0 : 1, $max_levels);
  93. for ($i = 1; $i <= $max_levels; $i++) {
  94. $url[] = devel_generate_word(mt_rand(6 / $i, 8));
  95. }
  96. return implode('/', $url);
  97. }
  98. function _path_redirect_generate_querystring() {
  99. $query = array(devel_generate_word(mt_rand(1, 3)) => devel_generate_word(mt_rand(2, 4)));
  100. return drupal_http_build_query($query);
  101. }