xref: /plugin/cachestats/cli.php (revision a3092f6c78abc16c6ef67e3c51fd72844d492d6b)
1*a3092f6cSAndreas Gohr<?php
2*a3092f6cSAndreas Gohr
3*a3092f6cSAndreas Gohruse dokuwiki\plugin\cachestats\FileStatistics;
4*a3092f6cSAndreas Gohruse splitbrain\phpcli\Options;
5*a3092f6cSAndreas Gohruse splitbrain\phpcli\TableFormatter;
6*a3092f6cSAndreas Gohr
7*a3092f6cSAndreas Gohr/**
8*a3092f6cSAndreas Gohr * DokuWiki Plugin cachestats (CLI Component)
9*a3092f6cSAndreas Gohr *
10*a3092f6cSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11*a3092f6cSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
12*a3092f6cSAndreas Gohr */
13*a3092f6cSAndreas Gohrclass cli_plugin_cachestats extends \dokuwiki\Extension\CLIPlugin
14*a3092f6cSAndreas Gohr{
15*a3092f6cSAndreas Gohr    /** @inheritDoc */
16*a3092f6cSAndreas Gohr    protected function setup(Options $options)
17*a3092f6cSAndreas Gohr    {
18*a3092f6cSAndreas Gohr        $options->setHelp('Collect statistics about the cache directory.');
19*a3092f6cSAndreas Gohr
20*a3092f6cSAndreas Gohr        $options->registerOption('json', 'Output results in JSON format', 'j', false);
21*a3092f6cSAndreas Gohr        $options->registerOption('sort', 'Sort by count|size|dups', 's', false);
22*a3092f6cSAndreas Gohr    }
23*a3092f6cSAndreas Gohr
24*a3092f6cSAndreas Gohr    /** @inheritDoc */
25*a3092f6cSAndreas Gohr    protected function main(Options $options)
26*a3092f6cSAndreas Gohr    {
27*a3092f6cSAndreas Gohr        global $conf;
28*a3092f6cSAndreas Gohr
29*a3092f6cSAndreas Gohr        $sort = $options->getOpt('sort', 'size');
30*a3092f6cSAndreas Gohr        if (!in_array($sort, ['count', 'size', 'dups'])) {
31*a3092f6cSAndreas Gohr            $this->error("Invalid sort option '$sort'. Allowed are: count, size, dups.");
32*a3092f6cSAndreas Gohr            return 1;
33*a3092f6cSAndreas Gohr        }
34*a3092f6cSAndreas Gohr
35*a3092f6cSAndreas Gohr        if ($options->getOpt('json')) {
36*a3092f6cSAndreas Gohr            fprintf(STDERR, 'Collecting cache statistics from ' . $conf['cachedir'] . "…\n");
37*a3092f6cSAndreas Gohr        } else {
38*a3092f6cSAndreas Gohr            $this->info('Collecting cache statistics from ' . $conf['cachedir'] . '…');
39*a3092f6cSAndreas Gohr        }
40*a3092f6cSAndreas Gohr
41*a3092f6cSAndreas Gohr        $stats = (new FileStatistics($conf['cachedir']))->collect();
42*a3092f6cSAndreas Gohr
43*a3092f6cSAndreas Gohr        // for debugging
44*a3092f6cSAndreas Gohr        print_r($stats);
45*a3092f6cSAndreas Gohr
46*a3092f6cSAndreas Gohr        $keys = array_unique(
47*a3092f6cSAndreas Gohr            array_merge(
48*a3092f6cSAndreas Gohr                array_keys($stats['extensions']),
49*a3092f6cSAndreas Gohr                array_keys($stats['sizes']),
50*a3092f6cSAndreas Gohr                array_keys($stats['duplicates'])
51*a3092f6cSAndreas Gohr            )
52*a3092f6cSAndreas Gohr        );
53*a3092f6cSAndreas Gohr        $result = [];
54*a3092f6cSAndreas Gohr        foreach ($keys as $key) {
55*a3092f6cSAndreas Gohr            $result[$key] = [
56*a3092f6cSAndreas Gohr                'count' => $stats['extensions'][$key] ?? 0,
57*a3092f6cSAndreas Gohr                'size' => $stats['sizes'][$key] ?? 0,
58*a3092f6cSAndreas Gohr                'dups' => $stats['duplicates'][$key] ?? 0,
59*a3092f6cSAndreas Gohr            ];
60*a3092f6cSAndreas Gohr        }
61*a3092f6cSAndreas Gohr
62*a3092f6cSAndreas Gohr        // sort with preserved keys
63*a3092f6cSAndreas Gohr        uasort($result, function ($a, $b) use ($sort) {
64*a3092f6cSAndreas Gohr            return $b[$sort] <=> $a[$sort];
65*a3092f6cSAndreas Gohr        });
66*a3092f6cSAndreas Gohr
67*a3092f6cSAndreas Gohr        if ($options->getOpt('json')) {
68*a3092f6cSAndreas Gohr            echo json_encode($result, JSON_PRETTY_PRINT);
69*a3092f6cSAndreas Gohr            return 0;
70*a3092f6cSAndreas Gohr        }
71*a3092f6cSAndreas Gohr
72*a3092f6cSAndreas Gohr        $tr = new TableFormatter($this->colors);
73*a3092f6cSAndreas Gohr        echo $tr->format(
74*a3092f6cSAndreas Gohr            ['*', 10, 10, 10],
75*a3092f6cSAndreas Gohr            ['Extension', 'File Count', 'Total Size (bytes)', 'Duplicate Files'],
76*a3092f6cSAndreas Gohr            ['', '', '', '']
77*a3092f6cSAndreas Gohr        );
78*a3092f6cSAndreas Gohr
79*a3092f6cSAndreas Gohr        foreach ($result as $ext => $data) {
80*a3092f6cSAndreas Gohr            echo $tr->format(
81*a3092f6cSAndreas Gohr                ['*', 10, 10, 10],
82*a3092f6cSAndreas Gohr                [
83*a3092f6cSAndreas Gohr                    $ext,
84*a3092f6cSAndreas Gohr                    sprintf("% 10s", number_format($data['count'])),
85*a3092f6cSAndreas Gohr                    sprintf("% 10s", filesize_h($data['size'])),
86*a3092f6cSAndreas Gohr                    sprintf("% 10s", number_format($data['dups']))
87*a3092f6cSAndreas Gohr                ],
88*a3092f6cSAndreas Gohr                ['', '', '', '']
89*a3092f6cSAndreas Gohr            );
90*a3092f6cSAndreas Gohr        }
91*a3092f6cSAndreas Gohr
92*a3092f6cSAndreas Gohr        return 0;
93*a3092f6cSAndreas Gohr    }
94*a3092f6cSAndreas Gohr}
95