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