RoundCube Webmail
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.5 KiB

  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. | Licensed under the GNU GPL |
  10. | |
  11. | PURPOSE: |
  12. | Finally remove all db records marked as deleted some time ago |
  13. | |
  14. +-----------------------------------------------------------------------+
  15. | Author: Thomas Bruederli <roundcube@gmail.com> |
  16. +-----------------------------------------------------------------------+
  17. $Id$
  18. */
  19. define('INSTALL_PATH', realpath(dirname(__FILE__) . '/..') . '/' );
  20. require INSTALL_PATH.'program/include/clisetup.php';
  21. // mapping for table name => primary key
  22. $primary_keys = array(
  23. 'contacts' => "contact_id",
  24. 'contactgroups' => "contactgroup_id",
  25. );
  26. // connect to DB
  27. $RCMAIL = rcmail::get_instance();
  28. $db = $RCMAIL->get_dbh();
  29. $db->db_connect('w');
  30. if (!$db->is_connected() || $db->is_error())
  31. die("No DB connection\n");
  32. if (!empty($_SERVER['argv'][1]))
  33. $days = intval($_SERVER['argv'][1]);
  34. else
  35. $days = 7;
  36. // remove all deleted records older than two days
  37. $threshold = date('Y-m-d 00:00:00', time() - $days * 86400);
  38. foreach (array('contacts','contactgroups','identities') as $table) {
  39. $sqltable = get_table_name($table);
  40. // also delete linked records
  41. // could be skipped for databases which respect foreign key constraints
  42. if ($db->db_provider == 'sqlite'
  43. && ($table == 'contacts' || $table == 'contactgroups')
  44. ) {
  45. $pk = $primary_keys[$table];
  46. $memberstable = get_table_name('contactgroupmembers');
  47. $db->query(
  48. "DELETE FROM $memberstable".
  49. " WHERE $pk IN (".
  50. "SELECT $pk FROM $sqltable".
  51. " WHERE del=1 AND changed < ?".
  52. ")",
  53. $threshold);
  54. echo $db->affected_rows() . " records deleted from '$memberstable'\n";
  55. }
  56. // delete outdated records
  57. $db->query("DELETE FROM $sqltable WHERE del=1 AND changed < ?", $threshold);
  58. echo $db->affected_rows() . " records deleted from '$table'\n";
  59. }
  60. ?>