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