Index: trunk/extensions/SemanticResultFormats/Ploticus/SRF_Ploticus_cleanCache.php |
— | — | @@ -38,34 +38,56 @@ |
39 | 39 | |
40 | 40 | $verbose = array_key_exists( 'v', $options ); |
41 | 41 | |
| 42 | +$now = strftime("%c", time()); |
42 | 43 | if ($fileAge <= 0) { |
43 | 44 | if ($verbose) |
44 | | - echo "Ploticus cache cleaning disabled.\n"; |
| 45 | + echo "$now: Ploticus cache cleaning disabled.\n"; |
45 | 46 | return; |
46 | 47 | } |
47 | 48 | |
48 | | -$ploticusDirectory = $wgUploadDirectory . '/ploticus'; |
49 | | -$deletecount = 0; |
| 49 | +$ploticusDirectory = $wgUploadDirectory . '/ploticus/'; |
50 | 50 | |
51 | | -// TODO: Modify to be shard dir aware |
52 | | -if( $dirhandle = @opendir($ploticusDirectory) ) { |
53 | | - while( false !== ($filename = readdir($dirhandle)) ) { |
54 | | - if( $filename != '.' && $filename != '..' ) { |
55 | | - $filename = $ploticusDirectory . '/' . $filename; |
| 51 | +if (!is_dir($ploticusDirectory)) { |
| 52 | + if ($verbose) |
| 53 | + echo "$now: $ploticusDirectory does not exist!\n"; |
| 54 | + return; |
| 55 | +} |
| 56 | + |
| 57 | +if ($verbose) |
| 58 | + echo "$now: Purging $ploticusDirectory cache of files >= $fileAge seconds old.\n"; |
56 | 59 | |
57 | | - if( @filemtime($filename) < (time()-$fileAge) ) { |
58 | | - if ($verbose) |
59 | | - echo "deleting $filename...\n"; |
60 | | - if (@unlink($filename)) |
61 | | - $deletecount ++; |
62 | | - } |
63 | | - } |
| 60 | +$deletecount = rfr($ploticusDirectory, "*.*", $fileAge, $verbose); |
| 61 | + |
| 62 | +if ($verbose) { |
| 63 | + $now = strftime("%c", time()); |
| 64 | + if ($deletecount > 0 ) { |
| 65 | + echo "$now: $deletecount files successfully deleted from Ploticus cache.\n"; |
| 66 | + } else { |
| 67 | + echo "$now: No files deleted. Check if you have permission to delete files from $ploticusDirectory.\n"; |
64 | 68 | } |
65 | | -} else { |
66 | | - if ($verbose) |
67 | | - echo "$ploticusDirectory not found... Aborting...\n"; |
68 | 69 | } |
69 | 70 | |
70 | | -@closedir($dirhandle); |
71 | | -if ($verbose) |
72 | | - echo "$deletecount files successfully deleted from Ploticus cache.\n"; |
| 71 | +/* recursive file delete function */ |
| 72 | +function rfr($path, $match, $fileAge, $verbose) { |
| 73 | + static $deleted = 0; |
| 74 | + $dirs = glob($path."*"); |
| 75 | + $files = glob($path.$match); |
| 76 | + foreach($files as $file){ |
| 77 | + if(is_file($file) && @filemtime($file) < (time()-$fileAge) ) { |
| 78 | + if ($verbose) echo "$file..."; |
| 79 | + if (@unlink($file)) { |
| 80 | + if ($verbose) echo "deleted.\n"; |
| 81 | + $deleted++; |
| 82 | + } else { |
| 83 | + if ($verbose) echo "ooops!\n"; |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + foreach($dirs as $dir) { |
| 88 | + if(is_dir($dir)) { |
| 89 | + $dir = basename($dir) . "/"; |
| 90 | + rfr($path.$dir, $match, $fileAge, $verbose); |
| 91 | + } |
| 92 | + } |
| 93 | + return $deleted; |
| 94 | +} |
\ No newline at end of file |