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.
 
 
 
 
 
 

173 lines
5.4 KiB

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