xref: /plugin/upgrade/cli.php (revision fc9fd1dacdabb9cf1d802003da12b51682e02ca8)
14e3e87e4SAndreas Gohr<?php
24e3e87e4SAndreas Gohr
34e3e87e4SAndreas Gohruse splitbrain\phpcli\Options;
44e3e87e4SAndreas Gohr
54e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
64e3e87e4SAndreas Gohr
74e3e87e4SAndreas Gohr/**
84e3e87e4SAndreas Gohr * DokuWiki Plugin upgrade (CLI Component)
94e3e87e4SAndreas Gohr *
104e3e87e4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
114e3e87e4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
124e3e87e4SAndreas Gohr */
13*fc9fd1daSAndreas Gohrclass cli_plugin_upgrade extends DokuWiki_CLI_Plugin
144e3e87e4SAndreas Gohr{
154e3e87e4SAndreas Gohr    protected $logdefault = 'notice';
16eb7336c4SAndreas Gohr    protected $helper;
17eb7336c4SAndreas Gohr
18eb7336c4SAndreas Gohr    /**
19eb7336c4SAndreas Gohr     * initialize
20eb7336c4SAndreas Gohr     */
21eb7336c4SAndreas Gohr    public function __construct()
22eb7336c4SAndreas Gohr    {
23eb7336c4SAndreas Gohr        parent::__construct();
24eb7336c4SAndreas Gohr        $this->helper = new helper_plugin_upgrade();
25eb7336c4SAndreas Gohr        $this->helper->setLogger($this);
26eb7336c4SAndreas Gohr    }
27eb7336c4SAndreas Gohr
284e3e87e4SAndreas Gohr    /** @inheritDoc */
294e3e87e4SAndreas Gohr    protected function setup(Options $options)
304e3e87e4SAndreas Gohr    {
314e3e87e4SAndreas Gohr        $options->setHelp('Upgrade the wiki to the latest version');
324e3e87e4SAndreas Gohr        $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
334e3e87e4SAndreas Gohr        $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
344e3e87e4SAndreas Gohr    }
354e3e87e4SAndreas Gohr
364e3e87e4SAndreas Gohr    /** @inheritDoc */
374e3e87e4SAndreas Gohr    protected function main(Options $options)
384e3e87e4SAndreas Gohr    {
394e3e87e4SAndreas Gohr        $arguments = $options->getArgs();
404e3e87e4SAndreas Gohr        if ($arguments[0] === 'check') {
414e3e87e4SAndreas Gohr            $dryrun = true;
424e3e87e4SAndreas Gohr        } elseif ($arguments[0] === 'run') {
434e3e87e4SAndreas Gohr            $dryrun = false;
444e3e87e4SAndreas Gohr        } else {
454e3e87e4SAndreas Gohr            $this->fatal('Unknown command');
464e3e87e4SAndreas Gohr        }
474e3e87e4SAndreas Gohr
48eb7336c4SAndreas Gohr        if (!$this->helper->checkVersions() && !$options->getOpt('ignoreversions')) {
494e3e87e4SAndreas Gohr            $this->fatal('Upgrade failed');
504e3e87e4SAndreas Gohr        }
51eb7336c4SAndreas Gohr        $this->helper->downloadTarball() || $this->fatal('Upgrade failed');
52eb7336c4SAndreas Gohr        $this->helper->extractTarball() || $this->fatal('Upgrade failed');
53eb7336c4SAndreas Gohr        $this->helper->checkPermissions() || $this->fatal('Upgrade failed');
544e3e87e4SAndreas Gohr        if (!$dryrun) {
55eb7336c4SAndreas Gohr            $this->helper->copyFiles() || $this->fatal('Upgrade failed');
56eb7336c4SAndreas Gohr            $this->helper->deleteObsoleteFiles() || $this->fatal('Upgrade failed');
574e3e87e4SAndreas Gohr        }
58eb7336c4SAndreas Gohr        $this->helper->cleanUp();
594e3e87e4SAndreas Gohr    }
604e3e87e4SAndreas Gohr
614e3e87e4SAndreas Gohr    /** @inheritDoc */
624e3e87e4SAndreas Gohr    public function log($level, $message, array $context = array())
634e3e87e4SAndreas Gohr    {
644e3e87e4SAndreas Gohr        // Log messages are HTML formatted, we need to clean them for console
654e3e87e4SAndreas Gohr        $message = strip_tags($message);
664e3e87e4SAndreas Gohr        $message = htmlspecialchars_decode($message);
674e3e87e4SAndreas Gohr        $message = preg_replace('/\s+/', ' ', $message);
684e3e87e4SAndreas Gohr        parent::log($level, $message, $context);
694e3e87e4SAndreas Gohr    }
704e3e87e4SAndreas Gohr}
714e3e87e4SAndreas Gohr
72eb7336c4SAndreas Gohr// run the script ourselves if called directly
73eb7336c4SAndreas Gohrif(basename($_SERVER['SCRIPT_NAME']) == 'cli.php') {
74eb7336c4SAndreas Gohr    $cli = new cli_plugin_upgrade();
75eb7336c4SAndreas Gohr    $cli->run();
76eb7336c4SAndreas Gohr}
77