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