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