1<?php
2/**
3 * DokuWiki Plugin cleanoldips (CLI Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Michael Große <dokuwiki@cosmocode.de>
7 */
8
9
10use splitbrain\phpcli\Options;
11
12class cli_plugin_cleanoldips extends DokuWiki_CLI_Plugin
13{
14
15    /**
16     * Register options and arguments on the given $options object
17     *
18     * @param Options $options
19     *
20     * @return void
21     */
22    protected function setup(Options $options)
23    {
24        $options->setHelp('Clean ips from all changelog entries older than $conf[\'recent_days\']');
25    }
26
27    /**
28     * Your main program
29     *
30     * Arguments and options have been parsed when this is run
31     *
32     * @param Options $options
33     *
34     * @return void
35     */
36    protected function main(Options $options)
37    {
38        global $conf;
39        $searchOpts = array('depth' => 0, 'skipacl' => true);
40
41        $this->log('info', 'Collecting pages...');
42        $pagedata = [];
43        search($pagedata, $conf['datadir'], 'search_allpages', $searchOpts);
44        $pages = array_column($pagedata, 'id');
45        $this->log('info', count($pages) . ' pages found.');
46        $this->log('info', 'Cleaning page changelogs...');
47        /** @var action_plugin_cleanoldips $action */
48        $action = plugin_load('action', 'cleanoldips');
49        foreach ($pages as $pageid) {
50            $this->log('debug', 'Cleaning changelog for page ' . $pageid);
51            $action->cleanChangelog($pageid, metaFN($pageid, '.changes'));
52        }
53        $this->log('success', 'The page changelogs have been cleaned.');
54
55        $this->log('info', 'Collecting media files...');
56        $mediadata = [];
57        search($mediadata, $conf['mediadir'], 'search_media', $searchOpts);
58        $media = array_column($mediadata, 'id');
59        $this->log('info', count($media) . ' media files found.');
60        $this->log('info', 'Cleaning media changelogs...');
61        foreach ($media as $mediaid) {
62            $this->log('debug', 'Cleaning changelog for media file ' . $mediaid);
63            $action->cleanChangelog($mediaid, mediaMetaFN($mediaid, '.changes'));
64        }
65        $this->log('success', 'The media changelogs have been cleaned.');
66    }
67}
68