Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / redirect / redirect.generate.inc
1 <?php
2
3 /**
4  * @file
5  * Generate callbacks for the redirect module.
6  */
7
8 use Drupal\Component\Utility\Random;
9 use Drupal\devel_generate\DevelGenerateBase;
10 use Drupal\redirect\Entity\Redirect;
11
12 /**
13  * @file
14  * Devel generate integration for the redirect module.
15  */
16
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,
22     '#size' => 4,
23   );
24   $form['delete'] = array(
25     '#type' => 'checkbox',
26     '#title' => t('Delete all URL redirects before generating new URL redirects.'),
27     '#default_value' => FALSE,
28   );
29   $form['submit'] = array(
30     '#type' => 'submit',
31     '#value' => t('Generate'),
32   );
33
34   return $form;
35 }
36
37 function redirect_generate_form_submit(&$form, &$form_state) {
38   // Run the batch.
39   $batch = redirect_generate_redirects_batch_info($form_state['values']['count'], $form_state['values']['delete']);
40   batch_set($batch);
41 }
42
43 function redirect_generate_redirects_batch_info($count, $delete = FALSE) {
44   if ($delete) {
45     $operations[] = array('redirect_generate_batch_delete', array());
46   }
47
48   $operations[] = array('redirect_generate_batch_generate', array($count));
49
50   return array(
51     'operations' => $operations,
52     'finished' => 'redirect_generate_batch_finished',
53     'file' => drupal_get_path('module', 'redirect') . '/redirect.generate.inc',
54   );
55 }
56
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();
63   }
64
65   $limit = 20;
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) {
68     $redirect->delete();
69   }
70
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)));
75
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']);
80   }
81 }
82
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);
93   }
94
95   module_load_include('inc', 'devel_generate');
96
97   $limit = 20;
98   $types = array_keys(redirect_status_code_options());
99   $languages = \Drupal::moduleHandler()->moduleExists('locale') ? array_keys(\Drupal::languageManager()->getLanguages()) : array();
100
101   for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
102     $rand = mt_rand(0, 100);
103
104     $redirect = Redirect::create();
105
106     $source = _redirect_generate_url();
107     $source_options = array();
108     $redirect_options = array();
109
110     if ($context['sandbox']['nids'] && $rand >= 40) {
111       $redirect_target = 'node/' . array_rand($context['sandbox']['nids']);
112     }
113     else {
114       $redirect_target = _redirect_generate_url(TRUE);
115       if ($rand <= 20) {
116         $redirect_options['query'] = _redirect_generate_querystring();
117       }
118       if ($rand <= 5) {
119         $redirect_options['fragment'] = DevelGenerateBase::generateWord(mt_rand(4, 8));
120       }
121     }
122
123     if ($rand <= 20) {
124       $redirect->setStatusCode($types[array_rand($types)]);
125     }
126
127     if ($languages && $rand <= 20) {
128       $redirect->setLanguage($languages[array_rand($languages)]);
129     }
130
131     $query = array();
132     if ($rand <= 30) {
133       $query = _redirect_generate_querystring();
134     }
135
136     $redirect->setSource($source, $query);
137     $redirect->setRedirect($redirect_target);
138
139     $redirect->save();
140
141     if (mt_rand(0, 1)) {
142     $query = \Drupal::database();
143     $query->update('redirect')
144       ->fields(array(
145         'count' => mt_rand(1, 500),
146         'access' => mt_rand(REQUEST_TIME - 31536000, REQUEST_TIME),
147       ))
148       ->condition('rid', $redirect->id())
149       ->execute();
150     }
151
152     $context['results'][] = $redirect->id();
153   }
154
155   // Update our progress information.
156   $context['sandbox']['progress'] += $limit;
157   //$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
158
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']);
163   }
164 }
165
166 function redirect_generate_batch_finished($success, $results, $operations) {
167   if ($success) {
168     drupal_set_message(\Drupal::translation()->formatPlural(count($results), 'One URL redirect created.', '@count URL redirects created.'));
169   }
170   else {
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))));
175   }
176 }
177
178 function _redirect_generate_url($external = FALSE, $max_levels = 2) {
179   $url = array();
180   if ($external) {
181     $tlds = array('com', 'net', 'org');
182     $url[] = 'http://www.example.'. $tlds[array_rand($tlds)];
183   }
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));
187   }
188   return implode('/', $url);
189 }
190
191 function _redirect_generate_querystring() {
192   $query = array(DevelGenerateBase::generateWord(mt_rand(1, 3)) => DevelGenerateBase::generateWord(mt_rand(2, 4)));
193   return $query;
194 }