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