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