xref: /plugin/upgrade/cli.php (revision eb7336c4b47d1f36be83cf74dab460ab385544d0)
14e3e87e4SAndreas Gohr<?php
24e3e87e4SAndreas Gohr
3*eb7336c4SAndreas Gohruse splitbrain\phpcli\CLI;
44e3e87e4SAndreas Gohruse splitbrain\phpcli\Options;
54e3e87e4SAndreas Gohr
64e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php';
74e3e87e4SAndreas Gohr
84e3e87e4SAndreas Gohr/**
94e3e87e4SAndreas Gohr * DokuWiki Plugin upgrade (CLI Component)
104e3e87e4SAndreas Gohr *
114e3e87e4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
124e3e87e4SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
134e3e87e4SAndreas Gohr */
14*eb7336c4SAndreas Gohrclass cli_plugin_upgrade extends CLI
154e3e87e4SAndreas Gohr{
164e3e87e4SAndreas Gohr    protected $logdefault = 'notice';
17*eb7336c4SAndreas Gohr    protected $helper;
18*eb7336c4SAndreas Gohr
19*eb7336c4SAndreas Gohr    /**
20*eb7336c4SAndreas Gohr     * initialize
21*eb7336c4SAndreas Gohr     */
22*eb7336c4SAndreas Gohr    public function __construct()
23*eb7336c4SAndreas Gohr    {
24*eb7336c4SAndreas Gohr        parent::__construct();
25*eb7336c4SAndreas Gohr        $this->helper = new helper_plugin_upgrade();
26*eb7336c4SAndreas Gohr        $this->helper->setLogger($this);
27*eb7336c4SAndreas Gohr    }
28*eb7336c4SAndreas Gohr
29*eb7336c4SAndreas Gohr    /**
30*eb7336c4SAndreas Gohr     * This class is not really a plugin so we have to implement the plugin trait ourselves
31*eb7336c4SAndreas Gohr     *
32*eb7336c4SAndreas Gohr     * @return string[]
33*eb7336c4SAndreas Gohr     */
34*eb7336c4SAndreas Gohr    public function getInfo()
35*eb7336c4SAndreas Gohr    {
36*eb7336c4SAndreas Gohr        return $this->helper->getInfo();
37*eb7336c4SAndreas Gohr    }
384e3e87e4SAndreas Gohr
394e3e87e4SAndreas Gohr    /** @inheritDoc */
404e3e87e4SAndreas Gohr    protected function setup(Options $options)
414e3e87e4SAndreas Gohr    {
424e3e87e4SAndreas Gohr        $options->setHelp('Upgrade the wiki to the latest version');
434e3e87e4SAndreas Gohr        $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
444e3e87e4SAndreas Gohr        $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
454e3e87e4SAndreas Gohr    }
464e3e87e4SAndreas Gohr
474e3e87e4SAndreas Gohr    /** @inheritDoc */
484e3e87e4SAndreas Gohr    protected function main(Options $options)
494e3e87e4SAndreas Gohr    {
504e3e87e4SAndreas Gohr        $arguments = $options->getArgs();
514e3e87e4SAndreas Gohr        if ($arguments[0] === 'check') {
524e3e87e4SAndreas Gohr            $dryrun = true;
534e3e87e4SAndreas Gohr        } elseif ($arguments[0] === 'run') {
544e3e87e4SAndreas Gohr            $dryrun = false;
554e3e87e4SAndreas Gohr        } else {
564e3e87e4SAndreas Gohr            $this->fatal('Unknown command');
574e3e87e4SAndreas Gohr        }
584e3e87e4SAndreas Gohr
59*eb7336c4SAndreas Gohr        if (!$this->helper->checkVersions() && !$options->getOpt('ignoreversions')) {
604e3e87e4SAndreas Gohr            $this->fatal('Upgrade failed');
614e3e87e4SAndreas Gohr        }
62*eb7336c4SAndreas Gohr        $this->helper->downloadTarball() || $this->fatal('Upgrade failed');
63*eb7336c4SAndreas Gohr        $this->helper->extractTarball() || $this->fatal('Upgrade failed');
64*eb7336c4SAndreas Gohr        $this->helper->checkPermissions() || $this->fatal('Upgrade failed');
654e3e87e4SAndreas Gohr        if (!$dryrun) {
66*eb7336c4SAndreas Gohr            $this->helper->copyFiles() || $this->fatal('Upgrade failed');
67*eb7336c4SAndreas Gohr            $this->helper->deleteObsoleteFiles() || $this->fatal('Upgrade failed');
684e3e87e4SAndreas Gohr        }
69*eb7336c4SAndreas Gohr        $this->helper->cleanUp();
704e3e87e4SAndreas Gohr    }
714e3e87e4SAndreas Gohr
724e3e87e4SAndreas Gohr    /** @inheritDoc */
734e3e87e4SAndreas Gohr    public function log($level, $message, array $context = array())
744e3e87e4SAndreas Gohr    {
754e3e87e4SAndreas Gohr        // Log messages are HTML formatted, we need to clean them for console
764e3e87e4SAndreas Gohr        $message = strip_tags($message);
774e3e87e4SAndreas Gohr        $message = htmlspecialchars_decode($message);
784e3e87e4SAndreas Gohr        $message = preg_replace('/\s+/', ' ', $message);
794e3e87e4SAndreas Gohr        parent::log($level, $message, $context);
804e3e87e4SAndreas Gohr    }
814e3e87e4SAndreas Gohr}
824e3e87e4SAndreas Gohr
83*eb7336c4SAndreas Gohr// run the script ourselves if called directly
84*eb7336c4SAndreas Gohrif(basename($_SERVER['SCRIPT_NAME']) == 'cli.php') {
85*eb7336c4SAndreas Gohr    if(!defined('DOKU_INC')) define('DOKU_INC', __DIR__ . '/../../../');
86*eb7336c4SAndreas Gohr    require_once(DOKU_INC . 'inc/init.php');
87*eb7336c4SAndreas Gohr    $cli = new cli_plugin_upgrade();
88*eb7336c4SAndreas Gohr    $cli->run();
89*eb7336c4SAndreas Gohr}
90