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