Commit e281d5db authored by moshe weitzman's avatar moshe weitzman

major update for devel_generate module. lots of stuff like comment threading...

major update for devel_generate module. lots of stuff like comment threading was busted. further, we were attaching taxonomy terms to nodes in the wrong place (outside of node_save()). all is working ok in my tests except for the main block of links on /admin. for some reason, that isn't showing up.
parent 0f79f53f
......@@ -30,7 +30,7 @@ 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) {
function devel_generate_content($num_nodes, $num_comments, $title_length, $kill, $add_upload = FALSE, $add_terms = FALSE, $add_alias = FALSE) {
if ($kill) {
db_query("DELETE FROM {comments}");
db_query("DELETE FROM {node}");
......@@ -42,7 +42,11 @@ function devel_generate_content($num_nodes, $num_comments, $title_length, $kill)
if (db_table_exists('forum')) {
db_query("DELETE FROM {forum}");
}
if (db_table_exists('upload')) {
db_query("DELETE FROM {upload}");
}
db_query("DELETE FROM {url_alias}");
// TODO: use schema API here?
switch ($GLOBALS['db_type']) {
case 'mysql':
case 'mysqli':
......@@ -60,112 +64,92 @@ function devel_generate_content($num_nodes, $num_comments, $title_length, $kill)
// 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)));
}
}
// Create $num_nodes pseudo-random nodes and comments (if specified)
devel_create_nodes($num_nodes, $users, $title_length, $add_upload, $add_terms, $num_comments, $add_alias);
drupal_set_message(format_plural($num_nodes, '1 node created.', '@count nodes created'));
}
function devel_create_nodes($records, $users, $title_length = 8) {
function devel_create_nodes($records, $users, $title_length = 8, $add_upload = FALSE, $add_terms = FALSE, $num_comments = 0, $add_alias = FALSE) {
$possible_types = array("story", "blog", "forum", "page");
// Only use types that exist.
$installed_types = array_keys(node_get_types('types'));
foreach ($possible_types as $type) {
if (in_array($type, $installed_types)) {
$types[] = $type;
}
}
$types = array_intersect($possible_types, $installed_types);
$users = array_merge($users, array('0'));
$nids = array();
// $nids = array();
if (is_array($types)) {
if (count($types)) {
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$node->uid = $users[array_rand($users)];
$node->type = $types[array_rand($types)];
require_once './modules/node/node.pages.inc';
node_object_prepare($node);
$node->uid = $users[array_rand($users)];
$title = devel_create_greeking(rand(1, $title_length), TRUE);
/*
$title_words = rand(1, 8);
while($i < $title_words){
$title .= devel_generate_word(rand(4, 9));
$i;
}
*/
$node->title = $title;
$node->title = devel_create_greeking(rand(1, $title_length), TRUE);
$node->body = "node ($node->type) - ". devel_create_content();
$node->teaser = node_teaser($node->body);
$node->filter = variable_get('filter_default_format', 1);
$node->format = FILTER_FORMAT_DEFAULT;
$node->language = '';
$node->log = '';
$node->sticky = '';
$node->format = '';
$node->status = 1;
$node->revision = rand(0,1);
$node->promote = rand(0, 1);
$node->comment = 2;
$node->created = time();
$node->changed = time();
// Add an url alias
// TODO: the nid is not known yet. move back tpo post creation.
// if ($add_alias) {
// $node->path = "node-$node->nid-$node->type";
// }
if ($add_upload) {
devel_generate_add_upload($node);
}
if ($add_terms) {
devel_generate_add_terms($node);
}
// Save the node:
node_save($node);
if ($num_comments) {
devel_generate_add_comments($node, $users, $num_comments, $title_length);
}
// 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();
function devel_generate_add_comments($node, $users, $num_comments, $title_length = 8) {
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$comment->nid = array_rand($nodes);
for ($i = 1; $i <= $num_comments; $i++) {
$comment->nid = $node->nid;
$comment->cid = NULL;
$comment->format = FILTER_FORMAT_DEFAULT;
$comment->name = 'devel generate';
$comment->mail = 'devel_generate@example.com';
switch ($i % 3) {
case 1:
$comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid = 0 AND nid = %d ORDER BY RAND() LIMIT 1", $comment->nid));
$comment->pid = db_result(db_query_range("SELECT cid FROM {comments} WHERE pid = 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
break;
case 2:
$comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid > 0 AND nid = %d ORDER BY RAND() LIMIT 1", $comment->nid));
$comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid > 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
break;
default:
$comment->pid = 0;
}
$comment->subject = "comment #". $i;
$comment->comment = "body of comment #". $i;
$comment->subject = devel_create_greeking(rand(1, $title_length), TRUE);
$comment->comment = devel_create_content();
$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());
// this is slow but gets the threading right.
comment_save((array)$comment);
$cids[] = db_last_insert_id('comments', 'cid');
}
return $cids;
}
function devel_generate_vocabs($records, $maxlength = 12, $types = array('story', 'blog', 'forum', 'page')) {
......@@ -396,63 +380,46 @@ function devel_create_greeking($words, $title = FALSE) {
return $greeking;
}
function devel_add_terms($nodes, $terms) {
$used = array();
if(count($terms) > 0){
foreach($nodes as $nid => $type) {
$rand = rand(1, 6); // how many terms*
while ($rand--) {
$again = TRUE;
while ($again) {
$tid = $terms[$type][@array_rand($terms[$type])];
if (!isset($used[$nid][$tid])) {
$again = FALSE;
$used[$nid][$tid] = $tid;
}
}
if ($tid) {
db_query("INSERT INTO {term_node} (nid, tid) VALUES (%d, %d)", $nid, $tid);
}
}
function devel_generate_add_terms(&$node) {
$sql = "SELECT DISTINCT(tid) as tid, v.tags FROM {term_data} td INNER JOIN {vocabulary} v ON td.vid=v.vid INNER JOIN {vocabulary_node_types} vnt ON v.vid=vnt.vid WHERE vnt.type = '%s'";
$result = db_query_range($sql, $node->type, 0, 5);
while ($row = db_fetch_object($result)) {
if ($row->tags) {
$node->taxonomy['tags'][] = $row->tid;
}
else {
$node->taxonomy[$row->tid] = $row->tid;
}
}
}
function devel_get_users() {
$users = array();
$result = db_query("SELECT uid FROM {users}");
$result = db_query_range("SELECT uid FROM {users}", 0, 50);
while($user = db_fetch_object($result)){
$users[] = $user->uid;
}
return $users;
}
function devel_get_nodes($nid = 0) {
$nodes = array();
$result = db_query("SELECT nid, type FROM {node} WHERE type IN ('story', 'blog', 'forum', 'page') AND comment = 2 AND nid > %d", $nid);
while($node = db_fetch_object($result)){
$nodes[$node->nid] = $node->type ;
}
return $nodes;
}
function devel_get_comments() {
$comments = array();
$result = db_query("SELECT nid, cid FROM {comments}");
while($comment = db_fetch_object($result)){
$comments[$comment->nid][] = $comment->cid ;
}
return $comments;
}
function devel_get_terms() {
$terms = array();
$result = db_query("SELECT d.tid, v.vid FROM {vocabulary} v, {term_data} d WHERE v.vid = d.vid");
while($term = db_fetch_object($result)){
$result2 = db_query("SELECT n.type FROM {vocabulary_node_types} n WHERE n.vid = %d", $term->vid);
while ($nt = db_fetch_object($result2)) {
$terms[$nt->type][] = $term->tid;
}
}
return $terms;
}
function devel_generate_add_upload(&$node) {
$source = 'misc/blog.png';
$size = filesize($source);
// $after this call, $source contains the new path.
file_copy($source);
$file = new stdClass();
$file->filename = 'blog.png';
$file->filepath = $source;
$file->filemime = 'image/png';
$file->list = variable_get('upload_list_default', TRUE);
$file->description = 'b log.png was here';
$file->filesize = $size;
// If we made it this far it's safe to record this file in the database.
db_query("INSERT INTO {files} (uid, filename, filepath, filemime, filesize, status, timestamp) VALUES (%d, '%s', '%s', '%s', %d, %d, %d)", $node->uid, $file->filename, $file->filepath, $file->filemime, $file->filesize, FILE_STATUS_TEMPORARY, time());
$file->fid = db_last_insert_id('files', 'fid');
$_SESSION['upload_files'][$file->fid] = $file;
$node->files[$file->fid] = $file;
}
\ No newline at end of file
......@@ -7,43 +7,43 @@
function devel_generate_menu() {
$items = array();
$items['admin/user/generate'] = array(
// Admin user pages
$items['admin/generate'] = array(
'title' => 'Generate items',
'description' => "Populate your database with dummy items.",
'position' => 'left',
'page callback' => 'system_admin_menu_block_page',
'access arguments' => array('administer site configuration'),
'file' => 'system.admin.inc',
'file path' => drupal_get_path('module', 'system'),
);
$items['admin/generate/user'] = array(
'title' => t('Generate users'),
'description' => t('Generate a given number of users. Optionally delete current users.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_users_form'),
'access arguments' => array('administer users')
);
$items['admin/content/generate'] = array(
$items['admin/generate/content'] = array(
'title' => t('Generate content'),
'description' => t('Generate a given number of nodes and comments. Optionally delete current items.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_content_form'),
'access arguments' => array('administer nodes')
);
$items['admin/content/generate_taxonomy'] = array(
'title' => t('Generate categories'),
'description' => t('Generate a given number of vocabularies and terms. Optionally delete current categories.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_taxonomy_form'),
'access arguments' => array('administer nodes')
);
if (module_exists('taxonomy')) {
$items['admin/generate/taxonomy'] = array(
'title' => t('Generate categories'),
'description' => t('Generate a given number of vocabularies and terms. Optionally delete current categories.'),
'page callback' => 'drupal_get_form',
'page arguments' => array('devel_generate_taxonomy_form'),
'access arguments' => array('administer nodes'),
);
}
return $items;
}
/**
* Overview page for admin/generate page
*/
function devel_generate_overview() {
$menu = menu_get_item(NULL, 'admin/generate');
$content = system_admin_menu_block($menu);
$output = theme('admin_block_content', $content);
return $output;
}
function devel_generate_users_form() {
$form['num'] = array(
'#type' => 'textfield',
......@@ -76,10 +76,12 @@ function devel_generate_content_form() {
'#size' => 10,
);
$form['num_comments'] = array(
'#type' => 'textfield',
'#title' => t('How many comments would you like to generate?'),
'#default_value' => 500,
'#size' => 10,
'#type' => module_exists('comment') ? 'textfield' : 'value',
'#title' => t('How many comments per node would you like to generate?'),
'#description' => t('You must also enable comments for the node types you are generating.'),
'#default_value' => 5,
'#size' => 5,
'#access' => module_exists('comment'),
);
$form['title_length'] = array(
'#type' => 'textfield',
......@@ -87,6 +89,23 @@ function devel_generate_content_form() {
'#default_value' => 8,
'#size' => 10,
);
$form['add_upload'] = array(
'#type' => module_exists('upload') ? 'checkbox' : 'value',
'#title' => t('Add an upload to each generated node'),
'#default_value' => FALSE,
);
$form['add_terms'] = array(
'#type' => module_exists('taxonomy') ? 'checkbox' : 'value',
'#title' => t('Add taxonomy terms to each node.'),
'#default_value' => FALSE,
'#access' => module_exists('taxonomy'),
);
// TODO: restore this. see devel_create_content()
$form['add_alias'] = array(
'#type' => 'value',
'#title' => t('Add an url alias for each node.'),
'#default_value' => FALSE,
);
$form['kill_content'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing content before generating new content.'),
......@@ -96,12 +115,13 @@ function devel_generate_content_form() {
'#type' => 'submit',
'#value' => t('Do it!'),
);
return $form;
}
function devel_generate_content_form_submit($form_id, &$form_state) {
require_once('devel_generate.inc');
devel_generate_content($form_state['values']['num_nodes'], $form_state['values']['num_comments'], $form_state['values']['title_length'], $form_state['values']['kill_content']);
devel_generate_content($form_state['values']['num_nodes'], $form_state['values']['num_comments'], $form_state['values']['title_length'], $form_state['values']['kill_content'], $form_state['values']['add_upload'], $form_state['values']['add_terms'], $form_state['values']['add_alias']);
}
function devel_generate_taxonomy_form() {
......
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