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