Prune.php
[Pman.Core] / Prune.php
1 <?php
2
3
4 /**
5  * Description of Prune
6  *
7  * @author chris
8  */
9
10 require_once 'Pman.php';
11 class Pman_Core_Prune extends Pman
12 {
13     //put your code here
14     static $cli_desc = "Core Prune -- remove old event data (6 months is normally a good idea).";
15     static $cli_opts = array(
16         'months' => array(
17             'desc' => 'How many months',
18             //'default' => 0,
19             'short' => 'm',
20             'min' => 1,
21             'max' => 1,
22             
23         )
24     );
25     var $cli = false;
26     
27     function getAuth() {
28         $ff = HTML_FlexyFramework::get();
29         if (!empty($ff->cli)) {
30             $this->cli = true;
31             return true;
32         }
33 //        return true;// for test only
34         return false;
35     }
36     
37     function get($m="", $opts=array())
38     {
39         echo "start?";
40         // prune irrelivant stuff..
41        
42         
43         
44         $this->prune((int)$opts['months']);
45     }
46     
47     function prune($inM)
48     {
49         
50         // rather than deleting them all, it's probably best to just delete notify events that occured to often.
51         // eg. when we tried to deliver multiple times without success...
52         /*
53          *
54          SELECT on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm FROM Events
55          WHERE action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK GROUP BY  on_id, on_table HAVING  mm > 2 ORDER BY mm desc;
56          */
57         
58         DB_DataObject::debugLevel(1);
59         $f = DB_DataObject::Factory('Events');
60         $f->selectAdd();
61         $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
62         $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
63         $f->groupBy('on_id, on_table');
64         $f->having("mm > 2");
65         $f->orderBy('mm desc') ;
66         $f->limit(10000);
67         exit;
68         $ar = $f->fetchAll();
69         foreach($ar as $f) {
70             $q = DB_DataObject::Factory('Events');
71             $q->query("DELETE FROM Events where 
72                   action = 'NOTIFY'
73                   AND
74                   on_id = {$f->on_id}
75                   AND
76                   on_table = '{$q->escape($f->on_table)}'
77                   AND
78                   id > {$f->min_id}
79                   AND
80                   id < {$f->max_id}
81             ");
82         }
83         
84         
85
86         
87         
88         
89         // pruning is for our press project - so we do not clean up dependant tables at present..
90         
91         if (function_exists('posix_getpwuid')) {
92             $uinfo = posix_getpwuid( posix_getuid () ); 
93          
94             $user = $uinfo['name'];
95         } else {
96             $user = getenv('USERNAME'); // windows.
97         }
98         
99         $ff = HTML_Flexyframework::get()->Pman;
100         
101         $y = date("Y");
102         $m = date("m");
103         $rootDir = $ff['storedir'].'/_events_/'.$user;
104         
105         $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
106         foreach($dirs as $d){
107             $mdirs = array_filter(glob($d."/*"), 'is_dir');
108             foreach($mdirs as $md){
109                 $dirDate = str_replace($rootDir."/", '', $md);
110                 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
111                     //echo "remove $md\n";
112                     $this->delTree($md);
113                       //  echo $md . " is removed. \n";
114                     
115                 }
116             }
117         }
118         
119         exit;
120     }
121     
122     function delTree($dir)
123     {
124         $files = array_diff(scandir($dir), array('.','..'));
125         echo "$dir : Removing " . count($files) . " files\n";
126         clearstatcache();
127         foreach ($files as $file){
128             if (!file_exists("$dir/$file")) {
129                 continue;
130             }
131             if (is_dir("$dir/$file")) {
132                 $this->delTree("$dir/$file");
133                 continue;
134             }
135             unlink("$dir/$file");
136         }
137         return rmdir($dir); 
138     }
139     
140 }