commit | author | age
|
a35062
|
1 |
#!/usr/bin/env php |
T |
2 |
<?php |
|
3 |
/* |
|
4 |
|
|
5 |
+-----------------------------------------------------------------------+ |
|
6 |
| bin/cleandb.php | |
|
7 |
| | |
|
8 |
| This file is part of the RoundCube Webmail client | |
|
9 |
| Copyright (C) 2010, RoundCube Dev. - Switzerland | |
|
10 |
| Licensed under the GNU GPL | |
|
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 |
if (php_sapi_name() != 'cli') { |
|
24 |
die('Not on the "shell" (php-cli).'); |
|
25 |
} |
|
26 |
|
|
27 |
define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' ); |
|
28 |
require INSTALL_PATH.'program/include/iniset.php'; |
|
29 |
|
|
30 |
// mapping for table name => primary key |
|
31 |
$primary_keys = array( |
|
32 |
'contacts' => "contact_id", |
|
33 |
'contactgroups' => "contactgroup_id", |
|
34 |
); |
|
35 |
|
|
36 |
// connect to DB |
|
37 |
$RCMAIL = rcmail::get_instance(); |
|
38 |
$db = $RCMAIL->get_dbh(); |
|
39 |
|
|
40 |
if (!$db->is_connected() || $db->is_error) |
|
41 |
die("No DB connection"); |
|
42 |
|
5ba5fd
|
43 |
if (!empty($_SERVER['argv'][1])) |
A |
44 |
$days = intval($_SERVER['argv'][1]); |
|
45 |
else |
|
46 |
$days = 7; |
|
47 |
|
a35062
|
48 |
// remove all deleted records older than two days |
5ba5fd
|
49 |
$threshold = date('Y-m-d 00:00:00', time() - $days * 86400); |
a35062
|
50 |
|
T |
51 |
foreach (array('contacts','contactgroups','identities') as $table) { |
|
52 |
// also delete linked records |
|
53 |
// could be skipped for databases which respect foreign key constraints |
5ba5fd
|
54 |
/* |
a35062
|
55 |
if ($table == 'contacts' || $table == 'contactgroups') { |
T |
56 |
$ids = array(); |
|
57 |
$pk = $primary_keys[$table]; |
|
58 |
|
|
59 |
$result = $db->query( |
|
60 |
"SELECT $pk FROM ".get_table_name($table)." |
|
61 |
WHERE del=1 AND changed < ".$db->quote($threshold)); |
|
62 |
|
|
63 |
while ($result && ($sql_arr = $db->fetch_assoc($result))) |
|
64 |
$ids[] = $sql_arr[$pk]; |
|
65 |
|
|
66 |
if (count($ids)) { |
|
67 |
$db->query( |
|
68 |
"DELETE FROM ".get_table_name('contactgroupmembers')." |
|
69 |
WHERE $pk IN (".join(',', $ids).")"); |
|
70 |
|
|
71 |
echo $db->affected_rows() . " records deleted from '".get_table_name('contactgroupmembers')."'\n"; |
|
72 |
} |
|
73 |
} |
5ba5fd
|
74 |
*/ |
a35062
|
75 |
// delete outdated records |
T |
76 |
$db->query( |
|
77 |
"DELETE FROM ".get_table_name($table)." |
|
78 |
WHERE del=1 AND changed < ".$db->quote($threshold)); |
|
79 |
|
|
80 |
echo $db->affected_rows() . " records deleted from '$table'\n"; |
|
81 |
} |
|
82 |
|
|
83 |
?> |