cms/drupal/scripts/generate-d7-content.sh
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 #!/usr/bin/env php
       
     2 <?php
       
     3 
       
     4 /**
       
     5  * @file
       
     6  * Generates content for a Drupal 7 database to test the upgrade process.
       
     7  *
       
     8  * Run this script at the root of an existing Drupal 6 installation.
       
     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 if (module_exists('taxonomy')) {
       
    61   $terms = array();
       
    62 
       
    63   // All possible combinations of these vocabulary properties.
       
    64   $hierarchy = array(0, 1, 2, 0, 1, 2, 0, 1, 2, 0, 1, 2);
       
    65   $multiple  = array(0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1);
       
    66   $required  = array(0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1);
       
    67 
       
    68   $voc_id = 0;
       
    69   $term_id = 0;
       
    70   for ($i = 0; $i < 24; $i++) {
       
    71     $vocabulary = new stdClass;
       
    72     ++$voc_id;
       
    73     $vocabulary->name = "vocabulary $voc_id (i=$i)";
       
    74     $vocabulary->machine_name = 'vocabulary_' . $voc_id . '_' . $i;
       
    75     $vocabulary->description = "description of ". $vocabulary->name;
       
    76     $vocabulary->multiple = $multiple[$i % 12];
       
    77     $vocabulary->required = $required[$i % 12];
       
    78     $vocabulary->relations = 1;
       
    79     $vocabulary->hierarchy = $hierarchy[$i % 12];
       
    80     $vocabulary->weight = $i;
       
    81     taxonomy_vocabulary_save($vocabulary);
       
    82     $field = array(
       
    83       'field_name' => 'taxonomy_'. $vocabulary->machine_name,
       
    84       'module' => 'taxonomy',
       
    85       'type' => 'taxonomy_term_reference',
       
    86       'cardinality' => $vocabulary->multiple || $vocabulary->tags ? FIELD_CARDINALITY_UNLIMITED : 1,
       
    87       'settings' => array(
       
    88         'required' => $vocabulary->required ? TRUE : FALSE,
       
    89         'allowed_values' => array(
       
    90           array(
       
    91             'vocabulary' => $vocabulary->machine_name,
       
    92             'parent' => 0,
       
    93           ),
       
    94         ),
       
    95       ),
       
    96     );
       
    97     field_create_field($field);
       
    98     $node_types = $i > 11 ? array('page') : array_keys(node_type_get_types());
       
    99     foreach ($node_types as $bundle) {
       
   100       $instance = array(
       
   101         'label' => $vocabulary->name,
       
   102         'field_name' => $field['field_name'],
       
   103         'bundle' => $bundle,
       
   104         'entity_type' => 'node',
       
   105         'settings' => array(),
       
   106         'description' => $vocabulary->help,
       
   107         'required' => $vocabulary->required,
       
   108         'widget' => array(),
       
   109         'display' => array(
       
   110           'default' => array(
       
   111             'type' => 'taxonomy_term_reference_link',
       
   112             'weight' => 10,
       
   113           ),
       
   114           'teaser' => array(
       
   115             'type' => 'taxonomy_term_reference_link',
       
   116             'weight' => 10,
       
   117           ),
       
   118         ),
       
   119       );
       
   120       if ($vocabulary->tags) {
       
   121         $instance['widget'] = array(
       
   122           'type' => 'taxonomy_autocomplete',
       
   123           'module' => 'taxonomy',
       
   124           'settings' => array(
       
   125             'size' => 60,
       
   126             'autocomplete_path' => 'taxonomy/autocomplete',
       
   127           ),
       
   128         );
       
   129       }
       
   130       else {
       
   131         $instance['widget'] = array(
       
   132           'type' => 'options_select',
       
   133           'settings' => array(),
       
   134         );
       
   135       }
       
   136       field_create_instance($instance);
       
   137     }
       
   138     $parents = array();
       
   139     // Vocabularies without hierarchy get one term; single parent vocabularies
       
   140     // get one parent and one child term. Multiple parent vocabularies get
       
   141     // three terms: t0, t1, t2 where t0 is a parent of both t1 and t2.
       
   142     for ($j = 0; $j < $vocabulary->hierarchy + 1; $j++) {
       
   143       $term = new stdClass;
       
   144       $term->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       $term->parent = $vocabulary->hierarchy == 2 && i == 1 ? array() : $parents;
       
   148       ++$term_id;
       
   149       $term->name = "term $term_id of vocabulary $voc_id (j=$j)";
       
   150       $term->description = 'description of ' . $term->name;
       
   151       $term->format = 'filtered_html';
       
   152       $term->weight = $i * 3 + $j;
       
   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 }
       
   160 
       
   161 $node_id = 0;
       
   162 $revision_id = 0;
       
   163 module_load_include('inc', 'node', 'node.pages');
       
   164 for ($i = 0; $i < 24; $i++) {
       
   165   $uid = intval($i / 8) + 3;
       
   166   $user = user_load($uid);
       
   167   $node = new stdClass();
       
   168   $node->uid = $uid;
       
   169   $node->type = $i < 12 ? 'page' : 'story';
       
   170   $node->sticky = 0;
       
   171   ++$node_id;
       
   172   ++$revision_id;
       
   173   $node->title = "node title $node_id rev $revision_id (i=$i)";
       
   174   $node->language = LANGUAGE_NONE;
       
   175   $body_text =  str_repeat("node body ($node->type) - $i", 100);
       
   176   $node->body[$node->language][0]['value'] = $body_text;
       
   177   $node->body[$node->language][0]['summary'] = text_summary($body_text);
       
   178   $node->body[$node->language][0]['format'] = 'filtered_html';
       
   179   $node->status = intval($i / 4) % 2;
       
   180   $node->revision = $i < 12;
       
   181   $node->promote = $i % 2;
       
   182   $node->created = $now + $i * 86400;
       
   183   $node->log = "added $i node";
       
   184   // Make every term association different a little. For nodes with revisions,
       
   185   // make the initial revision have a different set of terms than the
       
   186   // newest revision.
       
   187   $items = array();
       
   188   if (module_exists('taxonomy')) {
       
   189     if ($node->revision) {
       
   190       $node_terms = array($terms[$i], $terms[47-$i]);
       
   191     }
       
   192     else {
       
   193       $node_terms = $terms;
       
   194       unset($node_terms[$i], $node_terms[47 - $i]);
       
   195     }
       
   196     foreach ($node_terms as $tid) {
       
   197       $field_name = $term_vocabs[$tid];
       
   198       $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
       
   199     }
       
   200   }
       
   201   $node->path = array('alias' => "content/$node->created");
       
   202   node_save($node);
       
   203   if ($node->revision) {
       
   204     $user = user_load($uid + 3);
       
   205     ++$revision_id;
       
   206     $node->title .= " rev2 $revision_id";
       
   207     $body_text =  str_repeat("node revision body ($node->type) - $i", 100);
       
   208     $node->body[$node->language][0]['value'] = $body_text;
       
   209     $node->body[$node->language][0]['summary'] = text_summary($body_text);
       
   210     $node->body[$node->language][0]['format'] = 'filtered_html';
       
   211     $node->log = "added $i revision";
       
   212     $node_terms = $terms;
       
   213     unset($node_terms[$i], $node_terms[47 - $i]);
       
   214     foreach ($node_terms as $tid) {
       
   215       $field_name = $term_vocabs[$tid];
       
   216       $node->{$field_name}[LANGUAGE_NONE][] = array('tid' => $tid);
       
   217     }
       
   218     node_save($node);
       
   219   }
       
   220 }
       
   221 
       
   222 if (module_exists('poll')) {
       
   223   // Create poll content.
       
   224   for ($i = 0; $i < 12; $i++) {
       
   225     $uid = intval($i / 4) + 3;
       
   226     $user = user_load($uid);
       
   227     $node = new stdClass();
       
   228     $node->uid = $uid;
       
   229     $node->type = 'poll';
       
   230     $node->sticky = 0;
       
   231     $node->title = "poll title $i";
       
   232     $node->language = LANGUAGE_NONE;
       
   233     $node->status = intval($i / 2) % 2;
       
   234     $node->revision = 1;
       
   235     $node->promote = $i % 2;
       
   236     $node->created = REQUEST_TIME + $i * 43200;
       
   237     $node->runtime = 0;
       
   238     $node->active = 1;
       
   239     $node->log = "added $i poll";
       
   240     $node->path = array('alias' => "content/poll/$i");
       
   241 
       
   242     $nbchoices = ($i % 4) + 2;
       
   243     for ($c = 0; $c < $nbchoices; $c++) {
       
   244       $node->choice[] = array('chtext' => "Choice $c for poll $i", 'chvotes' => 0, 'weight' => 0);
       
   245     }
       
   246     node_save($node);
       
   247     $path = array(
       
   248       'alias' => "content/poll/$i/results",
       
   249       'source' => "node/$node->nid/results",
       
   250     );
       
   251     path_save($path);
       
   252 
       
   253     // Add some votes.
       
   254     $node = node_load($node->nid);
       
   255     $choices = array_keys($node->choice);
       
   256     $original_user = $GLOBALS['user'];
       
   257     for ($v = 0; $v < ($i % 4); $v++) {
       
   258       drupal_static_reset('ip_address');
       
   259       $_SERVER['REMOTE_ADDR'] = "127.0.$v.1";
       
   260       $GLOBALS['user'] = drupal_anonymous_user();// We should have already allowed anon to vote.
       
   261       $c = $v % $nbchoices;
       
   262       $form_state = array();
       
   263       $form_state['values']['choice'] = $choices[$c];
       
   264       $form_state['values']['op'] = t('Vote');
       
   265       drupal_form_submit('poll_view_voting', $form_state, $node);
       
   266     }
       
   267   }
       
   268 }
       
   269 
       
   270 // Test that upgrade works even on a bundle whose parent module was disabled.
       
   271 // This is simulated by creating an existing content type and changing the
       
   272 // bundle to another type through direct database update queries.
       
   273 $node_type = 'broken';
       
   274 $uid = 6;
       
   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 if (db_table_exists('field_data_body')) {
       
   300   db_update('field_data_body')
       
   301     ->fields(array(
       
   302       'bundle' => $node_type,
       
   303     ))
       
   304     ->condition('entity_id', $node->nid)
       
   305     ->condition('entity_type', 'node')
       
   306     ->execute();
       
   307   db_update('field_revision_body')
       
   308     ->fields(array(
       
   309       'bundle' => $node_type,
       
   310     ))
       
   311     ->condition('entity_id', $node->nid)
       
   312     ->condition('entity_type', 'node')
       
   313     ->execute();
       
   314 }
       
   315 db_update('field_config_instance')
       
   316   ->fields(array(
       
   317     'bundle' => $node_type,
       
   318   ))
       
   319   ->condition('bundle', 'article')
       
   320   ->execute();