2 // $Id: devel_generate.inc,v 1.14.2.12 2009/04/09 19:22:56 weitzman Exp $
3 // If not in 'safe mode', increase the maximum execution time:
4 if (!ini_get('safe_mode')) {
9 * Generate some random users.
12 * Number of users to generate.
14 * Boolean that indicates if existing users should be removed first.
16 * The max age of each randomly-generated user, in seconds.
18 function devel_create_users($num, $kill, $age = 0) {
19 $url = parse_url($GLOBALS['base_url']);
21 db_query('DELETE FROM {users} WHERE uid > 1');
22 drupal_set_message(t('Users deleted.'));
24 for ($i = 0; $i < $num; $i++) {
25 $length = rand(6, 12);
26 $name = devel_generate_word($length);
27 $pass = md5(user_password());
28 $mail = $name .'@'. $url['host'];
29 $now = time() - rand(0, $age);
30 db_query("INSERT INTO {users} (name, pass, mail, status, created, access) VALUES ('%s', '%s', '%s', %d, %d, %d)", $name, $pass, $mail, 1, $now, $now);
32 drupal_set_message(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users'))));
37 * The main API function for creating content.
39 * See devel_generate_content_form() for the supported keys in $form_state['values'].
40 * Other modules may participate by form_alter() on that form and then handling their data during hook_nodeapi('pre_save') or in own submit handler for the form.
42 * @param string $form_state
45 function devel_generate_content($form_state) {
46 if ($form_state['values']['kill_content']) {
47 devel_generate_content_kill($form_state['values']);
50 if (count($form_state['values']['node_types'])) {
52 devel_generate_content_pre_node($form_state['values']);
53 for ($i = 1; $i <= $form_state['values']['num_nodes']; $i ++) {
54 devel_generate_content_add_node($form_state['values']);
58 drupal_set_message(format_plural($form_state['values']['num_nodes'], '1 node created.', '@count nodes created'));
61 function devel_generate_add_comments($node, $users, $max_comments, $title_length = 8) {
63 $num_comments = mt_rand(1, $max_comments);
64 for ($i = 1; $i <= $num_comments; $i++) {
65 $comment->nid = $node->nid;
67 $comment->format = FILTER_FORMAT_DEFAULT;
68 $comment->name = 'devel generate';
69 $comment->mail = 'devel_generate@example.com';
70 $comment->timestamp = rand($node->created, time());
74 $comment->pid = db_result(db_query_range("SELECT cid FROM {comments} WHERE pid = 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
77 $comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid > 0 AND nid = %d ORDER BY RAND()", $comment->nid, 0, 1));
83 $comment->subject = devel_create_greeking(rand(1, $title_length), TRUE);
84 $comment->comment = devel_create_content();
85 $comment->uid = $users[array_rand($users)];
87 // this is slow but gets the threading right.
88 comment_save((array)$comment);
93 function devel_generate_vocabs($records, $maxlength = 12, $types = array('story', 'blog', 'forum', 'page')) {
97 for ($i = 1; $i <= $records; $i++) {
99 $voc['name'] = devel_generate_word(rand(2, $maxlength));
100 $voc['description'] = "description of ". $voc['name'];
101 $voc['nodes'] = array_flip(array($types[array_rand($types)]));
102 foreach ($voc['nodes'] as $key => $value) {
103 $voc['nodes'][$key] = $key;
105 $voc['multiple'] = 1;
106 $voc['required'] = 0;
107 $voc['relations'] = 1;
108 $voc['hierarchy'] = 1;
109 $voc['weight'] = rand(0,10);
111 taxonomy_save_vocabulary($voc);
112 $vocs[] = $voc['name'];
117 function devel_generate_terms($records, $vocs, $maxlength = 12) {
121 for ($i = 1; $i <= $records; $i++) {
124 $term['vid'] = $vocs[array_rand($vocs)];
125 // dont set a parent. handled by taxonomy_save_term()
126 // $term->parent = 0;
130 $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));
131 $term['parent'] = array($parent->tid);
132 $term['vid'] = $parent->vid;
136 $term['name'] = devel_generate_word(rand(2, $maxlength));
137 $term['description'] = "description of ". $term['name'];
138 $term['weight'] = rand(0,10);
140 $status = taxonomy_save_term($term);
144 $terms[] = $term['name'];
152 function devel_generate_get_vocabs() {
154 $result = db_query("SELECT vid FROM {vocabulary}");
155 while($voc = db_fetch_object($result)){
161 function devel_generate_taxonomy_data($num_vocab, $num_terms, $title_length, $kill) {
164 db_query("DELETE FROM {term_data}");
165 db_query("DELETE FROM {term_node}");
166 db_query("DELETE FROM {term_hierarchy}");
167 db_query("DELETE FROM {term_relation}");
168 db_query("DELETE FROM {term_synonym}");
169 db_query("DELETE FROM {vocabulary}");
170 db_query("DELETE FROM {vocabulary_node_types}");
171 switch ($GLOBALS['db_type']) {
174 db_query("ALTER TABLE {vocabulary} AUTO_INCREMENT = 1");
175 db_query("ALTER TABLE {term_data} AUTO_INCREMENT = 1");
178 db_query("SELECT setval('{vocabulary}_vid_seq', 1, false)");
179 db_query("SELECT setval('{term_data}_tid_seq', 1, false)");
182 drupal_set_message(t('Deleted taxonomy.'));
185 $new_vocs = devel_generate_vocabs($num_vocab, $title_length);
186 if (!empty($new_vocs)) {
187 drupal_set_message(t('Created the following new vocabularies: !vocs', array('!vocs' => theme('item_list', $new_vocs))));
189 $vocs = devel_generate_get_vocabs();
190 $new_terms = devel_generate_terms($num_terms, $vocs, $title_length);
191 if (!empty($new_terms)) {
192 drupal_set_message(t('Created the following new terms: !terms', array('!terms' => theme('item_list', $new_terms))));
196 function devel_generate_word($length){
197 srand((double)microtime()*1000000);
199 $vowels = array("a", "e", "i", "o", "u");
200 $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
201 "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
203 $num_vowels = count($vowels);
204 $num_cons = count($cons);
207 while(strlen($word) < $length){
208 $word .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
211 return substr($word, 0, $length);
214 function devel_create_content($type = NULL) {
215 $nparas = rand(1,12);
216 $type = empty($type) ? rand(0,3) : $type;
221 for ($i = 1; $i <= $nparas; $i++) {
222 $output .= devel_create_para(rand(10,60),1);
227 for ($i = 1; $i <= $nparas; $i++) {
228 $output .= devel_create_para(rand(10,60),2);
232 default: // plain text
233 for ($i = 1; $i <= $nparas; $i++) {
234 $output .= devel_create_para(rand(10,60)) ."\n\n";
241 function devel_create_para($words, $type = 0) {
246 $output .= devel_create_greeking($words);
247 $output = trim($output) ."</p>";
251 $output .= devel_create_greeking($words);
252 $output = trim($output) ."<br />";
256 $output .= devel_create_greeking($words);
257 $output = trim($output);
262 function devel_create_greeking($words, $title = FALSE) {
263 $dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan",
264 "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo",
265 "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit",
266 "brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis",
267 "commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui",
268 "damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor",
269 "dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat",
270 "eros", "esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci",
271 "exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino",
272 "genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic",
273 "humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto",
274 "importunus", "imputo", "in", "incassum", "inhibeo", "interdico",
275 "iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet",
276 "lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus",
277 "luptatum", "macto", "magna", "mauris", "melior", "metuo", "meus",
278 "minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh",
279 "nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo",
280 "occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus",
281 "patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum",
282 "populus", "praemitto", "praesent", "premo", "probo", "proprius",
283 "quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis",
284 "ratis", "refero", "refoveo", "roto", "rusticus", "saepius",
285 "sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis",
286 "singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen",
287 "tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis",
288 "typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam",
289 "utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit",
290 "veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico",
291 "virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo",
298 $sentence_length = rand(3,10);
300 $greeking .= ucfirst($dictionary[array_rand($dictionary)]);
301 for ($i = 1; $i < $sentence_length; $i++) {
302 $greeking .= " " . $dictionary[array_rand($dictionary)];
306 $words -= $sentence_length;
310 // use different method for titles
311 $title_length = $words;
313 for ($i = 0; $i < $words; $i++) {
314 $array[] = $dictionary[array_rand($dictionary)];
316 $greeking = ucwords(implode(' ', $array));
321 function devel_generate_add_terms(&$node) {
322 $vocabs = taxonomy_get_vocabularies($node->type);
323 foreach ($vocabs as $vocab) {
324 $sql = "SELECT tid FROM {term_data} WHERE vid = %d ORDER BY RAND()";
325 $result = db_query_range($sql, $vocab->vid, 0, 5);
326 while ($row = db_fetch_object($result)) {
328 $node->taxonomy['tags'][] = $row->tid;
331 $node->taxonomy[$row->tid] = $row->tid;
333 if (!$vocab->multiple) {
340 function devel_get_users() {
342 $result = db_query_range("SELECT uid FROM {users}", 0, 50);
343 while($user = db_fetch_object($result)){
344 $users[] = $user->uid;
349 function devel_generate_add_upload(&$node) {
350 $source = 'misc/blog.png';
351 $size = filesize($source);
353 // $after this call, $source contains the new path.
355 $file = new stdClass();
356 $file->filename = 'blog.png';
357 $file->filepath = $source;
358 $file->filemime = 'image/png';
359 $file->list = variable_get('upload_list_default', TRUE);
360 $file->description = 'b log.png was here';
361 $file->filesize = $size;
362 $file->weight = rand(0,10);
364 // If we made it this far it's safe to record this file in the database.
365 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());
366 $file->fid = db_last_insert_id('files', 'fid');
368 $_SESSION['upload_files'][$file->fid] = $file;
369 $node->files[$file->fid] = $file;
373 * Generate statistics information for a node.
378 function devel_generate_add_statistics($node) {
379 $totalcount = mt_rand(0, 500);
380 $daycount = mt_rand(0, $totalcount);
381 $timestamp = time() - mt_rand(0, $node->created);
382 db_query('INSERT INTO {node_counter} (nid, daycount, totalcount, timestamp) VALUES (%d, %d, %d, %d)', $node->nid, $daycount, $totalcount, $timestamp);
386 * Handle the devel_generate_content_form request to kill all of the content.
387 * This is used by both the batch and non-batch branches of the code.
390 * array of options obtained from devel_generate_content_form.
392 function devel_generate_content_kill($results) {
393 $sql = 'SELECT nid FROM {node} WHERE type IN ('. db_placeholders($results['node_types'], 'text'). ')';
394 $result = db_query($sql, $results['node_types']);
396 while ($row = db_fetch_object($result)) {
397 node_delete($row->nid);
400 drupal_set_message(format_plural($i, 'Deleted one post', 'Deleted @count posts'));
404 * Pre-process the devel_generate_content_form request. This is needed so
405 * batch api can get the list of users once. This is used by both the batch
406 * and non-batch branches of the code.
409 * array of options obtained from devel_generate_content_form.
411 function devel_generate_content_pre_node(&$results) {
413 $users = devel_get_users();
414 $users = array_merge($users, array('0'));
415 $results['users'] = $users;
419 * Create one node. This is used by both the batch and non-batch branches of
423 * array of options obtained from devel_generate_content_form.
425 function devel_generate_content_add_node(&$results) {
427 $node->type = array_rand($results['node_types']);
428 module_load_include('inc', 'node', 'node.pages');
429 node_object_prepare($node);
430 $users = $results['users'];
431 $node->uid = $users[array_rand($users)];
433 $node->title = devel_create_greeking(rand(1, $results['title_length']), TRUE);
434 $node->body = "node ($node->type) - ". devel_create_content();
435 $node->teaser = node_teaser($node->body);
436 $node->filter = variable_get('filter_default_format', 1);
437 $node->format = FILTER_FORMAT_DEFAULT;
438 $node->language = '';
439 $node->revision = rand(0,1);
440 $node->promote = rand(0, 1);
441 $node->created = time() - rand(0, $results['time_range']);
443 // A flag to let hook_nodeapi() implementations know that this is a generated node.
444 $node->devel_generate = $results;
446 // See devel_generate_nodeapi() for actions that happen before and after this save.
451 function devel_generate_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
452 if (isset($node->devel_generate)) {
453 $results = $node->devel_generate;
456 // Modules that want to affect generated nodes may implement hook_nodeapi('presave'). See OG module or CCK.
457 // A few implementations live here because core doesn't do bulk node generation.
459 if ($results['add_upload']) {
460 devel_generate_add_upload($node);
463 if ($results['add_terms']) {
464 devel_generate_add_terms($node);
468 if ($results['max_comments']) {
469 devel_generate_add_comments($node, $results['users'], $results['max_comments'], $results['title_length']);
472 // Add an url alias. Cannot happen before save becasue we don't know the nid.
473 if ($results['add_alias']) {
474 path_set_alias("node/$node->nid", "node-$node->nid-$node->type");
477 // Add node statistics.
478 if ($results['add_statistics']) {
479 devel_generate_add_statistics($node);