function activity_creator_post_update_8802_remove_orphaned_activities in Open Social 10.3.x
Same name and namespace in other branches
- 8.9 modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8802_remove_orphaned_activities()
- 8.8 modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8802_remove_orphaned_activities()
- 10.0.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8802_remove_orphaned_activities()
- 10.1.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8802_remove_orphaned_activities()
- 10.2.x modules/custom/activity_creator/activity_creator.post_update.php \activity_creator_post_update_8802_remove_orphaned_activities()
Remove orphaned activities notification status.
File
- modules/
custom/ activity_creator/ activity_creator.post_update.php, line 76 - Contains post update hook implementations.
Code
function activity_creator_post_update_8802_remove_orphaned_activities(&$sandbox) {
$database = \Drupal::database();
// On the first run, we gather all of our initial
// data as well as initialize all of our sandbox variables to be used in
// managing the future batch requests.
if (!isset($sandbox['activities_id'])) {
// We start the batch by running some SELECT queries up front
// as concisely as possible. The results of these expensive queries
// will be cached by the Batch API so we do not have to look up
// this data again during each iteration of the batch.
// Get all the activity ids from our notification table.
$activity_notification_ids = $database
->select('activity_notification_status', 'ans')
->fields('ans', [
'aid',
])
->execute()
->fetchCol();
// Get activity ids from entity table.
$activity_ids = $database
->select('activity', 'aid')
->fields('aid', [
'id',
])
->execute()
->fetchCol();
// Now we initialize the sandbox variables.
// These variables will persist across the Batch API’s subsequent calls
// to our update hook, without us needing to make those initial
// expensive SELECT queries above ever again.
// 'count' is the number of total records we’ll be processing.
$sandbox['count'] = 0;
// We take store a diff of both the results which will contain the result
// of activity ids which are not present in system anymore. We will
// remove them in batch later.
if (!empty($activity_notification_ids) && !empty($activity_ids)) {
$sandbox['activities_id'] = array_diff($activity_notification_ids, $activity_ids);
$sandbox['count'] = count($sandbox['activities_id']);
}
// If 'count' is empty, we have nothing to process.
if (empty($sandbox['count'])) {
$sandbox['#finished'] = 1;
return;
}
// 'progress' will represent the current progress of our processing.
$sandbox['progress'] = 0;
// 'activities_per_batch' is a custom amount that we’ll use to limit
// how many activities we’re processing in each batch.
// The variables value can be declared in settings file of Drupal.
$sandbox['activities_per_batch'] = Settings::get('activity_update_batch_size', 5000);
}
// Initialization code done.
// The following code will always run:
// both during the first run AND during any subsequent batches.
// Remove the entries from activity_notification_status table which have
// activity id that doest not exists any more.
\Drupal::service('activity_creator.activity_notifications')
->deleteNotificationsbyIds(array_splice($sandbox['activities_id'], 0, 5000));
// Calculates current batch range.
$range_end = $sandbox['progress'] + $sandbox['activities_per_batch'];
if ($range_end > $sandbox['count']) {
$range_end = $sandbox['count'];
}
// Update the batch variables to track our progress.
$sandbox['progress'] = $range_end;
// We can calculate our current progress via a mathematical fraction.
$progress_fraction = $sandbox['progress'] / $sandbox['count'];
// Drupal’s Batch API will stop executing our update hook as soon as
// $sandbox['#finished'] == 1 (viz., it evaluates to TRUE).
$sandbox['#finished'] = empty($sandbox['activities_id']) ? 1 : ($sandbox['count'] - count($sandbox['activities_id'])) / $sandbox['count'];
}