alecpl
2012-04-15 831c830124c5cafeb36e952acacc01e36de50d78
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                            |
7fe381 10  |                                                                       |
T 11  | Licensed under the GNU General Public License version 3 or            |
12  | any later version with exceptions for skins & plugins.                |
13  | See the README file for a full license statement.                     |
a35062 14  |                                                                       |
T 15  | PURPOSE:                                                              |
16  |   Finally remove all db records marked as deleted some time ago       |
17  |                                                                       |
18  +-----------------------------------------------------------------------+
19  | Author: Thomas Bruederli <roundcube@gmail.com>                        |
20  +-----------------------------------------------------------------------+
21
22  $Id$
23
24 */
25
26 define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
6cc3f5 27
A 28 require INSTALL_PATH.'program/include/clisetup.php';
a35062 29
T 30 // mapping for table name => primary key
31 $primary_keys = array(
60ff01 32     'contacts' => "contact_id",
A 33     'contactgroups' => "contactgroup_id",
a35062 34 );
T 35
36 // connect to DB
37 $RCMAIL = rcmail::get_instance();
38 $db = $RCMAIL->get_dbh();
d36646 39 $db->db_connect('w');
a35062 40
d36646 41 if (!$db->is_connected() || $db->is_error())
T 42     die("No DB connection\n");
a35062 43
5ba5fd 44 if (!empty($_SERVER['argv'][1]))
60ff01 45     $days = intval($_SERVER['argv'][1]);
5ba5fd 46 else
60ff01 47     $days = 7;
5ba5fd 48
a35062 49 // remove all deleted records older than two days
5ba5fd 50 $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
a35062 51
T 52 foreach (array('contacts','contactgroups','identities') as $table) {
53
60ff01 54     $sqltable = get_table_name($table);
a35062 55
60ff01 56     // also delete linked records
A 57     // could be skipped for databases which respect foreign key constraints
58     if ($db->db_provider == 'sqlite'
59         && ($table == 'contacts' || $table == 'contactgroups')
60     ) {
61         $pk = $primary_keys[$table];
62         $memberstable = get_table_name('contactgroupmembers');
a35062 63
60ff01 64         $db->query(
A 65             "DELETE FROM $memberstable".
66             " WHERE $pk IN (".
67                 "SELECT $pk FROM $sqltable".
68                 " WHERE del=1 AND changed < ?".
69             ")",
70             $threshold);
a35062 71
60ff01 72         echo $db->affected_rows() . " records deleted from '$memberstable'\n";
a35062 73     }
T 74
60ff01 75     // delete outdated records
A 76     $db->query("DELETE FROM $sqltable WHERE del=1 AND changed < ?", $threshold);
77
78     echo $db->affected_rows() . " records deleted from '$table'\n";
a35062 79 }
T 80
81 ?>