-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathactivity.batch.inc
More file actions
185 lines (171 loc) · 5.68 KB
/
activity.batch.inc
File metadata and controls
185 lines (171 loc) · 5.68 KB
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
<?php
// $Id: $
/**
* @file:
* Contains Batch API functions for long running processing.
*/
/**
* Batch API processing operation. Rebuilding Access table.
*
* @param array $context
* The batch api context array.
*
* @return none
*/
function activity_batch_access_rebuild_process(&$context) {
if (!isset($context['sandbox']['last_aid'])) {
// Set up the sandbox for the first time.
$context['sandbox']['last_aid'] = 0;
$context['sandbox']['progress'] = 0;
// Activity can be happening on the site. Any Activity happening after this point
// will not be rebuilt. This is ok, as in that case, the new Activity will receive
// any and all new Activity Access Realms.
$context['sandbox']['max'] = db_query("SELECT COUNT(aid) FROM {activity}")->fetchField();
}
// Process 100 Activities at a time.
$limit = 100;
$activities = db_select('activity', 'a')
->fields('a')
->condition('aid', $context['sandbox']['last_aid'], '>')
->range(0, $limit)
->execute()
->fetchAll();
foreach ($activities as $activity) {
$grants = activity_get_grants($activity);
// Delete existing records.
db_delete('activity_access')
->condition('aid', $activity->aid)
->execute();
// Insert new ones.
foreach ($grants as $realm => $values) {
foreach ($values as $value) {
$perm = new stdClass();
$perm->aid = $activity->aid;
$perm->realm = $realm;
$perm->value = $value;
drupal_write_record('activity_access', $perm);
}
}
// Update sandbox variables.
$context['sandbox']['last_aid'] = $activity->aid;
$context['sandbox']['progress']++;
}
// Check if not finished.
if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
// If finished, delete the sandbox.
unset($context['sandbox']);
}
}
/**
* Batch operation callback to generate new messages.
*
* @param $action_id
* The {actions}.aid to generate messages for.
* @param array $context
* The context of this batch operation.
*/
function activity_batch_recreate_messages_step($action_id, &$context) {
if (!isset($context['sandbox']['last_aid'])) {
$context['sandbox'] = array(
'last_aid' => 0,
'progress' => 0,
'max' => db_query("SELECT COUNT(aid) FROM {activity} WHERE actions_id = :action_id", array(":action_id" => $action_id))->fetchField(),
);
}
$limit = 50;
$records = db_select('activity', 'a')
->fields('a')
->condition('a.actions_id', $action_id)
->condition('a.aid', $context['sandbox']['last_aid'], '>')
->range(0, $limit)
->execute()
->fetchAll();
$context['sandbox']['progress'] += count($records);
activity_recreate_messages($records);
if ($context['sandbox']['progress'] < $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
else {
unset($context['sandbox']);
}
}
/**
* Batch deletion step.
*
* @param $aid
* The actions.aid for this template.
* @param $batch_context
* The context array for this batch operation.
*/
function activity_batch_delete($aid, &$batch_context) {
if (!isset($batch_context['sandbox']['last_activity_id'])) {
$batch_context['sandbox']['last_activity_id'] = 0;
$batch_context['sandbox']['progress'] = 0;
$batch_context['sandbox']['max'] = db_query("SELECT COUNT(aid) FROM {activity} WHERE actions_id = :aid", array(':aid' => $aid))->fetchField();
}
$limit = 200;
$activity_to_be_deleted = db_select('activity', 'a')
->fields('a', array('aid'))
->condition('a.aid', $batch_context['sandbox']['last_activity_id'], '>')
->condition('a.actions_id', $aid)
->orderBy('a.aid', 'ASC')
->range(0, $limit)
->execute()
->fetchAll();
$activity_ids = array();
if (!empty($activity_to_be_deleted)) {
foreach ($activity_to_be_deleted as $row) {
$activity_ids[] = $row->aid;
$batch_context['sandbox']['last_activity_id'] = $row->aid;
$batch_context['sandbox']['progress']++;
}
activity_delete($activity_ids);
}
// Check if not finished.
if (!empty($activity_to_be_deleted) && $batch_context['sandbox']['progress'] < $batch_context['sandbox']['max']) {
$batch_context['finished'] = $batch_context['sandbox']['progress'] / $batch_context['sandbox']['max'];
}
else {
// If finished, delete the sandbox.
unset($batch_context['sandbox']);
}
}
/**
* Batch regeneration step.
*
* @param $aid
* The actions.aid for this template.
* @param $batch_context
* An array representing the context for the batch operation.
*/
function activity_batch_regenerate_step($aid, &$batch_context) {
$handler = activity_handler_batchable_load($aid);
if (empty($handler)) {
// Return error? Done?
}
$limit = 50;
// Set up the sandbox for the regeneration.
if (!isset($batch_context['sandbox']['progress'])) {
$batch_context['sandbox']['progress'] = 0;
$batch_context['sandbox']['last_eid'] = 0;
}
// Extracts $arguments and $total.
extract($handler->listEids($batch_context['sandbox']['progress'], $limit));
foreach ($arguments as $eid => $arguments) {
// Ensure they are there.
$arguments += array('argument1' => NULL, 'argument2' => NULL);
activity_save_activity($handler, $eid, $arguments['argument1'], $arguments['argument2']);
$batch_context['sandbox']['progress']++;
}
// Check if not finished.
if ($batch_context['sandbox']['progress'] < $total) {
$batch_context['finished'] = $batch_context['sandbox']['progress'] / $batch_context['sandbox']['max'];
}
else {
// If finished, delete the sandbox.
unset($batch_context['sandbox']);
}
}