upgrade_datadirs.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * Move user data directories
  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. // Migrate also directories that belong to hidden users
  9. $access_status = access_get_show_hidden_status();
  10. access_show_hidden_entities(true);
  11. $helper = new Elgg\Upgrades\Helper2013022000(
  12. elgg_get_site_entity()->guid,
  13. elgg_get_config('dbprefix')
  14. );
  15. // from engine/start.php
  16. global $START_MICROTIME;
  17. $batch_run_time_in_secs = 2;
  18. $data_root = elgg_get_config('dataroot');
  19. $cleanup_years = array();
  20. $num_successes = 0;
  21. $num_errors = 0;
  22. $is_complete = true;
  23. _elgg_services()->db->disableQueryCache();
  24. $batch = new ElggBatch('elgg_get_entities', $helper->getBatchOptions(), null, 50, false);
  25. foreach ($batch as $user_row) {
  26. if ((microtime(true) - $START_MICROTIME) > $batch_run_time_in_secs) {
  27. $is_complete = false;
  28. break;
  29. }
  30. $guid = $user_row->guid;
  31. $from = $data_root . $helper->makeMatrix($user_row);
  32. $bucket_dir = $data_root . $helper->getLowerBucketBound($guid);
  33. $to = "$bucket_dir/$guid";
  34. if (!is_dir($from)) {
  35. $num_successes += 1;
  36. $helper->markSuccess($guid);
  37. continue;
  38. }
  39. // make sure bucket dir exists
  40. if (!is_dir($bucket_dir)) {
  41. // same perms as ElggDiskFilestore.
  42. if (!mkdir($bucket_dir, 0700, true)) {
  43. register_error("[$guid] Failed creating `$bucket_dir`");
  44. $num_errors += 1;
  45. $helper->markFailure($guid);
  46. continue;
  47. }
  48. }
  49. if (!rename($from, $to)) {
  50. register_error("[$guid] Failed moving `$from` to `$to`");
  51. $num_errors += 1;
  52. $helper->markFailure($guid);
  53. } else {
  54. $num_successes += 1;
  55. $helper->markSuccess($guid);
  56. }
  57. // store the year for cleanup
  58. $year = date('Y', $user_row->time_created);
  59. if (!in_array($year, $cleanup_years)) {
  60. $cleanup_years[] = $year;
  61. }
  62. }
  63. // remove all dirs that are empty.
  64. // @todo this could take some time, so we may want to lower the batch run time to compensate.
  65. foreach ($cleanup_years as $year) {
  66. $helper->removeDirIfEmpty($data_root . $year);
  67. }
  68. if ($is_complete && !$helper->hasFailures()) {
  69. // migration has completed, lets clean up
  70. $helper->forgetSuccesses();
  71. // set the upgrade as completed
  72. $factory = new ElggUpgrade();
  73. $upgrade = $factory->getUpgradeFromPath('admin/upgrades/datadirs');
  74. if ($upgrade instanceof ElggUpgrade) {
  75. $upgrade->setCompleted();
  76. }
  77. }
  78. access_show_hidden_entities($access_status);
  79. _elgg_services()->db->enableQueryCache();
  80. echo json_encode(array(
  81. 'numSuccess' => $num_successes,
  82. 'numErrors' => $num_errors,
  83. ));