10 require_once 'Pman.php';
11 class Pman_Core_Prune extends Pman
14 static $cli_desc = "Core Prune -- remove old event data (6 months is normally a good idea).";
15 static $cli_opts = array(
17 'desc' => 'How many months',
30 $ff = HTML_FlexyFramework::get();
31 if (!empty($ff->cli)) {
36 // return true;// for test only
40 function get($m="", $opts=array())
43 // prune irrelivant stuff..
47 $this->prune((int)$opts['months']);
52 // 40 seconds ? to delete 100K records..
53 // DB_DataObject::debugLevel(1);
55 $f = DB_DataObject::Factory('Events');
57 DELETE FROM Events where
58 event_when < NOW() - INTERVAL {$inM} MONTH
64 // notificication events occur alot - so we should trash them more frequently..
65 /* $f = DB_DataObject::Factory('reader_article');
67 DELETE FROM Events where
68 event_when < NOW() - INTERVAL 1 MONTH
74 // rather than deleting them all, it's probably best to just delete notify events that occured to often.
75 // eg. when we tried to deliver multiple times without success...
78 SELECT on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm FROM Events
79 WHERE action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK GROUP BY on_id, on_table HAVING mm > 2 ORDER BY mm desc;
82 DB_DataObject::debugLevel(1);
83 $f = DB_DataObject::Factory('Events');
85 $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
86 $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
87 $f->groupBy('on_id, on_table');
89 $f->orderBy('mm desc') ;
94 $q = DB_DataObject::Factory('Events');
95 $q->query("DELETE FROM Events where
100 on_table = '{$q->escape($f->on_table)}'
113 // pruning is for our press project - so we do not clean up dependant tables at present..
115 if (function_exists('posix_getpwuid')) {
116 $uinfo = posix_getpwuid( posix_getuid () );
118 $user = $uinfo['name'];
120 $user = getenv('USERNAME'); // windows.
123 $ff = HTML_Flexyframework::get()->Pman;
127 $rootDir = $ff['storedir'].'/_events_/'.$user;
129 $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
130 foreach($dirs as $d){
131 $mdirs = array_filter(glob($d."/*"), 'is_dir');
132 foreach($mdirs as $md){
133 $dirDate = str_replace($rootDir."/", '', $md);
134 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
135 //echo "remove $md\n";
137 // echo $md . " is removed. \n";
146 function delTree($dir)
148 $files = array_diff(scandir($dir), array('.','..'));
149 echo "$dir : Removing " . count($files) . " files\n";
151 foreach ($files as $file){
152 if (!file_exists("$dir/$file")) {
155 if (is_dir("$dir/$file")) {
156 $this->delTree("$dir/$file");
159 unlink("$dir/$file");