0
|
1 #!/usr/bin/env php
|
|
2 <?php
|
|
3 /*
|
|
4 +-----------------------------------------------------------------------+
|
|
5 | bin/cleandb.sh |
|
|
6 | |
|
|
7 | This file is part of the Roundcube Webmail client |
|
|
8 | Copyright (C) 2010, The Roundcube Dev Team |
|
|
9 | |
|
|
10 | Licensed under the GNU General Public License version 3 or |
|
|
11 | any later version with exceptions for skins & plugins. |
|
|
12 | See the README file for a full license statement. |
|
|
13 | |
|
|
14 | PURPOSE: |
|
|
15 | Finally remove all db records marked as deleted some time ago |
|
|
16 | |
|
|
17 +-----------------------------------------------------------------------+
|
|
18 | Author: Thomas Bruederli <roundcube@gmail.com> |
|
|
19 +-----------------------------------------------------------------------+
|
|
20 */
|
|
21
|
|
22 define('INSTALL_PATH', realpath(__DIR__ . '/..') . '/' );
|
|
23
|
|
24 require INSTALL_PATH.'program/include/clisetup.php';
|
|
25
|
|
26 // mapping for table name => primary key
|
|
27 $primary_keys = array(
|
|
28 'contacts' => "contact_id",
|
|
29 'contactgroups' => "contactgroup_id",
|
|
30 );
|
|
31
|
|
32 // connect to DB
|
|
33 $RCMAIL = rcube::get_instance();
|
|
34 $db = $RCMAIL->get_dbh();
|
|
35 $db->db_connect('w');
|
|
36
|
|
37 if (!$db->is_connected() || $db->is_error()) {
|
|
38 rcube::raise_error("No DB connection", false, true);
|
|
39 }
|
|
40
|
|
41 if (!empty($_SERVER['argv'][1]))
|
|
42 $days = intval($_SERVER['argv'][1]);
|
|
43 else
|
|
44 $days = 7;
|
|
45
|
|
46 // remove all deleted records older than two days
|
|
47 $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
|
|
48
|
|
49 foreach (array('contacts','contactgroups','identities') as $table) {
|
|
50
|
|
51 $sqltable = $db->table_name($table, true);
|
|
52
|
|
53 // also delete linked records
|
|
54 // could be skipped for databases which respect foreign key constraints
|
|
55 if ($db->db_provider == 'sqlite'
|
|
56 && ($table == 'contacts' || $table == 'contactgroups')
|
|
57 ) {
|
|
58 $pk = $primary_keys[$table];
|
|
59 $memberstable = $db->table_name('contactgroupmembers');
|
|
60
|
|
61 $db->query(
|
|
62 "DELETE FROM " . $db->quote_identifier($memberstable).
|
|
63 " WHERE `$pk` IN (".
|
|
64 "SELECT `$pk` FROM $sqltable".
|
|
65 " WHERE `del` = 1 AND `changed` < ?".
|
|
66 ")",
|
|
67 $threshold);
|
|
68
|
|
69 echo $db->affected_rows() . " records deleted from '$memberstable'\n";
|
|
70 }
|
|
71
|
|
72 // delete outdated records
|
|
73 $db->query("DELETE FROM $sqltable WHERE `del` = 1 AND `changed` < ?", $threshold);
|
|
74
|
|
75 echo $db->affected_rows() . " records deleted from '$table'\n";
|
|
76 }
|
|
77
|
|
78 ?>
|