devel_generate.module 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  1. <?php
  2. /**
  3. * Implements hook_menu().
  4. */
  5. function devel_generate_menu() {
  6. $items = array();
  7. $items['admin/config/development/generate/user'] = array(
  8. 'title' => 'Generate users',
  9. 'description' => 'Generate a given number of users. Optionally delete current users.',
  10. 'page callback' => 'drupal_get_form',
  11. 'page arguments' => array('devel_generate_users_form'),
  12. 'access arguments' => array('administer users'),
  13. );
  14. $items['admin/config/development/generate/content'] = array(
  15. 'title' => 'Generate content',
  16. 'description' => 'Generate a given number of nodes and comments. Optionally delete current items.',
  17. 'page callback' => 'drupal_get_form',
  18. 'page arguments' => array('devel_generate_content_form'),
  19. 'access arguments' => array('administer nodes'),
  20. );
  21. if (module_exists('taxonomy')) {
  22. $items['admin/config/development/generate/taxonomy'] = array(
  23. 'title' => 'Generate terms',
  24. 'description' => 'Generate a given number of terms. Optionally delete current terms.',
  25. 'page callback' => 'drupal_get_form',
  26. 'page arguments' => array('devel_generate_term_form'),
  27. 'access arguments' => array('administer taxonomy'),
  28. );
  29. $items['admin/config/development/generate/vocabs'] = array(
  30. 'title' => 'Generate vocabularies',
  31. 'description' => 'Generate a given number of vocabularies. Optionally delete current vocabularies.',
  32. 'page callback' => 'drupal_get_form',
  33. 'page arguments' => array('devel_generate_vocab_form'),
  34. 'access arguments' => array('administer taxonomy'),
  35. );
  36. }
  37. $items['admin/config/development/generate/menu'] = array(
  38. 'title' => 'Generate menus',
  39. 'description' => 'Generate a given number of menus and menu links. Optionally delete current menus.',
  40. 'page callback' => 'drupal_get_form',
  41. 'page arguments' => array('devel_generate_menu_form'),
  42. 'access arguments' => array('administer menu'),
  43. );
  44. return $items;
  45. }
  46. /**
  47. * Generates users using FormAPI.
  48. */
  49. function devel_generate_users_form() {
  50. $form['num'] = array(
  51. '#type' => 'textfield',
  52. '#title' => t('How many users would you like to generate?'),
  53. '#default_value' => 50,
  54. '#size' => 10,
  55. );
  56. $form['kill_users'] = array(
  57. '#type' => 'checkbox',
  58. '#title' => t('Delete all users (except user id 1) before generating new users.'),
  59. '#default_value' => FALSE,
  60. );
  61. $options = user_roles(TRUE);
  62. unset($options[DRUPAL_AUTHENTICATED_RID]);
  63. $form['roles'] = array(
  64. '#type' => 'checkboxes',
  65. '#title' => t('Which roles should the users receive?'),
  66. '#description' => t('Users always receive the <em>authenticated user</em> role.'),
  67. '#options' => $options,
  68. );
  69. $options = array(1 => t('Now'));
  70. foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
  71. $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
  72. }
  73. $form['time_range'] = array(
  74. '#type' => 'select',
  75. '#title' => t('How old should user accounts be?'),
  76. '#description' => t('User ages will be distributed randomly from the current time, back to the selected time.'),
  77. '#options' => $options,
  78. '#default_value' => 604800,
  79. );
  80. $form['submit'] = array(
  81. '#type' => 'submit',
  82. '#value' => t('Generate'),
  83. );
  84. return $form;
  85. }
  86. /**
  87. * FormAPI submission to generate users.
  88. */
  89. function devel_generate_users_form_submit($form_id, &$form_state) {
  90. module_load_include('inc', 'devel_generate');
  91. $values = $form_state['values'];
  92. devel_create_users($values['num'], $values['kill_users'], $values['time_range'], $values['roles']);
  93. }
  94. /**
  95. * Generates nodes using FormAPI.
  96. */
  97. function devel_generate_content_form() {
  98. $options = array();
  99. if (module_exists('content')) {
  100. $types = content_types();
  101. foreach ($types as $type) {
  102. $warn = '';
  103. if (count($type['fields'])) {
  104. $warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
  105. }
  106. $options[$type['type']] = t($type['name']). $warn;
  107. }
  108. }
  109. else {
  110. $types = node_type_get_types();
  111. $suffix = '';
  112. foreach ($types as $type) {
  113. if (module_exists('comment')) {
  114. $default = variable_get('comment_' . $type->type, COMMENT_NODE_OPEN);
  115. $map = array(t('Hidden'), t('Closed'), t('Open'));
  116. $suffix = '<small>. ' . t('Comments: ') . $map[$default]. '</small>';
  117. }
  118. $options[$type->type] = t($type->name) . $suffix;
  119. }
  120. }
  121. // we cannot currently generate valid polls.
  122. unset($options['poll']);
  123. if (empty($options)) {
  124. drupal_set_message(t('You do not have any content types that can be generated. <a href="@create-type">Go create a new content type</a> already!</a>', array('@create-type' => url('admin/structure/types/add'))), 'error', FALSE);
  125. return;
  126. }
  127. $form['node_types'] = array(
  128. '#type' => 'checkboxes',
  129. '#title' => t('Content types'),
  130. '#options' => $options,
  131. '#default_value' => array_keys($options),
  132. );
  133. if (module_exists('checkall')) $form['node_types']['#checkall'] = TRUE;
  134. $form['kill_content'] = array(
  135. '#type' => 'checkbox',
  136. '#title' => t('<strong>Delete all content</strong> in these content types before generating new content.'),
  137. '#default_value' => FALSE,
  138. );
  139. $form['num_nodes'] = array(
  140. '#type' => 'textfield',
  141. '#title' => t('How many nodes would you like to generate?'),
  142. '#default_value' => 50,
  143. '#size' => 10,
  144. );
  145. $options = array(1 => t('Now'));
  146. foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
  147. $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
  148. }
  149. $form['time_range'] = array(
  150. '#type' => 'select',
  151. '#title' => t('How far back in time should the nodes be dated?'),
  152. '#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
  153. '#options' => $options,
  154. '#default_value' => 604800,
  155. );
  156. $form['max_comments'] = array(
  157. '#type' => module_exists('comment') ? 'textfield' : 'value',
  158. '#title' => t('Maximum number of comments per node.'),
  159. '#description' => t('You must also enable comments for the content types you are generating. Note that some nodes will randomly receive zero comments. Some will receive the max.'),
  160. '#default_value' => 0,
  161. '#size' => 3,
  162. '#access' => module_exists('comment'),
  163. );
  164. $form['title_length'] = array(
  165. '#type' => 'textfield',
  166. '#title' => t('Max word length of titles'),
  167. '#default_value' => 4,
  168. '#size' => 10,
  169. );
  170. $form['add_alias'] = array(
  171. '#type' => 'checkbox',
  172. '#disabled' => !module_exists('path'),
  173. '#description' => t('Requires path.module'),
  174. '#title' => t('Add an url alias for each node.'),
  175. '#default_value' => FALSE,
  176. );
  177. $form['add_statistics'] = array(
  178. '#type' => 'checkbox',
  179. '#title' => t('Add statistics for each node (node_counter table).'),
  180. '#default_value' => TRUE,
  181. '#access' => module_exists('statistics'),
  182. );
  183. unset($options);
  184. $options[LANGUAGE_NONE] = t('Language neutral');
  185. if (module_exists('locale')) {
  186. $options += locale_language_list();
  187. }
  188. $form['add_language'] = array(
  189. '#type' => 'select',
  190. '#title' => t('Set language on nodes'),
  191. '#multiple' => TRUE,
  192. '#disabled' => !module_exists('locale'),
  193. '#description' => t('Requires locale.module'),
  194. '#options' => $options,
  195. '#default_value' => array(LANGUAGE_NONE),
  196. );
  197. $form['submit'] = array(
  198. '#type' => 'submit',
  199. '#value' => t('Generate'),
  200. );
  201. $form['#redirect'] = FALSE;
  202. return $form;
  203. }
  204. /**
  205. * FormAPI submission to generate nodes.
  206. */
  207. function devel_generate_content_form_submit($form_id, &$form_state) {
  208. module_load_include('inc', 'devel_generate', 'devel_generate');
  209. $form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
  210. if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['max_comments'] <= 10) {
  211. module_load_include('inc', 'devel_generate');
  212. devel_generate_content($form_state);
  213. }
  214. else {
  215. module_load_include('inc', 'devel_generate', 'devel_generate_batch');
  216. devel_generate_batch_content($form_state);
  217. }
  218. }
  219. /**
  220. * Generates taxonomy terms using FormAPI.
  221. */
  222. function devel_generate_term_form() {
  223. $options = array();
  224. foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
  225. $options[$vid] = $vocab->machine_name;
  226. }
  227. $form['vids'] = array(
  228. '#type' => 'select',
  229. '#multiple' => TRUE,
  230. '#title' => t('Vocabularies'),
  231. '#required' => TRUE,
  232. '#options' => $options,
  233. '#description' => t('Restrict terms to these vocabularies.'),
  234. );
  235. $form['num_terms'] = array(
  236. '#type' => 'textfield',
  237. '#title' => t('Number of terms?'),
  238. '#default_value' => 10,
  239. '#size' => 10,
  240. );
  241. $form['title_length'] = array(
  242. '#type' => 'textfield',
  243. '#title' => t('Max word length of term names'),
  244. '#default_value' => 12,
  245. '#size' => 10,
  246. );
  247. $form['kill_taxonomy'] = array(
  248. '#type' => 'checkbox',
  249. '#title' => t('Delete existing terms in specified vocabularies before generating new terms.'),
  250. '#default_value' => FALSE,
  251. );
  252. $form['submit'] = array(
  253. '#type' => 'submit',
  254. '#value' => t('Generate'),
  255. );
  256. return $form;
  257. }
  258. /**
  259. * Generates taxonomy vocabularies using FormAPI.
  260. */
  261. function devel_generate_vocab_form() {
  262. $form['num_vocabs'] = array(
  263. '#type' => 'textfield',
  264. '#title' => t('Number of vocabularies?'),
  265. '#default_value' => 1,
  266. '#size' => 10,
  267. );
  268. $form['title_length'] = array(
  269. '#type' => 'textfield',
  270. '#title' => t('Max word length of vocabulary names'),
  271. '#default_value' => 12,
  272. '#size' => 10,
  273. );
  274. $form['kill_taxonomy'] = array(
  275. '#type' => 'checkbox',
  276. '#title' => t('Delete existing vocabularies before generating new ones.'),
  277. '#default_value' => FALSE,
  278. );
  279. $form['submit'] = array(
  280. '#type' => 'submit',
  281. '#value' => t('Generate'),
  282. );
  283. return $form;
  284. }
  285. /**
  286. * FormAPI submission to generate taxonomy terms.
  287. */
  288. function devel_generate_term_form_submit($form_id, &$form_state) {
  289. module_load_include('inc', 'devel_generate');
  290. $vocabs = taxonomy_vocabulary_load_multiple($form_state['values']['vids']);
  291. devel_generate_term_data($vocabs, $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
  292. }
  293. /**
  294. * FormAPI submission to generate taxonomy vocabularies.
  295. */
  296. function devel_generate_vocab_form_submit($form_id, &$form_state) {
  297. module_load_include('inc', 'devel_generate');
  298. devel_generate_vocab_data($form_state['values']['num_vocabs'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
  299. }
  300. /**
  301. * Inserts nodes properly based on generation options.
  302. *
  303. * @param $node
  304. * The base node created on submit. Inspects $node->devel_generate.
  305. */
  306. function devel_generate_node_insert($node) {
  307. if (isset($node->devel_generate)) {
  308. $results = $node->devel_generate;
  309. if (!empty($results['max_comments']) && $node->comment >= COMMENT_NODE_OPEN) {
  310. devel_generate_add_comments($node, $results['users'], $results['max_comments'], $results['title_length']);
  311. }
  312. // Add an url alias. Cannot happen before save because we don't know the nid.
  313. if (!empty($results['add_alias'])) {
  314. $path = array(
  315. 'source' => 'node/' . $node->nid,
  316. 'alias' => 'node-' . $node->nid . '-' . $node->type,
  317. );
  318. path_save($path);
  319. }
  320. // Add node statistics.
  321. if (!empty($results['add_statistics']) && module_exists('statistics')) {
  322. devel_generate_add_statistics($node);
  323. }
  324. }
  325. }
  326. /**
  327. * Set a message for either drush or the web interface.
  328. *
  329. * @param $msg
  330. * The message to display.
  331. * @param $type
  332. * The message type, as defined by drupal_set_message().
  333. *
  334. * @return
  335. * Context-appropriate message output.
  336. */
  337. function devel_generate_set_message($msg, $type = 'status') {
  338. $function = function_exists('drush_log') ? 'drush_log' : 'drupal_set_message';
  339. $function($msg, $type);
  340. }
  341. /**
  342. * Generates menus using FormAPI.
  343. */
  344. function devel_generate_menu_form() {
  345. $menu_enabled = module_exists('menu');
  346. if ($menu_enabled) {
  347. $menus = array('__new-menu__' => t('Create new menu(s)')) + menu_get_menus();
  348. }
  349. else {
  350. $menus = menu_list_system_menus();
  351. }
  352. $form['existing_menus'] = array(
  353. '#type' => 'checkboxes',
  354. '#title' => t('Generate links for these menus'),
  355. '#options' => $menus,
  356. '#default_value' => array('__new-menu__'),
  357. '#required' => TRUE,
  358. );
  359. if ($menu_enabled) {
  360. $form['num_menus'] = array(
  361. '#type' => 'textfield',
  362. '#title' => t('Number of new menus to create'),
  363. '#default_value' => 2,
  364. '#size' => 10,
  365. '#states' => array(
  366. 'visible' => array(
  367. ':input[name=existing_menus[__new-menu__]]' => array('checked' => TRUE),
  368. ),
  369. ),
  370. );
  371. }
  372. $form['num_links'] = array(
  373. '#type' => 'textfield',
  374. '#title' => t('Number of links to generate'),
  375. '#default_value' => 50,
  376. '#size' => 10,
  377. '#required' => TRUE,
  378. );
  379. $form['title_length'] = array(
  380. '#type' => 'textfield',
  381. '#title' => t('Max word length of menu and menu link names'),
  382. '#default_value' => 12,
  383. '#size' => 10,
  384. '#required' => TRUE,
  385. );
  386. $form['link_types'] = array(
  387. '#type' => 'checkboxes',
  388. '#title' => t('Types of links to generate'),
  389. '#options' => array(
  390. 'node' => t('Nodes'),
  391. 'front' => t('Front page'),
  392. 'external' => t('External'),
  393. ),
  394. '#default_value' => array('node', 'front', 'external'),
  395. '#required' => TRUE,
  396. );
  397. $form['max_depth'] = array(
  398. '#type' => 'select',
  399. '#title' => t('Maximum link depth'),
  400. '#options' => range(0, MENU_MAX_DEPTH),
  401. '#default_value' => floor(MENU_MAX_DEPTH / 2),
  402. '#required' => TRUE,
  403. );
  404. unset($form['max_depth']['#options'][0]);
  405. $form['max_width'] = array(
  406. '#type' => 'textfield',
  407. '#title' => t('Maximum menu width'),
  408. '#default_value' => 6,
  409. '#size' => 10,
  410. '#description' => t("Limit the width of the generated menu's first level of links to a certain number of items."),
  411. '#required' => TRUE,
  412. );
  413. $form['kill'] = array(
  414. '#type' => 'checkbox',
  415. '#title' => t('Delete existing custom generated menus and menu links before generating new ones.'),
  416. '#default_value' => FALSE,
  417. );
  418. $form['submit'] = array(
  419. '#type' => 'submit',
  420. '#value' => t('Generate'),
  421. );
  422. return $form;
  423. }
  424. /**
  425. * FormAPI submission to generate menus.
  426. */
  427. function devel_generate_menu_form_submit($form_id, &$form_state) {
  428. // If the create new menus checkbox is off, set the number of new menus to 0.
  429. if (!isset($form_state['values']['existing_menus']['__new-menu__']) || !$form_state['values']['existing_menus']['__new-menu__']) {
  430. $form_state['values']['num_menus'] = 0;
  431. }
  432. module_load_include('inc', 'devel_generate');
  433. devel_generate_menu_data($form_state['values']['num_menus'], $form_state['values']['existing_menus'], $form_state['values']['num_links'], $form_state['values']['title_length'], $form_state['values']['link_types'], $form_state['values']['max_depth'], $form_state['values']['max_width'], $form_state['values']['kill']);
  434. }