first import
This commit is contained in:
@ -0,0 +1,202 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Generate content, taxonomy, menu, and users via drush framework.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implementation of hook_drush_command().
|
||||
*/
|
||||
function devel_generate_drush_command() {
|
||||
$items['generate-users'] = array(
|
||||
'description' => 'Create users.',
|
||||
'arguments' => array(
|
||||
'number_users' => 'Number of users to generate.',
|
||||
),
|
||||
'options' => array(
|
||||
'kill' => 'Delete all users before generating new ones.',
|
||||
'roles' => 'A comma delimited list of role IDs which should be granted to the new users. No need to specify authenticated user role.',
|
||||
),
|
||||
'aliases' => array('genu'),
|
||||
);
|
||||
$items['generate-terms'] = array(
|
||||
'description' => 'Create terms in specified vocabulary.',
|
||||
'arguments' => array(
|
||||
'machine_name' => 'Vocabulary machine name into which new terms will be inserted.',
|
||||
'number_terms' => 'Number of terms to insert. Defaults to 10.',
|
||||
),
|
||||
'options' => array(
|
||||
'kill' => 'Delete all terms in specified vocabulary before generating.',
|
||||
'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
|
||||
),
|
||||
'aliases' => array('gent'),
|
||||
|
||||
);
|
||||
$items['generate-vocabs'] = array(
|
||||
'description' => 'Create vocabularies.',
|
||||
'arguments' => array(
|
||||
'num_vocabs' => 'Number of vocabularies to create. Defaults to 1.',
|
||||
),
|
||||
'options' => array(
|
||||
'kill' => 'Delete all vocabularies before generating.',
|
||||
),
|
||||
'aliases' => array('genv'),
|
||||
);
|
||||
$items['generate-content'] = array(
|
||||
'description' => 'Create content.',
|
||||
'drupal dependencies' => array('devel_generate'),
|
||||
'arguments' => array(
|
||||
'number_nodes' => 'Number of nodes to generate.',
|
||||
'maximum_comments' => 'Maximum number of comments to generate.',
|
||||
),
|
||||
'options' => array(
|
||||
'kill' => 'Delete all content before generating new content.',
|
||||
'types' => 'A comma delimited list of content types to create. Defaults to page,article.',
|
||||
'feedback' => 'An integer representing interval for insertion rate logging. Defaults to 500',
|
||||
'skip-fields' => 'A comma delimited list of fields to omit when generating random values',
|
||||
'languages' => 'A comma-separated list of language codes',
|
||||
),
|
||||
'aliases' => array('genc'),
|
||||
);
|
||||
$items['generate-menus'] = array(
|
||||
'description' => 'Create menus and menu items.',
|
||||
'drupal dependencies' => array('devel_generate'), // Remove these once devel.module is moved down a directory. http://drupal.org/node/925246
|
||||
'arguments' => array(
|
||||
'number_menus' => 'Number of menus to generate. Defaults to 2.',
|
||||
'number_links' => 'Number of links to generate. Defaults to 50.',
|
||||
'max_depth' => 'Max link depth. Defaults to 3',
|
||||
'max_width' => 'Max width of first level of links. Defaults to 8.',
|
||||
),
|
||||
'options' => array(
|
||||
'kill' => 'Delete all previously generated menus and links before generating new menus and links.',
|
||||
),
|
||||
'aliases' => array('genm'),
|
||||
);
|
||||
return $items;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Command callback. Generate a number of users.
|
||||
*/
|
||||
function drush_devel_generate_users($num_users = NULL) {
|
||||
if (drush_generate_is_number($num_users) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', t('Invalid number of users.'));
|
||||
}
|
||||
drush_generate_include_devel();
|
||||
$roles = drush_get_option('roles') ? explode(',', drush_get_option('roles')) : array();
|
||||
devel_create_users($num_users, drush_get_option('kill'), 0, $roles);
|
||||
drush_log(t('Generated @number users.', array('@number' => $num_users)), 'success');
|
||||
}
|
||||
|
||||
/**
|
||||
* Command callback. Generate a number of terms in given vocabs.
|
||||
*/
|
||||
function drush_devel_generate_terms($vname = NULL, $num_terms = 10) {
|
||||
// Try to convert machine name to a vocab ID
|
||||
if (!$vocab = taxonomy_vocabulary_machine_name_load($vname)) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid vocabulary name: !name', array('!name' => $vname)));
|
||||
}
|
||||
if (drush_generate_is_number($num_terms) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of terms: !num', array('!num' => $num_terms)));
|
||||
}
|
||||
|
||||
drush_generate_include_devel();
|
||||
$vocabs[$vocab->vid] = $vocab;
|
||||
devel_generate_term_data($vocabs, $num_terms, '12', drush_get_option('kill'));
|
||||
drush_log(dt('Generated @num_terms terms.', array('@num_terms' => $num_terms)), 'success');
|
||||
}
|
||||
|
||||
/**
|
||||
* Command callback. Generate a number of vocabularies.
|
||||
*/
|
||||
function drush_devel_generate_vocabs($num_vocab = 1) {
|
||||
if (drush_generate_is_number($num_vocab) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of vocabularies: !num.', array('!num' => $num_vocab)));
|
||||
}
|
||||
drush_generate_include_devel();
|
||||
devel_generate_vocab_data($num_vocab, '12', drush_get_option('kill'));
|
||||
drush_log(dt('Generated @num_vocab vocabularies.', array('@num_vocab' => $num_vocab)), 'success');
|
||||
}
|
||||
|
||||
/**
|
||||
* Command callback. Generate a number of nodes and comments.
|
||||
*/
|
||||
function drush_devel_generate_content($num_nodes = NULL, $max_comments = NULL) {
|
||||
if (drush_generate_is_number($num_nodes) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of nodes'));
|
||||
}
|
||||
if (!empty($max_comments) && drush_generate_is_number($max_comments) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of comments.'));
|
||||
}
|
||||
|
||||
$add_language = drush_get_option('languages');
|
||||
if (!empty($add_language)) {
|
||||
$add_language = explode(',', str_replace(' ', '', $add_language));
|
||||
// Intersect with the enabled languages to make sure the language args
|
||||
// passed are actually enabled.
|
||||
$values['values']['add_language'] = array_intersect($add_language, array_keys(locale_language_list()));
|
||||
}
|
||||
|
||||
// Load user 1; is needed for creating *published* comments.
|
||||
if ($max_comments) {
|
||||
global $user;
|
||||
$user_one = user_load(1);
|
||||
$user = $user_one;
|
||||
drupal_save_session(FALSE);
|
||||
}
|
||||
|
||||
$values['values']['kill_content'] = drush_get_option('kill');
|
||||
$values['values']['title_length'] = 6;
|
||||
$values['values']['num_nodes'] = $num_nodes;
|
||||
$values['values']['max_comments'] = $max_comments;
|
||||
$values['values']['node_types'] = drupal_map_assoc(explode(',', drush_get_option('types', 'page,article')));
|
||||
drush_generate_include_devel();
|
||||
devel_generate_content($values);
|
||||
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');
|
||||
}
|
||||
|
||||
/**
|
||||
* Command callback. Generate a number of menus and menu links.
|
||||
*/
|
||||
function drush_devel_generate_menus($number_menus = 2, $number_links = 50, $max_depth = 3, $max_width = 8) {
|
||||
if (drush_generate_is_number($number_menus) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of menus'));
|
||||
}
|
||||
if (drush_generate_is_number($number_links) == FALSE) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid number of links'));
|
||||
}
|
||||
if (drush_generate_is_number($max_depth) == FALSE || $max_depth > 9 || $max_depth < 1) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum link depth. Use a value between 1 and 9'));
|
||||
}
|
||||
if (drush_generate_is_number($max_width) == FALSE || $max_width < 1) {
|
||||
return drush_set_error('DEVEL_GENERATE_INVALID_INPUT', dt('Invalid maximum menu width. Use a positive numeric value.'));
|
||||
}
|
||||
|
||||
global $user;
|
||||
$user_one = user_load(1);
|
||||
$user = $user_one;
|
||||
drupal_save_session(FALSE);
|
||||
|
||||
$kill = drush_get_option('kill');
|
||||
drush_generate_include_devel();
|
||||
$link_types = drupal_map_assoc(array('node', 'front', 'external'));
|
||||
devel_generate_menu_data($number_menus, array(), $number_links, 12, $link_types, $max_depth, $max_width, $kill);
|
||||
drush_log(t('Generated @number_menus menus, @number_links links.', array('@number_menus' => (int)$number_menus, '@number_links' => (int)$number_links)), 'success');
|
||||
}
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Helper functions
|
||||
|
||||
// Verify if param is a number.
|
||||
function drush_generate_is_number($number) {
|
||||
if ($number == NULL) return FALSE;
|
||||
if (!is_numeric($number)) return FALSE;
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// Include devel_generate.inc.
|
||||
function drush_generate_include_devel() {
|
||||
$path = drupal_get_path('module', 'devel_generate');
|
||||
require_once($path .'/devel_generate.inc');
|
||||
}
|
@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Functions needed for devel_generate Fields API integration.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Enrich the $object that is about to be saved with arbitrary
|
||||
* information in each of its fields.
|
||||
**/
|
||||
function devel_generate_fields(&$object, $obj_type, $bundle) {
|
||||
$field_types = field_info_field_types();
|
||||
$instances = field_info_instances($obj_type, $bundle);
|
||||
$skips = function_exists('drush_get_option') ? drush_get_option('skip-fields', '') : @$_REQUEST['skip-fields'];
|
||||
foreach (explode(',', $skips) as $skip) {
|
||||
unset($instances[$skip]);
|
||||
}
|
||||
foreach ($instances as $instance) {
|
||||
$field_name = $instance['field_name'];
|
||||
$field = field_info_field($field_name);
|
||||
|
||||
$object_field = array();
|
||||
// If module handles own multiples, then only call its hook once.
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
$max = 0;
|
||||
}
|
||||
else {
|
||||
switch ($field['cardinality']) {
|
||||
case FIELD_CARDINALITY_UNLIMITED:
|
||||
$max = rand(0, 3); //just an arbitrary number for 'unlimited'
|
||||
break;
|
||||
default:
|
||||
$max = $field['cardinality'] - 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for ($i = 0; $i <= $max; $i++) {
|
||||
$module = $field_types[$field['type']]['module'];
|
||||
|
||||
// Include any support file that might exist for this field.
|
||||
if (in_array($module, array('file', 'image', 'taxonomy', 'number', 'text', 'comment', 'list'))) {
|
||||
// devel_generate implements on behalf of core and special friends.
|
||||
module_load_include('inc', 'devel_generate', "$module.devel_generate");
|
||||
}
|
||||
else {
|
||||
module_load_include('inc', $module, "$module.devel_generate");
|
||||
}
|
||||
$function = $module . '_devel_generate';
|
||||
if (function_exists($function)) {
|
||||
if ($result = $function($object, $field, $instance, $bundle)) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
// Fields that handle their own multiples will add their own deltas.
|
||||
$object_field = $result;
|
||||
}
|
||||
else {
|
||||
// When multiples are handled by the content module, add a delta for each result.
|
||||
$object_field[$i] = $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// TODO: Completely overriding any existing $object->{$field['field_name']}
|
||||
// is necessary here because the forum module has a bug where it
|
||||
// initializes the property with incorrect data.
|
||||
// @see http://drupal.org/node/652176
|
||||
$object->{$field['field_name']} = array(
|
||||
$object->language => $object_field,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A simple function to return multiple values for fields that use
|
||||
* custom multiple value widgets but don't need any other special multiple
|
||||
* values handling. This will call the field generation function
|
||||
* a random number of times and compile the results into a node array.
|
||||
*/
|
||||
function devel_generate_multiple($function, $object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
if (function_exists($function)) {
|
||||
switch ($field['cardinality']) {
|
||||
case FIELD_CARDINALITY_UNLIMITED:
|
||||
$max = rand(0, 3); //just an arbitrary number for 'unlimited'
|
||||
break;
|
||||
default:
|
||||
$max = $field['cardinality'] - 1;
|
||||
break;
|
||||
}
|
||||
for ($i = 0; $i <= $max; $i++) {
|
||||
$result = $function($object, $field, $instance, $bundle);
|
||||
if (!empty($result)) {
|
||||
$object_field[$i] = $result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $object_field;
|
||||
}
|
@ -0,0 +1,711 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Generate some random users.
|
||||
*
|
||||
* @param $num
|
||||
* Number of users to generate.
|
||||
* @param $kill
|
||||
* Boolean that indicates if existing users should be removed first.
|
||||
* @param $age
|
||||
* The max age of each randomly-generated user, in seconds.
|
||||
* @param $roles
|
||||
* An array of role IDs that the users should receive.
|
||||
*/
|
||||
function devel_create_users($num, $kill, $age = 0, $roles = array()) {
|
||||
$url = parse_url($GLOBALS['base_url']);
|
||||
if ($kill) {
|
||||
$uids = db_select('users', 'u')
|
||||
->fields('u', array('uid'))
|
||||
->condition('uid', 1, '>')
|
||||
->execute()
|
||||
->fetchAllAssoc('uid');
|
||||
user_delete_multiple(array_keys($uids));
|
||||
drupal_set_message(format_plural(count($uids), '1 user deleted', '@count users deleted.'));
|
||||
}
|
||||
// Determine if we should create user pictures.
|
||||
$pic_config = FALSE;
|
||||
module_load_include('inc', 'system', 'image.gd');
|
||||
if (variable_get('user_pictures', 0) && function_exists('image_gd_check_settings') && image_gd_check_settings()) {
|
||||
$pic_config['path'] = variable_get('user_picture_path', 'pictures');
|
||||
list($pic_config['width'], $pic_config['height']) = explode('x', variable_get('user_picture_dimensions', '85x85'));
|
||||
}
|
||||
|
||||
if ($num > 0) {
|
||||
$names = array();
|
||||
while (count($names) < $num) {
|
||||
$name = devel_generate_word(mt_rand(6, 12));
|
||||
$names[$name] = '';
|
||||
}
|
||||
|
||||
if (empty($roles)) {
|
||||
$roles = array(DRUPAL_AUTHENTICATED_RID);
|
||||
}
|
||||
foreach ($names as $name => $value) {
|
||||
$edit = array(
|
||||
'uid' => NULL,
|
||||
'name' => $name,
|
||||
'pass' => NULL, // No password avoids user_hash_password() which is expensive.
|
||||
'mail' => $name . '@' . $url['host'],
|
||||
'status' => 1,
|
||||
'created' => REQUEST_TIME - mt_rand(0, $age),
|
||||
'roles' => drupal_map_assoc($roles),
|
||||
);
|
||||
|
||||
// Populate all core fields on behalf of field.module
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
|
||||
$edit = (object) $edit;
|
||||
$edit->language = LANGUAGE_NONE;
|
||||
devel_generate_fields($edit, 'user', 'user');
|
||||
$edit = (array) $edit;
|
||||
|
||||
$account = user_save(drupal_anonymous_user(), $edit);
|
||||
|
||||
if ($pic_config) {
|
||||
// Since the image.module should scale the picture just pick an
|
||||
// arbitrary size that it's too big for our font.
|
||||
$im = imagecreatetruecolor(200, 200);
|
||||
|
||||
// Randomize the foreground using the md5 of the user id, then invert it
|
||||
// for the background color so there's enough contrast to read the text.
|
||||
$parts = array_map('hexdec', str_split(md5($account->uid), 2));
|
||||
$fg = imagecolorallocate($im, $parts[1], $parts[3], $parts[5]);
|
||||
$bg = imagecolorallocate($im, 255 - $parts[0], 255 - $parts[1], 255 - $parts[2]);
|
||||
|
||||
// Fill the background then print their user info.
|
||||
imagefill($im, 0, 0, $bg);
|
||||
imagestring($im, 5, 5, 5, "#" . $account->uid, $fg);
|
||||
imagestring($im, 5, 5, 25, $account->name, $fg);
|
||||
|
||||
|
||||
// Create an empty, managed file where we want the user's picture to
|
||||
// be so we can have GD overwrite it with the image.
|
||||
$picture_directory = variable_get('file_default_scheme', 'public') . '://' . variable_get('user_picture_path', 'pictures');
|
||||
file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
|
||||
$destination = file_stream_wrapper_uri_normalize($picture_directory . '/picture-' . $account->uid . '.png');
|
||||
$file = file_save_data('', $destination);
|
||||
|
||||
// GD doesn't like stream wrapped paths so convert the URI to a normal
|
||||
// file system path.
|
||||
if (isset($file) && $wrapper = file_stream_wrapper_get_instance_by_uri($file->uri)) {
|
||||
imagepng($im, $wrapper->realpath());
|
||||
}
|
||||
imagedestroy($im);
|
||||
|
||||
// Clear the cached filesize, set the owner and MIME-type then re-save
|
||||
// the file.
|
||||
clearstatcache();
|
||||
$file->uid = $account->uid;
|
||||
$file->filemime = 'image/png';
|
||||
$file = file_save($file);
|
||||
|
||||
// Save the user record with the new picture.
|
||||
$edit = (array) $account;
|
||||
$edit['picture'] = $file;
|
||||
user_save($account, $edit);
|
||||
}
|
||||
}
|
||||
}
|
||||
drupal_set_message(t('!num_users created.', array('!num_users' => format_plural($num, '1 user', '@count users'))));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The main API function for creating content.
|
||||
*
|
||||
* See devel_generate_content_form() for the supported keys in $form_state['values'].
|
||||
* Other modules may participate by form_alter() on that form and then handling their data during hook_nodeapi('pre_save') or in own submit handler for the form.
|
||||
*
|
||||
* @param string $form_state
|
||||
* @return void
|
||||
*/
|
||||
function devel_generate_content($form_state) {
|
||||
if (!empty($form_state['values']['kill_content'])) {
|
||||
devel_generate_content_kill($form_state['values']);
|
||||
}
|
||||
|
||||
if (count($form_state['values']['node_types'])) {
|
||||
// Generate nodes.
|
||||
devel_generate_content_pre_node($form_state['values']);
|
||||
$start = time();
|
||||
for ($i = 1; $i <= $form_state['values']['num_nodes']; $i++) {
|
||||
devel_generate_content_add_node($form_state['values']);
|
||||
if (function_exists('drush_log') && $i % drush_get_option('feedback', 1000) == 0) {
|
||||
$now = time();
|
||||
drush_log(dt('Completed !feedback nodes (!rate nodes/min)', array('!feedback' => drush_get_option('feedback', 1000), '!rate' => (drush_get_option('feedback', 1000)*60)/($now-$start))), 'ok');
|
||||
$start = $now;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
devel_generate_set_message(format_plural($form_state['values']['num_nodes'], '1 node created.', 'Finished creating @count nodes'));
|
||||
}
|
||||
|
||||
function devel_generate_add_comments($node, $users, $max_comments, $title_length = 8) {
|
||||
$num_comments = mt_rand(1, $max_comments);
|
||||
for ($i = 1; $i <= $num_comments; $i++) {
|
||||
$comment = new stdClass;
|
||||
$comment->nid = $node->nid;
|
||||
$comment->cid = NULL;
|
||||
$comment->name = 'devel generate';
|
||||
$comment->mail = 'devel_generate@example.com';
|
||||
$comment->timestamp = mt_rand($node->created, REQUEST_TIME);
|
||||
|
||||
switch ($i % 3) {
|
||||
case 1:
|
||||
$comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid = 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
|
||||
break;
|
||||
case 2:
|
||||
$comment->pid = db_query_range("SELECT cid FROM {comment} WHERE pid > 0 AND nid = :nid ORDER BY RAND()", 0, 1, array(':nid' => $comment->nid))->fetchField();
|
||||
break;
|
||||
default:
|
||||
$comment->pid = 0;
|
||||
}
|
||||
|
||||
// The subject column has a max character length of 64
|
||||
// See bug: http://drupal.org/node/1024340
|
||||
$comment->subject = substr(devel_create_greeking(mt_rand(2, $title_length), TRUE), 0, 63);
|
||||
$comment->uid = $users[array_rand($users)];
|
||||
$comment->language = LANGUAGE_NONE;
|
||||
// Populate all core fields on behalf of field.module
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
|
||||
devel_generate_fields($comment, 'comment', 'comment_node_' . $node->type);
|
||||
comment_save($comment);
|
||||
}
|
||||
}
|
||||
|
||||
function devel_generate_vocabs($records, $maxlength = 12, $types = array('page', 'article')) {
|
||||
$vocs = array();
|
||||
|
||||
// Insert new data:
|
||||
for ($i = 1; $i <= $records; $i++) {
|
||||
$voc = new stdClass();
|
||||
$voc->name = devel_generate_word(mt_rand(2, $maxlength));
|
||||
$voc->machine_name = drupal_strtolower($voc->name);
|
||||
$voc->description = "description of ". $voc->name;
|
||||
// TODO: not working
|
||||
$voc->nodes = array_flip(array($types[array_rand($types)]));
|
||||
foreach ($voc->nodes as $key => $value) {
|
||||
$voc->nodes[$key] = $key;
|
||||
}
|
||||
|
||||
$voc->multiple = 1;
|
||||
$voc->required = 0;
|
||||
$voc->relations = 1;
|
||||
$voc->hierarchy = 1;
|
||||
$voc->weight = mt_rand(0,10);
|
||||
$voc->language = LANGUAGE_NONE;
|
||||
|
||||
taxonomy_vocabulary_save($voc);
|
||||
$vocs[] = $voc->name;
|
||||
|
||||
unset($voc);
|
||||
}
|
||||
return $vocs;
|
||||
}
|
||||
|
||||
function devel_generate_terms($records, $vocabs, $maxlength = 12) {
|
||||
$terms = array();
|
||||
|
||||
// Insert new data:
|
||||
$max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data}')->fetchField();
|
||||
$start = time();
|
||||
for ($i = 1; $i <= $records; $i++) {
|
||||
$term = new stdClass;
|
||||
switch ($i % 2) {
|
||||
case 1:
|
||||
// Set vid and vocabulary_machine_name properties.
|
||||
$vocab = $vocabs[array_rand($vocabs)];
|
||||
$term->vid = $vocab->vid;
|
||||
$term->vocabulary_machine_name = $vocab->machine_name;
|
||||
// Don't set a parent. Handled by taxonomy_save_term()
|
||||
// $term->parent = 0;
|
||||
break;
|
||||
default:
|
||||
while (TRUE) {
|
||||
// Keep trying to find a random parent.
|
||||
$candidate = mt_rand(1, $max);
|
||||
$query = db_select('taxonomy_term_data', 't');
|
||||
$query->innerJoin('taxonomy_vocabulary', 'v', 't.vid = v.vid');
|
||||
$parent = $query
|
||||
->fields('t', array('tid', 'vid'))
|
||||
->fields('v', array('machine_name'))
|
||||
->condition('v.vid', array_keys($vocabs), 'IN')
|
||||
->condition('t.tid', $candidate, '>=')
|
||||
->range(0,1)
|
||||
->execute()
|
||||
->fetchAssoc();
|
||||
if ($parent['tid']) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$term->parent = $parent['tid'];
|
||||
// Slight speedup due to this property being set.
|
||||
$term->vocabulary_machine_name = $parent['machine_name'];
|
||||
$term->vid = $parent['vid'];
|
||||
break;
|
||||
}
|
||||
|
||||
$term->name = devel_generate_word(mt_rand(2, $maxlength));
|
||||
$term->description = "description of ". $term->name;
|
||||
$term->format = filter_fallback_format();
|
||||
$term->weight = mt_rand(0, 10);
|
||||
$term->language = LANGUAGE_NONE;
|
||||
|
||||
// Populate all core fields on behalf of field.module
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
|
||||
devel_generate_fields($term, 'term', $term->vocabulary_machine_name);
|
||||
|
||||
if ($status = taxonomy_term_save($term)) {
|
||||
$max += 1;
|
||||
if (function_exists('drush_log')) {
|
||||
$feedback = drush_get_option('feedback', 1000);
|
||||
if ($i % $feedback == 0) {
|
||||
$now = time();
|
||||
drush_log(dt('Completed !feedback terms (!rate terms/min)', array('!feedback' => $feedback, '!rate' => $feedback*60 / ($now-$start) )), 'ok');
|
||||
$start = $now;
|
||||
}
|
||||
}
|
||||
|
||||
// Limit memory usage. Only report first 20 created terms.
|
||||
if ($i < 20) {
|
||||
$terms[] = $term->name;
|
||||
}
|
||||
|
||||
unset($term);
|
||||
}
|
||||
}
|
||||
return $terms;
|
||||
}
|
||||
|
||||
// TODO: use taxonomy_get_entries once that exists.
|
||||
function devel_generate_get_terms($vids) {
|
||||
return db_select('taxonomy_term_data', 'td')
|
||||
->fields('td', array('tid'))
|
||||
->condition('vid', $vids, 'IN')
|
||||
->orderBy('tid', 'ASC')
|
||||
->execute()
|
||||
->fetchCol('tid');
|
||||
}
|
||||
|
||||
function devel_generate_term_data($vocabs, $num_terms, $title_length, $kill) {
|
||||
if ($kill) {
|
||||
foreach (devel_generate_get_terms(array_keys($vocabs)) as $tid) {
|
||||
taxonomy_term_delete($tid);
|
||||
}
|
||||
drupal_set_message(t('Deleted existing terms.'));
|
||||
}
|
||||
|
||||
$new_terms = devel_generate_terms($num_terms, $vocabs, $title_length);
|
||||
if (!empty($new_terms)) {
|
||||
drupal_set_message(t('Created the following new terms: !terms', array('!terms' => theme('item_list', array('items' => $new_terms)))));
|
||||
}
|
||||
}
|
||||
|
||||
function devel_generate_vocab_data($num_vocab, $title_length, $kill) {
|
||||
|
||||
if ($kill) {
|
||||
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
|
||||
taxonomy_vocabulary_delete($vid);
|
||||
}
|
||||
drupal_set_message(t('Deleted existing vocabularies.'));
|
||||
}
|
||||
|
||||
$new_vocs = devel_generate_vocabs($num_vocab, $title_length);
|
||||
if (!empty($new_vocs)) {
|
||||
drupal_set_message(t('Created the following new vocabularies: !vocs', array('!vocs' => theme('item_list', array('items' => $new_vocs)))));
|
||||
}
|
||||
}
|
||||
|
||||
function devel_generate_menu_data($num_menus, $existing_menus, $num_links, $title_length, $link_types, $max_depth, $max_width, $kill) {
|
||||
// Delete menus and menu links.
|
||||
if ($kill) {
|
||||
if (module_exists('menu')) {
|
||||
foreach (menu_get_menus(FALSE) as $menu => $menu_title) {
|
||||
if (strpos($menu, 'devel-') === 0) {
|
||||
$menu = menu_load($menu);
|
||||
menu_delete($menu);
|
||||
}
|
||||
}
|
||||
}
|
||||
// Delete menu links generated by devel.
|
||||
$result = db_select('menu_links', 'm')
|
||||
->fields('m', array('mlid'))
|
||||
->condition('m.menu_name', 'devel', '<>')
|
||||
// Look for the serialized version of 'devel' => TRUE.
|
||||
->condition('m.options', '%' . db_like('s:5:"devel";b:1') . '%', 'LIKE')
|
||||
->execute();
|
||||
foreach ($result as $link) {
|
||||
menu_link_delete($link->mlid);
|
||||
}
|
||||
drupal_set_message(t('Deleted existing menus and links.'));
|
||||
}
|
||||
|
||||
// Generate new menus.
|
||||
$new_menus = devel_generate_menus($num_menus, $title_length);
|
||||
if (!empty($new_menus)) {
|
||||
drupal_set_message(t('Created the following new menus: !menus', array('!menus' => theme('item_list', array('items' => $new_menus)))));
|
||||
}
|
||||
|
||||
// Generate new menu links.
|
||||
$menus = $new_menus + $existing_menus;
|
||||
$new_links = devel_generate_links($num_links, $menus, $title_length, $link_types, $max_depth, $max_width);
|
||||
drupal_set_message(t('Created @count new menu links.', array('@count' => count($new_links))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates new menus.
|
||||
*/
|
||||
function devel_generate_menus($num_menus, $title_length = 12) {
|
||||
$menus = array();
|
||||
|
||||
if (!module_exists('menu')) {
|
||||
$num_menus = 0;
|
||||
}
|
||||
|
||||
for ($i = 1; $i <= $num_menus; $i++) {
|
||||
$menu = array();
|
||||
$menu['title'] = devel_generate_word(mt_rand(2, $title_length));
|
||||
$menu['menu_name'] = 'devel-' . drupal_strtolower($menu['title']);
|
||||
$menu['description'] = t('Description of @name', array('@name' => $menu['title']));
|
||||
menu_save($menu);
|
||||
$menus[$menu['menu_name']] = $menu['title'];
|
||||
}
|
||||
|
||||
return $menus;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates menu links in a tree structure.
|
||||
*/
|
||||
function devel_generate_links($num_links, $menus, $title_length, $link_types, $max_depth, $max_width) {
|
||||
$links = array();
|
||||
$menus = array_keys(array_filter($menus));
|
||||
$link_types = array_keys(array_filter($link_types));
|
||||
|
||||
$nids = array();
|
||||
for ($i = 1; $i <= $num_links; $i++) {
|
||||
// Pick a random menu.
|
||||
$menu_name = $menus[array_rand($menus)];
|
||||
// Build up our link.
|
||||
$link = array(
|
||||
'menu_name' => $menu_name,
|
||||
'options' => array('devel' => TRUE),
|
||||
'weight' => mt_rand(-50, 50),
|
||||
'mlid' => 0,
|
||||
'link_title' => devel_generate_word(mt_rand(2, $title_length)),
|
||||
);
|
||||
$link['options']['attributes']['title'] = t('Description of @title.', array('@title' => $link['link_title']));
|
||||
|
||||
// For the first $max_width items, make first level links.
|
||||
if ($i <= $max_width) {
|
||||
$depth = 0;
|
||||
}
|
||||
else {
|
||||
// Otherwise, get a random parent menu depth.
|
||||
$depth = mt_rand(1, $max_depth - 1);
|
||||
}
|
||||
// Get a random parent link from the proper depth.
|
||||
do {
|
||||
$link['plid'] = db_select('menu_links', 'm')
|
||||
->fields('m', array('mlid'))
|
||||
->condition('m.menu_name', $menus, 'IN')
|
||||
->condition('m.depth', $depth)
|
||||
->range(0, 1)
|
||||
->orderRandom()
|
||||
->execute()
|
||||
->fetchField();
|
||||
$depth--;
|
||||
} while (!$link['plid'] && $depth > 0);
|
||||
if (!$link['plid']) {
|
||||
$link['plid'] = 0;
|
||||
}
|
||||
|
||||
$link_type = array_rand($link_types);
|
||||
switch ($link_types[$link_type]) {
|
||||
case 'node':
|
||||
// Grab a random node ID.
|
||||
$select = db_select('node', 'n')
|
||||
->fields('n', array('nid', 'title'))
|
||||
->condition('n.status', 1)
|
||||
->range(0, 1)
|
||||
->orderRandom();
|
||||
// Don't put a node into the menu twice.
|
||||
if (!empty($nids[$menu_name])) {
|
||||
$select->condition('n.nid', $nids[$menu_name], 'NOT IN');
|
||||
}
|
||||
$node = $select->execute()->fetchAssoc();
|
||||
if (isset($node['nid'])) {
|
||||
$nids[$menu_name][] = $node['nid'];
|
||||
$link['link_path'] = $link['router_path'] = 'node/' . $node['nid'];
|
||||
$link['link_title'] = $node['title'];
|
||||
break;
|
||||
}
|
||||
case 'external':
|
||||
$link['link_path'] = 'http://www.example.com/';
|
||||
break;
|
||||
case 'front':
|
||||
$link['link_path'] = $link['router_path'] = '<front>';
|
||||
break;
|
||||
default:
|
||||
$link['devel_link_type'] = $link_type;
|
||||
break;
|
||||
}
|
||||
|
||||
menu_link_save($link);
|
||||
|
||||
$links[$link['mlid']] = $link['link_title'];
|
||||
}
|
||||
|
||||
return $links;
|
||||
}
|
||||
|
||||
function devel_generate_word($length){
|
||||
mt_srand((double)microtime()*1000000);
|
||||
|
||||
$vowels = array("a", "e", "i", "o", "u");
|
||||
$cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr",
|
||||
"cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl", "sh");
|
||||
|
||||
$num_vowels = count($vowels);
|
||||
$num_cons = count($cons);
|
||||
$word = '';
|
||||
|
||||
while(strlen($word) < $length){
|
||||
$word .= $cons[mt_rand(0, $num_cons - 1)] . $vowels[mt_rand(0, $num_vowels - 1)];
|
||||
}
|
||||
|
||||
return substr($word, 0, $length);
|
||||
}
|
||||
|
||||
function devel_create_content($type = NULL) {
|
||||
$nparas = mt_rand(1,12);
|
||||
$type = empty($type) ? mt_rand(0,3) : $type;
|
||||
|
||||
$output = "";
|
||||
switch($type % 3) {
|
||||
// MW: This appears undesireable. Was giving <p> in text fields
|
||||
// case 1: // html
|
||||
// for ($i = 1; $i <= $nparas; $i++) {
|
||||
// $output .= devel_create_para(mt_rand(10,60),1);
|
||||
// }
|
||||
// break;
|
||||
//
|
||||
// case 2: // brs only
|
||||
// for ($i = 1; $i <= $nparas; $i++) {
|
||||
// $output .= devel_create_para(mt_rand(10,60),2);
|
||||
// }
|
||||
// break;
|
||||
|
||||
default: // plain text
|
||||
for ($i = 1; $i <= $nparas; $i++) {
|
||||
$output .= devel_create_para(mt_rand(10,60)) ."\n\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $output;
|
||||
}
|
||||
|
||||
function devel_create_para($words, $type = 0) {
|
||||
$output = '';
|
||||
switch ($type) {
|
||||
case 1:
|
||||
$output .= "<p>" . devel_create_greeking($words) . "</p>";
|
||||
break;
|
||||
|
||||
case 2:
|
||||
$output .= devel_create_greeking($words) . "<br />";
|
||||
break;
|
||||
|
||||
default:
|
||||
$output .= devel_create_greeking($words);
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
function devel_create_greeking($word_count, $title = FALSE) {
|
||||
$dictionary = array("abbas", "abdo", "abico", "abigo", "abluo", "accumsan",
|
||||
"acsi", "ad", "adipiscing", "aliquam", "aliquip", "amet", "antehabeo",
|
||||
"appellatio", "aptent", "at", "augue", "autem", "bene", "blandit",
|
||||
"brevitas", "caecus", "camur", "capto", "causa", "cogo", "comis",
|
||||
"commodo", "commoveo", "consectetuer", "consequat", "conventio", "cui",
|
||||
"damnum", "decet", "defui", "diam", "dignissim", "distineo", "dolor",
|
||||
"dolore", "dolus", "duis", "ea", "eligo", "elit", "enim", "erat",
|
||||
"eros", "esca", "esse", "et", "eu", "euismod", "eum", "ex", "exerci",
|
||||
"exputo", "facilisi", "facilisis", "fere", "feugiat", "gemino",
|
||||
"genitus", "gilvus", "gravis", "haero", "hendrerit", "hos", "huic",
|
||||
"humo", "iaceo", "ibidem", "ideo", "ille", "illum", "immitto",
|
||||
"importunus", "imputo", "in", "incassum", "inhibeo", "interdico",
|
||||
"iriure", "iusto", "iustum", "jugis", "jumentum", "jus", "laoreet",
|
||||
"lenis", "letalis", "lobortis", "loquor", "lucidus", "luctus", "ludus",
|
||||
"luptatum", "macto", "magna", "mauris", "melior", "metuo", "meus",
|
||||
"minim", "modo", "molior", "mos", "natu", "neo", "neque", "nibh",
|
||||
"nimis", "nisl", "nobis", "nostrud", "nulla", "nunc", "nutus", "obruo",
|
||||
"occuro", "odio", "olim", "oppeto", "os", "pagus", "pala", "paratus",
|
||||
"patria", "paulatim", "pecus", "persto", "pertineo", "plaga", "pneum",
|
||||
"populus", "praemitto", "praesent", "premo", "probo", "proprius",
|
||||
"quadrum", "quae", "qui", "quia", "quibus", "quidem", "quidne", "quis",
|
||||
"ratis", "refero", "refoveo", "roto", "rusticus", "saepius",
|
||||
"sagaciter", "saluto", "scisco", "secundum", "sed", "si", "similis",
|
||||
"singularis", "sino", "sit", "sudo", "suscipere", "suscipit", "tamen",
|
||||
"tation", "te", "tego", "tincidunt", "torqueo", "tum", "turpis",
|
||||
"typicus", "ulciscor", "ullamcorper", "usitas", "ut", "utinam",
|
||||
"utrum", "uxor", "valde", "valetudo", "validus", "vel", "velit",
|
||||
"veniam", "venio", "vereor", "vero", "verto", "vicis", "vindico",
|
||||
"virtus", "voco", "volutpat", "vulpes", "vulputate", "wisi", "ymo",
|
||||
"zelus");
|
||||
$dictionary_flipped = array_flip($dictionary);
|
||||
|
||||
$greeking = '';
|
||||
|
||||
if (!$title) {
|
||||
$words_remaining = $word_count;
|
||||
while ($words_remaining > 0) {
|
||||
$sentence_length = mt_rand(3, 10);
|
||||
$words = array_rand($dictionary_flipped, $sentence_length);
|
||||
$sentence = implode(' ', $words);
|
||||
$greeking .= ucfirst($sentence) . '. ';
|
||||
$words_remaining -= $sentence_length;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Use slightly different method for titles.
|
||||
$words = array_rand($dictionary_flipped, $word_count);
|
||||
$words = is_array($words) ? implode(' ', $words) : $words;
|
||||
$greeking = ucwords($words);
|
||||
}
|
||||
|
||||
// Work around possible php garbage collection bug. Without an unset(), this
|
||||
// function gets very expensive over many calls (php 5.2.11).
|
||||
unset($dictionary, $dictionary_flipped);
|
||||
return trim($greeking);
|
||||
}
|
||||
|
||||
function devel_generate_add_terms(&$node) {
|
||||
$vocabs = taxonomy_get_vocabularies($node->type);
|
||||
foreach ($vocabs as $vocab) {
|
||||
$sql = "SELECT tid FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY RAND()";
|
||||
$result = db_query_range($sql, 0, 5 , array(':vid' => $vocab->vid));
|
||||
foreach($result as $row) {
|
||||
$node->taxonomy[] = $row->tid;
|
||||
if (!$vocab->multiple) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function devel_get_users() {
|
||||
$users = array();
|
||||
$result = db_query_range("SELECT uid FROM {users}", 0, 50);
|
||||
foreach ($result as $record) {
|
||||
$users[] = $record->uid;
|
||||
}
|
||||
return $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate statistics information for a node.
|
||||
*
|
||||
* @param $node
|
||||
* A node object.
|
||||
*/
|
||||
function devel_generate_add_statistics($node) {
|
||||
$statistic = array(
|
||||
'nid' => $node->nid,
|
||||
'totalcount' => mt_rand(0, 500),
|
||||
'timestamp' => REQUEST_TIME - mt_rand(0, $node->created),
|
||||
);
|
||||
$statistic['daycount'] = mt_rand(0, $statistic['totalcount']);
|
||||
db_insert('node_counter')->fields($statistic)->execute();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the devel_generate_content_form request to kill all of the content.
|
||||
* This is used by both the batch and non-batch branches of the code.
|
||||
*
|
||||
* @param $num
|
||||
* array of options obtained from devel_generate_content_form.
|
||||
*/
|
||||
function devel_generate_content_kill($values) {
|
||||
$results = db_select('node', 'n')
|
||||
->fields('n', array('nid'))
|
||||
->condition('type', $values['node_types'], 'IN')
|
||||
->execute();
|
||||
foreach ($results as $result) {
|
||||
$nids[] = $result->nid;
|
||||
}
|
||||
|
||||
if (!empty($nids)) {
|
||||
node_delete_multiple($nids);
|
||||
drupal_set_message(t('Deleted %count nodes.', array('%count' => count($nids))));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Pre-process the devel_generate_content_form request. This is needed so
|
||||
* batch api can get the list of users once. This is used by both the batch
|
||||
* and non-batch branches of the code.
|
||||
*
|
||||
* @param $num
|
||||
* array of options obtained from devel_generate_content_form.
|
||||
*/
|
||||
function devel_generate_content_pre_node(&$results) {
|
||||
// Get user id.
|
||||
$users = devel_get_users();
|
||||
$users = array_merge($users, array('0'));
|
||||
$results['users'] = $users;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create one node. Used by both batch and non-batch code branches.
|
||||
*
|
||||
* @param $num
|
||||
* array of options obtained from devel_generate_content_form.
|
||||
*/
|
||||
function devel_generate_content_add_node(&$results) {
|
||||
$node = new stdClass();
|
||||
$node->nid = NULL;
|
||||
|
||||
// Insert new data:
|
||||
$node->type = array_rand($results['node_types']);
|
||||
node_object_prepare($node);
|
||||
$users = $results['users'];
|
||||
$node->uid = $users[array_rand($users)];
|
||||
$type = node_type_get_type($node);
|
||||
$node->title = $type->has_title ? devel_create_greeking(mt_rand(2, $results['title_length']), TRUE) : '';
|
||||
$node->revision = mt_rand(0,1);
|
||||
$node->promote = mt_rand(0, 1);
|
||||
// Avoid NOTICE.
|
||||
if (!isset($results['time_range'])) {
|
||||
$results['time_range'] = 0;
|
||||
}
|
||||
|
||||
devel_generate_set_language($results, $node);
|
||||
|
||||
$node->created = REQUEST_TIME - mt_rand(0, $results['time_range']);
|
||||
|
||||
// A flag to let hook_nodeapi() implementations know that this is a generated node.
|
||||
$node->devel_generate = $results;
|
||||
|
||||
// Populate all core fields on behalf of field.module
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate.fields');
|
||||
devel_generate_fields($node, 'node', $node->type);
|
||||
|
||||
// See devel_generate_nodeapi() for actions that happen before and after this save.
|
||||
node_save($node);
|
||||
}
|
||||
|
||||
/*
|
||||
* Populate $object->language based on $results
|
||||
*/
|
||||
function devel_generate_set_language($results, $object) {
|
||||
if (isset($results['add_language'])) {
|
||||
$languages = $results['add_language'];
|
||||
$object->language = $languages[array_rand($languages)];
|
||||
}
|
||||
else {
|
||||
$default = language_default('language');
|
||||
$object->language = $default == 'en' ? LANGUAGE_NONE : $default;
|
||||
}
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
name = Devel generate
|
||||
description = Generate dummy users, nodes, and taxonomy terms.
|
||||
package = Development
|
||||
core = 7.x
|
||||
dependencies[] = devel
|
||||
tags[] = developer
|
||||
|
||||
; Information added by drupal.org packaging script on 2011-07-22
|
||||
version = "7.x-1.2"
|
||||
core = "7.x"
|
||||
project = "devel"
|
||||
datestamp = "1311355316"
|
||||
|
@ -0,0 +1,463 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Implements hook_menu().
|
||||
*/
|
||||
function devel_generate_menu() {
|
||||
$items = array();
|
||||
|
||||
$items['admin/config/development/generate/user'] = array(
|
||||
'title' => 'Generate users',
|
||||
'description' => 'Generate a given number of users. Optionally delete current users.',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('devel_generate_users_form'),
|
||||
'access arguments' => array('administer users'),
|
||||
);
|
||||
$items['admin/config/development/generate/content'] = array(
|
||||
'title' => 'Generate content',
|
||||
'description' => 'Generate a given number of nodes and comments. Optionally delete current items.',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('devel_generate_content_form'),
|
||||
'access arguments' => array('administer nodes'),
|
||||
);
|
||||
if (module_exists('taxonomy')) {
|
||||
$items['admin/config/development/generate/taxonomy'] = array(
|
||||
'title' => 'Generate terms',
|
||||
'description' => 'Generate a given number of terms. Optionally delete current terms.',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('devel_generate_term_form'),
|
||||
'access arguments' => array('administer taxonomy'),
|
||||
);
|
||||
$items['admin/config/development/generate/vocabs'] = array(
|
||||
'title' => 'Generate vocabularies',
|
||||
'description' => 'Generate a given number of vocabularies. Optionally delete current vocabularies.',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('devel_generate_vocab_form'),
|
||||
'access arguments' => array('administer taxonomy'),
|
||||
);
|
||||
}
|
||||
$items['admin/config/development/generate/menu'] = array(
|
||||
'title' => 'Generate menus',
|
||||
'description' => 'Generate a given number of menus and menu links. Optionally delete current menus.',
|
||||
'page callback' => 'drupal_get_form',
|
||||
'page arguments' => array('devel_generate_menu_form'),
|
||||
'access arguments' => array('administer menu'),
|
||||
);
|
||||
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates users using FormAPI.
|
||||
*/
|
||||
function devel_generate_users_form() {
|
||||
$form['num'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('How many users would you like to generate?'),
|
||||
'#default_value' => 50,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['kill_users'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Delete all users (except user id 1) before generating new users.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$options = user_roles(TRUE);
|
||||
unset($options[DRUPAL_AUTHENTICATED_RID]);
|
||||
$form['roles'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Which roles should the users receive?'),
|
||||
'#description' => t('Users always receive the <em>authenticated user</em> role.'),
|
||||
'#options' => $options,
|
||||
);
|
||||
|
||||
$options = array(1 => t('Now'));
|
||||
foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
|
||||
$options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
|
||||
}
|
||||
$form['time_range'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('How old should user accounts be?'),
|
||||
'#description' => t('User ages will be distributed randomly from the current time, back to the selected time.'),
|
||||
'#options' => $options,
|
||||
'#default_value' => 604800,
|
||||
);
|
||||
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormAPI submission to generate users.
|
||||
*/
|
||||
function devel_generate_users_form_submit($form_id, &$form_state) {
|
||||
module_load_include('inc', 'devel_generate');
|
||||
$values = $form_state['values'];
|
||||
devel_create_users($values['num'], $values['kill_users'], $values['time_range'], $values['roles']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates nodes using FormAPI.
|
||||
*/
|
||||
function devel_generate_content_form() {
|
||||
$options = array();
|
||||
|
||||
if (module_exists('content')) {
|
||||
$types = content_types();
|
||||
foreach ($types as $type) {
|
||||
$warn = '';
|
||||
if (count($type['fields'])) {
|
||||
$warn = t('. This type contains CCK fields which will only be populated by fields that implement the content_generate hook.');
|
||||
}
|
||||
$options[$type['type']] = t($type['name']). $warn;
|
||||
}
|
||||
}
|
||||
else {
|
||||
$types = node_type_get_types();
|
||||
$suffix = '';
|
||||
foreach ($types as $type) {
|
||||
if (module_exists('comment')) {
|
||||
$default = variable_get('comment_' . $type->type, COMMENT_NODE_OPEN);
|
||||
$map = array(t('Hidden'), t('Closed'), t('Open'));
|
||||
$suffix = '<small>. ' . t('Comments: ') . $map[$default]. '</small>';
|
||||
}
|
||||
$options[$type->type] = t($type->name) . $suffix;
|
||||
}
|
||||
}
|
||||
// we cannot currently generate valid polls.
|
||||
unset($options['poll']);
|
||||
|
||||
if (empty($options)) {
|
||||
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);
|
||||
return;
|
||||
}
|
||||
|
||||
$form['node_types'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Content types'),
|
||||
'#options' => $options,
|
||||
'#default_value' => array_keys($options),
|
||||
);
|
||||
if (module_exists('checkall')) $form['node_types']['#checkall'] = TRUE;
|
||||
$form['kill_content'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('<strong>Delete all content</strong> in these content types before generating new content.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$form['num_nodes'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('How many nodes would you like to generate?'),
|
||||
'#default_value' => 50,
|
||||
'#size' => 10,
|
||||
);
|
||||
|
||||
$options = array(1 => t('Now'));
|
||||
foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
|
||||
$options[$interval] = format_interval($interval, 1) . ' ' . t('ago');
|
||||
}
|
||||
$form['time_range'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('How far back in time should the nodes be dated?'),
|
||||
'#description' => t('Node creation dates will be distributed randomly from the current time, back to the selected time.'),
|
||||
'#options' => $options,
|
||||
'#default_value' => 604800,
|
||||
);
|
||||
|
||||
$form['max_comments'] = array(
|
||||
'#type' => module_exists('comment') ? 'textfield' : 'value',
|
||||
'#title' => t('Maximum number of comments per node.'),
|
||||
'#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.'),
|
||||
'#default_value' => 0,
|
||||
'#size' => 3,
|
||||
'#access' => module_exists('comment'),
|
||||
);
|
||||
$form['title_length'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Max word length of titles'),
|
||||
'#default_value' => 4,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['add_alias'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#disabled' => !module_exists('path'),
|
||||
'#description' => t('Requires path.module'),
|
||||
'#title' => t('Add an url alias for each node.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$form['add_statistics'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Add statistics for each node (node_counter table).'),
|
||||
'#default_value' => TRUE,
|
||||
'#access' => module_exists('statistics'),
|
||||
);
|
||||
|
||||
unset($options);
|
||||
$options[LANGUAGE_NONE] = t('Language neutral');
|
||||
if (module_exists('locale')) {
|
||||
$options += locale_language_list();
|
||||
}
|
||||
$form['add_language'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Set language on nodes'),
|
||||
'#multiple' => TRUE,
|
||||
'#disabled' => !module_exists('locale'),
|
||||
'#description' => t('Requires locale.module'),
|
||||
'#options' => $options,
|
||||
'#default_value' => array(LANGUAGE_NONE),
|
||||
);
|
||||
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
);
|
||||
$form['#redirect'] = FALSE;
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormAPI submission to generate nodes.
|
||||
*/
|
||||
function devel_generate_content_form_submit($form_id, &$form_state) {
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate');
|
||||
$form_state['values']['node_types'] = array_filter($form_state['values']['node_types']);
|
||||
if ($form_state['values']['num_nodes'] <= 50 && $form_state['values']['max_comments'] <= 10) {
|
||||
module_load_include('inc', 'devel_generate');
|
||||
devel_generate_content($form_state);
|
||||
}
|
||||
else {
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate_batch');
|
||||
devel_generate_batch_content($form_state);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates taxonomy terms using FormAPI.
|
||||
*/
|
||||
function devel_generate_term_form() {
|
||||
$options = array();
|
||||
foreach (taxonomy_get_vocabularies() as $vid => $vocab) {
|
||||
$options[$vid] = $vocab->machine_name;
|
||||
}
|
||||
$form['vids'] = array(
|
||||
'#type' => 'select',
|
||||
'#multiple' => TRUE,
|
||||
'#title' => t('Vocabularies'),
|
||||
'#required' => TRUE,
|
||||
'#options' => $options,
|
||||
'#description' => t('Restrict terms to these vocabularies.'),
|
||||
);
|
||||
$form['num_terms'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Number of terms?'),
|
||||
'#default_value' => 10,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['title_length'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Max word length of term names'),
|
||||
'#default_value' => 12,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['kill_taxonomy'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Delete existing terms in specified vocabularies before generating new terms.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates taxonomy vocabularies using FormAPI.
|
||||
*/
|
||||
function devel_generate_vocab_form() {
|
||||
$form['num_vocabs'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Number of vocabularies?'),
|
||||
'#default_value' => 1,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['title_length'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Max word length of vocabulary names'),
|
||||
'#default_value' => 12,
|
||||
'#size' => 10,
|
||||
);
|
||||
$form['kill_taxonomy'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Delete existing vocabularies before generating new ones.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormAPI submission to generate taxonomy terms.
|
||||
*/
|
||||
function devel_generate_term_form_submit($form_id, &$form_state) {
|
||||
module_load_include('inc', 'devel_generate');
|
||||
$vocabs = taxonomy_vocabulary_load_multiple($form_state['values']['vids']);
|
||||
devel_generate_term_data($vocabs, $form_state['values']['num_terms'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* FormAPI submission to generate taxonomy vocabularies.
|
||||
*/
|
||||
function devel_generate_vocab_form_submit($form_id, &$form_state) {
|
||||
module_load_include('inc', 'devel_generate');
|
||||
devel_generate_vocab_data($form_state['values']['num_vocabs'], $form_state['values']['title_length'], $form_state['values']['kill_taxonomy']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts nodes properly based on generation options.
|
||||
*
|
||||
* @param $node
|
||||
* The base node created on submit. Inspects $node->devel_generate.
|
||||
*/
|
||||
function devel_generate_node_insert($node) {
|
||||
if (isset($node->devel_generate)) {
|
||||
$results = $node->devel_generate;
|
||||
|
||||
if (!empty($results['max_comments']) && $node->comment >= COMMENT_NODE_OPEN) {
|
||||
devel_generate_add_comments($node, $results['users'], $results['max_comments'], $results['title_length']);
|
||||
}
|
||||
|
||||
|
||||
// Add an url alias. Cannot happen before save because we don't know the nid.
|
||||
if (!empty($results['add_alias'])) {
|
||||
$path = array(
|
||||
'source' => 'node/' . $node->nid,
|
||||
'alias' => 'node-' . $node->nid . '-' . $node->type,
|
||||
);
|
||||
path_save($path);
|
||||
}
|
||||
|
||||
// Add node statistics.
|
||||
if (!empty($results['add_statistics']) && module_exists('statistics')) {
|
||||
devel_generate_add_statistics($node);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a message for either drush or the web interface.
|
||||
*
|
||||
* @param $msg
|
||||
* The message to display.
|
||||
* @param $type
|
||||
* The message type, as defined by drupal_set_message().
|
||||
*
|
||||
* @return
|
||||
* Context-appropriate message output.
|
||||
*/
|
||||
function devel_generate_set_message($msg, $type = 'status') {
|
||||
$function = function_exists('drush_log') ? 'drush_log' : 'drupal_set_message';
|
||||
$function($msg, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generates menus using FormAPI.
|
||||
*/
|
||||
function devel_generate_menu_form() {
|
||||
$menu_enabled = module_exists('menu');
|
||||
if ($menu_enabled) {
|
||||
$menus = array('__new-menu__' => t('Create new menu(s)')) + menu_get_menus();
|
||||
}
|
||||
else {
|
||||
$menus = menu_list_system_menus();
|
||||
}
|
||||
$form['existing_menus'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Generate links for these menus'),
|
||||
'#options' => $menus,
|
||||
'#default_value' => array('__new-menu__'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
if ($menu_enabled) {
|
||||
$form['num_menus'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Number of new menus to create'),
|
||||
'#default_value' => 2,
|
||||
'#size' => 10,
|
||||
'#states' => array(
|
||||
'visible' => array(
|
||||
':input[name=existing_menus[__new-menu__]]' => array('checked' => TRUE),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
$form['num_links'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Number of links to generate'),
|
||||
'#default_value' => 50,
|
||||
'#size' => 10,
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['title_length'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Max word length of menu and menu link names'),
|
||||
'#default_value' => 12,
|
||||
'#size' => 10,
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['link_types'] = array(
|
||||
'#type' => 'checkboxes',
|
||||
'#title' => t('Types of links to generate'),
|
||||
'#options' => array(
|
||||
'node' => t('Nodes'),
|
||||
'front' => t('Front page'),
|
||||
'external' => t('External'),
|
||||
),
|
||||
'#default_value' => array('node', 'front', 'external'),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['max_depth'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Maximum link depth'),
|
||||
'#options' => range(0, MENU_MAX_DEPTH),
|
||||
'#default_value' => floor(MENU_MAX_DEPTH / 2),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
unset($form['max_depth']['#options'][0]);
|
||||
$form['max_width'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Maximum menu width'),
|
||||
'#default_value' => 6,
|
||||
'#size' => 10,
|
||||
'#description' => t("Limit the width of the generated menu's first level of links to a certain number of items."),
|
||||
'#required' => TRUE,
|
||||
);
|
||||
$form['kill'] = array(
|
||||
'#type' => 'checkbox',
|
||||
'#title' => t('Delete existing custom generated menus and menu links before generating new ones.'),
|
||||
'#default_value' => FALSE,
|
||||
);
|
||||
$form['submit'] = array(
|
||||
'#type' => 'submit',
|
||||
'#value' => t('Generate'),
|
||||
);
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* FormAPI submission to generate menus.
|
||||
*/
|
||||
function devel_generate_menu_form_submit($form_id, &$form_state) {
|
||||
// If the create new menus checkbox is off, set the number of new menus to 0.
|
||||
if (!isset($form_state['values']['existing_menus']['__new-menu__']) || !$form_state['values']['existing_menus']['__new-menu__']) {
|
||||
$form_state['values']['num_menus'] = 0;
|
||||
}
|
||||
module_load_include('inc', 'devel_generate');
|
||||
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']);
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Devel Generate batch handling functions using the BatchAPI
|
||||
*/
|
||||
|
||||
/**
|
||||
* Functions called from FAPI:
|
||||
*/
|
||||
|
||||
function devel_generate_batch_content($form_state) {
|
||||
$operations = array();
|
||||
|
||||
// Setup the batch operations and save the variables.
|
||||
$operations[] = array('devel_generate_batch_content_pre_node', array($form_state['values']));
|
||||
|
||||
// add the kill operation
|
||||
if ($form_state['values']['kill_content']) {
|
||||
$operations[] = array('devel_generate_batch_content_kill', array());
|
||||
}
|
||||
|
||||
// add the operations to create the nodes
|
||||
for ($num = 0; $num < $form_state['values']['num_nodes']; $num ++) {
|
||||
$operations[] = array('devel_generate_batch_content_add_node', array());
|
||||
}
|
||||
|
||||
// start the batch
|
||||
$batch = array(
|
||||
'title' => t('Generating Content'),
|
||||
'operations' => $operations,
|
||||
'finished' => 'devel_generate_batch_finished',
|
||||
'file' => drupal_get_path('module', 'devel_generate') . '/devel_generate_batch.inc',
|
||||
);
|
||||
batch_set($batch);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create Content Batch Functions:
|
||||
*/
|
||||
|
||||
function devel_generate_batch_content_kill(&$context) {
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate');
|
||||
devel_generate_content_kill($context['results']);
|
||||
}
|
||||
|
||||
function devel_generate_batch_content_pre_node($vars, &$context) {
|
||||
$context['results'] = $vars;
|
||||
$context['results']['num_nids'] = 0;
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate');
|
||||
devel_generate_content_pre_node($context['results']);
|
||||
}
|
||||
|
||||
function devel_generate_batch_content_add_node(&$context) {
|
||||
module_load_include('inc', 'devel_generate', 'devel_generate');
|
||||
devel_generate_content_add_node($context['results']);
|
||||
$context['results']['num_nids'] ++;
|
||||
}
|
||||
|
||||
function devel_generate_batch_finished($success, $results, $operations) {
|
||||
if ($success) {
|
||||
$message = t('Finished @num_nids nodes created successfully.', array('@num_nids' => $results['num_nids']));
|
||||
}
|
||||
else {
|
||||
$message = t('Finished with an error.');
|
||||
}
|
||||
drupal_set_message($message);
|
||||
}
|
||||
|
@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
function file_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_file_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _file_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
|
||||
function _file_devel_generate($object, $field, $instance, $bundle) {
|
||||
static $file;
|
||||
|
||||
if (empty($file)) {
|
||||
if ($path = devel_generate_textfile()) {
|
||||
$source->uri = $path;
|
||||
$source->uid = 1; // TODO: randomize? use case specific.
|
||||
$source->filemime = 'text/plain';
|
||||
$destination = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'] . '/' . basename($path);
|
||||
$file = file_move($source, $destination);
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
$object_field['fid'] = $file->fid;
|
||||
$object_field['display'] = $field['settings']['display_default'];
|
||||
$object_field['description'] = devel_create_greeking(10);
|
||||
|
||||
return $object_field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private function for generating a random text file.
|
||||
*/
|
||||
function devel_generate_textfile($filesize = 1024) {
|
||||
if ($tmp_file = drupal_tempnam('temporary://', 'filefield_')) {
|
||||
$destination = $tmp_file . '.txt';
|
||||
file_unmanaged_move($tmp_file, $destination);
|
||||
|
||||
$fp = fopen($destination, 'w');
|
||||
fwrite($fp, str_repeat('01', $filesize/2));
|
||||
fclose($fp);
|
||||
|
||||
return $destination;
|
||||
}
|
||||
}
|
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
|
||||
define('DEVEL_GENERATE_IMAGE_MAX', 5);
|
||||
|
||||
function image_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (function_exists('imagejpeg')) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_image_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _image_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function _image_devel_generate($object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
static $images = array();
|
||||
|
||||
$min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
|
||||
$max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
|
||||
$extensions = array_intersect(explode(' ', $instance['settings']['file_extensions']), array('png', 'jpg'));
|
||||
$extension = array_rand(drupal_map_assoc($extensions));
|
||||
|
||||
// Generate a max of 5 different images.
|
||||
if (!isset($images[$extension][$min_resolution][$max_resolution]) || count($images[$extension][$min_resolution][$max_resolution]) <= DEVEL_GENERATE_IMAGE_MAX) {
|
||||
if ($path = devel_generate_image($extension, $min_resolution, $max_resolution)) {
|
||||
$source = new stdClass();
|
||||
$source->uri = $path;
|
||||
$source->uid = 1; // TODO: randomize? Use case specific.
|
||||
$source->filemime = 'image/' . pathinfo($path, PATHINFO_EXTENSION);
|
||||
$destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
|
||||
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
|
||||
$destination = $destination_dir . '/' . basename($path);
|
||||
$file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
|
||||
$images[$extension][$min_resolution][$max_resolution][$file->fid] = $file;
|
||||
}
|
||||
else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Select one of the images we've already generated for this field.
|
||||
$file = new stdClass();
|
||||
$file->fid = array_rand($images[$extension][$min_resolution][$max_resolution]);
|
||||
}
|
||||
|
||||
$object_field['fid'] = $file->fid;
|
||||
$object_field['alt'] = devel_create_greeking(4);
|
||||
$object_field['title'] = devel_create_greeking(4);
|
||||
return $object_field;
|
||||
}
|
||||
|
||||
/**
|
||||
* Private function for creating a random image.
|
||||
*
|
||||
* This function only works with the GD toolkit. ImageMagick is not supported.
|
||||
*/
|
||||
function devel_generate_image($extension = 'png', $min_resolution, $max_resolution) {
|
||||
if ($tmp_file = drupal_tempnam('temporary://', 'imagefield_')) {
|
||||
$destination = $tmp_file . '.' . $extension;
|
||||
file_unmanaged_move($tmp_file, $destination, FILE_CREATE_DIRECTORY);
|
||||
|
||||
$min = explode('x', $min_resolution);
|
||||
$max = explode('x', $max_resolution);
|
||||
|
||||
$width = rand((int)$min[0], (int)$max[0]);
|
||||
$height = rand((int)$min[0], (int)$max[0]);
|
||||
|
||||
// Make a image split into 4 sections with random colors.
|
||||
$im = imagecreate($width, $height);
|
||||
for ($n = 0; $n < 4; $n++) {
|
||||
$color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
|
||||
$x = $width/2 * ($n % 2);
|
||||
$y = $height/2 * (int) ($n >= 2);
|
||||
imagefilledrectangle($im, $x, $y, $x + $width/2, $y + $height/2, $color);
|
||||
}
|
||||
|
||||
// Make a perfect circle in the image middle.
|
||||
$color = imagecolorallocate($im, rand(0, 255), rand(0, 255), rand(0, 255));
|
||||
$smaller_dimension = min($width, $height);
|
||||
$smaller_dimension = ($smaller_dimension % 2) ? $smaller_dimension : $smaller_dimension;
|
||||
imageellipse($im, $width/2, $height/2, $smaller_dimension, $smaller_dimension, $color);
|
||||
|
||||
$save_function = 'image'. ($extension == 'jpg' ? 'jpeg' : $extension);
|
||||
$save_function($im, drupal_realpath($destination));
|
||||
|
||||
$images[$extension][$min_resolution][$max_resolution][$destination] = $destination;
|
||||
}
|
||||
return $destination;
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
<?php
|
||||
// Id$
|
||||
|
||||
function list_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_list_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _list_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
|
||||
function _list_devel_generate($object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
if ($allowed_values = list_allowed_values($field)) {
|
||||
$keys = array_keys($allowed_values);
|
||||
$object_field['value'] = $keys[mt_rand(0, count($allowed_values) - 1)];
|
||||
}
|
||||
return $object_field;
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
<?php
|
||||
// Id$
|
||||
|
||||
function number_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_number_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _number_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
|
||||
function _number_devel_generate($object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
// Make sure the instance settings are all set.
|
||||
foreach (array('min', 'max', 'precision', 'scale') as $key) {
|
||||
if (empty($instance['settings'][$key])) {
|
||||
$instance['settings'][$key] = NULL;
|
||||
}
|
||||
}
|
||||
$min = is_numeric($instance['settings']['min']) ? $instance['settings']['min'] : 0;
|
||||
switch ($field['type']) {
|
||||
case 'number_integer':
|
||||
$max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : 10000;
|
||||
$decimal = 0;
|
||||
$scale = 0;
|
||||
break;
|
||||
|
||||
case 'number_decimal':
|
||||
$precision = is_numeric($instance['settings']['precision']) ? $instance['settings']['precision'] : 10;
|
||||
$scale = is_numeric($instance['settings']['scale']) ? $instance['settings']['scale'] : 2;
|
||||
$max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
|
||||
$decimal = rand(0, (10 * $scale)) / 100;
|
||||
break;
|
||||
|
||||
case 'number_float':
|
||||
$precision = rand(10, 32);
|
||||
$scale = rand(0, 2);
|
||||
$decimal = rand(0, (10 * $scale)) / 100;
|
||||
$max = is_numeric($instance['settings']['max']) ? $instance['settings']['max'] : pow(10, ($precision - $scale));
|
||||
break;
|
||||
}
|
||||
$object_field['value'] = round((rand($min, $max) + $decimal), $scale);
|
||||
return $object_field;
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
function taxonomy_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_taxonomy_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _taxonomy_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
|
||||
function _taxonomy_devel_generate($object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
// TODO: For free tagging vocabularies that do not already have terms, this
|
||||
// will not result in any tags being added.
|
||||
$machine_name = $field['settings']['allowed_values'][0]['vocabulary'];
|
||||
$vocabulary = taxonomy_vocabulary_machine_name_load($machine_name);
|
||||
if ($max = db_query('SELECT MAX(tid) FROM {taxonomy_term_data} WHERE vid = :vid', array(':vid' => $vocabulary->vid))->fetchField()) {
|
||||
$candidate = mt_rand(1, $max);
|
||||
$query = db_select('taxonomy_term_data', 't');
|
||||
$tid = $query
|
||||
->fields('t', array('tid'))
|
||||
->condition('t.vid', $vocabulary->vid, '=')
|
||||
->condition('t.tid', $candidate, '>=')
|
||||
->range(0,1)
|
||||
->execute()
|
||||
->fetchField();
|
||||
// If there are no terms for the taxonomy, the query will fail, in which
|
||||
// case we return NULL.
|
||||
if ($tid === FALSE) {
|
||||
return NULL;
|
||||
}
|
||||
$object_field['tid'] = (int) $tid;
|
||||
return $object_field;
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
function text_devel_generate($object, $field, $instance, $bundle) {
|
||||
if (field_behaviors_widget('multiple values', $instance) == FIELD_BEHAVIOR_CUSTOM) {
|
||||
return devel_generate_multiple('_text_devel_generate', $object, $field, $instance, $bundle);
|
||||
}
|
||||
else {
|
||||
return _text_devel_generate($object, $field, $instance, $bundle);
|
||||
}
|
||||
}
|
||||
|
||||
function _text_devel_generate($object, $field, $instance, $bundle) {
|
||||
$object_field = array();
|
||||
if (!empty($instance['settings']['text_processing'])) {
|
||||
$formats = filter_formats();
|
||||
$format = array_rand($formats);
|
||||
}
|
||||
else {
|
||||
$format = filter_fallback_format();
|
||||
}
|
||||
|
||||
if ($instance['widget']['type'] != 'text_textfield') {
|
||||
// Textarea handling
|
||||
$object_field['value'] = devel_create_content($format);
|
||||
if ($instance['widget']['type'] == 'text_textarea_with_summary' && !empty($instance['display_summary'])) {
|
||||
$object_field['summary'] = devel_create_content($format);
|
||||
}
|
||||
}
|
||||
else {
|
||||
// Textfield handling.
|
||||
// Generate a value that respects max_length.
|
||||
if (empty($field['settings']['max_length'])) {
|
||||
$field['settings']['max_length'] = 12;
|
||||
}
|
||||
$object_field['value'] = user_password($field['settings']['max_length']);
|
||||
}
|
||||
$object_field['format'] = $format;
|
||||
return $object_field;
|
||||
}
|
Reference in New Issue
Block a user