Remove old comment.
[project/devel.git] / devel_generate.drush.inc
CommitLineData
eb0dd8a4 1<?php
2// $Id$
3
4/**
5 * @file
6 * Generate content, taxonomy, and users via drush framework.
7 */
8
9/**
10 * Implementation of hook_drush_help().
11 */
12function devel_generate_drush_help($section) {
13 switch ($section) {
14 case 'drush:generate users':
828df1d7 15 return dt('Usage: drush generate users <number_users>. Specify --kill if you want to delete the users first.');
eb0dd8a4 16 case 'drush:generate taxonomy':
828df1d7 17 return dt('Usage: drush generate tax <number_vocab> <number_terms>. Specify --kill if you want to delete all taxonomy first.');
eb0dd8a4 18 case 'drush:generate content':
828df1d7 19 return dt('Usage: drush generate content <number_nodes> <number_comments>. Specify --kill if you want to delete all content first.');
eb0dd8a4 20 }
21}
22
23/**
24 * Implementation of hook_drush_command().
25 */
26function devel_generate_drush_command() {
27 $items['generate users'] = array(
28 'callback' => 'drush_generate_users',
504bb2ef 29 'description' => 'Creates users.',
30 'arguments' => array(
828df1d7 31 'number_users' => 'Number of users to generate.',
504bb2ef 32 ),
33 'options' => array(
34 'kill' => 'Specify \'kill\' to delete all users before generating new ones.',
0e6117de 35 'roles' => 'A comma delimited list of role IDs which should be granted to the new users. No need to specify authenticated user role.',
504bb2ef 36 ),
eb0dd8a4 37 );
38 $items['generate taxonomy'] = array(
39 'callback' => 'drush_generate_tax',
504bb2ef 40 'description' => 'Creates taxonomy.',
41 'arguments' => array(
42 'n' => 'Vocabulary ID into which new terms will be inserted.',
43 ),
44 'options' => array(
45 'kill' => 'Specify \'kill\' to delete all terms in specified vocab before generating.'
46 ),
47
eb0dd8a4 48 );
49 $items['generate content'] = array(
50 'callback' => 'drush_generate_nodescontent',
504bb2ef 51 'description' => 'Creates content.',
47332544 52 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_LOGIN, // Various D7 code assumes we have a uid.
f0713b0e 53 'drupal dependencies' => array('devel_generate'),
504bb2ef 54 'arguments' => array(
828df1d7 55 'number_nodes' => 'Number of nodes to generate.',
f0713b0e 56 'maximum_comments' => 'Maximum number of comments to generate.',
504bb2ef 57 ),
58 'options' => array(
828df1d7 59 'kill' => 'Specify \'kill\' to delete all content before generating new content.',
60 'types' => 'A comma delimited list of content types to create. Defaults to page,article.',
f0713b0e 61 'add-upload' => 'Add an attached file to generated nodes.',
504bb2ef 62 ),
eb0dd8a4 63 );
64 return $items;
65}
66
67
68/**
69 * Command callback. Generate a number of users.
70 */
504bb2ef 71function drush_generate_users($num_users = NULL) {
eb0dd8a4 72 if (drush_generate_is_number($num_users) == FALSE) {
bf5880eb 73 drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of users.'));
eb0dd8a4 74 }
eb0dd8a4 75 drush_generate_include_devel();
0e6117de 76 $roles = drush_get_option('roles') ? explode(',', drush_get_option('roles')) : array();
77 devel_create_users($num_users, drush_get_option('kill'), 0, $roles);
bf5880eb 78 drush_log(t('Generated @number users.', array('@number' => $num_users)), 'success');
eb0dd8a4 79}
80
81/**
82 * Command callback. Generate a number of taxonomy.
83 */
504bb2ef 84function drush_generate_tax($num_vocab = NULL, $num_terms = NULL) {
eb0dd8a4 85 if (drush_generate_is_number($num_vocab) == FALSE) {
bf5880eb 86 drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid vocabulary ID.'));
eb0dd8a4 87 }
88 if (drush_generate_is_number($num_terms) == FALSE) {
bf5880eb 89 drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of terms.'));
eb0dd8a4 90 }
eb0dd8a4 91 drush_generate_include_devel();
504bb2ef 92 devel_generate_taxonomy_data($num_vocab, $num_terms, '12', drush_get_option('kill'));
bf5880eb 93 drush_log(t('Generated @num_vocab vocabulary, @num_terms terms', array('@num_vocab' => $num_vocab, '@num_terms' => $num_terms)), 'success');
eb0dd8a4 94}
95
96/**
97 * Command callback. Generate a number of content.
98 */
f0713b0e 99function drush_generate_nodescontent($num_nodes = NULL, $max_comments = NULL) {
eb0dd8a4 100 if (drush_generate_is_number($num_nodes) == FALSE) {
bf5880eb 101 drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of nodes'));
eb0dd8a4 102 }
f0713b0e 103 if (!empty($max_comments) && drush_generate_is_number($max_comments) == FALSE) {
bf5880eb 104 drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of comments.'));
eb0dd8a4 105 }
106
828df1d7 107 // Let's load user 1, seems to be needed for creating comments.
eb0dd8a4 108 global $user;
47332544 109 $user_one = user_load(1);
eb0dd8a4 110 $user = $user_one;
111 drupal_save_session(FALSE);
83013f8f 112
504bb2ef 113 if (drush_get_option('kill')) {
114 $values['values']['kill_content'] = 1;
115 }
eb0dd8a4 116 $values['values']['title_length'] = '8';
117 $values['values']['num_nodes'] = $num_nodes;
f0713b0e 118 $values['values']['max_comments'] = $max_comments;
f0713b0e 119 $values['values']['add_upload'] = drush_get_option('add-upload', FALSE);
828df1d7 120 $values['values']['node_types'] = drupal_map_assoc(explode(',', drush_get_option('types', 'page,article')));
eb0dd8a4 121 drush_generate_include_devel();
122 devel_generate_content($values);
f0713b0e 123 drush_log(t('Generated @num_nodes nodes, @max_comments comments per node', array('@num_nodes' => (int)$num_nodes, '@max_comments' => (int)$max_comments)), 'success');
eb0dd8a4 124}
125
126//////////////////////////////////////////////////////////////////////////////
127// Helper functions
128
129// verify if param is a number
130function drush_generate_is_number($number) {
131 if ($number == NULL) return FALSE;
132 if (!is_numeric($number)) return FALSE;
eb0dd8a4 133 return TRUE;
134}
135
136// include devel_generate.inc
137function drush_generate_include_devel() {
138 $path = drupal_get_path('module', 'devel_generate');
139 require_once($path .'/devel_generate.inc');
140}