devel_generate.inc

  1. devel
    1. 5 devel_generate.inc
    2. 6 devel_generate.inc
    3. 7 devel_generate/devel_generate.inc
    4. 8 devel_generate/devel_generate.inc

Functions & methods

NameDescription
devel_create_comments
devel_create_content
devel_create_greeking
devel_create_nodes
devel_create_para
devel_create_usersGenerate some random users.
devel_generate_add_terms
devel_generate_content
devel_generate_get_vocabs
devel_generate_taxonomy_data
devel_generate_terms
devel_generate_vocabs
devel_generate_word
devel_get_comments
devel_get_nodes
devel_get_users

File

devel_generate.inc
View source
  1. <?php
  2. // If not in 'safe mode', increase the maximum execution time:
  3. if (!ini_get('safe_mode')) {
  4. set_time_limit(240);
  5. }
  6. /**
  7. * Generate some random users.
  8. *
  9. * @param $num
  10. * Number of users to generate.
  11. * @param $kill
  12. * Boolean that indicates if existing users should be removed first.
  13. */
  14. function devel_create_users($num, $kill) {
  15. $url = parse_url($GLOBALS['base_url']);
  16. if ($kill) {
  17. db_query('DELETE FROM {users} WHERE uid > 1');
  18. drupal_set_message(t('Users deleted.'));
  19. }
  20. for ($i = 0; $i < $num; $i++) {
  21. $uid = db_next_id('{users}_uid');
  22. $length = rand(4, 12);
  23. $name = devel_generate_word($length);
  24. $pass = md5(user_password());
  25. $mail = $name .'@'. $url['host'];
  26. $now = time();
  27. db_query("INSERT INTO {users} (uid, name, pass, mail, status, created, access) VALUES (%d, '%s', '%s', '%s', %d, %d, %d)", $uid, $name, $pass, $mail, 1, $now, $now);
  28. }
  29. drupal_set_message(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users'))));
  30. }
  31. function devel_generate_content($num_nodes, $num_comments, $title_length, $kill, $node_types = array()) {
  32. if ($kill) {
  33. $sql = 'SELECT nid FROM {node} WHERE type IN ('. implode(', ', array_fill(0, count($node_types), "'%s'")). ')';
  34. $result = db_query($sql, $node_types);
  35. while ($row = db_fetch_object($result)) {
  36. node_delete($row->nid);
  37. }
  38. }
  39. // Get user id.
  40. $users = devel_get_users();
  41. // Create $num_nodes pseudo-random nodes.
  42. devel_create_nodes($num_nodes, $users, $title_length, $node_types);
  43. drupal_set_message(t('%num nodes created.', array('%num' => $num_nodes)));
  44. $nodes = devel_get_nodes($next_nid);
  45. $comments = devel_get_comments();
  46. devel_create_comments($num_comments, $users, $nodes, $comments);
  47. drupal_set_message(t('%num comments created.', array('%num' => $num_comments)));
  48. }
  49. function devel_create_nodes($records, $users, $title_length = 8, $types = array()) {
  50. if (is_array($types)) {
  51. // Insert new data:
  52. for ($i = 1; $i <= $records; $i++) {
  53. $node->uid = $users[array_rand($users)];
  54. $node->type = $types[array_rand($types)];
  55. // Get the next nid without incrementing it.
  56. switch ($GLOBALS['db_type']) {
  57. case 'mysql':
  58. case 'mysqli':
  59. $next_nid = db_result(db_query("SELECT id FROM {sequences} WHERE name = '{node}_nid'")) + 1;
  60. break;
  61. case 'pgsql':
  62. $next_nid = db_result(db_query("SELECT currval('{node}_nid_seq')")) + 1;
  63. break;
  64. }
  65. $title = devel_create_greeking(rand(1, $title_length), TRUE);
  66. $node->title = $title;
  67. $node->body = "node #$next_nid ($node->type) - ". devel_create_content();
  68. $node->teaser = node_teaser($node->body);
  69. $node->filter = variable_get('filter_default_format', 1);
  70. $node->status = 1;
  71. $node->revision = rand(0,1);
  72. $node->promote = rand(0, 1);
  73. $node->comment = 2;
  74. $node->created = time();
  75. $node->changed = time();
  76. // TODO: add ability to disable terms
  77. if (1 || $add_terms) {
  78. devel_generate_add_terms($node);
  79. }
  80. // A flag to let hook_nodeapi() implementations know that this is a generated node.
  81. $node->devel_generate = $records;
  82. // Save the node:
  83. node_save($node);
  84. // Setup a path:
  85. db_query("INSERT INTO {url_alias} (src, dst) VALUES ('%s', '%s')", "node/$node->nid", "node-$node->nid-$node->type");
  86. unset($node);
  87. }
  88. }
  89. }
  90. function devel_generate_add_terms(&$node) {
  91. $vocabs = taxonomy_get_vocabularies($node->type);
  92. // dsm($vocabs);
  93. foreach ($vocabs as $vocab) {
  94. $sql = "SELECT tid FROM {term_data} WHERE vid = %d ORDER BY RAND()";
  95. $result = db_query_range($sql, $vocab->vid, 0, 5);
  96. while ($row = db_fetch_object($result)) {
  97. if ($vocab->tags) {
  98. $node->taxonomy['tags'][] = $row->tid;
  99. }
  100. else {
  101. $node->taxonomy[$row->tid] = $row->tid;
  102. }
  103. if (!$vocab->multiple) {
  104. break;
  105. }
  106. }
  107. }
  108. }
  109. function devel_create_comments($records, $users, $nodes, $comments) {
  110. $users = array_merge($users, array('0'));
  111. // Insert new data:
  112. for ($i = 1; $i <= $records; $i++) {
  113. $comment->cid = db_next_id("{comments}_cid");
  114. $comment->nid = array_rand($nodes);
  115. switch ($i % 3) {
  116. case 1:
  117. $comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid = 0 AND nid = %d ORDER BY RAND() LIMIT 1", $comment->nid));
  118. break;
  119. case 2:
  120. $comment->pid = db_result(db_query("SELECT cid FROM {comments} WHERE pid > 0 AND nid = %d ORDER BY RAND() LIMIT 1", $comment->nid));
  121. break;
  122. default:
  123. $comment->pid = 0;
  124. }
  125. $comment->subject = "comment #". $comment->cid;
  126. $comment->comment = "body of comment #". $comment->cid;
  127. $comment->uid = $users[array_rand($users)];
  128. db_query("INSERT INTO {comments} (cid, nid, pid, uid, subject, comment, status, thread, timestamp) VALUES (%d, %d, %d, %d, '%s', '%s', %d, %d, %d)", $comment->cid, $comment->nid, $comment->pid, $comment->uid, $comment->subject, $comment->comment, 0, 0, time());
  129. }
  130. // update comments statistics
  131. foreach ($nodes as $nid => $type) {
  132. _comment_update_node_statistics($nid);
  133. }
  134. }
  135. function devel_generate_vocabs($records, $maxlength = 12, $types = array('story', 'blog', 'forum', 'page')) {
  136. $vocs = array();
  137. // Insert new data:
  138. for ($i = 1; $i <= $records; $i++) {
  139. $voc = array();
  140. $voc['name'] = devel_generate_word(rand(2, $maxlength));
  141. $voc['description'] = "description of ". $voc['name'];
  142. $voc['nodes'] = array_flip(array($types[array_rand($types)]));
  143. foreach ($voc['nodes'] as $key => $value) {
  144. $voc['nodes'][$key] = $key;
  145. }
  146. $voc['multiple'] = 1;
  147. $voc['required'] = 0;
  148. $voc['relations'] = 1;
  149. $voc['hierarchy'] = 1;
  150. $voc['weight'] = rand(0,10);
  151. taxonomy_save_vocabulary($voc);
  152. $vocs[] = $voc['name'];
  153. }
  154. return $vocs;
  155. }
  156. function devel_generate_terms($records, $vocs, $maxlength = 12) {
  157. $terms = array();
  158. // Insert new data:
  159. for ($i = 1; $i <= $records; $i++) {
  160. switch ($i % 2) {
  161. case 1:
  162. $term['vid'] = $vocs[array_rand($vocs)];
  163. // dont set a parent. handled by taxonomy_save_term()
  164. // $term->parent = 0;
  165. break;
  166. case 2:
  167. default:
  168. $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));
  169. $term['parent'] = array($parent->tid);
  170. $term['vid'] = $parent->vid;
  171. break;
  172. }
  173. $term['name'] = devel_generate_word(rand(2, $maxlength));
  174. $term['description'] = "description of ". $term['name'];
  175. $term['weight'] = rand(0,10);
  176. $status = taxonomy_save_term($term);
  177. if ($status) {
  178. $terms[] = $term['name'];
  179. }
  180. unset($term);
  181. }
  182. return $terms;
  183. }
  184. function devel_generate_get_vocabs() {
  185. $vocs = array();
  186. $result = db_query("SELECT vid FROM {vocabulary}");
  187. while($voc = db_fetch_object($result)){
  188. $vocs[] = $voc->vid;
  189. }
  190. return $vocs;
  191. }
  192. function devel_generate_taxonomy_data($num_vocab, $num_terms, $title_length, $kill) {
  193. if ($kill) {
  194. db_query("DELETE FROM {term_data}");
  195. db_query("DELETE FROM {term_node}");
  196. db_query("DELETE FROM {term_hierarchy}");
  197. db_query("DELETE FROM {term_relation}");
  198. db_query("DELETE FROM {term_synonym}");
  199. db_query("DELETE FROM {vocabulary}");
  200. db_query("DELETE FROM {vocabulary_node_types}");
  201. switch ($GLOBALS['db_type']) {
  202. case 'mysql':
  203. case 'mysqli':
  204. db_query("UPDATE {sequences} SET id = '0' WHERE name = '{vocabulary}_vid'");
  205. db_query("UPDATE {sequences} SET id = '0' WHERE name = '{term_data}_tid'");
  206. db_query("ALTER TABLE {vocabulary} AUTO_INCREMENT = 1");
  207. db_query("ALTER TABLE {term_data} AUTO_INCREMENT = 1");
  208. break;
  209. case 'pgsql':
  210. db_query("SELECT setval('{vocabulary}_vid_seq', 1, false)");
  211. db_query("SELECT setval('{term_data}_tid_seq', 1, false)");
  212. break;
  213. }
  214. drupal_set_message(t('Deleted taxonomy.'));
  215. }
  216. $new_vocs = devel_generate_vocabs($num_vocab, $title_length);
  217. if (!empty($new_vocs)) {
  218. drupal_set_message(t('Created the following new vocabularies: !vocs', array('!vocs' => theme('item_list', $new_vocs))));
  219. }
  220. $vocs = devel_generate_get_vocabs();
  221. $new_terms = devel_generate_terms($num_terms, $vocs, $title_length);
  222. if (!empty($new_terms)) {
  223. drupal_set_message(t('Created the following new terms: !terms', array('!terms' => theme('item_list', $new_terms))));
  224. }
  225. }
  226. function devel_generate_word($length){
  227. srand((double)microtime()*1000000);
  228. $vowels = array("a", "e", "i", "o", "u");
  229. $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
  230. "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
  231. $num_vowels = count($vowels);
  232. $num_cons = count($cons);
  233. while(strlen($word) < $length){
  234. $word .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)];
  235. }
  236. return substr($word, 0, $length);
  237. }
  238. function devel_create_content() {
  239. $nparas = rand(1,12);
  240. $type = rand(0,3);
  241. $output = "";
  242. switch($type % 3) {
  243. case 1: // html
  244. for ($i = 1; $i <= $nparas; $i++) {
  245. $output .= devel_create_para(rand(10,60),1);
  246. }
  247. break;
  248. case 2: // brs only
  249. for ($i = 1; $i <= $nparas; $i++) {
  250. $output .= devel_create_para(rand(10,60),2);
  251. }
  252. break;
  253. default: // plain text
  254. for ($i = 1; $i <= $nparas; $i++) {
  255. $output .= devel_create_para(rand(10,60)) ."\n\n";
  256. }
  257. }
  258. return $output;
  259. }
  260. function devel_create_para($words, $type = 0) {
  261. $output = "";
  262. switch ($type) {
  263. case 1:
  264. $output .= "<p>";
  265. $output .= devel_create_greeking($words);
  266. $output = trim($output) ."</p>";
  267. break;
  268. case 2:
  269. $output .= devel_create_greeking($words);
  270. $output = trim($output) ."<br />";
  271. break;
  272. default:
  273. $output .= devel_create_greeking($words);
  274. $output = trim($output);
  275. }
  276. return $output;
  277. }
  278. function devel_create_greeking($words, $title = FALSE) {
  279. $dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan",
  280. "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo",
  281. "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit",
  282. "brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis",
  283. "commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui",
  284. "damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor",
  285. "dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat",
  286. "eros", "esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci",
  287. "exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino",
  288. "genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic",
  289. "humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto",
  290. "importunus", "imputo", "in", "incassum", "inhibeo", "interdico",
  291. "iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet",
  292. "lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus",
  293. "luptatum", "macto", "magna", "mauris", "melior", "metuo", "meus",
  294. "minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh",
  295. "nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo",
  296. "occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus",
  297. "patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum",
  298. "populus", "praemitto", "praesent", "premo", "probo", "proprius",
  299. "quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis",
  300. "ratis", "refero", "refoveo", "roto", "rusticus", "saepius",
  301. "sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis",
  302. "singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen",
  303. "tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis",
  304. "typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam",
  305. "utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit",
  306. "veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico",
  307. "virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo",
  308. "zelus");
  309. $greeking = "";
  310. if (!$title) {
  311. while ($words > 0) {
  312. $sentence_length = rand(3,10);
  313. $greeking .= ucfirst($dictionary[array_rand($dictionary)]);
  314. for ($i = 1; $i < $sentence_length; $i++) {
  315. $greeking .= " " . $dictionary[array_rand($dictionary)];
  316. }
  317. $greeking .= ". ";
  318. $words -= $sentence_length;
  319. }
  320. }
  321. else {
  322. // use different method for titles
  323. $title_length = $words;
  324. $array = array();
  325. for ($i = 0; $i < $words; $i++) {
  326. $array[] = $dictionary[array_rand($dictionary)];
  327. }
  328. $greeking = ucwords(implode(' ', $array));
  329. }
  330. return $greeking;
  331. }
  332. function devel_get_users() {
  333. $users = array();
  334. $result = db_query("SELECT uid FROM {users}");
  335. while($user = db_fetch_object($result)){
  336. $users[] = $user->uid;
  337. }
  338. return $users;
  339. }
  340. function devel_get_nodes($nid = 0) {
  341. $nodes = array();
  342. $result = db_query("SELECT nid, type FROM {node} WHERE type IN ('story', 'blog', 'forum', 'page') AND comment = 2 AND nid > %d", $nid);
  343. while($node = db_fetch_object($result)){
  344. $nodes[$node->nid] = $node->type ;
  345. }
  346. return $nodes;
  347. }
  348. function devel_get_comments() {
  349. $comments = array();
  350. $result = db_query("SELECT nid, cid FROM {comments}");
  351. while($comment = db_fetch_object($result)){
  352. $comments[$comment->nid][] = $comment->cid ;
  353. }
  354. return comments;
  355. }
Login or register to post comments

API Navigation