upgrade.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. /**
  3. * Convert content subscriptions to the new elgg subscription
  4. *
  5. * Run for 5 seconds per request as set by $batch_run_time_in_secs. This includes
  6. * the engine loading time.
  7. */
  8. // from engine/start.php
  9. global $START_MICROTIME;
  10. $batch_run_time_in_secs = 5;
  11. if (get_input('upgrade_completed')) {
  12. // set the upgrade as completed
  13. $factory = new ElggUpgrade();
  14. $upgrade = $factory->getUpgradeFromPath('admin/upgrades/content_subscriptions');
  15. if ($upgrade instanceof ElggUpgrade) {
  16. $upgrade->setCompleted();
  17. }
  18. return true;
  19. }
  20. // Offset is the total amount of errors so far. We skip these
  21. // annotations to prevent them from possibly repeating the same error.
  22. $offset = (int) get_input('offset', 0);
  23. $limit = 25;
  24. $access_status = access_get_show_hidden_status();
  25. access_show_hidden_entities(true);
  26. // don't want any event or plugin hook handlers from plugins to run
  27. $original_events = _elgg_services()->events;
  28. $original_hooks = _elgg_services()->hooks;
  29. _elgg_services()->events = new Elgg\EventsService();
  30. _elgg_services()->hooks = new Elgg\PluginHooksService();
  31. elgg_register_plugin_hook_handler('permissions_check', 'all', 'elgg_override_permissions');
  32. elgg_register_plugin_hook_handler('container_permissions_check', 'all', 'elgg_override_permissions');
  33. _elgg_services()->db->disableQueryCache();
  34. $success_count = 0;
  35. $error_count = 0;
  36. while ((microtime(true) - $START_MICROTIME) < $batch_run_time_in_secs) {
  37. $options = [
  38. 'type' => 'user',
  39. 'relationship' => CONTENT_SUBSCRIPTIONS_SUBSCRIPTION,
  40. 'inverse_relationship' => true,
  41. 'count' => true,
  42. ];
  43. $count = elgg_get_entities_from_relationship($options);
  44. if (!$count) {
  45. // no old subscriptions left
  46. $factory = new ElggUpgrade();
  47. $upgrade = $factory->getUpgradeFromPath('admin/upgrades/content_subscriptions');
  48. if ($upgrade instanceof ElggUpgrade) {
  49. $upgrade->setCompleted();
  50. }
  51. break;
  52. }
  53. $options['count'] = false;
  54. $options['offset'] = $offset;
  55. $options['limit'] = $limit;
  56. $users = elgg_get_entities_from_relationship($options);
  57. foreach ($users as $user) {
  58. $error_counter = 0;
  59. $subscription_options = [
  60. 'relationship' => CONTENT_SUBSCRIPTIONS_SUBSCRIPTION,
  61. 'relationship_guid' => $user->getGUID(),
  62. 'limit' => false,
  63. ];
  64. $batch = new ElggBatch('elgg_get_entities_from_relationship', $subscription_options);
  65. $batch->setIncrementOffset(false);
  66. foreach ($batch as $entity) {
  67. // for some reason you can't subscribe
  68. if (!content_subscriptions_can_subscribe($entity, $user->getGUID())) {
  69. if (!remove_entity_relationship($user->getGUID(), CONTENT_SUBSCRIPTIONS_SUBSCRIPTION, $entity->getGUID())) {
  70. $error_counter++;
  71. }
  72. continue;
  73. }
  74. // subscribe the new way
  75. content_subscriptions_subscribe($entity->getGUID(), $user->getGUID());
  76. // remove old link
  77. if (!remove_entity_relationship($user->getGUID(), CONTENT_SUBSCRIPTIONS_SUBSCRIPTION, $entity->getGUID())) {
  78. $error_counter++;
  79. }
  80. }
  81. if ($error_counter > 0) {
  82. $error_count++;
  83. } else {
  84. $success_count++;
  85. }
  86. }
  87. }
  88. access_show_hidden_entities($access_status);
  89. // replace events and hooks
  90. _elgg_services()->events = $original_events;
  91. _elgg_services()->hooks = $original_hooks;
  92. _elgg_services()->db->enableQueryCache();
  93. // Give some feedback for the UI
  94. echo json_encode([
  95. 'numSuccess' => $success_count,
  96. 'numErrors' => $error_count
  97. ]);