1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'admin.php');
5
6
7/**
8 * All DokuWiki plugins to extend the admin function
9 * need to inherit from this class
10 */
11class admin_plugin_maintenance extends DokuWiki_Admin_Plugin {
12
13    function __construct() {
14        $this->helper =& plugin_load('helper', 'maintenance');
15    }
16
17    /**
18     * return sort order for position in admin menu
19     */
20    function getMenuSort() {
21        return 10;
22    }
23
24    /**
25     * handle user request
26     */
27    function handle() {
28        switch($_REQUEST['fn']) {
29            case 'start':
30                $this->start();
31                break;
32            case 'stop':
33                $this->stop();
34                break;
35            case 'lock':
36                $this->lock();
37                break;
38            case 'unlock':
39                $this->unlock();
40                break;
41        }
42    }
43
44    /**
45     * output appropriate html
46     */
47    function html() {
48        print $this->locale_xhtml('intro');
49
50        $form = new Doku_Form(array('id'=>'start'));
51        $form->addHidden("page", $_REQUEST['page']);
52        $form->addHidden("fn", "start");
53        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('start_btn')));
54        $form->addElement('<p>'.$this->getLang('start_desc').'</p>');
55        html_form('', $form);
56
57        $form = new Doku_Form(array('id'=>'stop'));
58        $form->addHidden("page", $_REQUEST['page']);
59        $form->addHidden("fn", "stop");
60        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('stop_btn')));
61        $form->addElement('<p>'.$this->getLang('stop_desc').'</p>');
62        html_form('', $form);
63
64        $form = new Doku_Form(array('id'=>'lock'));
65        $form->addHidden("page", $_REQUEST['page']);
66        $form->addHidden("fn", "lock");
67        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('lock_btn')));
68        $form->addElement('<p>'.$this->getLang('lock_desc').'</p>');
69        html_form('', $form);
70
71        $form = new Doku_Form(array('id'=>'unlock'));
72        $form->addHidden("page", $_REQUEST['page']);
73        $form->addHidden("fn", "unlock");
74        $form->addElement(form_makeButton('submit', 'admin', $this->getLang('unlock_btn')));
75        $form->addElement('<p>'.$this->getLang('unlock_desc').'</p>');
76        html_form('', $form);
77    }
78
79    function start() {
80        // check script
81        $script = $this->helper->get_script();
82        if (!is_file($script)) {
83            $msg = sprintf( $this->getLang('start_no_script'), $script);
84            msg($msg, -1);
85            return;
86        }
87        // check if already locked
88        if ($this->helper->is_locked()) {
89            $msg = sprintf( $this->getLang('locked'), $script);
90            msg($msg, -1);
91            return;
92        }
93        // lock the site and run script
94        $result = $this->helper->script_start($script);
95        switch ($result) {
96            case 0:
97                $msg = sprintf( $this->getLang('start_fail'), $script);
98                msg($msg, -1);
99                break;
100            case 1:
101                $msg = $this->getLang('start_success');
102                msg($msg, 1);
103                break;
104            case 2:
105                $msg = $this->getLang('start_already');
106                msg($msg, -1);
107                break;
108        }
109    }
110
111    function stop() {
112        $result = $this->helper->script_stop();
113        switch ($result) {
114            case 0:
115                $msg = $this->getLang('stop_fail');
116                msg($msg, -1);
117                break;
118            case 1:
119                $msg = $this->getLang('stop_success');
120                msg($msg, 1);
121                break;
122            case 2:
123                $msg = $this->getLang('stop_already');
124                msg($msg, -1);
125                break;
126        }
127    }
128
129    function lock() {
130        // check if already locked
131        if ($this->helper->is_locked()) {
132            $msg = sprintf( $this->getLang('locked'), $script);
133            msg($msg, -1);
134            return;
135        }
136        $result = $this->helper->manual_lock();
137        switch ($result) {
138            case 0:
139                $msg = $this->getLang('lock_fail');
140                msg($msg, -1);
141                break;
142            case 1:
143                $msg = $this->getLang('lock_success');
144                msg($msg, 1);
145                break;
146            case 2:
147                $msg = $this->getLang('lock_already');
148                msg($msg, -1);
149                break;
150        }
151    }
152
153    function unlock() {
154        $result = $this->helper->manual_unlock();
155        switch ($result) {
156            case 0:
157                $msg = $this->getLang('unlock_fail');
158                msg($msg, -1);
159                break;
160            case 1:
161                $msg = $this->getLang('unlock_success');
162                msg($msg, 1);
163                break;
164            case 2:
165                $msg = $this->getLang('unlock_already');
166                msg($msg, -1);
167                break;
168        }
169    }
170}
171