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