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