]> git.mxchange.org Git - friendica.git/blob - include/dbclean.php
5b80b084a3adf8e8dd9b05ad8fb0c772d3916ef4
[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 function dbclean_run(&$argv, &$argc) {
9         global $a, $db;
10
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
21         load_config('config');
22         load_config('system');
23
24         if ($argc == 2) {
25                 $stage = intval($argv[1]);
26         } else {
27                 $stage = 0;
28         }
29         remove_orphans($stage);
30         killme();
31 }
32
33 /**
34  * @brief Remove orphaned database entries
35  */
36 function remove_orphans($stage = 0) {
37         global $db;
38
39         if (($stage == 1) OR ($stage == 0)) {
40                 logger("Deleting orphaned data from thread table");
41                 if ($db->q("SELECT `iid` FROM `thread` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`parent` = `thread`.`iid`)", true)) {
42                         logger("found thread orphans: ".$db->num_rows());
43                         while ($orphan = $db->qfetch()) {
44                                 q("DELETE FROM `thread` WHERE `iid` = %d", intval($orphan["iid"]));
45                         }
46                 }
47                 $db->qclose();
48         }
49
50         if (($stage == 2) OR ($stage == 0)) {
51                 logger("Deleting orphaned data from notify table");
52                 if ($db->q("SELECT `iid` FROM `notify` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `notify`.`iid`)", true)) {
53                         logger("found notify orphans: ".$db->num_rows());
54                         while ($orphan = $db->qfetch()) {
55                                 q("DELETE FROM `notify` WHERE `iid` = %d", intval($orphan["iid"]));
56                         }
57                 }
58                 $db->qclose();
59         }
60
61
62         if (($stage == 3) OR ($stage == 0)) {
63                 logger("Deleting orphaned data from sign table");
64                 if ($db->q("SELECT `iid` FROM `sign` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `sign`.`iid`)", true)) {
65                         logger("found sign orphans: ".$db->num_rows());
66                         while ($orphan = $db->qfetch()) {
67                                 q("DELETE FROM `sign` WHERE `iid` = %d", intval($orphan["iid"]));
68                         }
69                 }
70                 $db->qclose();
71         }
72
73
74         if (($stage == 4) OR ($stage == 0)) {
75                 logger("Deleting orphaned data from term table");
76                 if ($db->q("SELECT `oid` FROM `term` WHERE NOT EXISTS (SELECT `id` FROM `item` WHERE `item`.`id` = `term`.`oid`)", true)) {
77                         logger("found term orphans: ".$db->num_rows());
78                         while ($orphan = $db->qfetch()) {
79                                 q("DELETE FROM `term` WHERE `oid` = %d", intval($orphan["oid"]));
80                         }
81                 }
82                 $db->qclose();
83         }
84
85         /// @todo Based on the following query we should remove some more data
86         // 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;
87
88         logger("Done deleting orphaned data from tables");
89 }
90
91 if (array_search(__file__,get_included_files())===0){
92   dbclean_run($_SERVER["argv"],$_SERVER["argc"]);
93   killme();
94 }
95 ?>