1<?php
2
3use dokuwiki\Extension\AdminPlugin;
4
5/**
6 * DokuWiki Plugin data (Admin Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author  Andreas Gohr <gohr@cosmocode.de>
10 */
11/**
12 * Let admin remove non-existing pages from sqlite db
13 */
14class admin_plugin_data_clean extends AdminPlugin
15{
16    /**
17     * will hold the data helper plugin
18     * @var helper_plugin_data
19     */
20    protected $dthlp;
21
22    /**
23     * Constructor. Load helper plugin
24     */
25    public function __construct()
26    {
27        $this->dthlp = plugin_load('helper', 'data');
28    }
29
30    /**
31     * Determine position in list in admin window
32     * Lower values are sorted up
33     *
34     * @return int
35     */
36    public function getMenuSort()
37    {
38        return 502;
39    }
40
41    /**
42     * Return true for access only by admins (config:superuser) or false if managers are allowed as well
43     *
44     * @return bool
45     */
46    public function forAdminOnly()
47    {
48        return true;
49    }
50
51    /**
52     * Return the text that is displayed at the main admin menu
53     *
54     * @param string $language lang code
55     * @return string menu string
56     */
57    public function getMenuText($language)
58    {
59        return $this->getLang('menu_clean');
60    }
61
62    /**
63     * Carry out required processing
64     */
65    public function handle()
66    {
67        if (!isset($_REQUEST['data_go']) || !checkSecurityToken()) return;
68
69        $sqlite = $this->dthlp->getDB();
70        if (!$sqlite) return;
71
72        $rows = $sqlite->queryAll('SELECT pid, page FROM pages');
73
74        $count = 0;
75        foreach ($rows as $row) {
76            if (!page_exists($row['page'])) {
77                $sqlite->exec('DELETE FROM data WHERE pid = ?', $row['pid']);
78                $sqlite->exec('DELETE FROM pages WHERE pid = ?', $row['pid']);
79                $count++;
80            }
81        }
82
83        msg(sprintf($this->getLang('pages_del'), $count), 1);
84    }
85
86    /**
87     * Render HTML output
88     */
89    public function html()
90    {
91
92        echo $this->locale_xhtml('intro_clean');
93
94        $form = new Doku_Form(['method' => 'post']);
95        $form->addHidden('page', 'data_clean');
96        $form->addHidden('data_go', 'go');
97
98        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('submit_clean')));
99        $form->printForm();
100    }
101}
102