xref: /plugin/upgrade/admin.php (revision 0aa2505940b7b9f5125b4e30bc38f2d9c08916bd)
134aae6dbSAndreas Gohr<?php
234aae6dbSAndreas Gohr/**
3d391262fSAndreas Gohr * DokuWiki Plugin upgrade (Admin Component)
434aae6dbSAndreas Gohr *
534aae6dbSAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
634aae6dbSAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
734aae6dbSAndreas Gohr */
834aae6dbSAndreas Gohr
94e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
1034aae6dbSAndreas Gohr
114e3e87e4SAndreas Gohrclass admin_plugin_upgrade extends DokuWiki_Admin_Plugin
124e3e87e4SAndreas Gohr{
134ed3677eSAndreas Gohr    protected $haderrors = false;
144ed3677eSAndreas Gohr
154e3e87e4SAndreas Gohr    /** @var helper_plugin_upgrade */
164e3e87e4SAndreas Gohr    protected $helper;
1734aae6dbSAndreas Gohr
184e3e87e4SAndreas Gohr    /**
194e3e87e4SAndreas Gohr     * initialize helper
204e3e87e4SAndreas Gohr     */
214e3e87e4SAndreas Gohr    public function __construct()
224e3e87e4SAndreas Gohr    {
234e3e87e4SAndreas Gohr        $this->helper = plugin_load('helper', 'upgrade');
244e3e87e4SAndreas Gohr        $this->helper->setLogger($this);
2534aae6dbSAndreas Gohr    }
2634aae6dbSAndreas Gohr
274e3e87e4SAndreas Gohr    /** @inheritDoc */
284e3e87e4SAndreas Gohr    public function getMenuSort()
294e3e87e4SAndreas Gohr    {
30bd08ebd1SAndreas Gohr        return 555;
31bd08ebd1SAndreas Gohr    }
3234aae6dbSAndreas Gohr
334e3e87e4SAndreas Gohr    /** @inheritDoc */
344e3e87e4SAndreas Gohr    public function handle()
354e3e87e4SAndreas Gohr    {
3633bed757SAndreas Gohr        if (!empty($_REQUEST['step']) && !checkSecurityToken()) {
3741163f44SAndreas Gohr            unset($_REQUEST['step']);
3841163f44SAndreas Gohr        }
3934aae6dbSAndreas Gohr    }
4034aae6dbSAndreas Gohr
414e3e87e4SAndreas Gohr    public function html()
424e3e87e4SAndreas Gohr    {
4334aae6dbSAndreas Gohr        $abrt = false;
4434aae6dbSAndreas Gohr        $next = false;
454e3e87e4SAndreas Gohr        $ok = true;
4634aae6dbSAndreas Gohr
4734aae6dbSAndreas Gohr        echo '<h1>' . $this->getLang('menu') . '</h1>';
4834aae6dbSAndreas Gohr
494e3e87e4SAndreas Gohr        echo '<div id="plugin__upgrade">';
5075e9d164SAndreas Gohr        // enable auto scroll
5175e9d164SAndreas Gohr        ?>
524e3e87e4SAndreas Gohr        <script type="text/javascript">
53d391262fSAndreas Gohr            var plugin_upgrade = window.setInterval(function () {
54d4376367SAndreas Gohr                var obj = document.getElementById('plugin__upgrade');
5575e9d164SAndreas Gohr                if (obj) obj.scrollTop = obj.scrollHeight;
5675e9d164SAndreas Gohr            }, 25);
5775e9d164SAndreas Gohr        </script>
5875e9d164SAndreas Gohr        <?php
5975e9d164SAndreas Gohr
6075e9d164SAndreas Gohr        // handle current step
614e3e87e4SAndreas Gohr        $this->nextStep($abrt, $next, $ok);
6234aae6dbSAndreas Gohr
6375e9d164SAndreas Gohr        // disable auto scroll
6475e9d164SAndreas Gohr        ?>
654e3e87e4SAndreas Gohr        <script type="text/javascript">
6675e9d164SAndreas Gohr            window.setTimeout(function () {
67d391262fSAndreas Gohr                window.clearInterval(plugin_upgrade);
6875e9d164SAndreas Gohr            }, 50);
6975e9d164SAndreas Gohr        </script>
7075e9d164SAndreas Gohr        <?php
714e3e87e4SAndreas Gohr        echo '</div>';
7275e9d164SAndreas Gohr
734ed3677eSAndreas Gohr        $careful = '';
744e3e87e4SAndreas Gohr        if (!$ok) {
754ed3677eSAndreas Gohr            echo '<div id="plugin__upgrade_careful">' . $this->getLang('careful') . '</div>';
764ed3677eSAndreas Gohr            $careful = 'careful';
774ed3677eSAndreas Gohr        }
784ed3677eSAndreas Gohr
799f8d4355SAndreas Gohr        $action = script();
809f8d4355SAndreas Gohr        echo '<form action="' . $action . '" method="post" id="plugin__upgrade_form">';
8134aae6dbSAndreas Gohr        echo '<input type="hidden" name="do" value="admin" />';
82d391262fSAndreas Gohr        echo '<input type="hidden" name="page" value="upgrade" />';
8341163f44SAndreas Gohr        echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />';
8438665ab9SAndreas Gohr        if ($next) echo '<button type="submit" name="step[' . $next . ']" value="1" class="button continue ' . $careful . '">' . $this->getLang('btn_continue') . ' ➡</button>';
859f8d4355SAndreas Gohr        if ($abrt) echo '<button type="submit" name="step[cancel]" value="1" class="button abort">✖ ' . $this->getLang('btn_abort') . '</button>';
8634aae6dbSAndreas Gohr        echo '</form>';
87eb52c46aSAndreas Gohr
884e3e87e4SAndreas Gohr        $this->displayProgressBar($next);
89eb52c46aSAndreas Gohr    }
90eb52c46aSAndreas Gohr
91eb52c46aSAndreas Gohr    /**
92eb52c46aSAndreas Gohr     * Display a progress bar of all steps
93eb52c46aSAndreas Gohr     *
94eb52c46aSAndreas Gohr     * @param string $next the next step
95eb52c46aSAndreas Gohr     */
964e3e87e4SAndreas Gohr    private function displayProgressBar($next)
974e3e87e4SAndreas Gohr    {
984e3e87e4SAndreas Gohr        $steps = ['version', 'download', 'unpack', 'check', 'upgrade'];
99eb52c46aSAndreas Gohr        $active = true;
100eb52c46aSAndreas Gohr        $count = 0;
101eb52c46aSAndreas Gohr
102eb52c46aSAndreas Gohr        echo '<div id="plugin__upgrade_meter"><ol>';
103eb52c46aSAndreas Gohr        foreach ($steps as $step) {
104eb52c46aSAndreas Gohr            $count++;
105eb52c46aSAndreas Gohr            if ($step == $next) $active = false;
106eb52c46aSAndreas Gohr            if ($active) {
107eb52c46aSAndreas Gohr                echo '<li class="active">';
108eb52c46aSAndreas Gohr                echo '<span class="step">✔</span>';
109eb52c46aSAndreas Gohr            } else {
110eb52c46aSAndreas Gohr                echo '<li>';
111eb52c46aSAndreas Gohr                echo '<span class="step">' . $count . '</span>';
112eb52c46aSAndreas Gohr            }
113eb52c46aSAndreas Gohr
114eb52c46aSAndreas Gohr            echo '<span class="stage">' . $this->getLang('step_' . $step) . '</span>';
115eb52c46aSAndreas Gohr            echo '</li>';
116eb52c46aSAndreas Gohr        }
117eb52c46aSAndreas Gohr        echo '</ol></div>';
11834aae6dbSAndreas Gohr    }
11934aae6dbSAndreas Gohr
120bd08ebd1SAndreas Gohr    /**
121bd08ebd1SAndreas Gohr     * Decides the current step and executes it
122bd08ebd1SAndreas Gohr     *
123bd08ebd1SAndreas Gohr     * @param bool $abrt
124bd08ebd1SAndreas Gohr     * @param bool $next
1254e3e87e4SAndreas Gohr     * @param bool $ok
126bd08ebd1SAndreas Gohr     */
1274e3e87e4SAndreas Gohr    private function nextStep(&$abrt, &$next, &$ok)
1284e3e87e4SAndreas Gohr    {
1294bed5591SAndreas Gohr
13034aae6dbSAndreas Gohr        if (isset($_REQUEST['step']) && is_array($_REQUEST['step'])) {
1319e48b5c0SAndreas Gohr            $keys = array_keys($_REQUEST['step']);
1329e48b5c0SAndreas Gohr            $step = array_shift($keys);
13334aae6dbSAndreas Gohr        } else {
13434aae6dbSAndreas Gohr            $step = '';
13534aae6dbSAndreas Gohr        }
13634aae6dbSAndreas Gohr
137fb8e77caSAndreas Gohr        if ($step == 'cancel' || $step == 'done') {
1384e3e87e4SAndreas Gohr            $ok = $this->helper->cleanup();
139fb8e77caSAndreas Gohr            if ($step == 'cancel') $step = '';
14034aae6dbSAndreas Gohr        }
14134aae6dbSAndreas Gohr
14234aae6dbSAndreas Gohr        if ($step) {
14334aae6dbSAndreas Gohr            $abrt = true;
14434aae6dbSAndreas Gohr            $next = false;
145f378cb7eSAndreas Gohr            if ($step == 'version') {
1464e3e87e4SAndreas Gohr                $ok = $this->helper->checkVersions();
147f378cb7eSAndreas Gohr                $next = 'download';
148fb8e77caSAndreas Gohr            } elseif ($step == 'done') {
1494e3e87e4SAndreas Gohr                echo $this->locale_xhtml('final');
150fb8e77caSAndreas Gohr                $next = '';
151fb8e77caSAndreas Gohr                $abrt = '';
1524e3e87e4SAndreas Gohr            } elseif (!file_exists($this->helper->tgzfile)) {
1534e3e87e4SAndreas Gohr                if ($ok = $this->helper->downloadTarball()) $next = 'unpack';
1544e3e87e4SAndreas Gohr            } elseif (!is_dir($this->helper->tgzdir)) {
1554e3e87e4SAndreas Gohr                if ($ok = $this->helper->extractTarball()) $next = 'check';
15634aae6dbSAndreas Gohr            } elseif ($step != 'upgrade') {
1574e3e87e4SAndreas Gohr                if ($ok = $this->helper->checkPermissions()) $next = 'upgrade';
15834aae6dbSAndreas Gohr            } elseif ($step == 'upgrade') {
1594e3e87e4SAndreas Gohr                if ($ok = $this->helper->copyFiles()) {
1604e3e87e4SAndreas Gohr                    $ok = $this->helper->deleteObsoleteFiles();
1614e3e87e4SAndreas Gohr                    $this->helper->cleanup();
162fb8e77caSAndreas Gohr                    $next = 'done';
163fb8e77caSAndreas Gohr                    $abrt = '';
164fb8e77caSAndreas Gohr                }
16534aae6dbSAndreas Gohr            } else {
16675e9d164SAndreas Gohr                echo 'uhm. what happened? where am I? This should not happen';
16734aae6dbSAndreas Gohr            }
16834aae6dbSAndreas Gohr        } else {
16934aae6dbSAndreas Gohr            # first time run, show intro
17034aae6dbSAndreas Gohr            echo $this->locale_xhtml('step0');
171*0aa25059SAndreas Gohr            $this->helper->cleanup(); // make sure we start clean
17234aae6dbSAndreas Gohr            $abrt = false;
173f378cb7eSAndreas Gohr            $next = 'version';
17434aae6dbSAndreas Gohr        }
17534aae6dbSAndreas Gohr    }
17634aae6dbSAndreas Gohr
177bd08ebd1SAndreas Gohr    /**
1784e3e87e4SAndreas Gohr     * Print the given message and flush buffers
1794e3e87e4SAndreas Gohr     *
1804e3e87e4SAndreas Gohr     * @param string $level
1814e3e87e4SAndreas Gohr     * @param string $message
182bd08ebd1SAndreas Gohr     */
1834e3e87e4SAndreas Gohr    public function log($level, $message) {
1844e3e87e4SAndreas Gohr        echo '<div class="log-' . $level . '">' . $message . '</div>';
1856e7afd39SAndreas Gohr        flush();
1866e7afd39SAndreas Gohr        ob_flush();
1876e7afd39SAndreas Gohr    }
18834aae6dbSAndreas Gohr}
189