xref: /plugin/upgrade/cli.php (revision 4e3e87e45b11e1184e5fb1995e14bdc3ab70ccf5)
1*4e3e87e4SAndreas Gohr<?php
2*4e3e87e4SAndreas Gohr
3*4e3e87e4SAndreas Gohruse splitbrain\phpcli\Options;
4*4e3e87e4SAndreas Gohr
5*4e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
6*4e3e87e4SAndreas Gohr
7*4e3e87e4SAndreas Gohr/**
8*4e3e87e4SAndreas Gohr * DokuWiki Plugin upgrade (CLI Component)
9*4e3e87e4SAndreas Gohr *
10*4e3e87e4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
11*4e3e87e4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
12*4e3e87e4SAndreas Gohr */
13*4e3e87e4SAndreas Gohrclass cli_plugin_upgrade extends \dokuwiki\Extension\CLIPlugin
14*4e3e87e4SAndreas Gohr{
15*4e3e87e4SAndreas Gohr    protected $logdefault = 'notice';
16*4e3e87e4SAndreas Gohr
17*4e3e87e4SAndreas Gohr    /** @inheritDoc */
18*4e3e87e4SAndreas Gohr    protected function setup(Options $options)
19*4e3e87e4SAndreas Gohr    {
20*4e3e87e4SAndreas Gohr        $options->setHelp('Upgrade the wiki to the latest version');
21*4e3e87e4SAndreas Gohr        $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
22*4e3e87e4SAndreas Gohr        $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
23*4e3e87e4SAndreas Gohr    }
24*4e3e87e4SAndreas Gohr
25*4e3e87e4SAndreas Gohr    /** @inheritDoc */
26*4e3e87e4SAndreas Gohr    protected function main(Options $options)
27*4e3e87e4SAndreas Gohr    {
28*4e3e87e4SAndreas Gohr        $arguments = $options->getArgs();
29*4e3e87e4SAndreas Gohr        if ($arguments[0] === 'check') {
30*4e3e87e4SAndreas Gohr            $dryrun = true;
31*4e3e87e4SAndreas Gohr        } elseif ($arguments[0] === 'run') {
32*4e3e87e4SAndreas Gohr            $dryrun = false;
33*4e3e87e4SAndreas Gohr        } else {
34*4e3e87e4SAndreas Gohr            $this->fatal('Unknown command');
35*4e3e87e4SAndreas Gohr        }
36*4e3e87e4SAndreas Gohr
37*4e3e87e4SAndreas Gohr
38*4e3e87e4SAndreas Gohr        $helper = plugin_load('helper', 'upgrade');
39*4e3e87e4SAndreas Gohr        /** @var helper_plugin_upgrade $helper */
40*4e3e87e4SAndreas Gohr        $helper->setLogger($this);
41*4e3e87e4SAndreas Gohr
42*4e3e87e4SAndreas Gohr        if(!$helper->checkVersions() && !$options->getOpt('ignoreversions')) {
43*4e3e87e4SAndreas Gohr            $this->fatal('Upgrade failed');
44*4e3e87e4SAndreas Gohr        }
45*4e3e87e4SAndreas Gohr        $helper->downloadTarball() || $this->fatal('Upgrade failed');
46*4e3e87e4SAndreas Gohr        $helper->extractTarball() || $this->fatal('Upgrade failed');
47*4e3e87e4SAndreas Gohr        $helper->checkPermissions() || $this->fatal('Upgrade failed');
48*4e3e87e4SAndreas Gohr        if (!$dryrun) {
49*4e3e87e4SAndreas Gohr            $helper->copyFiles() || $this->fatal('Upgrade failed');
50*4e3e87e4SAndreas Gohr            $helper->deleteObsoleteFiles() || $this->fatal('Upgrade failed');
51*4e3e87e4SAndreas Gohr        }
52*4e3e87e4SAndreas Gohr        $helper->cleanUp();
53*4e3e87e4SAndreas Gohr    }
54*4e3e87e4SAndreas Gohr
55*4e3e87e4SAndreas Gohr    /** @inheritDoc */
56*4e3e87e4SAndreas Gohr    public function log($level, $message, array $context = array())
57*4e3e87e4SAndreas Gohr    {
58*4e3e87e4SAndreas Gohr        // Log messages are HTML formatted, we need to clean them for console
59*4e3e87e4SAndreas Gohr        $message = strip_tags($message);
60*4e3e87e4SAndreas Gohr        $message = htmlspecialchars_decode($message);
61*4e3e87e4SAndreas Gohr        $message = preg_replace('/\s+/', ' ', $message);
62*4e3e87e4SAndreas Gohr        parent::log($level, $message, $context);
63*4e3e87e4SAndreas Gohr    }
64*4e3e87e4SAndreas Gohr}
65*4e3e87e4SAndreas Gohr
66