devel_generate.drush.inc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * @file
  4. * Generate content, taxonomy, menu, and users via drush framework.
  5. */
  6. /**
  7. * Implementation of hook_drush_command().
  8. */
  9. function devel_generate_drush_command() {
  10. $items['generate-users'] = array(
  11. 'description' => 'Create users.',
  12. 'arguments' => array(
  13. 'number_users' => 'Number of users to generate.',
  14. ),
  15. 'options' => array(
  16. 'kill' => 'Delete all users before generating new ones.',
  17. 'roles' => 'A comma delimited list of role IDs which should be granted to the new users. No need to specify authenticated user role.',
  18. ),
  19. 'aliases' => array('genu'),
  20. );
  21. $items['generate-terms'] = array(
  22. 'description' => 'Create terms in specified vocabulary.',
  23. 'arguments' => array(
  24. 'machine_name' => 'Vocabulary machine name into which new terms will be inserted.',
  25. 'number_terms' => 'Number of terms to insert. Defaults to 10.',
  26. ),
  27. 'options' => array(
  28. 'kill' => 'Delete all terms in specified vocabulary before generating.',
  29. 'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
  30. ),
  31. 'aliases' => array('gent'),
  32. );
  33. $items['generate-vocabs'] = array(
  34. 'description' => 'Create vocabularies.',
  35. 'arguments' => array(
  36. 'num_vocabs' => 'Number of vocabularies to create. Defaults to 1.',
  37. ),
  38. 'options' => array(
  39. 'kill' => 'Delete all vocabularies before generating.',
  40. ),
  41. 'aliases' => array('genv'),
  42. );
  43. $items['generate-content'] = array(
  44. 'description' => 'Create content.',
  45. 'drupal dependencies' => array('devel_generate'),
  46. 'arguments' => array(
  47. 'number_nodes' => 'Number of nodes to generate.',
  48. 'maximum_comments' => 'Maximum number of comments to generate.',
  49. ),
  50. 'options' => array(
  51. 'kill' => 'Delete all content before generating new content.',
  52. 'types' => 'A comma delimited list of content types to create. Defaults to page,article.',
  53. 'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
  54. 'skip-fields' => 'A comma delimited list of fields to omit when generating random values',
  55. 'languages' => 'A comma-separated list of language codes',
  56. ),
  57. 'aliases' => array('genc'),
  58. );
  59. $items['generate-menus'] = array(
  60. 'description' => 'Create menus and menu items.',
  61. 'drupal dependencies' => array('devel_generate'), // Remove these once devel.module is moved down a directory. http://drupal.org/node/925246
  62. 'arguments' => array(
  63. 'number_menus' => 'Number of menus to generate. Defaults to 2.',
  64. 'number_links' => 'Number of links to generate. Defaults to 50.',
  65. 'max_depth' => 'Max link depth. Defaults to 3',
  66. 'max_width' => 'Max width of first level of links. Defaults to 8.',
  67. ),
  68. 'options' => array(
  69. 'kill' => 'Delete all previously generated menus and links before generating new menus and links.',
  70. ),
  71. 'aliases' => array('genm'),
  72. );
  73. return $items;
  74. }
  75. /**
  76. * Command callback. Generate a number of users.
  77. */
  78. function drush_devel_generate_users($num_users = NULL) {
  79. if (drush_generate_is_number($num_users) == FALSE) {
  80. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of users.'));
  81. }
  82. drush_generate_include_devel();
  83. $roles = drush_get_option('roles') ? explode(',', drush_get_option('roles')) : array();
  84. devel_create_users($num_users, drush_get_option('kill'), 0, $roles);
  85. drush_log(t('Generated @number users.', array('@number' => $num_users)), 'success');
  86. }
  87. /**
  88. * Command callback. Generate a number of terms in given vocabs.
  89. */
  90. function drush_devel_generate_terms($vname = NULL, $num_terms = 10) {
  91. // Try to convert machine name to a vocab ID
  92. if (!$vocab = taxonomy_vocabulary_machine_name_load($vname)) {
  93. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid vocabulary name: !name', array('!name' => $vname)));
  94. }
  95. if (drush_generate_is_number($num_terms) == FALSE) {
  96. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of terms: !num', array('!num' => $num_terms)));
  97. }
  98. drush_generate_include_devel();
  99. $vocabs[$vocab->vid] = $vocab;
  100. devel_generate_term_data($vocabs, $num_terms, '12', drush_get_option('kill'));
  101. drush_log(dt('Generated @num_terms terms.', array('@num_terms' => $num_terms)), 'success');
  102. }
  103. /**
  104. * Command callback. Generate a number of vocabularies.
  105. */
  106. function drush_devel_generate_vocabs($num_vocab = 1) {
  107. if (drush_generate_is_number($num_vocab) == FALSE) {
  108. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of vocabularies: !num.', array('!num' => $num_vocab)));
  109. }
  110. drush_generate_include_devel();
  111. devel_generate_vocab_data($num_vocab, '12', drush_get_option('kill'));
  112. drush_log(dt('Generated @num_vocab vocabularies.', array('@num_vocab' => $num_vocab)), 'success');
  113. }
  114. /**
  115. * Command callback. Generate a number of nodes and comments.
  116. */
  117. function drush_devel_generate_content($num_nodes = NULL, $max_comments = NULL) {
  118. if (drush_generate_is_number($num_nodes) == FALSE) {
  119. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of nodes'));
  120. }
  121. if (!empty($max_comments) && drush_generate_is_number($max_comments) == FALSE) {
  122. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of comments.'));
  123. }
  124. $add_language = drush_get_option('languages');
  125. if (!empty($add_language)) {
  126. $add_language = explode(',', str_replace(' ', '', $add_language));
  127. // Intersect with the enabled languages to make sure the language args
  128. // passed are actually enabled.
  129. $values['values']['add_language'] = array_intersect($add_language, array_keys(locale_language_list()));
  130. }
  131. // Load user 1; is needed for creating *published* comments.
  132. if ($max_comments) {
  133. global $user;
  134. $user_one = user_load(1);
  135. $user = $user_one;
  136. drupal_save_session(FALSE);
  137. }
  138. $values['values']['kill_content'] = drush_get_option('kill');
  139. $values['values']['title_length'] = 6;
  140. $values['values']['num_nodes'] = $num_nodes;
  141. $values['values']['max_comments'] = $max_comments;
  142. $values['values']['node_types'] = drupal_map_assoc(explode(',', drush_get_option('types', 'page,article')));
  143. drush_generate_include_devel();
  144. devel_generate_content($values);
  145. drush_log(t('Generated @num_nodes nodes, @max_comments comments (or less) per node.', array('@num_nodes' => (int)$num_nodes, '@max_comments' => (int)$max_comments)), 'success');
  146. }
  147. /**
  148. * Command callback. Generate a number of menus and menu links.
  149. */
  150. function drush_devel_generate_menus($number_menus = 2, $number_links = 50, $max_depth = 3, $max_width = 8) {
  151. if (drush_generate_is_number($number_menus) == FALSE) {
  152. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of menus'));
  153. }
  154. if (drush_generate_is_number($number_links) == FALSE) {
  155. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of links'));
  156. }
  157. if (drush_generate_is_number($max_depth) == FALSE || $max_depth > 9 || $max_depth < 1) {
  158. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum link depth. Use a value between 1 and 9'));
  159. }
  160. if (drush_generate_is_number($max_width) == FALSE || $max_width < 1) {
  161. return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum menu width. Use a positive numeric value.'));
  162. }
  163. global $user;
  164. $user_one = user_load(1);
  165. $user = $user_one;
  166. drupal_save_session(FALSE);
  167. $kill = drush_get_option('kill');
  168. drush_generate_include_devel();
  169. $link_types = drupal_map_assoc(array('node', 'front', 'external'));
  170. devel_generate_menu_data($number_menus, array(), $number_links, 12, $link_types, $max_depth, $max_width, $kill);
  171. drush_log(t('Generated @number_menus menus, @number_links links.', array('@number_menus' => (int)$number_menus, '@number_links' => (int)$number_links)), 'success');
  172. }
  173. //////////////////////////////////////////////////////////////////////////////
  174. // Helper functions
  175. // Verify if param is a number.
  176. function drush_generate_is_number($number) {
  177. if ($number == NULL) return FALSE;
  178. if (!is_numeric($number)) return FALSE;
  179. return TRUE;
  180. }
  181. // Include devel_generate.inc.
  182. function drush_generate_include_devel() {
  183. $path = drupal_get_path('module', 'devel_generate');
  184. require_once($path .'/devel_generate.inc');
  185. }