3 * @file include/dbclean.php
4 * @brief The script is called from time to time to clean the database entries and remove orphaned data.
6 require_once("boot.php");
14 @include(".htconfig.php");
15 require_once("include/dba.php");
16 $db = new dba($db_host, $db_user, $db_pass, $db_data);
17 unset($db_host, $db_user, $db_pass, $db_data);
20 load_config('config');
21 load_config('system');
27 * @brief Remove orphaned database entries
29 function remove_orphans() {
32 logger("Deleting orphaned data from thread table");
33 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
34 logger("found thread orphans: ".$db->num_rows());
35 while ($orphan = $db->qfetch())
36 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
41 logger("Deleting orphaned data from notify table");
42 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
43 logger("found notify orphans: ".$db->num_rows());
44 while ($orphan = $db->qfetch())
45 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
50 logger("Deleting orphaned data from sign table");
51 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
52 logger("found sign orphans: ".$db->num_rows());
53 while ($orphan = $db->qfetch())
54 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
59 logger("Deleting orphaned data from term table");
60 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
61 logger("found term orphans: ".$db->num_rows());
62 while ($orphan = $db->qfetch())
63 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
67 // SELECT `id`, `received`, `created`, `guid` FROM `item` WHERE `uid` = 0 AND NOT EXISTS (SELECT `guid` FROM `item` AS `i` WHERE `item`.`guid` = `i`.`guid` AND `i`.`uid` != 0) LIMIT 1;
69 logger("Done deleting orphaned data from tables");