Thomas Bruederli
2012-12-16 a84efc689deded1a6593eed613e5270be562ec68
commit | author | age
a35062 1 #!/usr/bin/env php
T 2 <?php
3 /*
4  +-----------------------------------------------------------------------+
30aa4c 5  | bin/cleandb.sh                                                        |
a35062 6  |                                                                       |
e019f2 7  | This file is part of the Roundcube Webmail client                     |
f5e7b3 8  | Copyright (C) 2010, The Roundcube Dev Team                            |
7fe381 9  |                                                                       |
T 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.                     |
a35062 13  |                                                                       |
T 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(dirname(__FILE__) . '/..') . '/' );
6cc3f5 23
A 24 require INSTALL_PATH.'program/include/clisetup.php';
a35062 25
T 26 // mapping for table name => primary key
27 $primary_keys = array(
60ff01 28     'contacts' => "contact_id",
A 29     'contactgroups' => "contactgroup_id",
a35062 30 );
T 31
32 // connect to DB
33 $RCMAIL = rcmail::get_instance();
34 $db = $RCMAIL->get_dbh();
d36646 35 $db->db_connect('w');
a35062 36
d36646 37 if (!$db->is_connected() || $db->is_error())
T 38     die("No DB connection\n");
a35062 39
5ba5fd 40 if (!empty($_SERVER['argv'][1]))
60ff01 41     $days = intval($_SERVER['argv'][1]);
5ba5fd 42 else
60ff01 43     $days = 7;
5ba5fd 44
a35062 45 // remove all deleted records older than two days
5ba5fd 46 $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
a35062 47
T 48 foreach (array('contacts','contactgroups','identities') as $table) {
49
60ff01 50     $sqltable = get_table_name($table);
a35062 51
60ff01 52     // also delete linked records
A 53     // could be skipped for databases which respect foreign key constraints
54     if ($db->db_provider == 'sqlite'
55         && ($table == 'contacts' || $table == 'contactgroups')
56     ) {
57         $pk = $primary_keys[$table];
58         $memberstable = get_table_name('contactgroupmembers');
a35062 59
60ff01 60         $db->query(
A 61             "DELETE FROM $memberstable".
62             " WHERE $pk IN (".
63                 "SELECT $pk FROM $sqltable".
64                 " WHERE del=1 AND changed < ?".
65             ")",
66             $threshold);
a35062 67
60ff01 68         echo $db->affected_rows() . " records deleted from '$memberstable'\n";
a35062 69     }
T 70
60ff01 71     // delete outdated records
A 72     $db->query("DELETE FROM $sqltable WHERE del=1 AND changed < ?", $threshold);
73
74     echo $db->affected_rows() . " records deleted from '$table'\n";
a35062 75 }
T 76
77 ?>