1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Devel generate integration for the File Entity module.
|
6
|
*/
|
7
|
|
8
|
function file_entity_generate_file_form() {
|
9
|
$form['count'] = array(
|
10
|
'#type' => 'textfield',
|
11
|
'#title' => t('How many files would you like to generate?'),
|
12
|
'#default_value' => 50,
|
13
|
'#size' => 4,
|
14
|
);
|
15
|
$form['file_types'] = array(
|
16
|
'#type' => 'select',
|
17
|
'#title' => t('File types'),
|
18
|
'#description' => t('Restrict files to these file types.'),
|
19
|
'#options' => file_entity_type_get_names() + array(FILE_TYPE_NONE => t('Undetermined')),
|
20
|
'#multiple' => TRUE,
|
21
|
);
|
22
|
$form['delete'] = array(
|
23
|
'#type' => 'checkbox',
|
24
|
'#title' => t('Delete existing files in specified file types before generating new files.'),
|
25
|
'#default_value' => FALSE,
|
26
|
);
|
27
|
$form['submit'] = array(
|
28
|
'#type' => 'submit',
|
29
|
'#value' => t('Generate'),
|
30
|
);
|
31
|
|
32
|
return $form;
|
33
|
}
|
34
|
|
35
|
function file_entity_generate_file_form_submit(&$form, &$form_state) {
|
36
|
$file_types = array_values(array_filter($form_state['values']['file_types']));
|
37
|
$batch = file_entity_generate_file_batch_info($form_state['values']['count'], $file_types, $form_state['values']['delete']);
|
38
|
batch_set($batch);
|
39
|
}
|
40
|
|
41
|
function file_entity_generate_file_batch_info($count, array $file_types = array(), $delete = FALSE) {
|
42
|
if (empty($file_types)) {
|
43
|
$file_types = array_keys(file_entity_type_get_names());
|
44
|
}
|
45
|
|
46
|
if ($delete) {
|
47
|
$operations[] = array('file_entity_generate_file_batch_delete', array($file_types));
|
48
|
}
|
49
|
|
50
|
$operations[] = array('file_entity_generate_file_batch_generate', array($file_types, $count));
|
51
|
|
52
|
return array(
|
53
|
'operations' => $operations,
|
54
|
'finished' => 'file_entity_generate_file_batch_finished',
|
55
|
'file' => drupal_get_path('module', 'file_entity') . '/file_entity.devel_generate.inc',
|
56
|
);
|
57
|
}
|
58
|
|
59
|
function file_entity_generate_file_batch_delete(array $file_types, array &$context) {
|
60
|
if (empty($context['sandbox'])) {
|
61
|
$context['sandbox'] = array();
|
62
|
$context['sandbox']['progress'] = 0;
|
63
|
$context['sandbox']['current_fid'] = 0;
|
64
|
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT fid) FROM {file_managed} WHERE type IN (:types)', array(':types' => $file_types))->fetchField();
|
65
|
}
|
66
|
|
67
|
$limit = 20;
|
68
|
$fids = db_query_range("SELECT fid FROM {file_managed} WHERE type IN (:types) AND fid > :fid ORDER BY fid", 0, $limit, array(':types' => $file_types, ':fid' => $context['sandbox']['current_fid']))->fetchCol();
|
69
|
file_delete_multiple($fids);
|
70
|
|
71
|
// Update our progress information.
|
72
|
$context['sandbox']['progress'] += count($fids);
|
73
|
$context['sandbox']['current_rid'] = end($fids);
|
74
|
$context['message'] = t('Deleted file @fid.', array('@fid' => $context['sandbox']['current_rid']));
|
75
|
|
76
|
// Inform the batch engine that we are not finished,
|
77
|
// and provide an estimation of the completion level we reached.
|
78
|
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
|
79
|
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
|
80
|
}
|
81
|
}
|
82
|
|
83
|
function file_entity_generate_file_batch_generate(array $file_types, $num, array &$context) {
|
84
|
if (empty($context['sandbox'])) {
|
85
|
module_load_include('inc', 'devel_generate');
|
86
|
$context['sandbox'] = array();
|
87
|
$context['sandbox']['progress'] = 0;
|
88
|
$context['sandbox']['max'] = $num;
|
89
|
$context['sandbox']['users'] = devel_get_users();
|
90
|
}
|
91
|
|
92
|
$limit = 20;
|
93
|
|
94
|
for ($i = 0; $i < min($limit, $context['sandbox']['max'] - $context['sandbox']['progress']); $i++) {
|
95
|
$type = array_rand(drupal_map_assoc($file_types), 1);
|
96
|
if ($uri = file_entity_generate_file($type)) {
|
97
|
$file = file_uri_to_object($uri, FALSE);
|
98
|
$file->uid = array_rand(drupal_map_assoc($context['sandbox']['users']), 1);
|
99
|
file_save($file);
|
100
|
if (!empty($file->fid)) {
|
101
|
$context['results'][] = $file->fid;
|
102
|
}
|
103
|
}
|
104
|
}
|
105
|
|
106
|
// Update our progress information.
|
107
|
$context['sandbox']['progress'] += $limit;
|
108
|
//$context['message'] = t('Deleted URL redirect @rid.', array('@rid' => end($rids)));
|
109
|
|
110
|
// Inform the batch engine that we are not finished,
|
111
|
// and provide an estimation of the completion level we reached.
|
112
|
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
|
113
|
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
|
114
|
}
|
115
|
}
|
116
|
|
117
|
function file_entity_generate_file_batch_finished($success, $results, $operations) {
|
118
|
if ($success) {
|
119
|
drupal_set_message(format_plural(count($results), 'One file created.', '@count files created.'));
|
120
|
}
|
121
|
else {
|
122
|
// An error occurred.
|
123
|
// $operations contains the operations that remained unprocessed.
|
124
|
$error_operation = reset($operations);
|
125
|
drupal_set_message(t('An error occurred while processing @operation with arguments : @args', array('@operation' => $error_operation[0], '@args' => print_r($error_operation[0], TRUE))));
|
126
|
}
|
127
|
}
|
128
|
|
129
|
function file_entity_generate_file($file_type) {
|
130
|
$type = file_type_load($file_type);
|
131
|
$possible_extensions = file_type_get_valid_extensions($type);
|
132
|
|
133
|
$image_extensions = array('png', 'gif', 'jpg', 'jpeg');
|
134
|
if (array_intersect($possible_extensions, $image_extensions)) {
|
135
|
$extension = array_rand(array_flip($image_extensions));
|
136
|
module_load_include('inc', 'devel_generate', 'image.devel_generate');
|
137
|
$path = devel_generate_image($extension, '100x100', '1500x1500');
|
138
|
}
|
139
|
else {
|
140
|
$extension = array_rand(array_flip($possible_extensions));
|
141
|
module_load_include('inc', 'devel_generate', 'file.devel_generate');
|
142
|
$path = devel_generate_textfile(mt_rand(1024, 102400));
|
143
|
}
|
144
|
|
145
|
$uri = file_entity_generate_unique_uri($extension);
|
146
|
$dir = dirname($uri);
|
147
|
if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
|
148
|
return FALSE;
|
149
|
}
|
150
|
|
151
|
if (file_unmanaged_move($path, $uri)) {
|
152
|
return $uri;
|
153
|
}
|
154
|
|
155
|
return FALSE;
|
156
|
}
|
157
|
|
158
|
function file_entity_generate_unique_uri($extension) {
|
159
|
module_load_include('inc', 'devel_generate');
|
160
|
do {
|
161
|
$uri = 'public://devel-generate/' . devel_generate_word(3) . '/' . devel_generate_word(16) . '.' . $extension;
|
162
|
} while (is_file($uri) || db_query("SELECT 1 FROM {file_managed} WHERE uri = :uri", array(':uri' => $uri))->fetchField());
|
163
|
return $uri;
|
164
|
}
|