redirect.generate.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * @file
  4. * Devel generate integration for the redirect module.
  5. */
  6. function redirect_generate_form() {
  7. $form['count'] = array(
  8. '#type' => 'textfield',
  9. '#title' => t('How many URL redirects would you like to generate?'),
  10. '#default_value' => 50,
  11. '#size' => 4,
  12. );
  13. $form['delete'] = array(
  14. '#type' => 'checkbox',
  15. '#title' => t('Delete all URL redirects before generating new URL redirects.'),
  16. '#default_value' => FALSE,
  17. );
  18. $form['submit'] = array(
  19. '#type' => 'submit',
  20. '#value' => t('Generate'),
  21. );
  22. return $form;
  23. }
  24. function redirect_generate_form_submit(&$form, &$form_state) {
  25. // Run the batch.
  26. $batch = redirect_generate_redirects_batch_info($form_state['values']['count'], $form_state['values']['delete']);
  27. batch_set($batch);
  28. }
  29. function redirect_generate_redirects_batch_info($count, $delete = FALSE) {
  30. if ($delete) {
  31. $operations[] = array('redirect_generate_batch_delete', array());
  32. }
  33. $operations[] = array('redirect_generate_batch_generate', array($count));
  34. return array(
  35. 'operations' => $operations,
  36. 'finished' => 'redirect_generate_batch_finished',
  37. 'file' => drupal_get_path('module', 'redirect') . '/redirect.generate.inc',
  38. );
  39. }
  40. function redirect_generate_batch_delete(array &$context) {
  41. if (empty($context['sandbox'])) {
  42. $context['sandbox'] = array();
  43. $context['sandbox']['progress'] = 0;
  44. $context['sandbox']['current_rid'] = 0;
  45. $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT rid) FROM {redirect}')->fetchField();
  46. }
  47. $limit = 20;
  48. $rids = db_query_range("SELECT rid FROM {redirect} WHERE rid > :rid ORDER BY rid", 0, $limit, array(':rid' => $context['sandbox']['current_rid']))->fetchCol();
  49. redirect_delete_multiple($rids);
  50. // Update our progress information.
  51. $context['sandbox']['progress'] += count($rids);
  52. $context['sandbox']['current_rid'] = end($rids);
  53. $context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
  54. // Inform the batch engine that we are not finished,
  55. // and provide an estimation of the completion level we reached.
  56. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  57. $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
  58. }
  59. }
  60. function redirect_generate_batch_generate($num, array &$context) {
  61. if (empty($context['sandbox'])) {
  62. $context['sandbox'] = array();
  63. $context['sandbox']['progress'] = 0;
  64. $context['sandbox']['max'] = $num;
  65. $query = db_select('node', 'n');
  66. $query->addField('n', 'nid');
  67. $query->condition('n.status', NODE_PUBLISHED);
  68. $query->addTag('node_access');
  69. $context['sandbox']['nids'] = $query->execute()->fetchAllKeyed(0, 0);
  70. }
  71. module_load_include('inc', 'devel_generate');
  72. $limit = 20;
  73. $types = array_keys(redirect_status_code_options());
  74. $languages = module_exists('locale') ? array_keys(locale_language_list('name')) : array();
  75. for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
  76. $rand = mt_rand(0, 100);
  77. $redirect = new stdClass();
  78. redirect_object_prepare($redirect);
  79. $redirect->source = _redirect_generate_url();
  80. $redirect->devel_generate = TRUE;
  81. if ($context['sandbox']['nids'] && $rand >= 40) {
  82. $redirect->redirect = 'node/'. array_rand($context['sandbox']['nids']);
  83. }
  84. else {
  85. $redirect->redirect = _redirect_generate_url(TRUE);
  86. if ($rand <= 20) {
  87. $redirect->redirect_options['query'] = _redirect_generate_querystring();
  88. }
  89. if ($rand <= 5) {
  90. $redirect->redirect_options['fragment'] = devel_generate_word(mt_rand(4, 8));
  91. }
  92. }
  93. if ($rand <= 20) {
  94. $redirect->status_code = $types[array_rand($types)];
  95. }
  96. if ($languages && $rand <= 20) {
  97. $redirect->language = $languages[array_rand($languages)];
  98. }
  99. if ($rand <= 30) {
  100. $redirect->source_options['query'] = _redirect_generate_querystring();
  101. }
  102. redirect_save($redirect);
  103. if (mt_rand(0, 1)) {
  104. db_update('redirect')
  105. ->fields(array(
  106. 'count' => mt_rand(1, 500),
  107. 'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
  108. ))
  109. ->condition('rid', $redirect->rid)
  110. ->execute();
  111. }
  112. $context['results'][] = $redirect->rid;
  113. }
  114. // Update our progress information.
  115. $context['sandbox']['progress'] += $limit;
  116. //$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
  117. // Inform the batch engine that we are not finished,
  118. // and provide an estimation of the completion level we reached.
  119. if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
  120. $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
  121. }
  122. }
  123. function redirect_generate_batch_finished($success, $results, $operations) {
  124. if ($success) {
  125. drupal_set_message(format_plural(count($results), 'One URL redirect created.', '@count URL redirects created.'));
  126. }
  127. else {
  128. // An error occurred.
  129. // $operations contains the operations that remained unprocessed.
  130. $error_operation = reset($operations);
  131. drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
  132. }
  133. }
  134. function _redirect_generate_url($external = FALSE, $max_levels = 2) {
  135. module_load_include('inc', 'devel_generate');
  136. $url = array();
  137. if ($external) {
  138. $tlds = array('com', 'net', 'org');
  139. $url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
  140. }
  141. $max_levels = mt_rand($external ? 0 : 1, $max_levels);
  142. for ($i = 1; $i <= $max_levels; $i++) {
  143. $url[] = devel_generate_word(mt_rand(6 / $i, 8));
  144. }
  145. return implode('/', $url);
  146. }
  147. function _redirect_generate_querystring() {
  148. module_load_include('inc', 'devel_generate');
  149. $query = array(devel_generate_word(mt_rand(1, 3)) => devel_generate_word(mt_rand(2, 4)));
  150. return $query;
  151. }