5 * Generate callbacks for the redirect module.
8 use Drupal\Component\Utility\Random;
9 use Drupal\devel_generate\DevelGenerateBase;
10 use Drupal\redirect\Entity\Redirect;
14 * Devel generate integration for the redirect module.
17 function redirect_generate_form() {
18 $form['count'] = array(
19 '#type' => 'textfield',
20 '#title' => t('How many URL redirects would you like to generate?'),
21 '#default_value' => 50,
24 $form['delete'] = array(
25 '#type' => 'checkbox',
26 '#title' => t('Delete all URL redirects before generating new URL redirects.'),
27 '#default_value' => FALSE,
29 $form['submit'] = array(
31 '#value' => t('Generate'),
37 function redirect_generate_form_submit(&$form, &$form_state) {
39 $batch = redirect_generate_redirects_batch_info($form_state['values']['count'], $form_state['values']['delete']);
43 function redirect_generate_redirects_batch_info($count, $delete = FALSE) {
45 $operations[] = array('redirect_generate_batch_delete', array());
48 $operations[] = array('redirect_generate_batch_generate', array($count));
51 'operations' => $operations,
52 'finished' => 'redirect_generate_batch_finished',
53 'file' => drupal_get_path('module', 'redirect') . '/redirect.generate.inc',
57 function redirect_generate_batch_delete(array &$context) {
58 if (empty($context['sandbox'])) {
59 $context['sandbox'] = array();
60 $context['sandbox']['progress'] = 0;
61 $context['sandbox']['current_rid'] = 0;
62 $context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT rid) FROM {redirect}')->fetchField();
66 $rids = db_query_range("SELECT rid FROM {redirect} WHERE rid > :rid ORDER BY rid", 0, $limit, array(':rid' => $context['sandbox']['current_rid']))->fetchCol();
67 foreach (redirect_repository()->loadMultiple($rids) as $redirect) {
71 // Update our progress information.
72 $context['sandbox']['progress'] += count($rids);
73 $context['sandbox']['current_rid'] = end($rids);
74 $context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
76 // Inform the batch engine that we are not finished,
77 // and provide an estimation of the completion level we reached.
78 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
79 $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
83 function redirect_generate_batch_generate($num, array &$context) {
84 if (empty($context['sandbox'])) {
85 $context['sandbox'] = array();
86 $context['sandbox']['progress'] = 0;
87 $context['sandbox']['max'] = $num;
88 $query = \Drupal::database()->select('node', 'n');
89 $query->addField('n', 'nid');
90 $query->condition('n.status', NODE_PUBLISHED);
91 $query->addTag('node_access');
92 $context['sandbox']['nids'] = $query->execute()->fetchAllKeyed(0, 0);
95 module_load_include('inc', 'devel_generate');
98 $types = array_keys(redirect_status_code_options());
99 $languages = \Drupal::moduleHandler()->moduleExists('locale') ? array_keys(\Drupal::languageManager()->getLanguages()) : array();
101 for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
102 $rand = mt_rand(0, 100);
104 $redirect = Redirect::create();
106 $source = _redirect_generate_url();
107 $source_options = array();
108 $redirect_options = array();
110 if ($context['sandbox']['nids'] && $rand >= 40) {
111 $redirect_target = 'node/' . array_rand($context['sandbox']['nids']);
114 $redirect_target = _redirect_generate_url(TRUE);
116 $redirect_options['query'] = _redirect_generate_querystring();
119 $redirect_options['fragment'] = DevelGenerateBase::generateWord(mt_rand(4, 8));
124 $redirect->setStatusCode($types[array_rand($types)]);
127 if ($languages && $rand <= 20) {
128 $redirect->setLanguage($languages[array_rand($languages)]);
133 $query = _redirect_generate_querystring();
136 $redirect->setSource($source, $query);
137 $redirect->setRedirect($redirect_target);
142 $query = \Drupal::database();
143 $query->update('redirect')
145 'count' => mt_rand(1, 500),
146 'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
148 ->condition('rid', $redirect->id())
152 $context['results'][] = $redirect->id();
155 // Update our progress information.
156 $context['sandbox']['progress'] += $limit;
157 //$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
159 // Inform the batch engine that we are not finished,
160 // and provide an estimation of the completion level we reached.
161 if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
162 $context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
166 function redirect_generate_batch_finished($success, $results, $operations) {
168 drupal_set_message(\Drupal::translation()->formatPlural(count($results), 'One URL redirect created.', '@count URL redirects created.'));
171 // An error occurred.
172 // $operations contains the operations that remained unprocessed.
173 $error_operation = reset($operations);
174 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))));
178 function _redirect_generate_url($external = FALSE, $max_levels = 2) {
181 $tlds = array('com', 'net', 'org');
182 $url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
184 $max_levels = mt_rand($external ? 0 : 1, $max_levels);
185 for ($i = 1; $i <= $max_levels; $i++) {
186 $url[] = DevelGenerateBase::generateWord(mt_rand(6 / $i, 8));
188 return implode('/', $url);
191 function _redirect_generate_querystring() {
192 $query = array(DevelGenerateBase::generateWord(mt_rand(1, 3)) => DevelGenerateBase::generateWord(mt_rand(2, 4)));