devel_generate.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. <?php
  2. /**
  3. * Generate some random users.
  4. *
  5. * @param $num
  6. * Number of users to generate.
  7. * @param $kill
  8. * Boolean that indicates if existing users should be removed first.
  9. * @param $age
  10. * The max age of each randomly-generated user, in seconds.
  11. * @param $roles
  12. * An array of role IDs that the users should receive.
  13. */
  14. function devel_create_users($num, $kill, $age = 0, $roles = array()) {
  15. $url = parse_url($GLOBALS['base_url']);
  16. if ($kill) {
  17. $uids = db_select('users', 'u')
  18. ->fields('u', array('uid'))
  19. ->condition('uid', 1, '>')
  20. ->execute()
  21. ->fetchAllAssoc('uid');
  22. user_delete_multiple(array_keys($uids));
  23. drupal_set_message(format_plural(count($uids), '1 user deleted', '@count users deleted.'));
  24. }
  25. // Determine if we should create user pictures.
  26. $pic_config = FALSE;
  27. module_load_include('inc', 'system', 'image.gd');
  28. if (variable_get('user_pictures', 0) && function_exists('image_gd_check_settings') && image_gd_check_settings()) {
  29. $pic_config['path'] = variable_get('user_picture_path', 'pictures');
  30. list($pic_config['width'], $pic_config['height']) = explode('x', variable_get('user_picture_dimensions', '85x85'));
  31. }
  32. if ($num > 0) {
  33. $names = array();
  34. while (count($names) < $num) {
  35. $name = devel_generate_word(mt_rand(6, 12));
  36. $names[$name] = '';
  37. }
  38. if (empty($roles)) {
  39. $roles = array(DRUPAL_AUTHENTICATED_RID);
  40. }
  41. foreach ($names as $name => $value) {
  42. $edit = array(
  43. 'uid' => NULL,
  44. 'name' => $name,
  45. 'pass' => NULL, // No password avoids user_hash_password() which is expensive.
  46. 'mail' => $name . '@' . $url['host'],
  47. 'status' => 1,
  48. 'created' => REQUEST_TIME - mt_rand(0, $age),
  49. 'roles' => drupal_map_assoc($roles),
  50. );
  51. // Populate all core fields on behalf of field.module
  52. module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  53. $edit = (object) $edit;
  54. $edit->language = LANGUAGE_NONE;
  55. devel_generate_fields($edit, 'user', 'user');
  56. $edit = (array) $edit;
  57. $account = user_save(drupal_anonymous_user(), $edit);
  58. if ($pic_config) {
  59. // Since the image.module should scale the picture just pick an
  60. // arbitrary size that it's too big for our font.
  61. $im = imagecreatetruecolor(200, 200);
  62. // Randomize the foreground using the md5 of the user id, then invert it
  63. // for the background color so there's enough contrast to read the text.
  64. $parts = array_map('hexdec', str_split(md5($account->uid), 2));
  65. $fg = imagecolorallocate($im, $parts[1], $parts[3], $parts[5]);
  66. $bg = imagecolorallocate($im, 255 - $parts[0], 255 - $parts[1], 255 - $parts[2]);
  67. // Fill the background then print their user info.
  68. imagefill($im, 0, 0, $bg);
  69. imagestring($im, 5, 5, 5, "#" . $account->uid, $fg);
  70. imagestring($im, 5, 5, 25, $account->name, $fg);
  71. // Create an empty, managed file where we want the user's picture to
  72. // be so we can have GD overwrite it with the image.
  73. $picture_directory = variable_get('file_default_scheme', 'public') . '://' . variable_get('user_picture_path', 'pictures');
  74. file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
  75. $destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '.png');
  76. $file = file_save_data('', $destination);
  77. // GD doesn't like stream wrapped paths so convert the URI to a normal
  78. // file system path.
  79. if (isset($file) && $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri)) {
  80. imagepng($im, $wrapper->realpath());
  81. }
  82. imagedestroy($im);
  83. // Clear the cached filesize, set the owner and MIME-type then re-save
  84. // the file.
  85. clearstatcache();
  86. $file->uid = $account->uid;
  87. $file->filemime = 'image/png';
  88. $file = file_save($file);
  89. // Save the user record with the new picture.
  90. $edit = (array) $account;
  91. $edit['picture'] = $file;
  92. user_save($account, $edit);
  93. }
  94. }
  95. }
  96. drupal_set_message(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users'))));
  97. }
  98. /**
  99. * The main API function for creating content.
  100. *
  101. * See devel_generate_content_form() for the supported keys in $form_state['values'].
  102. * 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.
  103. *
  104. * @param string $form_state
  105. * @return void
  106. */
  107. function devel_generate_content($form_state) {
  108. if (!empty($form_state['values']['kill_content'])) {
  109. devel_generate_content_kill($form_state['values']);
  110. }
  111. if (count($form_state['values']['node_types'])) {
  112. // Generate nodes.
  113. devel_generate_content_pre_node($form_state['values']);
  114. $start = time();
  115. for ($i = 1; $i <= $form_state['values']['num_nodes']; $i++) {
  116. devel_generate_content_add_node($form_state['values']);
  117. if (function_exists('drush_log') && $i % drush_get_option('feedback', 1000) == 0) {
  118. $now = time();
  119. drush_log(dt('Completed !feedback nodes (!rate nodes/min)', array('!feedback' => drush_get_option('feedback', 1000), '!rate' => (drush_get_option('feedback', 1000)*60)/($now-$start))), 'ok');
  120. $start = $now;
  121. }
  122. }
  123. }
  124. devel_generate_set_message(format_plural($form_state['values']['num_nodes'], '1 node created.', 'Finished creating @count nodes'));
  125. }
  126. function devel_generate_add_comments($node, $users, $max_comments, $title_length = 8) {
  127. $num_comments = mt_rand(1, $max_comments);
  128. for ($i = 1; $i <= $num_comments; $i++) {
  129. $comment = new stdClass;
  130. $comment->nid = $node->nid;
  131. $comment->cid = NULL;
  132. $comment->name = 'devel generate';
  133. $comment->mail = 'devel_generate@example.com';
  134. $comment->timestamp = mt_rand($node->created, REQUEST_TIME);
  135. switch ($i % 3) {
  136. case 1:
  137. $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid = 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
  138. break;
  139. case 2:
  140. $comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid > 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
  141. break;
  142. default:
  143. $comment->pid = 0;
  144. }
  145. // The subject column has a max character length of 64
  146. // See bug: http://drupal.org/node/1024340
  147. $comment->subject = substr(devel_create_greeking(mt_rand(2, $title_length), TRUE), 0, 63);
  148. $comment->uid = $users[array_rand($users)];
  149. $comment->language = LANGUAGE_NONE;
  150. // Populate all core fields on behalf of field.module
  151. module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  152. devel_generate_fields($comment, 'comment', 'comment_node_' . $node->type);
  153. comment_save($comment);
  154. }
  155. }
  156. function devel_generate_vocabs($records, $maxlength = 12, $types = array('page', 'article')) {
  157. $vocs = array();
  158. // Insert new data:
  159. for ($i = 1; $i <= $records; $i++) {
  160. $voc = new stdClass();
  161. $voc->name = devel_generate_word(mt_rand(2, $maxlength));
  162. $voc->machine_name = drupal_strtolower($voc->name);
  163. $voc->description = "description of ". $voc->name;
  164. // TODO: not working
  165. $voc->nodes = array_flip(array($types[array_rand($types)]));
  166. foreach ($voc->nodes as $key => $value) {
  167. $voc->nodes[$key] = $key;
  168. }
  169. $voc->multiple = 1;
  170. $voc->required = 0;
  171. $voc->relations = 1;
  172. $voc->hierarchy = 1;
  173. $voc->weight = mt_rand(0,10);
  174. $voc->language = LANGUAGE_NONE;
  175. taxonomy_vocabulary_save($voc);
  176. $vocs[] = $voc->name;
  177. unset($voc);
  178. }
  179. return $vocs;
  180. }
  181. function devel_generate_terms($records, $vocabs, $maxlength = 12) {
  182. $terms = array();
  183. // Insert new data:
  184. $max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')->fetchField();
  185. $start = time();
  186. for ($i = 1; $i <= $records; $i++) {
  187. $term = new stdClass;
  188. switch ($i % 2) {
  189. case 1:
  190. // Set vid and vocabulary_machine_name properties.
  191. $vocab = $vocabs[array_rand($vocabs)];
  192. $term->vid = $vocab->vid;
  193. $term->vocabulary_machine_name = $vocab->machine_name;
  194. // Don't set a parent. Handled by taxonomy_save_term()
  195. // $term->parent = 0;
  196. break;
  197. default:
  198. while (TRUE) {
  199. // Keep trying to find a random parent.
  200. $candidate = mt_rand(1, $max);
  201. $query = db_select('taxonomy_term_data', 't');
  202. $query->innerJoin('taxonomy_vocabulary', 'v', 't.vid = v.vid');
  203. $parent = $query
  204. ->fields('t', array('tid', 'vid'))
  205. ->fields('v', array('machine_name'))
  206. ->condition('v.vid', array_keys($vocabs), 'IN')
  207. ->condition('t.tid', $candidate, '>=')
  208. ->range(0,1)
  209. ->execute()
  210. ->fetchAssoc();
  211. if ($parent['tid']) {
  212. break;
  213. }
  214. }
  215. $term->parent = $parent['tid'];
  216. // Slight speedup due to this property being set.
  217. $term->vocabulary_machine_name = $parent['machine_name'];
  218. $term->vid = $parent['vid'];
  219. break;
  220. }
  221. $term->name = devel_generate_word(mt_rand(2, $maxlength));
  222. $term->description = "description of ". $term->name;
  223. $term->format = filter_fallback_format();
  224. $term->weight = mt_rand(0, 10);
  225. $term->language = LANGUAGE_NONE;
  226. // Populate all core fields on behalf of field.module
  227. module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  228. devel_generate_fields($term, 'term', $term->vocabulary_machine_name);
  229. if ($status = taxonomy_term_save($term)) {
  230. $max += 1;
  231. if (function_exists('drush_log')) {
  232. $feedback = drush_get_option('feedback', 1000);
  233. if ($i % $feedback == 0) {
  234. $now = time();
  235. drush_log(dt('Completed !feedback terms (!rate terms/min)', array('!feedback' => $feedback, '!rate' => $feedback*60 / ($now-$start) )), 'ok');
  236. $start = $now;
  237. }
  238. }
  239. // Limit memory usage. Only report first 20 created terms.
  240. if ($i < 20) {
  241. $terms[] = $term->name;
  242. }
  243. unset($term);
  244. }
  245. }
  246. return $terms;
  247. }
  248. // TODO: use taxonomy_get_entries once that exists.
  249. function devel_generate_get_terms($vids) {
  250. return db_select('taxonomy_term_data', 'td')
  251. ->fields('td', array('tid'))
  252. ->condition('vid', $vids, 'IN')
  253. ->orderBy('tid', 'ASC')
  254. ->execute()
  255. ->fetchCol('tid');
  256. }
  257. function devel_generate_term_data($vocabs, $num_terms, $title_length, $kill) {
  258. if ($kill) {
  259. foreach (devel_generate_get_terms(array_keys($vocabs)) as $tid) {
  260. taxonomy_term_delete($tid);
  261. }
  262. drupal_set_message(t('Deleted existing terms.'));
  263. }
  264. $new_terms = devel_generate_terms($num_terms, $vocabs, $title_length);
  265. if (!empty($new_terms)) {
  266. drupal_set_message(t('Created the following new terms: !terms', array('!terms' => theme('item_list', array('items' => $new_terms)))));
  267. }
  268. }
  269. function devel_generate_vocab_data($num_vocab, $title_length, $kill) {
  270. if ($kill) {
  271. foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
  272. taxonomy_vocabulary_delete($vid);
  273. }
  274. drupal_set_message(t('Deleted existing vocabularies.'));
  275. }
  276. $new_vocs = devel_generate_vocabs($num_vocab, $title_length);
  277. if (!empty($new_vocs)) {
  278. drupal_set_message(t('Created the following new vocabularies: !vocs', array('!vocs' => theme('item_list', array('items' => $new_vocs)))));
  279. }
  280. }
  281. function devel_generate_menu_data($num_menus, $existing_menus, $num_links, $title_length, $link_types, $max_depth, $max_width, $kill) {
  282. // Delete menus and menu links.
  283. if ($kill) {
  284. if (module_exists('menu')) {
  285. foreach (menu_get_menus(FALSE) as $menu => $menu_title) {
  286. if (strpos($menu, 'devel-') === 0) {
  287. $menu = menu_load($menu);
  288. menu_delete($menu);
  289. }
  290. }
  291. }
  292. // Delete menu links generated by devel.
  293. $result = db_select('menu_links', 'm')
  294. ->fields('m', array('mlid'))
  295. ->condition('m.menu_name', 'devel', '<>')
  296. // Look for the serialized version of 'devel' => TRUE.
  297. ->condition('m.options', '%' . db_like('s:5:"devel";b:1') . '%', 'LIKE')
  298. ->execute();
  299. foreach ($result as $link) {
  300. menu_link_delete($link->mlid);
  301. }
  302. drupal_set_message(t('Deleted existing menus and links.'));
  303. }
  304. // Generate new menus.
  305. $new_menus = devel_generate_menus($num_menus, $title_length);
  306. if (!empty($new_menus)) {
  307. drupal_set_message(t('Created the following new menus: !menus', array('!menus' => theme('item_list', array('items' => $new_menus)))));
  308. }
  309. // Generate new menu links.
  310. $menus = $new_menus + $existing_menus;
  311. $new_links = devel_generate_links($num_links, $menus, $title_length, $link_types, $max_depth, $max_width);
  312. drupal_set_message(t('Created @count new menu links.', array('@count' => count($new_links))));
  313. }
  314. /**
  315. * Generates new menus.
  316. */
  317. function devel_generate_menus($num_menus, $title_length = 12) {
  318. $menus = array();
  319. if (!module_exists('menu')) {
  320. $num_menus = 0;
  321. }
  322. for ($i = 1; $i <= $num_menus; $i++) {
  323. $menu = array();
  324. $menu['title'] = devel_generate_word(mt_rand(2, $title_length));
  325. $menu['menu_name'] = 'devel-' . drupal_strtolower($menu['title']);
  326. $menu['description'] = t('Description of @name', array('@name' => $menu['title']));
  327. menu_save($menu);
  328. $menus[$menu['menu_name']] = $menu['title'];
  329. }
  330. return $menus;
  331. }
  332. /**
  333. * Generates menu links in a tree structure.
  334. */
  335. function devel_generate_links($num_links, $menus, $title_length, $link_types, $max_depth, $max_width) {
  336. $links = array();
  337. $menus = array_keys(array_filter($menus));
  338. $link_types = array_keys(array_filter($link_types));
  339. $nids = array();
  340. for ($i = 1; $i <= $num_links; $i++) {
  341. // Pick a random menu.
  342. $menu_name = $menus[array_rand($menus)];
  343. // Build up our link.
  344. $link = array(
  345. 'menu_name' => $menu_name,
  346. 'options' => array('devel' => TRUE),
  347. 'weight' => mt_rand(-50, 50),
  348. 'mlid' => 0,
  349. 'link_title' => devel_generate_word(mt_rand(2, $title_length)),
  350. );
  351. $link['options']['attributes']['title'] = t('Description of @title.', array('@title' => $link['link_title']));
  352. // For the first $max_width items, make first level links.
  353. if ($i <= $max_width) {
  354. $depth = 0;
  355. }
  356. else {
  357. // Otherwise, get a random parent menu depth.
  358. $depth = mt_rand(1, $max_depth - 1);
  359. }
  360. // Get a random parent link from the proper depth.
  361. do {
  362. $link['plid'] = db_select('menu_links', 'm')
  363. ->fields('m', array('mlid'))
  364. ->condition('m.menu_name', $menus, 'IN')
  365. ->condition('m.depth', $depth)
  366. ->range(0, 1)
  367. ->orderRandom()
  368. ->execute()
  369. ->fetchField();
  370. $depth--;
  371. } while (!$link['plid'] && $depth > 0);
  372. if (!$link['plid']) {
  373. $link['plid'] = 0;
  374. }
  375. $link_type = array_rand($link_types);
  376. switch ($link_types[$link_type]) {
  377. case 'node':
  378. // Grab a random node ID.
  379. $select = db_select('node', 'n')
  380. ->fields('n', array('nid', 'title'))
  381. ->condition('n.status', 1)
  382. ->range(0, 1)
  383. ->orderRandom();
  384. // Don't put a node into the menu twice.
  385. if (!empty($nids[$menu_name])) {
  386. $select->condition('n.nid', $nids[$menu_name], 'NOT IN');
  387. }
  388. $node = $select->execute()->fetchAssoc();
  389. if (isset($node['nid'])) {
  390. $nids[$menu_name][] = $node['nid'];
  391. $link['link_path'] = $link['router_path'] = 'node/' . $node['nid'];
  392. $link['link_title'] = $node['title'];
  393. break;
  394. }
  395. case 'external':
  396. $link['link_path'] = 'http://www.example.com/';
  397. break;
  398. case 'front':
  399. $link['link_path'] = $link['router_path'] = '<front>';
  400. break;
  401. default:
  402. $link['devel_link_type'] = $link_type;
  403. break;
  404. }
  405. menu_link_save($link);
  406. $links[$link['mlid']] = $link['link_title'];
  407. }
  408. return $links;
  409. }
  410. function devel_generate_word($length){
  411. mt_srand((double)microtime()*1000000);
  412. $vowels = array("a", "e", "i", "o", "u");
  413. $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
  414. "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
  415. $num_vowels = count($vowels);
  416. $num_cons = count($cons);
  417. $word = '';
  418. while(strlen($word) < $length){
  419. $word .= $cons[mt_rand(0, $num_cons - 1)] . $vowels[mt_rand(0, $num_vowels - 1)];
  420. }
  421. return substr($word, 0, $length);
  422. }
  423. function devel_create_content($type = NULL) {
  424. $nparas = mt_rand(1,12);
  425. $type = empty($type) ? mt_rand(0,3) : $type;
  426. $output = "";
  427. switch($type % 3) {
  428. // MW: This appears undesireable. Was giving <p> in text fields
  429. // case 1: // html
  430. // for ($i = 1; $i <= $nparas; $i++) {
  431. // $output .= devel_create_para(mt_rand(10,60),1);
  432. // }
  433. // break;
  434. //
  435. // case 2: // brs only
  436. // for ($i = 1; $i <= $nparas; $i++) {
  437. // $output .= devel_create_para(mt_rand(10,60),2);
  438. // }
  439. // break;
  440. default: // plain text
  441. for ($i = 1; $i <= $nparas; $i++) {
  442. $output .= devel_create_para(mt_rand(10,60)) ."\n\n";
  443. }
  444. }
  445. return $output;
  446. }
  447. function devel_create_para($words, $type = 0) {
  448. $output = '';
  449. switch ($type) {
  450. case 1:
  451. $output .= "<p>" . devel_create_greeking($words) . "</p>";
  452. break;
  453. case 2:
  454. $output .= devel_create_greeking($words) . "<br />";
  455. break;
  456. default:
  457. $output .= devel_create_greeking($words);
  458. }
  459. return $output;
  460. }
  461. function devel_create_greeking($word_count, $title = FALSE) {
  462. $dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan",
  463. "acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo",
  464. "appellatio", "aptent", "at", "augue", "autem", "bene", "blandit",
  465. "brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis",
  466. "commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui",
  467. "damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor",
  468. "dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat",
  469. "eros", "esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci",
  470. "exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino",
  471. "genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic",
  472. "humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto",
  473. "importunus", "imputo", "in", "incassum", "inhibeo", "interdico",
  474. "iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet",
  475. "lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus",
  476. "luptatum", "macto", "magna", "mauris", "melior", "metuo", "meus",
  477. "minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh",
  478. "nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo",
  479. "occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus",
  480. "patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum",
  481. "populus", "praemitto", "praesent", "premo", "probo", "proprius",
  482. "quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis",
  483. "ratis", "refero", "refoveo", "roto", "rusticus", "saepius",
  484. "sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis",
  485. "singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen",
  486. "tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis",
  487. "typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam",
  488. "utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit",
  489. "veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico",
  490. "virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo",
  491. "zelus");
  492. $dictionary_flipped = array_flip($dictionary);
  493. $greeking = '';
  494. if (!$title) {
  495. $words_remaining = $word_count;
  496. while ($words_remaining > 0) {
  497. $sentence_length = mt_rand(3, 10);
  498. $words = array_rand($dictionary_flipped, $sentence_length);
  499. $sentence = implode(' ', $words);
  500. $greeking .= ucfirst($sentence) . '. ';
  501. $words_remaining -= $sentence_length;
  502. }
  503. }
  504. else {
  505. // Use slightly different method for titles.
  506. $words = array_rand($dictionary_flipped, $word_count);
  507. $words = is_array($words) ? implode(' ', $words) : $words;
  508. $greeking = ucwords($words);
  509. }
  510. // Work around possible php garbage collection bug. Without an unset(), this
  511. // function gets very expensive over many calls (php 5.2.11).
  512. unset($dictionary, $dictionary_flipped);
  513. return trim($greeking);
  514. }
  515. function devel_generate_add_terms(&$node) {
  516. $vocabs = taxonomy_get_vocabularies($node->type);
  517. foreach ($vocabs as $vocab) {
  518. $sql = "SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY RAND()";
  519. $result = db_query_range($sql, 0, 5 , array(':vid' => $vocab->vid));
  520. foreach($result as $row) {
  521. $node->taxonomy[] = $row->tid;
  522. if (!$vocab->multiple) {
  523. break;
  524. }
  525. }
  526. }
  527. }
  528. function devel_get_users() {
  529. $users = array();
  530. $result = db_query_range("SELECT uid FROM {users}", 0, 50);
  531. foreach ($result as $record) {
  532. $users[] = $record->uid;
  533. }
  534. return $users;
  535. }
  536. /**
  537. * Generate statistics information for a node.
  538. *
  539. * @param $node
  540. * A node object.
  541. */
  542. function devel_generate_add_statistics($node) {
  543. $statistic = array(
  544. 'nid' => $node->nid,
  545. 'totalcount' => mt_rand(0, 500),
  546. 'timestamp' => REQUEST_TIME - mt_rand(0, $node->created),
  547. );
  548. $statistic['daycount'] = mt_rand(0, $statistic['totalcount']);
  549. db_insert('node_counter')->fields($statistic)->execute();
  550. }
  551. /**
  552. * Handle the devel_generate_content_form request to kill all of the content.
  553. * This is used by both the batch and non-batch branches of the code.
  554. *
  555. * @param $num
  556. * array of options obtained from devel_generate_content_form.
  557. */
  558. function devel_generate_content_kill($values) {
  559. $results = db_select('node', 'n')
  560. ->fields('n', array('nid'))
  561. ->condition('type', $values['node_types'], 'IN')
  562. ->execute();
  563. foreach ($results as $result) {
  564. $nids[] = $result->nid;
  565. }
  566. if (!empty($nids)) {
  567. node_delete_multiple($nids);
  568. drupal_set_message(t('Deleted %count nodes.', array('%count' => count($nids))));
  569. }
  570. }
  571. /**
  572. * Pre-process the devel_generate_content_form request. This is needed so
  573. * batch api can get the list of users once. This is used by both the batch
  574. * and non-batch branches of the code.
  575. *
  576. * @param $num
  577. * array of options obtained from devel_generate_content_form.
  578. */
  579. function devel_generate_content_pre_node(&$results) {
  580. // Get user id.
  581. $users = devel_get_users();
  582. $users = array_merge($users, array('0'));
  583. $results['users'] = $users;
  584. }
  585. /**
  586. * Create one node. Used by both batch and non-batch code branches.
  587. *
  588. * @param $num
  589. * array of options obtained from devel_generate_content_form.
  590. */
  591. function devel_generate_content_add_node(&$results) {
  592. $node = new stdClass();
  593. $node->nid = NULL;
  594. // Insert new data:
  595. $node->type = array_rand($results['node_types']);
  596. node_object_prepare($node);
  597. $users = $results['users'];
  598. $node->uid = $users[array_rand($users)];
  599. $type = node_type_get_type($node);
  600. $node->title = $type->has_title ? devel_create_greeking(mt_rand(2, $results['title_length']), TRUE) : '';
  601. $node->revision = mt_rand(0,1);
  602. $node->promote = mt_rand(0, 1);
  603. // Avoid NOTICE.
  604. if (!isset($results['time_range'])) {
  605. $results['time_range'] = 0;
  606. }
  607. devel_generate_set_language($results, $node);
  608. $node->created = REQUEST_TIME - mt_rand(0, $results['time_range']);
  609. // A flag to let hook_nodeapi() implementations know that this is a generated node.
  610. $node->devel_generate = $results;
  611. // Populate all core fields on behalf of field.module
  612. module_load_include('inc', 'devel_generate', 'devel_generate.fields');
  613. devel_generate_fields($node, 'node', $node->type);
  614. // See devel_generate_nodeapi() for actions that happen before and after this save.
  615. node_save($node);
  616. }
  617. /*
  618. * Populate $object->language based on $results
  619. */
  620. function devel_generate_set_language($results, $object) {
  621. if (isset($results['add_language'])) {
  622. $languages = $results['add_language'];
  623. $object->language = $languages[array_rand($languages)];
  624. }
  625. else {
  626. $default = language_default('language');
  627. $object->language = $default == 'en' ? LANGUAGE_NONE : $default;
  628. }
  629. }