annotate core/scripts/generate-d7-content.sh @ 19:fa3358dc1485 tip

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