xref: /plugin/upgrade/cli.php (revision 601d513f448492c18a7f28c1848ee9738a83fce4)
1<?php
2
3use splitbrain\phpcli\Options;
4
5require_once __DIR__ . '/vendor/autoload.php';
6
7/**
8 * DokuWiki Plugin upgrade (CLI Component)
9 *
10 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11 * @author  Andreas Gohr <andi@splitbrain.org>
12 */
13class cli_plugin_upgrade extends DokuWiki_CLI_Plugin
14{
15    protected $logdefault = 'notice';
16    protected $helper;
17
18    /**
19     * initialize
20     */
21    public function __construct()
22    {
23        parent::__construct();
24        $this->helper = new helper_plugin_upgrade();
25        $this->helper->setLogger($this);
26    }
27
28    /** @inheritDoc */
29    protected function setup(Options $options)
30    {
31        $options->setHelp(
32            'This tool will upgrade your wiki to the newest release. It will automatically check file permissions '.
33            'and download the required tarball. Internet access is required.'
34        );
35        $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
36        $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
37    }
38
39    /** @inheritDoc */
40    protected function main(Options $options)
41    {
42        $arguments = $options->getArgs();
43        if ($arguments[0] === 'check') {
44            $dryrun = true;
45        } elseif ($arguments[0] === 'run') {
46            $dryrun = false;
47        } else {
48            $this->fatal('Unknown command');
49        }
50
51        if (!$this->helper->checkVersions() && !$options->getOpt('ignoreversions')) {
52            $this->fatal('Upgrade aborted');
53        }
54        $this->helper->downloadTarball() || $this->fatal('Upgrade aborted');
55        $this->helper->extractTarball() || $this->fatal('Upgrade aborted');
56        $this->helper->checkPermissions() || $this->fatal('Upgrade aborted');
57        if (!$dryrun) {
58            $this->helper->copyFiles() || $this->fatal('Upgrade aborted');
59            $this->helper->deleteObsoleteFiles() || $this->fatal('Upgrade aborted');
60        }
61        $this->helper->cleanUp();
62    }
63
64    /** @inheritDoc */
65    public function log($level, $message, array $context = array())
66    {
67        // Log messages are HTML formatted, we need to clean them for console
68        $message = strip_tags($message);
69        $message = htmlspecialchars_decode($message);
70        $message = preg_replace('/\s+/', ' ', $message);
71        parent::log($level, $message, $context);
72    }
73}
74
75// run the script ourselves if called directly
76if(basename($_SERVER['SCRIPT_NAME']) == 'cli.php') {
77    $cli = new cli_plugin_upgrade();
78    $cli->run();
79}
80