You are here

function devel_create_nodes in Devel 5

1 call to devel_create_nodes()
devel_generate_content in ./devel_generate.inc

File

./devel_generate.inc, line 57

Code

function devel_create_nodes($records, $users, $title_length = 8, $types = array()) {
  if (is_array($types)) {

    // Insert new data:
    for ($i = 1; $i <= $records; $i++) {
      $node->uid = $users[array_rand($users)];
      $node->type = $types[array_rand($types)];

      // Get the next nid without incrementing it.
      switch ($GLOBALS['db_type']) {
        case 'mysql':
        case 'mysqli':
          $next_nid = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{node}_nid'")) + 1;
          break;
        case 'pgsql':
          $next_nid = db_result(db_query("SELECT currval('{node}_nid_seq')")) + 1;
          break;
      }
      $title = devel_create_greeking(rand(1, $title_length), TRUE);
      $node->title = $title;
      $node->body = "node #{$next_nid} ({$node->type}) - " . devel_create_content();
      $node->teaser = node_teaser($node->body);
      $node->filter = variable_get('filter_default_format', 1);
      $node->status = 1;
      $node->revision = rand(0, 1);
      $node->promote = rand(0, 1);
      $node->comment = 2;
      $node->created = time();
      $node->changed = time();

      // TODO: add ability to disable terms
      if (1 || $add_terms) {
        devel_generate_add_terms($node);
      }

      // A flag to let hook_nodeapi() implementations know that this is a generated node.
      $node->devel_generate = $records;

      // Save the node:
      node_save($node);

      // Setup a path:
      db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", "node/{$node->nid}", "node-{$node->nid}-{$node->type}");
      unset($node);
    }
  }
}