/
checkstorage.php
109 lines (93 loc) · 3.08 KB
/
checkstorage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* A CLI script which checks for orphan storage files
*
* @author G. Giunta
* @copyright (C) G. Giunta 2016
* @license Licensed under GNU General Public License v2.0. See file license.txt
*/
require 'autoload.php';
// Inject our own autoloader after the std one, as this script is supposed to be
// executable even when extension has not been activated
//require_once ( dirname( __FILE__ ) . '/../../classes/ezdbiautoloadhelper.php' );
//spl_autoload_register( array( 'ezdbiAutoloadHelper', 'autoload' ) );
$cli = eZCLI::instance();
$script = eZScript::instance( array(
'description' => "List/delete orphan storage files (those on disk but not in db). NB: does not follow symlinks",
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );
$script->startup();
$options = $script->getOptions(
'[cleanup][displaychecks][displayfiles]', //'[database:]',
'',
array(
//'database' => 'DSN for database to connect to (default ez db)',
'displayfiles' => 'Display the offending files, not only their count',
'displaychecks' => 'Display the list of checks instead of executing them',
'cleanup' => 'Do delete orphan files instead of just listing them'
)
);
$script->initialize();
try
{
$violations = array();
$checker = new ezdbiStorageChecker();
$checks = $checker->getChecks();
if ( $options['displaychecks'])
{
}
else
{
if ( function_exists( 'pcntl_signal' ) )
{
pcntl_signal(SIGTERM, 'onStopSignal');
pcntl_signal(SIGINT, 'onStopSignal');
saveState( array(
'cli' => $cli,
'script' => $script,
'checks' => $checks,
'violations' => &$violations,
'options' => $options
) );
}
foreach ( array_keys( $checks ) as $check )
{
$cli->output( "\nNow checking $check ..." );
$violation = $checker->check( $check, $options['cleanup'], $options['displayfiles'] );
if ( count ( $violation ) )
{
$violations[$check] = $violation;
}
if ( function_exists( 'pcntl_signal' ) )
{
pcntl_signal_dispatch();
}
}
}
$cli->output( ezdbiReportGenerator::getText( $violations, $checks, $options['displaychecks'] ) );
$script->shutdown();
}
catch( Exception $e )
{
$cli->error( $e->getMessage() );
$script->shutdown( -1 );
}
function onStopSignal( $sigNo )
{
global $scriptState;
$violations = $scriptState['violations'];
$cli = $scriptState['cli'];
$checks = $scriptState['checks'];
$options = $scriptState['options'];
$script = $scriptState['script'];
$cli->output( ezdbiReportGenerator::getText( $violations, $checks, $options['displaychecks'] ) );
$script->shutdown();
die();
}
// We can not just use $GLOBALS as sometimes the script is run within a class (in eZ5), sometimes not...
function saveState($stateArray)
{
global $scriptState;
$scriptState = $stateArray;
}