xref: /plugin/upgrade/admin.php (revision 4e3e87e45b11e1184e5fb1995e14bdc3ab70ccf5)
1<?php
2/**
3 * DokuWiki Plugin upgrade (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9require_once __DIR__ . '/vendor/autoload.php';
10
11class admin_plugin_upgrade extends DokuWiki_Admin_Plugin
12{
13    protected $haderrors = false;
14
15    /** @var helper_plugin_upgrade */
16    protected $helper;
17
18    /**
19     * initialize helper
20     */
21    public function __construct()
22    {
23        $this->helper = plugin_load('helper', 'upgrade');
24        $this->helper->setLogger($this);
25    }
26
27    /** @inheritDoc */
28    public function getMenuSort()
29    {
30        return 555;
31    }
32
33    /** @inheritDoc */
34    public function handle()
35    {
36        if (!empty($_REQUEST['step']) && !checkSecurityToken()) {
37            unset($_REQUEST['step']);
38        }
39    }
40
41    public function html()
42    {
43        $abrt = false;
44        $next = false;
45        $ok = true;
46
47        echo '<h1>' . $this->getLang('menu') . '</h1>';
48
49        echo '<div id="plugin__upgrade">';
50        // enable auto scroll
51        ?>
52        <script type="text/javascript">
53            var plugin_upgrade = window.setInterval(function () {
54                var obj = document.getElementById('plugin__upgrade');
55                if (obj) obj.scrollTop = obj.scrollHeight;
56            }, 25);
57        </script>
58        <?php
59
60        // handle current step
61        $this->nextStep($abrt, $next, $ok);
62
63        // disable auto scroll
64        ?>
65        <script type="text/javascript">
66            window.setTimeout(function () {
67                window.clearInterval(plugin_upgrade);
68            }, 50);
69        </script>
70        <?php
71        echo '</div>';
72
73        $careful = '';
74        if (!$ok) {
75            echo '<div id="plugin__upgrade_careful">' . $this->getLang('careful') . '</div>';
76            $careful = 'careful';
77        }
78
79        $action = script();
80        echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">';
81        echo '<input type="hidden" name="do" value="admin" />';
82        echo '<input type="hidden" name="page" value="upgrade" />';
83        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
84        if ($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue ' . $careful . '">' . $this->getLang('btn_continue') . ' ➡</button>';
85        if ($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>';
86        echo '</form>';
87
88        $this->displayProgressBar($next);
89    }
90
91    /**
92     * Display a progress bar of all steps
93     *
94     * @param string $next the next step
95     */
96    private function displayProgressBar($next)
97    {
98        $steps = ['version', 'download', 'unpack', 'check', 'upgrade'];
99        $active = true;
100        $count = 0;
101
102        echo '<div id="plugin__upgrade_meter"><ol>';
103        foreach ($steps as $step) {
104            $count++;
105            if ($step == $next) $active = false;
106            if ($active) {
107                echo '<li class="active">';
108                echo '<span class="step">✔</span>';
109            } else {
110                echo '<li>';
111                echo '<span class="step">' . $count . '</span>';
112            }
113
114            echo '<span class="stage">' . $this->getLang('step_' . $step) . '</span>';
115            echo '</li>';
116        }
117        echo '</ol></div>';
118    }
119
120    /**
121     * Decides the current step and executes it
122     *
123     * @param bool $abrt
124     * @param bool $next
125     * @param bool $ok
126     */
127    private function nextStep(&$abrt, &$next, &$ok)
128    {
129
130        if (isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
131            $keys = array_keys($_REQUEST['step']);
132            $step = array_shift($keys);
133        } else {
134            $step = '';
135        }
136
137        if ($step == 'cancel' || $step == 'done') {
138            $ok = $this->helper->cleanup();
139            if ($step == 'cancel') $step = '';
140        }
141
142        if ($step) {
143            $abrt = true;
144            $next = false;
145            if ($step == 'version') {
146                $ok = $this->helper->checkVersions();
147                $next = 'download';
148            } elseif ($step == 'done') {
149                echo $this->locale_xhtml('final');
150                $next = '';
151                $abrt = '';
152            } elseif (!file_exists($this->helper->tgzfile)) {
153                if ($ok = $this->helper->downloadTarball()) $next = 'unpack';
154            } elseif (!is_dir($this->helper->tgzdir)) {
155                if ($ok = $this->helper->extractTarball()) $next = 'check';
156            } elseif ($step != 'upgrade') {
157                if ($ok = $this->helper->checkPermissions()) $next = 'upgrade';
158            } elseif ($step == 'upgrade') {
159                if ($ok = $this->helper->copyFiles()) {
160                    $ok = $this->helper->deleteObsoleteFiles();
161                    $this->helper->cleanup();
162                    $next = 'done';
163                    $abrt = '';
164                }
165            } else {
166                echo 'uhm. what happened? where am I? This should not happen';
167            }
168        } else {
169            # first time run, show intro
170            echo $this->locale_xhtml('step0');
171            $abrt = false;
172            $next = 'version';
173        }
174    }
175
176    /**
177     * Print the given message and flush buffers
178     *
179     * @param string $level
180     * @param string $message
181     */
182    public function log($level, $message) {
183        echo '<div class="log-' . $level . '">' . $message . '</div>';
184        flush();
185        ob_flush();
186    }
187}
188