fix #8131 - chinese translations
[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         $before = $f->count();
84
85         $f = DB_DataObject::Factory('Events');
86         $f->selectAdd();
87         $f->selectAdd("on_id, on_table, min(id) as min_id, max(id) as max_id, count(*) as mm");
88         $f->whereAdd("action = 'NOTIFY' and event_when < NOW() - INTERVAL 1 WEEK");
89         $f->groupBy('on_id, on_table');
90         $f->having("mm > 2");
91         $f->orderBy('mm desc') ;
92         $f->limit(10000);
93         $ar = $f->fetchAll();
94
95         foreach($ar as $f) {
96             $q = DB_DataObject::Factory('Events');
97             $q->query("DELETE FROM Events where 
98                   action = 'NOTIFY'
99                   AND
100                   on_id = {$f->on_id}
101                   AND
102                   on_table = '{$q->escape($f->on_table)}'
103                   AND
104                   id >  {$f->min_id}  -- allow the first one to stay....
105                   AND
106                   id <= {$f->max_id}
107             ");
108         }
109         
110         $f = DB_DataObject::Factory('Events');
111         $after = $f->count();
112         echo "DELETED : " . ($before - $after) . " records\n";
113
114         
115         
116         
117         // pruning is for our press project - so we do not clean up dependant tables at present..
118         
119         if (function_exists('posix_getpwuid')) {
120             $uinfo = posix_getpwuid( posix_getuid () ); 
121          
122             $user = $uinfo['name'];
123         } else {
124             $user = getenv('USERNAME'); // windows.
125         }
126         
127         $ff = HTML_Flexyframework::get()->Pman;
128         
129         $y = date("Y");
130         $m = date("m");
131         $rootDir = $ff['storedir'].'/_events_/'.$user;
132         
133         $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
134         foreach($dirs as $d){
135             $mdirs = array_filter(glob($d."/*"), 'is_dir');
136             foreach($mdirs as $md){
137                 $dirDate = str_replace($rootDir."/", '', $md);
138                 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
139                     //echo "remove $md\n";
140                     $this->delTree($md);
141                       //  echo $md . " is removed. \n";
142                     
143                 }
144             }
145         }
146         
147         exit;
148     }
149     
150     function delTree($dir)
151     {
152         $files = array_diff(scandir($dir), array('.','..'));
153         echo "$dir : Removing " . count($files) . " files\n";
154         clearstatcache();
155         foreach ($files as $file){
156             if (!file_exists("$dir/$file")) {
157                 continue;
158             }
159             if (is_dir("$dir/$file")) {
160                 $this->delTree("$dir/$file");
161                 continue;
162             }
163             unlink("$dir/$file");
164         }
165         return rmdir($dir); 
166     }
167     
168 }