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