*/ class cli_plugin_cachestats extends \dokuwiki\Extension\CLIPlugin { /** @inheritDoc */ protected function setup(Options $options) { $options->setHelp('Collect statistics about the cache directory.'); $options->registerOption('format', 'Output format: table|json|csv', 'f', 'table'); $options->registerOption('sort', 'Sort by count|size|dups', 's', false); } /** @inheritDoc */ protected function main(Options $options) { global $conf; $sort = $options->getOpt('sort', 'size'); if (!in_array($sort, ['count', 'size', 'dups'])) { $this->error("Invalid sort option '$sort'. Allowed are: count, size, dups."); return 1; } $format = $options->getOpt('format', 'table'); if (!in_array($format, ['table', 'json', 'csv'])) { $this->error("Invalid format option '$format'. Allowed are: table, json, csv."); return 1; } if ($format === 'json') { fprintf(STDERR, 'Collecting cache statistics from ' . $conf['cachedir'] . "…\n"); } else { $this->info('Collecting cache statistics from ' . $conf['cachedir'] . '…'); } $result = (new FileStatistics($conf['cachedir']))->collect(); // sort with preserved keys uasort($result, function ($a, $b) use ($sort) { return $b[$sort] <=> $a[$sort]; }); match ($format) { 'json' => $this->print_json($result), 'csv' => $this->print_csv($result), default => $this->print_table($result), }; return 0; } /** * Output statistics as JSON */ private function print_json(array $result): void { echo json_encode($result, JSON_PRETTY_PRINT); } /** * Output statistics as CSV */ private function print_csv(array $result): void { $handle = fopen('php://output', 'w'); if ($handle === false) { $this->error('Could not open output for CSV.'); return; } $header = array_merge(['extension'], array_keys(reset($result))); fputcsv($handle, $header); foreach ($result as $ext => $data) { fputcsv($handle, array_merge([$ext], $data)); } } /** * Output statistics as table */ private function print_table(array $result): void { $colWidth = 9; $headers = array_merge(['ext'], array_keys(reset($result))); $widths = array_merge(['*'], array_fill(0, count($headers) -1, $colWidth)); $colors = array_fill(0, count($headers), ''); // ensure terminal is wide enough, otherwise break ugly $tr = new TableFormatter($this->colors); if($tr->getMaxWidth() < $colWidth * count($headers)){; $tr->setMaxWidth($colWidth * count($headers) + 10); } echo $tr->format($widths, $headers, $colors); foreach ($result as $ext => $data) { array_walk( $data, fn (&$v, $k) => $v = sprintf( "% ${colWidth}s", ($k == 'size') ? filesize_h($v) : number_format($v) ) ); echo $tr->format( $widths, array_merge([$ext], array_values($data)), $colors ); } } }