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