Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.9KB

  1. <?php
  2. /**
  3. * @file include/dbclean.php
  4. * @brief The script is called from time to time to clean the database entries and remove orphaned data.
  5. */
  6. use \Friendica\Core\Config;
  7. function dbclean_run(&$argv, &$argc) {
  8. if (!Config::get('system', 'dbclean', false)) {
  9. return;
  10. }
  11. if ($argc == 2) {
  12. $stage = intval($argv[1]);
  13. } else {
  14. $stage = 0;
  15. }
  16. if ($stage == 0) {
  17. proc_run(PRIORITY_LOW, 'include/dbclean.php', 1);
  18. proc_run(PRIORITY_LOW, 'include/dbclean.php', 2);
  19. proc_run(PRIORITY_LOW, 'include/dbclean.php', 3);
  20. proc_run(PRIORITY_LOW, 'include/dbclean.php', 4);
  21. proc_run(PRIORITY_LOW, 'include/dbclean.php', 5);
  22. proc_run(PRIORITY_LOW, 'include/dbclean.php', 6);
  23. proc_run(PRIORITY_LOW, 'include/dbclean.php', 7);
  24. } else {
  25. remove_orphans($stage);
  26. }
  27. }
  28. /**
  29. * @brief Remove orphaned database entries
  30. */
  31. function remove_orphans($stage = 0) {
  32. global $db;
  33. $count = 0;
  34. // We split the deletion in many small tasks
  35. $limit = 1000;
  36. if (($stage == 1) OR ($stage == 0)) {
  37. logger("Deleting old global item entries from item table without user copy");
  38. $r = dba::p("SELECT `id` FROM `item` WHERE `uid` = 0
  39. AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0)
  40. AND `received` < UTC_TIMESTAMP() - INTERVAL 90 DAY LIMIT ".intval($limit));
  41. $count = dba::num_rows($r);
  42. if ($count > 0) {
  43. logger("found global item orphans: ".$count);
  44. while ($orphan = dba::fetch($r)) {
  45. q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
  46. }
  47. }
  48. dba::close($r);
  49. logger("Done deleting old global item entries from item table without user copy");
  50. }
  51. if (($stage == 2) OR ($stage == 0)) {
  52. logger("Deleting items without parents");
  53. $r = dba::p("SELECT `id` FROM `item` WHERE NOT EXISTS (SELECT `id` FROM `item` AS `i` WHERE `item`.`parent` = `i`.`id`) LIMIT ".intval($limit));
  54. $count = dba::num_rows($r);
  55. if ($count > 0) {
  56. logger("found item orphans without parents: ".$count);
  57. while ($orphan = dba::fetch($r)) {
  58. q("DELETE FROM `item` WHERE `id` = %d", intval($orphan["id"]));
  59. }
  60. }
  61. dba::close($r);
  62. logger("Done deleting items without parents");
  63. }
  64. if (($stage == 3) OR ($stage == 0)) {
  65. logger("Deleting orphaned data from thread table");
  66. $r = dba::p("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`) LIMIT ".intval($limit));
  67. $count = dba::num_rows($r);
  68. if ($count > 0) {
  69. logger("found thread orphans: ".$count);
  70. while ($orphan = dba::fetch($r)) {
  71. q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
  72. }
  73. }
  74. dba::close($r);
  75. logger("Done deleting orphaned data from thread table");
  76. }
  77. if (($stage == 4) OR ($stage == 0)) {
  78. logger("Deleting orphaned data from notify table");
  79. $r = dba::p("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`) LIMIT ".intval($limit));
  80. $count = dba::num_rows($r);
  81. if ($count > 0) {
  82. logger("found notify orphans: ".$count);
  83. while ($orphan = dba::fetch($r)) {
  84. q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
  85. }
  86. }
  87. dba::close($r);
  88. logger("Done deleting orphaned data from notify table");
  89. }
  90. if (($stage == 5) OR ($stage == 0)) {
  91. logger("Deleting orphaned data from notify-threads table");
  92. $r = dba::p("SELECT `id` FROM `notify-threads` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `notify-threads`.`master-parent-item`) LIMIT ".intval($limit));
  93. $count = dba::num_rows($r);
  94. if ($count > 0) {
  95. logger("found notify-threads orphans: ".$count);
  96. while ($orphan = dba::fetch($r)) {
  97. q("DELETE FROM `notify-threads` WHERE `id` = %d", intval($orphan["id"]));
  98. }
  99. }
  100. dba::close($r);
  101. logger("Done deleting orphaned data from notify-threads table");
  102. }
  103. if (($stage == 6) OR ($stage == 0)) {
  104. logger("Deleting orphaned data from sign table");
  105. $r = dba::p("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`) LIMIT ".intval($limit));
  106. $count = dba::num_rows($r);
  107. if ($count > 0) {
  108. logger("found sign orphans: ".$count);
  109. while ($orphan = dba::fetch($r)) {
  110. q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
  111. }
  112. }
  113. dba::close($r);
  114. logger("Done deleting orphaned data from sign table");
  115. }
  116. if (($stage == 7) OR ($stage == 0)) {
  117. logger("Deleting orphaned data from term table");
  118. $r = dba::p("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`) LIMIT ".intval($limit));
  119. $count = dba::num_rows($r);
  120. if ($count > 0) {
  121. logger("found term orphans: ".$count);
  122. while ($orphan = dba::fetch($r)) {
  123. q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
  124. }
  125. }
  126. dba::close($r);
  127. logger("Done deleting orphaned data from term table");
  128. }
  129. // Call it again if not all entries were purged
  130. if (($stage != 0) AND ($count > 0)) {
  131. proc_run(PRIORITY_MEDIUM, 'include/dbclean.php');
  132. }
  133. }