1<?php
2
3use dokuwiki\Extension\CLIPlugin;
4use splitbrain\phpcli\Options;
5
6/**
7 * statdisplay plugin cli component
8 *
9 * @author Andreas Gohr <gohr@cosmocode.de>
10 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 */
12class cli_plugin_statdisplay extends CLIPlugin
13{
14
15    /**
16     * @inheritDoc
17     */
18    protected function setup(Options $options)
19    {
20        $options->setHelp('Control the statdisplay plugin');
21        $options->registerCommand('parse', 'Parse and analyse the log file');
22        $options->registerOption('clear', 'Drop all previously parsed log data and reparse the whole log file', 'c',
23            false, 'parse');
24        $options->registerOption('lines', 'Number of lines to read per iteration', 'l', 'lines', 'parse');
25    }
26
27    /**
28     * @inheritDoc
29     */
30    protected function main(Options $options)
31    {
32        switch ($options->getCmd()) {
33            case 'parse':
34                $this->parseData(
35                    $this->options->getOpt('clear'),
36                    (int)$this->options->getOpt('lines', $this->getConf('lines'))
37                );
38                break;
39            default:
40                echo $this->options->help();
41        }
42    }
43
44    /**
45     * Parse the log data
46     *
47     * @param bool $clear
48     */
49    protected function parseData($clear, $maxlines)
50    {
51        /** @var helper_plugin_statdisplay_log $helper */
52        $helper = plugin_load('helper', 'statdisplay_log');
53
54        if ($clear) {
55            $helper->resetLogCache();
56        }
57
58        do {
59            $this->info(sprintf('%.2f%%', $helper->progress()));
60        } while ($helper->parseLogData($maxlines));
61    }
62}
63