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',
28 $ff = HTML_FlexyFramework::get();
29 if (!empty($ff->cli)) {
33 // return true;// for test only
37 function get($m="", $opts=array())
40 // prune irrelivant stuff..
44 $this->prune((int)$opts['months']);
49 // 40 seconds ? to delete 100K records..
50 // DB_DataObject::debugLevel(1);
52 $f = DB_DataObject::Factory('Events');
54 DELETE FROM Events where
55 event_when < NOW() - INTERVAL {$inM} MONTH
61 // notificication events occur alot - so we should trash them more frequently..
62 /* $f = DB_DataObject::Factory('reader_article');
64 DELETE FROM Events where
65 event_when < NOW() - INTERVAL 1 MONTH
71 // rather than deleting them all, it's probably best to just delete notify events that occured to often.
72 // eg. when we tried to deliver multiple times without success...
75 SELECT on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm FROM Events
76 WHERE action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK GROUP BY on_id, on_table HAVING mm > 2 ORDER BY mm desc;
79 //DB_DataObject::debugLevel(1);
80 $f = DB_DataObject::Factory('Events');
82 $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
83 $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
84 $f->groupBy('on_id, on_table');
86 $f->orderBy('mm desc') ;
90 $q = DB_DataObject::Factory('Events');
91 $q->query("DELETE FROM Events where
96 on_table = '{$q->escape($f->on_table)}'
109 // pruning is for our press project - so we do not clean up dependant tables at present..
111 if (function_exists('posix_getpwuid')) {
112 $uinfo = posix_getpwuid( posix_getuid () );
114 $user = $uinfo['name'];
116 $user = getenv('USERNAME'); // windows.
119 $ff = HTML_Flexyframework::get()->Pman;
123 $rootDir = $ff['storedir'].'/_events_/'.$user;
125 $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
126 foreach($dirs as $d){
127 $mdirs = array_filter(glob($d."/*"), 'is_dir');
128 foreach($mdirs as $md){
129 $dirDate = str_replace($rootDir."/", '', $md);
130 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
131 //echo "remove $md\n";
133 // echo $md . " is removed. \n";
142 function delTree($dir)
144 $files = array_diff(scandir($dir), array('.','..'));
145 echo "$dir : Removing " . count($files) . " files\n";
147 foreach ($files as $file){
148 if (!file_exists("$dir/$file")) {
151 if (is_dir("$dir/$file")) {
152 $this->delTree("$dir/$file");
155 unlink("$dir/$file");