upgrade_comments_access.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * Convert comment annotations to entities
  4. *
  5. * Run for 2 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 = 2;
  11. // if upgrade has run correctly, mark it done
  12. if (get_input('upgrade_completed')) {
  13. // set the upgrade as completed
  14. $factory = new ElggUpgrade();
  15. $upgrade = $factory->getUpgradeFromPath('admin/upgrades/commentaccess');
  16. if ($upgrade instanceof ElggUpgrade) {
  17. $upgrade->setCompleted();
  18. }
  19. return true;
  20. }
  21. // Offset is the total amount of errors so far. We skip these
  22. // comments to prevent them from possibly repeating the same error.
  23. $offset = get_input('offset', 0);
  24. $limit = 50;
  25. $access_status = access_get_show_hidden_status();
  26. access_show_hidden_entities(true);
  27. $success_count = 0;
  28. $error_count = 0;
  29. do {
  30. $dbprefix = elgg_get_config('dbprefix');
  31. $options = array(
  32. 'type' => 'object',
  33. 'subtype' => 'comment',
  34. 'joins' => array(
  35. "JOIN {$dbprefix}entities e2 ON e.container_guid = e2.guid"
  36. ),
  37. 'wheres' => array(
  38. "e.access_id != e2.access_id"
  39. ),
  40. 'offset' => $offset,
  41. 'limit' => $limit,
  42. 'preload_containers' => true
  43. );
  44. $comments = elgg_get_entities($options);
  45. foreach ($comments as $comment) {
  46. $container = $comment->getContainerEntity();
  47. if (!$container) {
  48. $error_count++;
  49. continue;
  50. }
  51. $comment->access_id = $container->access_id;
  52. if ($comment->save()) {
  53. $success_count++;
  54. } else {
  55. $error_count++;
  56. }
  57. }
  58. } while ((microtime(true) - $START_MICROTIME) < $batch_run_time_in_secs);
  59. access_show_hidden_entities($access_status);
  60. // Give some feedback for the UI
  61. echo json_encode(array(
  62. 'numSuccess' => $success_count,
  63. 'numErrors' => $error_count,
  64. ));