Completed
Push — master ( ea0e5d...10ff2a )
by mw
02:37
created

formats/ploticus/SRF_Ploticus_cleanCache.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 71 and the first side effect is on line 21.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Purges old/orphan/temporary plots, maps, CSVs, drawdumps from the ploticus cache directory.
4
 *
5
 *
6
 * Note: if SMW is not installed in its standard path under ./extensions
7
 *       then the MW_INSTALL_PATH environment variable must be set.
8
 *       See README in the maintenance directory.
9
 *
10
 * Usage:
11
 * php SRF_Ploticus_cleanCache.php [options...]
12
 *
13
 * -a <age in hours>    Override $srfgPloticusCacheAge setting and purge files of this age and greater
14
 * -v                   Be verbose about the progress.
15
 *
16
 * @author Joel Natividad
17
 * @file
18
 * @ingroup SRFMaintenance
19
 */
20
21
$optionsWithArgs = array( 'a' );
22
23
require_once ( getenv( 'MW_INSTALL_PATH' ) !== false
24
    ? getenv( 'MW_INSTALL_PATH' ) . "/maintenance/commandLine.inc"
25
    : dirname( __FILE__ ) . '/../../../maintenance/commandLine.inc' );
26
27
global $wgUploadDirectory, $srfgPloticusCacheAgeHours;
28
29
if ( !empty( $options['a'] ) ) {
30
	$fileAge = intval( $options['a'] ) * 3600; // 60 secs * 60 mins
31
} else {
32
        if ( isset( $srfgPloticusCacheAgeHours ) ) {
33
                $fileAge = $srfgPloticusCacheAgeHours * 3600;
34
        } else {
35
                $fileAge = 604800; // if $srfgPloticusCacheAgeHours is not set in LocalSettings.php defaults to 7 days
36
        }
37
}
38
39
$verbose = array_key_exists( 'v', $options );
40
41
$now = strftime( "%c", time() );
42
if ( $fileAge <= 0 ) {
43
    if ( $verbose )
44
        echo "$now: Ploticus cache cleaning disabled.\n";
45
    return;
46
}
47
48
$ploticusDirectory = $wgUploadDirectory . '/ploticus/';
49
50
if ( !is_dir( $ploticusDirectory ) ) {
51
    if ( $verbose )
52
        echo "$now: $ploticusDirectory does not exist!\n";
53
    return;
54
}
55
56
if ( $verbose )
57
    echo "$now: Purging $ploticusDirectory cache of files >= $fileAge seconds old.\n";
58
59
$deletecount = rfr( $ploticusDirectory, "*.*", $fileAge, $verbose );
60
61
if ( $verbose ) {
62
    $now = strftime( "%c", time() );
63
    if ( $deletecount > 0 ) {
64
        echo "$now: $deletecount files successfully deleted from Ploticus cache.\n";
65
    } else {
66
        echo "$now: No files deleted.  Check if you have permission to delete files from $ploticusDirectory.\n";
67
    }
68
}
69
70
/* recursive file delete function */
71
function rfr( $path, $match, $fileAge, $verbose ) {
72
   static $deleted = 0;
73
   $dirs = glob( $path . "*" );
74
   $files = glob( $path . $match );
75
   foreach ( $files as $file ) {
76
      if ( is_file( $file ) && @filemtime( $file ) < ( time() - $fileAge ) ) {
77
		if ( $verbose ) echo "$file...";
78
		if ( @unlink( $file ) ) {
79
			if ( $verbose ) echo "deleted.\n";
80
                        $deleted++;
81
		 } else {
82
			if ( $verbose ) echo "ooops!\n";
83
		 }
84
      }
85
   }
86
   foreach ( $dirs as $dir ) {
87
      if ( is_dir( $dir ) ) {
88
         $dir = basename( $dir ) . "/";
89
         rfr( $path . $dir, $match, $fileAge, $verbose );
90
      }
91
   }
92
   return $deleted;
93
}