]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
Merge remote-tracking branch 'upstream/develop' into 1610-performance
[friendica.git] / include / dbclean.php
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 require_once("boot.php");
7
8 global $a, $db;
9
10 if(is_null($a))
11         $a = new App;
12
13 if(is_null($db)) {
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);
18 }
19
20 load_config('config');
21 load_config('system');
22
23 remove_orphans();
24 killme();
25
26 /**
27  * @brief Remove orphaned database entries
28  */
29 function remove_orphans() {
30          global $db;
31
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"]));
37         }
38         $db->qclose();
39
40
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"]));
46         }
47         $db->qclose();
48
49
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"]));
55         }
56         $db->qclose();
57
58
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"]));
64         }
65         $db->qclose();
66
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;
68
69         logger("Done deleting orphaned data from tables");
70 }
71 ?>