xref: /plugin/upgrade/cli.php (revision 4e3e87e45b11e1184e5fb1995e14bdc3ab70ccf5)
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\Extension\CLIPlugin
14{
15    protected $logdefault = 'notice';
16
17    /** @inheritDoc */
18    protected function setup(Options $options)
19    {
20        $options->setHelp('Upgrade the wiki to the latest version');
21        $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
22        $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
23    }
24
25    /** @inheritDoc */
26    protected function main(Options $options)
27    {
28        $arguments = $options->getArgs();
29        if ($arguments[0] === 'check') {
30            $dryrun = true;
31        } elseif ($arguments[0] === 'run') {
32            $dryrun = false;
33        } else {
34            $this->fatal('Unknown command');
35        }
36
37
38        $helper = plugin_load('helper', 'upgrade');
39        /** @var helper_plugin_upgrade $helper */
40        $helper->setLogger($this);
41
42        if(!$helper->checkVersions() && !$options->getOpt('ignoreversions')) {
43            $this->fatal('Upgrade failed');
44        }
45        $helper->downloadTarball() || $this->fatal('Upgrade failed');
46        $helper->extractTarball() || $this->fatal('Upgrade failed');
47        $helper->checkPermissions() || $this->fatal('Upgrade failed');
48        if (!$dryrun) {
49            $helper->copyFiles() || $this->fatal('Upgrade failed');
50            $helper->deleteObsoleteFiles() || $this->fatal('Upgrade failed');
51        }
52        $helper->cleanUp();
53    }
54
55    /** @inheritDoc */
56    public function log($level, $message, array $context = array())
57    {
58        // Log messages are HTML formatted, we need to clean them for console
59        $message = strip_tags($message);
60        $message = htmlspecialchars_decode($message);
61        $message = preg_replace('/\s+/', ' ', $message);
62        parent::log($level, $message, $context);
63    }
64}
65
66