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          $f = DB_DataObject::Factory('reader_article');
42         $f->query("
43             DELETE FROM Events where 
44                   event_when < NOW() - INTERVAL 6 MONTH
45                   AND
46                   action IN ('RELOAD', 'LOGIN')
47                   LIMIT 100000
48         ");
49         
50         
51         $this->prune((int)$opts['months']);
52     }
53     
54     function prune($inM)
55     {
56         // 40 seconds ? to delete 100K records..
57         DB_DataObject::debugLevel(1);
58         $f = DB_DataObject::Factory('reader_article');
59         $f->query("
60             DELETE FROM Events where 
61                   event_when < NOW() - INTERVAL {$inM} MONTH
62                   LIMIT 100000
63         ");
64         // pruning is for our press project - so we do not clean up dependant tables at present..
65         
66         if (function_exists('posix_getpwuid')) {
67             $uinfo = posix_getpwuid( posix_getuid () ); 
68          
69             $user = $uinfo['name'];
70         } else {
71             $user = getenv('USERNAME'); // windows.
72         }
73         
74         $ff = HTML_Flexyframework::get()->Pman;
75         
76         $y = date("Y");
77         $m = date("m");
78         $rootDir = $ff['storedir'].'/_events_/'.$user;
79         
80         $dirs = array_filter(glob($rootDir."/*"), 'is_dir');
81         foreach($dirs as $d){
82             $mdirs = array_filter(glob($d."/*"), 'is_dir');
83             foreach($mdirs as $md){
84                 $dirDate = str_replace($rootDir."/", '', $md);
85                 if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){
86                     //echo "remove $md\n";
87                     $this->delTree($md);
88                       //  echo $md . " is removed. \n";
89                     
90                 }
91             }
92         }
93         
94         exit;
95     }
96     
97     function delTree($dir)
98     {
99         $files = array_diff(scandir($dir), array('.','..'));
100         echo "$dir : Removing " . count($files) . " files\n";
101         clearstatcache();
102         foreach ($files as $file){
103             if (!file_exists("$dir/$file")) {
104                 continue;
105             }
106             if (is_dir("$dir/$file")) {
107                 $this->delTree("$dir/$file");
108                 continue;
109             }
110             unlink("$dir/$file");
111         }
112         return rmdir($dir); 
113     }
114     
115 }