redirect.generate.inc 6.2 KB

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