Commit efba51fd authored by moshe weitzman's avatar moshe weitzman

you may now generate nodes only of specified types ... content deletion is...

you may now generate nodes only of specified types ...  content deletion is slower now but more proper. we only delete nodes from the specified types.
parent cccc6e69
......@@ -30,80 +30,32 @@ 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, $add_upload = FALSE, $add_terms = FALSE, $add_alias = FALSE) {
function devel_generate_content($num_nodes, $num_comments, $title_length, $kill, $add_upload = FALSE, $add_terms = FALSE, $add_alias = FALSE, $node_types) {
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}");
}
if (db_table_exists('upload')) {
db_query("DELETE FROM {upload}");
}
if (db_table_exists('relatedlinks')) {
db_query("DELETE FROM {relatedlinks}");
db_query("DELETE FROM {relatedlinks_tracker}");
}
if (db_table_exists('nodequeue_nodes')) {
db_query("DELETE FROM {nodequeue_nodes}");
}
db_query("DELETE FROM {url_alias}");
// TODO: use schema API here?
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;
$sql = 'SELECT nid FROM {node} WHERE type IN ('. db_placeholders($node_types, 'text'). ')';
$result = db_query($sql, $node_types);
while ($row = db_fetch_object($result)) {
node_delete($row->nid);
}
drupal_set_message(t('Content deleted.'));
}
// Get user id.
$users = devel_get_users();
// 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);
devel_create_nodes($num_nodes, $users, $title_length, $add_upload, $add_terms, $num_comments, $add_alias, $node_types);
drupal_set_message(format_plural($num_nodes, '1 node created.', '@count nodes created'));
}
function devel_create_nodes($records, $users, $title_length = 8, $add_upload = FALSE, $add_terms = FALSE, $num_comments = 0, $add_alias = FALSE) {
$sql = "SELECT type FROM node_type WHERE module IN ('node', 'forum')";
$result = db_query($sql);
while ($row = db_fetch_object($result)) {
$possible_types[] = $row->type;
}
// For now, eliminate types which have any CCK fields. Help wanted.
if (module_exists('content')) {
$fields = content_fields();
foreach ($fields as $field) {
$ineligible[] = $field['type_name'];
}
$possible_types = array_diff($possible_types, $ineligible);
}
// Only use types that exist.
$installed_types = array_keys(node_get_types('types'));
$types = array_intersect($possible_types, $installed_types);
function devel_create_nodes($records, $users, $title_length = 8, $add_upload = FALSE, $add_terms = FALSE, $num_comments = 0, $add_alias = FALSE, $node_types = array()) {
$users = array_merge($users, array('0'));
// $nids = array();
if (count($types)) {
if (count($node_types)) {
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$node->type = $types[array_rand($types)];
$node->type = $node_types[array_rand($node_types)];
require_once './modules/node/node.pages.inc';
node_object_prepare($node);
$node->uid = $users[array_rand($users)];
......
......@@ -69,18 +69,46 @@ function devel_generate_users_form_submit($form_id, &$form_state) {
}
function devel_generate_content_form() {
if (module_exists('content')) {
$types = content_types();
$warn = '';
foreach ($types as $type) {
if (count($type['fields'])) {
$warn = t('. <strong>Warning</strong>. This type contains CCK fields which won\'t be populated. See !url', array('!url' => l(t('this CCK issue.'), 'http://drupal.org/node/187599')));
}
$options[$type['type']] = t($type['name']). $warn;
unset($warn);
}
}
else {
$types = node_get_types();
foreach ($types as $type) {
$options[$type->type] = t($type->name);
}
}
$form['node_types'] = array(
'#type' => 'checkboxes',
'#title' => t('Which node types do you want to create?'),
'#options' => $options,
'#default_value' => array_keys($options),
);
$form['kill_content'] = array(
'#type' => 'checkbox',
'#title' => t('<strong>Delete all content</strong> in these node types before generating new content.'),
'#default_value' => FALSE,
);
$form['num_nodes'] = array(
'#type' => 'textfield',
'#title' => t('How many nodes would you like to generate?'),
'#default_value' => 100,
'#default_value' => 50,
'#size' => 10,
);
$form['num_comments'] = array(
'#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,
'#default_value' => 0,
'#size' => 3,
'#access' => module_exists('comment'),
);
$form['title_length'] = array(
......@@ -106,11 +134,6 @@ function devel_generate_content_form() {
'#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.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do it!'),
......@@ -121,7 +144,7 @@ function devel_generate_content_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'], $form_state['values']['add_upload'], $form_state['values']['add_terms'], $form_state['values']['add_alias']);
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'], array_filter($form_state['values']['node_types']));
}
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