Security update to Drupal 8.4.6
[yaffs-website] / web / core / scripts / generate-d7-content.sh
1 #!/usr/bin/env php
2 <?php
3
4 /**
5  * Generate content for a Drupal 7 database to test the upgrade process.
6  *
7  * Run this script at the root of an existing Drupal 7 installation.
8  *
9  * Steps to use this generation script:
10  * - Install drupal 7.
11  * - Run this script from your Drupal ROOT directory.
12  * - Use the dump-database-d7.sh to generate the D7 file
13  *   modules/simpletest/tests/upgrade/database.filled.php
14  */
15
16 // Define settings.
17 $cmd = 'index.php';
18 define('DRUPAL_ROOT', getcwd());
19 $_SERVER['HTTP_HOST']       = 'default';
20 $_SERVER['PHP_SELF']        = '/index.php';
21 $_SERVER['REMOTE_ADDR']     = '127.0.0.1';
22 $_SERVER['SERVER_SOFTWARE'] = NULL;
23 $_SERVER['REQUEST_METHOD']  = 'GET';
24 $_SERVER['QUERY_STRING']    = '';
25 $_SERVER['PHP_SELF']        = $_SERVER['REQUEST_URI'] = '/';
26 $_SERVER['HTTP_USER_AGENT'] = 'console';
27 $modules_to_enable          = array('path', 'poll', 'taxonomy');
28
29 // Bootstrap Drupal.
30 include_once './includes/bootstrap.inc';
31 drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
32
33 // Enable requested modules
34 require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');
35 include_once './modules/system/system.admin.inc';
36 $form = system_modules();
37 foreach ($modules_to_enable as $module) {
38   $form_state['values']['status'][$module] = TRUE;
39 }
40 $form_state['values']['disabled_modules'] = $form['disabled_modules'];
41 system_modules_submit(NULL, $form_state);
42 unset($form_state);
43
44 // Run cron after installing
45 drupal_cron_run();
46
47 // Create six users
48 $query = db_insert('users')->fields(array('uid', 'name', 'pass', 'mail', 'status', 'created', 'access'));
49 for ($i = 0; $i < 6; $i++) {
50   $name = "test user $i";
51   $pass = md5("test PassW0rd $i !(.)");
52   $mail = "test$i@example.com";
53   $now = mktime(0, 0, 0, 1, $i + 1, 2010);
54   $query->values(array(db_next_id(), $name, user_hash_password($pass), $mail, 1, $now, $now));
55 }
56 $query->execute();
57
58 // Create vocabularies and terms
59
60 $terms = array();
61
62 // All possible combinations of these vocabulary properties.
63 $hierarchy = array(0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2);
64 $multiple  = array(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1);
65 $required  = array(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1);
66
67 $voc_id = 0;
68 $term_id = 0;
69 for ($i = 0; $i < 24; $i++) {
70   $vocabulary = new stdClass;
71   ++$voc_id;
72   $vocabulary->name = "vocabulary $voc_id (i=$i)";
73   $vocabulary->machine_name = 'vocabulary_' . $voc_id . '_' . $i;
74   $vocabulary->description = "description of ". $vocabulary->name;
75   $vocabulary->multiple = $multiple[$i % 12];
76   $vocabulary->required = $required[$i % 12];
77   $vocabulary->relations = 1;
78   $vocabulary->hierarchy = $hierarchy[$i % 12];
79   $vocabulary->weight = $i;
80   taxonomy_vocabulary_save($vocabulary);
81   $field = array(
82     'field_name' => 'taxonomy_'. $vocabulary->machine_name,
83     'module' => 'taxonomy',
84     'type' => 'taxonomy_term_reference',
85     'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
86     'settings' => array(
87       'required' => $vocabulary->required ? TRUE : FALSE,
88       'allowed_values' => array(
89         array(
90           'vocabulary' => $vocabulary->machine_name,
91           'parent' => 0,
92         ),
93       ),
94     ),
95   );
96   field_create_field($field);
97   $node_types = $i > 11 ? array('page') : array_keys(node_type_get_types());
98   foreach ($node_types as $bundle) {
99     $instance = array(
100       'label' => $vocabulary->name,
101       'field_name' => $field['field_name'],
102       'bundle' => $bundle,
103       'entity_type' => 'node',
104       'settings' => array(),
105       'description' => $vocabulary->help,
106       'required' => $vocabulary->required,
107       'widget' => array(),
108       'display' => array(
109         'default' => array(
110           'type' => 'taxonomy_term_reference_link',
111           'weight' => 10,
112         ),
113         'teaser' => array(
114           'type' => 'taxonomy_term_reference_link',
115           'weight' => 10,
116         ),
117       ),
118     );
119     if ($vocabulary->tags) {
120       $instance['widget'] = array(
121         'type' => 'taxonomy_autocomplete',
122         'module' => 'taxonomy',
123         'settings' => array(
124           'size' => 60,
125           'autocomplete_path' => 'taxonomy/autocomplete',
126         ),
127       );
128     }
129     else {
130       $instance['widget'] = array(
131         'type' => 'options_select',
132         'settings' => array(),
133       );
134     }
135     field_create_instance($instance);
136   }
137   $parents = array();
138   // Vocabularies without hierarchy get one term, single parent vocabularies get
139   // one parent and one child term. Multiple parent vocabularies get three
140   // terms: t0, t1, t2 where t0 is a parent of both t1 and t2.
141   for ($j = 0; $j < $vocabulary->hierarchy + 1; $j++) {
142     ++$term_id;
143     $term = entity_create('taxonomy_term', array(
144       'vocabulary_machine_name' => $vocabulary->machine_name,
145       // For multiple parent vocabularies, omit the t0-t1 relation, otherwise
146       // every parent in the vocabulary is a parent.
147       'parent' => $vocabulary->hierarchy == 2 && i == 1 ? array() : $parents,
148       'name' => "term $term_id of vocabulary $voc_id (j=$j)",
149       'description' => 'description of ' . $term->name,
150       'format' => 'filtered_html',
151       'weight' => $i * 3 + $j,
152     ));
153     taxonomy_term_save($term);
154     $terms[] = $term->tid;
155     $term_vocabs[$term->tid] = 'taxonomy_' . $vocabulary->machine_name;
156     $parents[] = $term->tid;
157   }
158 }
159 $node_id = 0;
160 $revision_id = 0;
161 module_load_include('inc', 'node', 'node.pages');
162 for ($i = 0; $i < 36; $i++) {
163   $uid = intval($i / 8) + 3;
164   $user = user_load($uid);
165   $node = new stdClass();
166   $node->uid = $uid;
167   $node->type = 'page';
168   if ($i < 12) {
169     $node->type = 'page';
170   }
171   elseif ($i < 24) {
172     $node->type = 'story';
173   }
174   elseif (module_exists('blog')) {
175     $node->type = 'blog';
176   }
177   $node->sticky = 0;
178   ++$node_id;
179   ++$revision_id;
180   $node->title = "node title $node_id rev $revision_id (i=$i)";
181   $node->language = LANGUAGE_NONE;
182   $body_text =  str_repeat("node body ($node->type) - $i", 100);
183   $node->body[$node->language][0]['value'] = $body_text;
184   $node->body[$node->language][0]['summary'] = text_summary($body_text);
185   $node->body[$node->language][0]['format'] = 'filtered_html';
186   $node->status = intval($i / 4) % 2;
187   $node->revision = $i < 12;
188   $node->promote = $i % 2;
189   $node->created = $now + $i * 86400;
190   $node->log = "added $i node";
191   // Make every term association different a little. For nodes with revisions,
192   // make the initial revision have a different set of terms than the
193   // newest revision.
194   $items = array();
195   if ($node->revision) {
196     $node_terms = array($terms[$i], $terms[47-$i]);
197   }
198   else {
199     $node_terms = $terms;
200     unset($node_terms[$i], $node_terms[47 - $i]);
201   }
202   foreach ($node_terms as $tid) {
203     $field_name = $term_vocabs[$tid];
204     $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
205   }
206   $node->path = array('alias' => "content/$node->created");
207   node_save($node);
208   if ($node->revision) {
209     $user = user_load($uid + 3);
210     ++$revision_id;
211     $node->title .= " rev2 $revision_id";
212     $body_text =  str_repeat("node revision body ($node->type) - $i", 100);
213     $node->body[$node->language][0]['value'] = $body_text;
214     $node->body[$node->language][0]['summary'] = text_summary($body_text);
215     $node->body[$node->language][0]['format'] = 'filtered_html';
216     $node->log = "added $i revision";
217     $node_terms = $terms;
218     unset($node_terms[$i], $node_terms[47 - $i]);
219     foreach ($node_terms as $tid) {
220       $field_name = $term_vocabs[$tid];
221       $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
222     }
223     node_save($node);
224   }
225 }
226
227 // Create poll content
228 for ($i = 0; $i < 12; $i++) {
229   $uid = intval($i / 4) + 3;
230   $user = user_load($uid);
231   $node = new stdClass();
232   $node->uid = $uid;
233   $node->type = 'poll';
234   $node->sticky = 0;
235   $node->title = "poll title $i";
236   $node->language = LANGUAGE_NONE;
237   $node->status = intval($i / 2) % 2;
238   $node->revision = 1;
239   $node->promote = $i % 2;
240   $node->created = REQUEST_TIME + $i * 43200;
241   $node->runtime = 0;
242   $node->active = 1;
243   $node->log = "added $i poll";
244   $node->path = array('alias' => "content/poll/$i");
245
246   $nbchoices = ($i % 4) + 2;
247   for ($c = 0; $c < $nbchoices; $c++) {
248     $node->choice[] = array('chtext' => "Choice $c for poll $i", 'chvotes' => 0, 'weight' => 0);
249   }
250   node_save($node);
251   $path = array(
252     'alias' => "content/poll/$i/results",
253     'source' => "node/$node->nid/results",
254   );
255   path_save($path);
256
257   // Add some votes
258   $node = node_load($node->nid);
259   $choices = array_keys($node->choice);
260   $original_user = $GLOBALS['user'];
261   for ($v = 0; $v < ($i % 4); $v++) {
262     drupal_static_reset('ip_address');
263     $_SERVER['REMOTE_ADDR'] = "127.0.$v.1";
264     $GLOBALS['user'] = drupal_anonymous_user();// We should have already allowed anon to vote.
265     $c = $v % $nbchoices;
266     $form_state = array();
267     $form_state['values']['choice'] = $choices[$c];
268     $form_state['values']['op'] = t('Vote');
269     drupal_form_submit('poll_view_voting', $form_state, $node);
270   }
271 }
272
273 $uid = 6;
274 $node_type = 'broken';
275 $user = user_load($uid);
276 $node = new stdClass();
277 $node->uid = $uid;
278 $node->type = 'article';
279 $body_text = str_repeat("node body ($node_type) - 37", 100);
280 $node->sticky = 0;
281 $node->title = "node title 24";
282 $node->language = LANGUAGE_NONE;
283 $node->body[$node->language][0]['value'] = $body_text;
284 $node->body[$node->language][0]['summary'] = text_summary($body_text);
285 $node->body[$node->language][0]['format']  = 'filtered_html';
286 $node->status = 1;
287 $node->revision = 0;
288 $node->promote = 0;
289 $node->created = 1263769200;
290 $node->log = "added a broken node";
291 $node->path = array('alias' => "content/1263769200");
292 node_save($node);
293 db_update('node')
294   ->fields(array(
295     'type' => $node_type,
296   ))
297   ->condition('nid', $node->nid)
298   ->execute();
299 db_update('field_data_body')
300   ->fields(array(
301     'bundle' => $node_type,
302   ))
303   ->condition('entity_id', $node->nid)
304   ->condition('entity_type', 'node')
305   ->execute();
306 db_update('field_revision_body')
307   ->fields(array(
308     'bundle' => $node_type,
309   ))
310   ->condition('entity_id', $node->nid)
311   ->condition('entity_type', 'node')
312   ->execute();
313 db_update('field_config_instance')
314   ->fields(array(
315     'bundle' => $node_type,
316   ))
317   ->condition('bundle', 'article')
318   ->execute();