Commit b1c2b5f0 authored by moshe weitzman's avatar moshe weitzman

#159533 by hunmonk. refactor devel_generate and port to d6

parent 1234041c
......@@ -30,6 +30,55 @@ function devel_create_users($num, $kill) {
drupal_set_message(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users'))));
}
function devel_generate_content($num_nodes, $num_comments, $title_length, $kill) {
if ($kill) {
db_query("DELETE FROM {comments}");
db_query("DELETE FROM {node}");
db_query("DELETE FROM {node_revisions}");
db_query("DELETE FROM {node_comment_statistics}");
if (db_table_exists('term_node')) {
db_query("DELETE FROM {term_node}");
}
if (db_table_exists('forum')) {
db_query("DELETE FROM {forum}");
}
db_query("DELETE FROM {url_alias}");
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
db_query("ALTER TABLE {node} AUTO_INCREMENT = 1");
db_query("ALTER TABLE {comments} AUTO_INCREMENT = 1");
break;
case 'pgsql':
db_query("SELECT setval('{node}_nid_seq', 1, false)");
db_query("SELECT setval('{comments}_cid_seq', 1, false)");
break;
}
drupal_set_message(t('Content deleted.'));
}
// Get user id.
$users = devel_get_users();
$terms = devel_get_terms();
// Create $num_nodes pseudo-random nodes.
$nids = devel_create_nodes($num_nodes, $users, $title_length);
if (!empty($nids)) {
drupal_set_message(t('%num nodes created.', array('%num' => count($nids))));
$nodes = devel_get_nodes($nids[0]);
devel_add_terms($nodes, $terms);
$comments = devel_get_comments();
$cids = devel_create_comments($num_comments, $users, $nodes, $comments);
if (!empty($cids)) {
drupal_set_message(t('%num comments created.', array('%num' => $num_comments)));
}
}
}
function devel_create_nodes($records, $users, $title_length = 8) {
......@@ -42,14 +91,13 @@ function devel_create_nodes($records, $users, $title_length = 8) {
}
}
$users = array_merge($users, array('0'));
$nids = 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
$next_nid = db_result(db_query("SELECT MAX(nid) FROM {node} "))+1;
$title = devel_create_greeking(rand(1, $title_length), TRUE);
/*
......@@ -60,7 +108,7 @@ function devel_create_nodes($records, $users, $title_length = 8) {
}
*/
$node->title = $title;
$node->body = "node #$next_nid ($node->type) - ". devel_create_content();
$node->body = "node ($node->type) - ". devel_create_content();
$node->teaser = node_teaser($node->body);
$node->filter = variable_get('filter_default_format', 1);
$node->language = '';
......@@ -77,15 +125,22 @@ function devel_create_nodes($records, $users, $title_length = 8) {
// Save the node:
node_save($node);
// Store the nid.
$nids[] = $node->nid;
// Setup a path:
db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", "node/$node->nid", "node-$node->nid-$node->type");
unset($node);
}
}
return $nids;
}
function devel_create_comments($records, $users, $nodes, $comments) {
$users = array_merge($users, array('0'));
$cids = array();
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$comment->nid = array_rand($nodes);
......@@ -106,7 +161,11 @@ function devel_create_comments($records, $users, $nodes, $comments) {
$comment->uid = $users[array_rand($users)];
db_query("INSERT INTO {comments} (nid, pid, uid, subject, comment, status, thread, timestamp) VALUES (%d, %d, %d, '%s', '%s', %d, %d, %d)", $comment->nid, $comment->pid, $comment->uid, $comment->subject, $comment->comment, 0, 0, time());
$cids[] = db_last_insert_id('comments', 'cid');
}
return $cids;
}
function devel_create_content() {
......
......@@ -99,17 +99,17 @@ function devel_generate_content_form() {
'#default_value' => 500,
'#size' => 10,
);
$form['kill_content'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing content before generating new content.'),
'#default_value' => FALSE,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of titles'),
'#default_value' => 8,
'#size' => 10,
);
$form['kill_content'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing content before generating new content.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do it!'),
......@@ -118,42 +118,8 @@ function devel_generate_content_form() {
}
function devel_generate_content_form_submit($form_id, &$form_state) {
$path = drupal_get_path('module', 'devel');
require_once('devel_generate.inc');
if ($form_state['values']['kill_content']) {
db_query("DELETE FROM {comments}");
db_query("DELETE FROM {node}");
db_query("DELETE FROM {node_revisions}");
db_query("DELETE FROM {node_comment_statistics}");
db_query("DELETE FROM {term_node}");
if (db_table_exists('forum')) { db_query("DELETE FROM {forum}"); }
db_query("DELETE FROM {url_alias}");
db_query("ALTER TABLE {node} AUTO_INCREMENT = 1");
db_query("ALTER TABLE {comments} AUTO_INCREMENT = 1");
drupal_set_message(t('Content deleted.'));
$next_nid = 0;
}
else {
$next_nid = db_result(db_query("SELECT MAX(nid) FROM {node}"))+1;
}
// get user id
$users = devel_get_users();
$terms = devel_get_terms();
// create 100 pseudo-random nodes:
devel_create_nodes($form_state['values']['num_nodes'], $users, $form_state['values']['title_length']);
drupal_set_message(t('%num nodes created.', array('%num' => $form_state['values']['num_nodes'])));
$nodes = devel_get_nodes($next_nid);
devel_add_terms($nodes, $terms);
$comments = devel_get_comments();
devel_create_comments($form_state['values']['num_comments'], $users, $nodes, $comments);
drupal_set_message(t('%num comments created.', array('%num' => $form_state['values']['num_comments'])));
devel_generate_content($form_state['values']['num_nodes'], $form_state['values']['num_comments'], $form_state['values']['title_length'], $form_state['values']['kill_content']);
}
function devel_generate_vocabs($records, $maxlength = 12) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment