#371892 by Dave Reid, deekayen, drewish: Generate content pretends to work even when...
[project/devel.git] / devel_generate.module
CommitLineData
7eb12881 1<?php
2// $Id$
3
4/**
5 * Implementation of hook_menu().
6 */
7function devel_generate_menu() {
8 $items = array();
4a03a3ed 9
5de9665a 10 $items['admin/config/development/generate/user'] = array(
fb26f2c4 11 'title' => 'Generate users',
12 'description' => 'Generate a given number of users. Optionally delete current users.',
7eb12881 13 'page callback' => 'drupal_get_form',
14 'page arguments' => array('devel_generate_users_form'),
fb26f2c4 15 'access arguments' => array('administer users'),
7eb12881 16 );
5de9665a 17 $items['admin/config/development/generate/content'] = array(
fb26f2c4 18 'title' => 'Generate content',
19 'description' => 'Generate a given number of nodes and comments. Optionally delete current items.',
7eb12881 20 'page callback' => 'drupal_get_form',
21 'page arguments' => array('devel_generate_content_form'),
fb26f2c4 22 'access arguments' => array('administer nodes'),
7eb12881 23 );
e281d5db 24 if (module_exists('taxonomy')) {
5de9665a 25 $items['admin/config/development/generate/taxonomy'] = array(
1b66a2ee 26 'title' => 'Generate taxonomy',
27 'description' => 'Generate a given number of vocabularies and terms. Optionally delete current taxonomies.',
e281d5db 28 'page callback' => 'drupal_get_form',
29 'page arguments' => array('devel_generate_taxonomy_form'),
30 'access arguments' => array('administer nodes'),
31 );
32 }
7eb12881 33
34 return $items;
35}
36
7eb12881 37function devel_generate_users_form() {
38 $form['num'] = array(
39 '#type' => 'textfield',
40 '#title' => t('How many users would you like to generate?'),
41 '#default_value' => 50,
42 '#size' => 10,
43 );
44 $form['kill_users'] = array(
45 '#type' => 'checkbox',
53f826f6 46 '#title' => t('Delete all users (except user id 1) before generating new users.'),
039a5ec1 47 '#default_value' => FALSE,
7eb12881 48 );
16ebe122
JE
49
50 $options = array(1 => t('Now'));
51 foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
52 $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
53 }
54 $form['time_range'] = array(
55 '#type' => 'select',
56 '#title' => t('How old should user accounts be?'),
57 '#description' => t('User ages will be distributed randomly from the current time, back to the selected time.'),
58 '#options' => $options,
59 '#default_value' => 604800,
60 );
61
7eb12881 62 $form['submit'] = array(
63 '#type' => 'submit',
64 '#value' => t('Do it!'),
65 );
66 return $form;
67}
68
632721c7 69function devel_generate_users_form_submit($form_id, &$form_state) {
4c129b0b 70 module_load_include('inc', 'devel_generate', 'devel_generate');
16ebe122 71 devel_create_users($form_state['values']['num'], $form_state['values']['kill_users'], $form_state['values']['time_range']);
7eb12881 72}
73
7eb12881 74function devel_generate_content_form() {
4a03a3ed 75 $options = array();
76
efba51fd 77 if (module_exists('content')) {
78 $types = content_types();
efba51fd 79 foreach ($types as $type) {
5bfb1a86 80 $warn = '';
efba51fd 81 if (count($type['fields'])) {
087be481 82 $warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
efba51fd 83 }
84 $options[$type['type']] = t($type['name']). $warn;
efba51fd 85 }
86 }
87 else {
7e5ce2de 88 $types = node_type_get_types();
efba51fd 89 foreach ($types as $type) {
90 $options[$type->type] = t($type->name);
91 }
92 }
ba311f72 93 // we cannot currently generate valid polls.
94 unset($options['poll']);
4a03a3ed 95
96 if (empty($options)) {
97 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);
98 return;
99 }
100
efba51fd 101 $form['node_types'] = array(
102 '#type' => 'checkboxes',
103 '#title' => t('Which node types do you want to create?'),
104 '#options' => $options,
105 '#default_value' => array_keys($options),
106 );
107 $form['kill_content'] = array(
108 '#type' => 'checkbox',
109 '#title' => t('<strong>Delete all content</strong> in these node types before generating new content.'),
110 '#default_value' => FALSE,
111 );
7eb12881 112 $form['num_nodes'] = array(
113 '#type' => 'textfield',
114 '#title' => t('How many nodes would you like to generate?'),
efba51fd 115 '#default_value' => 50,
7eb12881 116 '#size' => 10,
117 );
16ebe122
JE
118
119 $options = array(1 => t('Now'));
120 foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
121 $options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
122 }
123 $form['time_range'] = array(
124 '#type' => 'select',
125 '#title' => t('How far back in time should the nodes be dated?'),
126 '#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
127 '#options' => $options,
128 '#default_value' => 604800,
129 );
130
7eb12881 131 $form['num_comments'] = array(
e281d5db 132 '#type' => module_exists('comment') ? 'textfield' : 'value',
133 '#title' => t('How many comments per node would you like to generate?'),
134 '#description' => t('You must also enable comments for the node types you are generating.'),
efba51fd 135 '#default_value' => 0,
136 '#size' => 3,
e281d5db 137 '#access' => module_exists('comment'),
7eb12881 138 );
7eb12881 139 $form['title_length'] = array(
140 '#type' => 'textfield',
141 '#title' => t('Max word length of titles'),
2fe38f39 142 '#default_value' => 4,
7eb12881 143 '#size' => 10,
144 );
e281d5db 145 $form['add_upload'] = array(
5026e8d9 146 '#type' => 'checkbox',
147 '#disabled' => !module_exists('upload'),
148 '#description' => t('Requires upload.module'),
149 '#title' => t('Add an upload to each node'),
e281d5db 150 '#default_value' => FALSE,
151 );
152 $form['add_terms'] = array(
5026e8d9 153 '#disabled' => !module_exists('taxonomy'),
154 '#description' => t('Requires taxonomy.module'),
155 '#type' => 'checkbox',
e281d5db 156 '#title' => t('Add taxonomy terms to each node.'),
157 '#default_value' => FALSE,
e281d5db 158 );
e281d5db 159 $form['add_alias'] = array(
5026e8d9 160 '#type' => 'checkbox',
161 '#disabled' => !module_exists('path'),
162 '#description' => t('Requires path.module'),
e281d5db 163 '#title' => t('Add an url alias for each node.'),
164 '#default_value' => FALSE,
165 );
eb578afa 166 $form['add_statistics'] = array(
167 '#type' => 'checkbox',
168 '#title' => t('Add statistics for each node (node_counter table).'),
169 '#default_value' => TRUE,
170 '#access' => module_exists('statistics'),
171 );
172
7eb12881 173 $form['submit'] = array(
174 '#type' => 'submit',
175 '#value' => t('Do it!'),
176 );
5026e8d9 177 $form['#redirect'] = FALSE;
e281d5db 178
7eb12881 179 return $form;
180}
181
632721c7 182function devel_generate_content_form_submit($form_id, &$form_state) {
e87cf471 183 $form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
e9f3cf9a 184 if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['num_comments'] <= 10) {
971c7a82 185 module_invoke('devel_generate', 'content', $form_state);
64bdc964
DG
186 }
187 else {
971c7a82 188 module_invoke('devel_generate', 'batch_content', $form_state);
64bdc964 189 }
7eb12881 190}
191
7eb12881 192function devel_generate_taxonomy_form() {
193 $form['num_vocab'] = array(
194 '#type' => 'textfield',
195 '#title' => t('How many vocabularies would you like to generate?'),
196 '#default_value' => 3,
197 '#size' => 10,
198 );
199 $form['num_terms'] = array(
200 '#type' => 'textfield',
201 '#title' => t('How many terms would you like to generate?'),
202 '#default_value' => 50,
203 '#size' => 10,
204 );
7eb12881 205 $form['title_length'] = array(
206 '#type' => 'textfield',
207 '#title' => t('Max word length of term/vocab names'),
208 '#default_value' => 12,
209 '#size' => 10,
210 );
039a5ec1
CP
211 $form['kill_taxonomy'] = array(
212 '#type' => 'checkbox',
213 '#title' => t('Delete existing terms and vocabularies before generating new content.'),
214 '#default_value' => FALSE,
215 );
7eb12881 216 $form['submit'] = array(
217 '#type' => 'submit',
218 '#value' => t('Do it!'),
219 );
220 return $form;
221}
222
632721c7 223function devel_generate_taxonomy_form_submit($form_id, &$form_state) {
4c129b0b 224 module_load_include('inc', 'devel_generate', 'devel_generate');
039a5ec1 225 devel_generate_taxonomy_data($form_state['values']['num_vocab'], $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
7eb12881 226}
971c7a82 227
7416654f 228// Modules that want to affect generated nodes may implement hook_node('presave'). See OG module or CCK.
60453f86 229// A few implementations live here because core doesn't do bulk node generation.
7416654f 230function devel_generate_node_presave(&$node) {
60453f86 231 if (isset($node->devel_generate)) {
83013f8f 232 $results = $node->devel_generate;
642ff8a9 233 if (!empty($results['add_upload'])) {
60453f86 234 devel_generate_add_upload($node);
235 }
642ff8a9 236 if (!empty($results['add_terms'])) {
60453f86 237 devel_generate_add_terms($node);
238 }
239 }
240}
241
7416654f 242function devel_generate_node_insert(&$node) {
971c7a82 243 if (isset($node->devel_generate)) {
244 $results = $node->devel_generate;
83013f8f 245
642ff8a9 246 if (!empty($results['num_comments'])) {
60453f86 247 devel_generate_add_comments($node, $results['users'], $results['num_comments'], $results['title_length']);
248 }
971c7a82 249
7416654f 250
60453f86 251 // Add an url alias. Cannot happen before save because we don't know the nid.
642ff8a9 252 if (!empty($results['add_alias'])) {
60453f86 253 path_set_alias("node/$node->nid", "node-$node->nid-$node->type");
971c7a82 254 }
eb578afa 255
256 // Add node statistics.
7416654f 257 if (!empty($results['add_statistics']) && module_exists('statistics')) {
eb578afa 258 devel_generate_add_statistics($node);
259 }
971c7a82 260 }
eb578afa 261}
262