1<?php
2
3use dokuwiki\Extension\CLIPlugin;
4use splitbrain\phpcli\Exception;
5use splitbrain\phpcli\Options;
6
7class cli_plugin_watchcycle extends CLIPlugin
8{
9    /** @var helper_plugin_watchcycle_db */
10    protected $dbHelper;
11
12    /** @var helper_plugin_watchcycle */
13    protected $helper;
14
15    /**
16     * Initialize helper plugins
17     */
18    public function __construct()
19    {
20        parent::__construct();
21        $this->dbHelper = plugin_load('helper', 'watchcycle_db');
22        $this->helper = plugin_load('helper', 'watchcycle');
23    }
24
25    /**
26     * Register options and arguments on the given $options object
27     *
28     * @param Options $options
29     * @return void
30     * @throws Exception
31     */
32    protected function setup(Options $options)
33    {
34        $options->setHelp('Watchcycle notification dispatcher');
35        $options->registerCommand('send', 'Notify maintainers if necessary');
36    }
37
38    /**
39     * Your main program
40     *
41     * Arguments and options have been parsed when this is run
42     *
43     * @param Options $options
44     * @return void
45     */
46    protected function main(Options $options)
47    {
48        $cmd = $options->getCmd();
49        switch ($cmd) {
50            case 'send':
51                $this->sendNotifications();
52                break;
53            default:
54                $this->error('No command provided');
55                exit(1);
56        }
57    }
58
59    /**
60     * Check and send notifications
61     */
62    protected function sendNotifications()
63    {
64        $rows = $this->dbHelper->getAll();
65        if (!is_array($rows)) {
66            $this->info('Exiting: no users to notify found.');
67            return;
68        }
69
70        auth_setup();
71
72        foreach ($rows as $row) {
73            if (!$row['uptodate']) {
74                $this->helper->informMaintainer($row['maintainer'], $row['page']);
75            }
76        }
77    }
78}
79