Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
// $Id$
/**
* Implementation of hook_menu().
*/
function devel_generate_menu() {
$items = array();
$items['admin/user/generate'] = 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(
'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')
);
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',
'#title' => t('How many users would you like to generate?'),
'#default_value' => 50,
'#size' => 10,
);
$form['kill_users'] = array(
'#type' => 'checkbox',
'#title' => t('Delete all users but user 1 before generating new users.'),
'#default_value' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do it!'),
);
return $form;
}
Moshe Weitzman
committed
function devel_generate_users_form_submit($form_id, &$form_state) {
require_once('devel_generate.inc');
Moshe Weitzman
committed
devel_create_users($form_state['values']['num'], $form_state['values']['kill_users']);
}
function devel_generate_word($length){
srand((double)microtime()*1000000);
$vowels = array("a", "e", "i", "o", "u");
$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
"cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
$num_vowels = count($vowels);
$num_cons = count($cons);
Moshe Weitzman
committed
$word = '';
while(strlen($word) < $length){
$word .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
}
return substr($word, 0, $length);
}
function devel_generate_content_form() {
$form['num_nodes'] = array(
'#type' => 'textfield',
'#title' => t('How many nodes would you like to generate?'),
'#default_value' => 100,
'#size' => 10,
);
$form['num_comments'] = array(
'#type' => 'textfield',
'#title' => t('How many comments would you like to generate?'),
'#default_value' => 500,
'#size' => 10,
);
$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!'),
);
return $form;
}
Moshe Weitzman
committed
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']);
}
function devel_generate_vocabs($records, $maxlength = 12) {
$types = array("story", "blog", "forum", "page");
Moshe Weitzman
committed
$output = NULL;
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
$voc = array();
$voc['name'] = devel_generate_word(rand(2, $maxlength));
$voc['description'] = "description of ". $voc['name'];
$voc['nodes'] = array_flip(array($types[array_rand($types)]));
foreach ($voc['nodes'] as $key => $value) {
$voc['nodes'][$key] = $key;
}
$voc['multiple'] = 1;
$voc['required'] = 0;
$voc['relations'] = 1;
$voc['hierarchy'] = 1;
$voc['weight'] = rand(0,10);
taxonomy_save_vocabulary($voc);
Moshe Weitzman
committed
$output .= "created vocabulary ". $voc['name']. "<br />";
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
}
return $output;
}
function devel_generate_terms($records, $vocs, $maxlength = 12) {
// Insert new data:
for ($i = 1; $i <= $records; $i++) {
switch ($i % 2) {
case 1:
$term['vid'] = $vocs[array_rand($vocs)];
// dont set a parent. handled by taxonomy_save_term()
// $term->parent = 0;
break;
case 2:
default:
$parent = db_fetch_object(db_query_range("SELECT t.tid, v.vid FROM {term_data} t INNER JOIN {vocabulary} v ON t.vid = v.vid ORDER BY RAND()", 0, 1));
$term['parent'] = array($parent->tid);
$term['vid'] = $parent->vid;
break;
}
$term['name'] = devel_generate_word(rand(2, $maxlength));
$term['description'] = "description of ". $term['name'];
$term['weight'] = rand(0,10);
$status = taxonomy_save_term($term);
Moshe Weitzman
committed
$output = NULL;
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
if ($status) {
$output .= t("Created term @term", array('@term' => $term['name'])). "<br />";
}
else {
$output .= t("There was an error creating term @term", array('@term' => $term['name'])). "<br />";
}
unset($term);
}
return $output;
}
function devel_generate_get_vocabs() {
$vocs = array();
$result = db_query("SELECT vid FROM {vocabulary}");
while($voc = db_fetch_object($result)){
$vocs[] = $voc->vid;
}
return $vocs;
}
function devel_generate_taxonomy_form() {
$form['num_vocab'] = array(
'#type' => 'textfield',
'#title' => t('How many vocabularies would you like to generate?'),
'#default_value' => 3,
'#size' => 10,
);
$form['num_terms'] = array(
'#type' => 'textfield',
'#title' => t('How many terms would you like to generate?'),
'#default_value' => 50,
'#size' => 10,
);
$form['kill_taxonomy'] = array(
'#type' => 'checkbox',
'#title' => t('Delete existing terms and vocabularies before generating new content.'),
'#default_value' => FALSE,
);
$form['title_length'] = array(
'#type' => 'textfield',
'#title' => t('Max word length of term/vocab names'),
'#default_value' => 12,
'#size' => 10,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Do it!'),
);
return $form;
}
Moshe Weitzman
committed
function devel_generate_taxonomy_form_submit($form_id, &$form_state) {
$vocs = array();
$result = db_query("SELECT vid FROM {vocabulary}");
while($voc = db_fetch_object($result)){
$vocs[] = $voc->vid;
}
Moshe Weitzman
committed
if ($form_state['values']['kill_taxonomy']) {
db_query("DELETE FROM {term_data}");
db_query("DELETE FROM {term_node}");
db_query("DELETE FROM {term_hierarchy}");
db_query("DELETE FROM {term_relation}");
db_query("DELETE FROM {term_synonym}");
db_query("DELETE FROM {vocabulary}");
db_query("DELETE FROM {vocabulary_node_types}");
}
Moshe Weitzman
committed
$output = devel_generate_vocabs($form_state['values']['num_vocab'], $form_state['values']['title_length']);
$vocs = devel_generate_get_vocabs();
Moshe Weitzman
committed
$output .= devel_generate_terms($form_state['values']['num_terms'], $vocs, $form_state['values']['title_length']);
drupal_set_message($output);
}