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         
40         // prune irrelivant stuff..
41        
42         
43         
44         $this->prune((int)$opts['months']);
45     }
46     
47     function prune($inM)
48     {
49         // 40 seconds ? to delete 100K records..
50        // DB_DataObject::debugLevel(1);
51        /*
52         $f = DB_DataObject::Factory('Events');
53         $f->query("
54             DELETE FROM Events where 
55                   event_when < NOW() - INTERVAL {$inM} MONTH
56                   AND
57                   action != 'NOTIFY'
58                   LIMIT 100000
59         ");
60         */
61         // notificication events occur alot - so we should trash them more frequently..
62       /*  $f = DB_DataObject::Factory('reader_article');
63         $f->query("
64             DELETE FROM Events where 
65                   event_when < NOW() - INTERVAL 1 MONTH
66                   AND
67                   action IN ('NOTIFY')
68                   LIMIT 100000
69         ");
70         */
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...
73         /*
74          *
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;
77          */
78         
79         DB_DataObject::debugLevel(1);
80         $f = DB_DataObject::Factory('Events');
81         $f->selectAdd();
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');
85         $f->having("mm > 2");
86         $f->orderBy('mm desc') ;
87         $f->limit(10000);
88         exit;
89         $ar = $f->fetchAll();
90         foreach($ar as $f) {
91             $q = DB_DataObject::Factory('Events');
92             $q->query("DELETE FROM Events where 
93                   action = 'NOTIFY'
94                   AND
95                   on_id = {$f->on_id}
96                   AND
97                   on_table = '{$q->escape($f->on_table)}'
98                   AND
99                   id > {$f->min_id}
100                   AND
101                   id < {$f->max_id}
102             ");
103         }
104         
105         
106
107         
108         
109         
110         // pruning is for our press project - so we do not clean up dependant tables at present..
111         
112         if (function_exists('posix_getpwuid')) {
113             $uinfo = posix_getpwuid( posix_getuid () ); 
114          
115             $user = $uinfo['name'];
116         } else {
117             $user = getenv('USERNAME'); // windows.
118         }
119         
120         $ff = HTML_Flexyframework::get()->Pman;
121         
122         $y = date("Y");
123         $m = date("m");
124         $rootDir = $ff['storedir'].'/_events_/'.$user;
125         
126         $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
127         foreach($dirs as $d){
128             $mdirs = array_filter(glob($d."/*"), 'is_dir');
129             foreach($mdirs as $md){
130                 $dirDate = str_replace($rootDir."/", '', $md);
131                 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
132                     //echo "remove $md\n";
133                     $this->delTree($md);
134                       //  echo $md . " is removed. \n";
135                     
136                 }
137             }
138         }
139         
140         exit;
141     }
142     
143     function delTree($dir)
144     {
145         $files = array_diff(scandir($dir), array('.','..'));
146         echo "$dir : Removing " . count($files) . " files\n";
147         clearstatcache();
148         foreach ($files as $file){
149             if (!file_exists("$dir/$file")) {
150                 continue;
151             }
152             if (is_dir("$dir/$file")) {
153                 $this->delTree("$dir/$file");
154                 continue;
155             }
156             unlink("$dir/$file");
157         }
158         return rmdir($dir); 
159     }
160     
161 }