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())
42 // prune irrelivant stuff..
46 $this->prune((int)$opts['months']);
51 // 40 seconds ? to delete 100K records..
52 // DB_DataObject::debugLevel(1);
54 $f = DB_DataObject::Factory('Events');
56 DELETE FROM Events where
57 event_when < NOW() - INTERVAL {$inM} MONTH
63 // notificication events occur alot - so we should trash them more frequently..
64 /* $f = DB_DataObject::Factory('reader_article');
66 DELETE FROM Events where
67 event_when < NOW() - INTERVAL 1 MONTH
73 // rather than deleting them all, it's probably best to just delete notify events that occured to often.
74 // eg. when we tried to deliver multiple times without success...
77 SELECT on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm FROM Events
78 WHERE action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK GROUP BY on_id, on_table HAVING mm > 2 ORDER BY mm desc;
81 //DB_DataObject::debugLevel(1);
82 $f = DB_DataObject::Factory('Events');
83 $before = $f->count();
85 $f = DB_DataObject::Factory('Events');
87 $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
88 $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
89 $f->groupBy('on_id, on_table');
91 $f->orderBy('mm desc') ;
96 $q = DB_DataObject::Factory('Events');
97 $q->query("DELETE FROM Events where
102 on_table = '{$q->escape($f->on_table)}'
110 $f = DB_DataObject::Factory('Events');
111 $after = $f->count();
112 echo "DELETED : " . ($before - $after) . " records\n";
117 // pruning is for our press project - so we do not clean up dependant tables at present..
119 if (function_exists('posix_getpwuid')) {
120 $uinfo = posix_getpwuid( posix_getuid () );
122 $user = $uinfo['name'];
124 $user = getenv('USERNAME'); // windows.
127 $ff = HTML_Flexyframework::get()->Pman;
131 $rootDir = $ff['storedir'].'/_events_/'.$user;
133 $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
134 foreach($dirs as $d){
135 $mdirs = array_filter(glob($d."/*"), 'is_dir');
136 foreach($mdirs as $md){
137 $dirDate = str_replace($rootDir."/", '', $md);
138 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
139 //echo "remove $md\n";
141 // echo $md . " is removed. \n";
150 function delTree($dir)
152 $files = array_diff(scandir($dir), array('.','..'));
153 echo "$dir : Removing " . count($files) . " files\n";
155 foreach ($files as $file){
156 if (!file_exists("$dir/$file")) {
159 if (is_dir("$dir/$file")) {
160 $this->delTree("$dir/$file");
163 unlink("$dir/$file");