From 8de42d717686e2daf3700b341817c55f8a972b28 Mon Sep 17 00:00:00 2001 From: Alan Knowles Date: Thu, 16 Oct 2014 13:31:27 +0800 Subject: [PATCH] Prune.php --- Prune.php | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 102 insertions(+) diff --git a/Prune.php b/Prune.php index e69de29b..9d1c103f 100644 --- a/Prune.php +++ b/Prune.php @@ -0,0 +1,102 @@ + array( + 'desc' => 'How many months', + //'default' => 0, + 'short' => 'm', + 'min' => 1, + 'max' => 1, + + ) + ); + var $cli = false; + + function getAuth() { + $ff = HTML_FlexyFramework::get(); + if (!empty($ff->cli)) { + $this->cli = true; + return true; + } +// return true;// for test only + return false; + } + + function get($m="", $opts) + { + + $this->prune((int)$opts['months']); + } + + function prune($inM) + { + // 40 seconds ? to delete 100K records.. + //DB_DataObject::debugLevel(1); + $f = DB_DataObject::Factory('reader_article'); + $f->query(" + DELETE FROM reader_article where + fetched_dt < NOW() - INTERVAL {$inM} MONTH + AND + fetched + AND + views < 1 + LIMIT 100000 + "); + // pruning is for our press project - so we do not clean up dependant tables at present.. + + + + $ff = HTML_Flexyframework::get()->Pman; + + $y = date("Y"); + $m = date("m"); + $rootDir = $ff['storedir'].'/rss'; + + $dirs = array_filter(glob($rootDir."/*"), 'is_dir'); + foreach($dirs as $d){ + $mdirs = array_filter(glob($d."/*"), 'is_dir'); + foreach($mdirs as $md){ + $dirDate = str_replace($rootDir."/", '', $md); + if(strtotime($dirDate."/01") < strtotime("now - {$inM} months")){ + //echo "remove $md\n"; + $this->delTree($md); + // echo $md . " is removed. \n"; + + } + } + } + + exit; + } + + function delTree($dir) + { + $files = array_diff(scandir($dir), array('.','..')); + echo "$dir : Removing " . count($files) . " files\n"; + clearstatcache(); + foreach ($files as $file){ + if (!file_exists("$dir/$file")) { + continue; + } + if (is_dir("$dir/$file")) { + $this->delTree("$dir/$file"); + continue; + } + unlink("$dir/$file"); + } + return rmdir($dir); + } + +} -- 2.39.2