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