#855060: Fix sorting of 'Switch Users' list.
[project/devel.git] / devel_generate.drush.inc
CommitLineData
bb24a5bf 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) {
56eb2ab0 14 case 'drush:generate-users':
25584bd4 15 return dt('Generate user accounts.');
56eb2ab0 16 case 'drush:generate-taxonomy':
25584bd4 17 return dt('Generate taxonomy terms for a given vocabulary.');
56eb2ab0 18 case 'drush:generate-content':
25584bd4 19 return dt('Generate nodes and comments.');
bb24a5bf 20 }
21}
22
23/**
24 * Implementation of hook_drush_command().
25 */
26function devel_generate_drush_command() {
56eb2ab0 27 $items['generate-users'] = array(
8ff6f2e3 28 'description' => 'Creates users.',
29 'arguments' => array(
25584bd4 30 'num_users' => 'Number of users to generate.',
8ff6f2e3 31 ),
32 'options' => array(
25584bd4 33 'kill' => 'Delete all users before generating new ones.',
8ff6f2e3 34 ),
56eb2ab0 35 'aliases' => array('genu'),
bb24a5bf 36 );
56eb2ab0 37 $items['generate-taxonomy'] = array(
8ff6f2e3 38 'description' => 'Creates taxonomy.',
39 'arguments' => array(
25584bd4 40 'vid' => 'Vocabulary ID into which new terms will be inserted.',
41 'num_terms' => 'Number of terms to generate',
8ff6f2e3 42 ),
43 'options' => array(
25584bd4 44 'kill' => 'Delete all terms in specified vocab before generating.'
8ff6f2e3 45 ),
56eb2ab0 46 'aliases' => array('gent'),
bb24a5bf 47 );
6676ec1a 48 $items['generate-content'] = array(
8ff6f2e3 49 'description' => 'Creates content.',
0ff8a9ee 50 'drupal dependencies' => array('devel_generate'),
8ff6f2e3 51 'arguments' => array(
25584bd4 52 'num_nodes' => 'Number of nodes to generate.',
37e03c3a 53 'max_comments' => 'Maximum number of comments to generate.',
8ff6f2e3 54 ),
55 'options' => array(
2ff06ec3 56 'kill' => 'Delete all content before generating new content.',
57 'add-terms' => 'Add terms to generated nodes.',
58 'add-upload' => 'Add an attached file to generated nodes.',
34a88823 59 'types' => 'Specify which types of nodes to generate. Should be a comma-separated list.',
6676ec1a 60 'languages' => 'A comma-separated list of language codes',
8ff6f2e3 61 ),
56eb2ab0 62 'aliases' => array('genc'),
bb24a5bf 63 );
64 return $items;
65}
66
bb24a5bf 67/**
68 * Command callback. Generate a number of users.
69 */
56eb2ab0 70function drush_devel_generate_generate_users($num_users = NULL) {
bb24a5bf 71 if (drush_generate_is_number($num_users) == FALSE) {
25584bd4 72 drush_set_error('DEVEL_GENERATE_INVALID_NUMBER', dt('Invalid number of users.'));
bb24a5bf 73 }
bb24a5bf 74 drush_generate_include_devel();
25584bd4 75 drush_op('devel_create_users', $num_users, drush_get_option('kill'));
76 drush_log(dt('Generated @number users.', array('@number' => $num_users)), 'ok');
bb24a5bf 77}
78
79/**
80 * Command callback. Generate a number of taxonomy.
81 */
56eb2ab0 82function drush_devel_generate_generate_taxonomy($num_vocab = NULL, $num_terms = NULL) {
bb24a5bf 83 if (drush_generate_is_number($num_vocab) == FALSE) {
25584bd4 84 drush_set_error('DEVEL_GENERATE_INVALID_VID', dt('Invalid vocabulary ID.'));
bb24a5bf 85 }
86 if (drush_generate_is_number($num_terms) == FALSE) {
25584bd4 87 drush_set_error('DEVEL_GENERATE_INVALID_NUM_TERMS', dt('Invalid number of terms.'));
bb24a5bf 88 }
bb24a5bf 89 drush_generate_include_devel();
25584bd4 90 drush_op('devel_generate_taxonomy_data', $num_vocab, $num_terms, '12', drush_get_option('kill'));
91 drush_log(dt('Generated @num_vocab vocabulary, @num_terms terms', array('@num_vocab' => $num_vocab, '@num_terms' => $num_terms)), 'ok');
bb24a5bf 92}
93
94/**
95 * Command callback. Generate a number of content.
96 */
6676ec1a 97function drush_devel_generate_generate_content($num_nodes = NULL, $max_comments = NULL, $add_language = NULL) {
bb24a5bf 98 if (drush_generate_is_number($num_nodes) == FALSE) {
25584bd4 99 drush_set_error('DEVEL_GENERATE_INVALID_NUM_NODES', dt('Invalid number of nodes'));
bb24a5bf 100 }
0ff8a9ee 101 if (!empty($max_comments) && drush_generate_is_number($max_comments) == FALSE) {
25584bd4 102 drush_set_error('DEVEL_GENERATE_INVALID_NUM_COMMENTS', dt('Invalid number of comments.'));
bb24a5bf 103 }
104
6676ec1a 105 $add_language = drush_get_option('languages');
106 if (!empty($add_language)) {
107 $add_language = explode(',', str_replace(' ', '', $add_language));
108 // Intersect with the enabled languages to make sure the language args
109 // passed are actually enabled.
110 $values['values']['add_language'] = array_flip(array_intersect($add_language, array_keys(locale_language_list())));
111 }
112
bb24a5bf 113 // Let's load user 1, seems to be need for creating comments.
114 global $user;
115 $user_one = user_load(array('uid' => '1'));
116 $user = $user_one;
d700bbcb 117
25584bd4 118 session_save_session(FALSE);
d700bbcb 119 drush_generate_include_devel();
bb24a5bf 120
25584bd4 121 if (drush_get_option('kill')) $values['values']['kill_content'] = 1;
bb24a5bf 122 $values['values']['title_length'] = '8';
123 $values['values']['num_nodes'] = $num_nodes;
0ff8a9ee 124 $values['values']['max_comments'] = $max_comments;
2ff06ec3 125 $values['values']['add_terms'] = drush_get_option('add-terms', FALSE);
126 $values['values']['add_upload'] = drush_get_option('add-upload', FALSE);
25584bd4 127 $all_types = array_keys(devel_generate_content_types());
a5777d5a 128 if (drush_get_option('types')) {
129 $types = explode(',', drush_get_option('types'));
25584bd4 130 $types = array_intersect($types, $all_types);
131 }
132 else {
133 $types = $all_types;
134 }
135 $values['values']['node_types'] = drupal_map_assoc($types);
4159c42f 136
25584bd4 137 drush_op('devel_generate_content', $values);
0ff8a9ee 138 drush_log(dt('Generated @num_nodes nodes, @max_comments comments per node', array('@num_nodes' => $num_nodes, '@max_comments' => $max_comments)), 'ok');
bb24a5bf 139}
140
141//////////////////////////////////////////////////////////////////////////////
142// Helper functions
143
144// verify if param is a number
145function drush_generate_is_number($number) {
146 if ($number == NULL) return FALSE;
147 if (!is_numeric($number)) return FALSE;
7277c11f 148 if ($number < 0) return FALSE;
bb24a5bf 149 return TRUE;
150}
151
152// include devel_generate.inc
153function drush_generate_include_devel() {
154 $path = drupal_get_path('module', 'devel_generate');
155 require_once($path .'/devel_generate.inc');
156}