1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 | <?php
function batch_example_menu() {
$items = array();
$items['examples/batch_example'] = array(
'title' => 'Batch example',
'description' => 'Example of Drupal batch processing',
'page callback' => 'drupal_get_form',
'page arguments' => array('batch_example_simple_form'),
'access callback' => TRUE,
);
return $items;
}
function batch_example_simple_form() {
$form['description'] = array(
'#type' => 'markup',
'#markup' => t('This example offers two different batches. The first does 1000 identical operations, each completed in on run; the second does 20 operations, but each takes more than one run to operate if there are more than 5 nodes.'),
);
$form['batch'] = array(
'#type' => 'select',
'#title' => 'Choose batch',
'#options' => array(
'batch_1' => t('batch 1 - 1000 operations, each loading the same node'),
'batch_2' => t('batch 2 - 20 operations. each one loads all nodes 5 at a time'),
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Go',
);
$nid = batch_example_lowest_nid();
if (empty($nid)) {
drupal_set_message(t("You don't currently have any nodes, and this example requires a node to work with. As a result, this form is disabled."));
$form['submit']['#disabled'] = TRUE;
}
return $form;
}
function batch_example_simple_form_submit($form, &$form_state) {
$function = 'batch_example_' . $form_state['values']['batch'];
$_SESSION['http_request_count'] = 0;
$batch = $function();
batch_set($batch);
}
function batch_example_batch_1() {
$nid = batch_example_lowest_nid();
$num_operations = 1000;
drupal_set_message(t('Creating an array of @num operations', array('@num' => $num_operations)));
$operations = array();
for ($i = 0; $i<$num_operations; $i++) {
$operations[] = array('batch_example_op_1', array($nid, t('(Operation @operation)', array('@operation' => $i))));
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
);
return $batch;
}
function batch_example_op_1($nid, $operation_details, &$context) {
$node = node_load($nid, NULL, TRUE);
$context['results'][] = $node->nid . ' : ' . check_plain($node->title);
$context['message'] = t('Loading node "@title"', array('@title' => $node->title)) . ' ' . $operation_details;
_batch_example_update_http_requests();
}
function batch_example_batch_2() {
$num_operations = 20;
$node_count = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
drupal_set_message(t('There are @node_count nodes so each of the @num operations will require @count HTTP requests.', array('@node_count' => $node_count, '@num' => $num_operations, '@count' => ceil($node_count / 5))));
$operations = array();
for ($i = 0; $i < $num_operations; $i++) {
$operations[] = array('batch_example_op_2', array(t('(Operation @operation)', array('@operation' => $i))));
}
$batch = array(
'operations' => $operations,
'finished' => 'batch_example_finished',
'title' => t('Processing batch 2'),
'init_message' => t('Batch 2 is starting.'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('Batch 2 has encountered an error.'),
);
return $batch;
}
function batch_example_op_2($operation_details, &$context) {
if (empty($context['sandbox'])) {
$context['sandbox'] = array();
$context['sandbox']['progress'] = 0;
$context['sandbox']['current_node'] = 0;
$context['sandbox']['max'] = db_query('SELECT COUNT(DISTINCT nid) FROM {node}')->fetchField();
}
$limit = 5;
$result = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->where('n.nid > :nid', array(':nid' => $context['sandbox']['current_node']))
->extend('PagerDefault')
->limit($limit)
->execute();
foreach ($result as $row) {
$node = node_load($row->nid, NULL, TRUE);
$context['results'][] = $node->nid . ' : ' . check_plain($node->title) . ' ' . $operation_details;
$context['sandbox']['progress']++;
$context['sandbox']['current_node'] = $node->nid;
$context['message'] = check_plain($node->title);
}
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = ($context['sandbox']['progress'] >= $context['sandbox']['max']);
}
_batch_example_update_http_requests();
}
function batch_example_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('@count results processed in @requests HTTP requests.', array('@count' => count($results), '@requests' => _batch_example_get_http_requests())));
drupal_set_message(t('The final result was "%final"', array('%final' => end($results))));
}
else {
$error_operation = reset($operations);
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))));
}
}
function batch_example_lowest_nid() {
$select = db_select('node', 'n')
->fields('n', array('nid'))
->orderBy('n.nid', 'ASC')
->extend('PagerDefault')
->limit(1);
$nid = $select->execute()->fetchField();
return $nid;
}
function _batch_example_update_http_requests() {
$_SESSION['http_request_count']++;
}
function _batch_example_get_http_requests() {
return !empty($_SESSION['http_request_count']) ? $_SESSION['http_request_count'] : 0;
}
|