123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Convert content subscriptions to the new elgg subscription
- *
- * Run for 5 seconds per request as set by $batch_run_time_in_secs. This includes
- * the engine loading time.
- */
- // from engine/start.php
- global $START_MICROTIME;
- $batch_run_time_in_secs = 5;
- if (get_input('upgrade_completed')) {
- // set the upgrade as completed
- $factory = new ElggUpgrade();
- $upgrade = $factory->getUpgradeFromPath('admin/upgrades/content_subscriptions');
- if ($upgrade instanceof ElggUpgrade) {
- $upgrade->setCompleted();
- }
- return true;
- }
- // Offset is the total amount of errors so far. We skip these
- // annotations to prevent them from possibly repeating the same error.
- $offset = (int) get_input('offset', 0);
- $limit = 25;
- $access_status = access_get_show_hidden_status();
- access_show_hidden_entities(true);
- // don't want any event or plugin hook handlers from plugins to run
- $original_events = _elgg_services()->events;
- $original_hooks = _elgg_services()->hooks;
- _elgg_services()->events = new Elgg\EventsService();
- _elgg_services()->hooks = new Elgg\PluginHooksService();
- elgg_register_plugin_hook_handler('permissions_check', 'all', 'elgg_override_permissions');
- elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'elgg_override_permissions');
- _elgg_services()->db->disableQueryCache();
- $success_count = 0;
- $error_count = 0;
- while ((microtime(true) - $START_MICROTIME) < $batch_run_time_in_secs) {
-
- $options = [
- 'type' => 'user',
- 'relationship' => CONTENT_SUBSCRIPTIONS_SUBSCRIPTION,
- 'inverse_relationship' => true,
- 'count' => true,
- ];
- $count = elgg_get_entities_from_relationship($options);
-
- if (!$count) {
- // no old subscriptions left
- $factory = new ElggUpgrade();
- $upgrade = $factory->getUpgradeFromPath('admin/upgrades/content_subscriptions');
- if ($upgrade instanceof ElggUpgrade) {
- $upgrade->setCompleted();
- }
-
- break;
- }
-
- $options['count'] = false;
- $options['offset'] = $offset;
- $options['limit'] = $limit;
-
- $users = elgg_get_entities_from_relationship($options);
- foreach ($users as $user) {
- $error_counter = 0;
-
- $subscription_options = [
- 'relationship' => CONTENT_SUBSCRIPTIONS_SUBSCRIPTION,
- 'relationship_guid' => $user->getGUID(),
- 'limit' => false,
- ];
-
- $batch = new ElggBatch('elgg_get_entities_from_relationship', $subscription_options);
- $batch->setIncrementOffset(false);
- foreach ($batch as $entity) {
-
- // for some reason you can't subscribe
- if (!content_subscriptions_can_subscribe($entity, $user->getGUID())) {
- if (!remove_entity_relationship($user->getGUID(), CONTENT_SUBSCRIPTIONS_SUBSCRIPTION, $entity->getGUID())) {
- $error_counter++;
- }
-
- continue;
- }
-
- // subscribe the new way
- content_subscriptions_subscribe($entity->getGUID(), $user->getGUID());
-
- // remove old link
- if (!remove_entity_relationship($user->getGUID(), CONTENT_SUBSCRIPTIONS_SUBSCRIPTION, $entity->getGUID())) {
- $error_counter++;
- }
- }
-
- if ($error_counter > 0) {
- $error_count++;
- } else {
- $success_count++;
- }
- }
- }
- access_show_hidden_entities($access_status);
- // replace events and hooks
- _elgg_services()->events = $original_events;
- _elgg_services()->hooks = $original_hooks;
- _elgg_services()->db->enableQueryCache();
- // Give some feedback for the UI
- echo json_encode([
- 'numSuccess' => $success_count,
- 'numErrors' => $error_count
- ]);
|