9 require_once
'WHAT/Lib.Common.php';
21 $usage = new \ApiUsage($action);
22 $usage->setDefinitionText(
'Analyze or clean orphan files');
23 $usage->setStrictMode(
true);
24 $opts[
'analyze'] = (
$usage->addEmptyParameter(
'analyze',
'Analyze orphan files (non-destructive)') !==
false);
25 $opts[
'clean'] = (
$usage->addEmptyParameter(
'clean',
'Clean orphan files') !==
false);
26 $opts[
'missing-files'] = (
$usage->addEmptyParameter(
'missing-files',
"Use in conjunction with '--analyze' to analyze missing physical files instead of orphan files") !==
false);
27 $opts[
'skip-trash'] = (
$usage->addEmptyParameter(
'skip-trash',
"Really delete file and do not move them under '<vault_root>/.trash/' sub-directory") !==
false);
29 if (!$opts[
'analyze'] && !$opts[
'clean']) {
30 $usage->exitError(
"Use '--analyze' or '--clean'.");
33 if ($opts[
'analyze'] xor !$opts[
'clean']) {
34 $usage->exitError(
"Use either '--analyze' or '--clean', but not both.");
39 if ($opts[
'analyze']) {
40 if ($opts[
'missing-files']) {
41 self::main_analyze_missing_files();
43 self::main_analyze_orphans();
45 } elseif ($opts[
'clean']) {
46 self::main_clean_orphans($opts[
'skip-trash']);
50 printf(
"\nError: %s\n\n", $e->getMessage());
61 printf(
"* Analyzing... ");
70 printf(
"\tcount = %d\n",
$report[
'all'][
'count']);
71 printf(
"\tsize = %d%s\n",
$report[
'all'][
'size'], (empty(
$report[
'all'][
'size_pretty']) ?
'' :
' (' .
$report[
'all'][
'size_pretty'] .
')'));
74 printf(
"\tcount = %d\n", $report[
'used'][
'count']);
75 printf(
"\tsize = %d%s (%3.02f%%)\n", $report[
'used'][
'size'], ((empty($report[
'used'][
'size_pretty'])) ?
'' :
' (' . $report[
'used'][
'size_pretty'] .
')') , (($report[
'all'][
'size'] != 0) ? ((100 * $report[
'used'][
'size']) / $report[
'all'][
'size']) :
'0'));
78 printf(
"\tcount = %d\n", $report[
'orphan'][
'count']);
79 printf(
"\tsize = %d%s (%3.02f%%)\n", $report[
'orphan'][
'size'], ((empty($report[
'orphan'][
'size_pretty'])) ?
'' :
' (' . $report[
'orphan'][
'size_pretty'] .
')') , (($report[
'all'][
'size'] != 0) ? ((100 * $report[
'orphan'][
'size']) / $report[
'all'][
'size']) :
'0'));
90 printf(
"* Cleanup docvaultindex: ");
92 printf(
"removed %d entries.\n",
$report[
'count']);
95 printf(
"* Deleting %d orphan files...\n",
$report[
'count']);
96 $pom = new \Dcp\ConsoleProgressOMeter();
97 $pom->setMax(
$report[
'count'])->setInterval(1000)->start();
99 foreach (
$report[
'iterator'] as $t) {
101 $vault_root = $t[
'vault_root'];
102 $trash_root = $vault_root .
'/.trash';
104 $vault_filename = $vault_root .
'/' .
$filename;
105 $trash_filename = $trash_root .
'/' .
$filename;
109 if (file_exists($vault_filename)) {
110 if ($skipTrash ===
true) {
111 if (unlink($vault_filename) ===
false) {
112 printf(
"Error: could not delete '%s'.\n", $vault_filename);
116 if (!is_dir($trash_root)) {
117 if (mkdir($trash_root) ===
false) {
121 $dir = dirname($trash_filename);
123 if (mkdir(
$dir, 0777,
true) ===
false) {
124 printf(
"Error: could not create trash subdir '%s'.\n",
$dir);
128 if (rename($vault_filename, $trash_filename) ===
false) {
129 printf(
"Error: could not move '%s' to '%s'.\n", $vault_filename, $trash_filename);
142 printf(
"* Reset sizes...\n");
144 $fs->recomputeDirectorySize();
157 printf(
"* Counting entries: ");
159 printf(
"found %d entries.\n",
$report[
'count']);
161 printf(
"* Checking missing physical files for %d entries...\n",
$report[
'count']);
164 $missing_nodoc = array();
165 $missing_doc = array();
166 foreach (
$report[
'iterator'] as $t) {
167 $vid = $t[
'id_file'];
169 $vault_root = $t[
'vault_root'];
171 $vault_filename = $vault_root .
'/' .
$filename;
172 if (file_exists($vault_filename)) {
175 if (!isset($missing[$vault_filename])) {
176 $missing[$vault_filename] = 0;
178 $missing[$vault_filename]++;
181 printf(
"Missing physical file '%s' (vid = %d) without docvaultindex entries\n", $vault_filename, $vid);
182 if (!isset($missing_nodoc[$vault_filename])) {
183 $missing_nodoc[$vault_filename] = 0;
188 printf(
"Missing physical file '%s' (vid = %d) with valid docvaultindex entries (docid = %d)\n", $vault_filename, $vid,
$docid);
189 if (!isset($missing_doc[$vault_filename])) {
190 $missing_doc[$vault_filename] = 0;
192 $missing_doc[$vault_filename]++;
200 printf(
"Found %d missing physical file:\n", count($missing));
201 printf(
"\twith valid docvaultindex entries = %d\n", count($missing_doc));
202 printf(
"\twithout docvaultindex entries = %d\n", count($missing_nodoc));
212 throw new VaultAnalyzerCLIException(sprintf(
"Found inconsistencies in 'docvaultindex': you might need to regenerate docvaultindex with \"./wsh.php --api=refreshVaultIndex\""));
Exception class use exceptionCode to identifiy correctly exception.
static main_clean_orphans($skipTrash=false)
static main_analyze_missing_files()
checkDocVaultIndex(&$report)
static main(\Action &$action)
static checkDocVaultIndex(VaultAnalyzer &$vaultAnalyzer)
static main_analyze_orphans()