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)
40 // prune irrelivant stuff..
44 $this->prune((int)$opts['months']);
49 // 40 seconds ? to delete 100K records..
50 // DB_DataObject::debugLevel(1);
51 $f = DB_DataObject::Factory('reader_article');
53 DELETE FROM Events where
54 event_when < NOW() - INTERVAL {$inM} MONTH
60 // notificication events occur alot - so we should trash them more frequently..
61 /* $f = DB_DataObject::Factory('reader_article');
63 DELETE FROM Events where
64 event_when < NOW() - INTERVAL 1 MONTH
70 // rather than deleting them all, it's probably best to just delete notify events that occured to often.
71 // eg. when we tried to deliver multiple times without success...
74 SELECT on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm FROM Events
75 WHERE action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK GROUP BY on_id, on_table HAVING mm > 2 ORDER BY mm desc;
78 //DB_DataObject::debugLevel(1);
79 $f = DB_DataObject::Factory('Events');
81 $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
82 $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
83 $f->groupBy('on_id, on_table');
85 $f->orderBy('mm desc') ;
89 $q = DB_DataObject::Factory('Events');
90 $q->query("DELETE FROM Events where
95 on_table = '{$q->escape($f->on_table)}'
108 // pruning is for our press project - so we do not clean up dependant tables at present..
110 if (function_exists('posix_getpwuid')) {
111 $uinfo = posix_getpwuid( posix_getuid () );
113 $user = $uinfo['name'];
115 $user = getenv('USERNAME'); // windows.
118 $ff = HTML_Flexyframework::get()->Pman;
122 $rootDir = $ff['storedir'].'/_events_/'.$user;
124 $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
125 foreach($dirs as $d){
126 $mdirs = array_filter(glob($d."/*"), 'is_dir');
127 foreach($mdirs as $md){
128 $dirDate = str_replace($rootDir."/", '', $md);
129 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
130 //echo "remove $md\n";
132 // echo $md . " is removed. \n";
141 function delTree($dir)
143 $files = array_diff(scandir($dir), array('.','..'));
144 echo "$dir : Removing " . count($files) . " files\n";
146 foreach ($files as $file){
147 if (!file_exists("$dir/$file")) {
150 if (is_dir("$dir/$file")) {
151 $this->delTree("$dir/$file");
154 unlink("$dir/$file");