14e3e87e4SAndreas Gohr<?php 24e3e87e4SAndreas Gohr 3*8dab6045SAndreas Gohr// phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols 4*8dab6045SAndreas Gohr 54e3e87e4SAndreas Gohruse splitbrain\phpcli\Options; 64e3e87e4SAndreas Gohr 74e3e87e4SAndreas Gohrrequire_once __DIR__ . '/vendor/autoload.php'; 84e3e87e4SAndreas Gohr 94e3e87e4SAndreas Gohr/** 104e3e87e4SAndreas Gohr * DokuWiki Plugin upgrade (CLI Component) 114e3e87e4SAndreas Gohr * 124e3e87e4SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 134e3e87e4SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 144e3e87e4SAndreas Gohr */ 15fc9fd1daSAndreas Gohrclass cli_plugin_upgrade extends DokuWiki_CLI_Plugin 164e3e87e4SAndreas Gohr{ 174e3e87e4SAndreas Gohr protected $logdefault = 'notice'; 18eb7336c4SAndreas Gohr protected $helper; 19eb7336c4SAndreas Gohr 20eb7336c4SAndreas Gohr /** 21eb7336c4SAndreas Gohr * initialize 22eb7336c4SAndreas Gohr */ 23eb7336c4SAndreas Gohr public function __construct() 24eb7336c4SAndreas Gohr { 25eb7336c4SAndreas Gohr parent::__construct(); 26eb7336c4SAndreas Gohr $this->helper = new helper_plugin_upgrade(); 27eb7336c4SAndreas Gohr $this->helper->setLogger($this); 28eb7336c4SAndreas Gohr } 29eb7336c4SAndreas Gohr 304e3e87e4SAndreas Gohr /** @inheritDoc */ 314e3e87e4SAndreas Gohr protected function setup(Options $options) 324e3e87e4SAndreas Gohr { 33601d513fSAndreas Gohr $options->setHelp( 34601d513fSAndreas Gohr 'This tool will upgrade your wiki to the newest release. It will automatically check file permissions ' . 35601d513fSAndreas Gohr 'and download the required tarball. Internet access is required.' 36601d513fSAndreas Gohr ); 374e3e87e4SAndreas Gohr $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true'); 384e3e87e4SAndreas Gohr $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i'); 394e3e87e4SAndreas Gohr } 404e3e87e4SAndreas Gohr 414e3e87e4SAndreas Gohr /** @inheritDoc */ 424e3e87e4SAndreas Gohr protected function main(Options $options) 434e3e87e4SAndreas Gohr { 444e3e87e4SAndreas Gohr $arguments = $options->getArgs(); 454e3e87e4SAndreas Gohr if ($arguments[0] === 'check') { 464e3e87e4SAndreas Gohr $dryrun = true; 474e3e87e4SAndreas Gohr } elseif ($arguments[0] === 'run') { 484e3e87e4SAndreas Gohr $dryrun = false; 494e3e87e4SAndreas Gohr } else { 504e3e87e4SAndreas Gohr $this->fatal('Unknown command'); 514e3e87e4SAndreas Gohr } 524e3e87e4SAndreas Gohr 53eb7336c4SAndreas Gohr if (!$this->helper->checkVersions() && !$options->getOpt('ignoreversions')) { 54601d513fSAndreas Gohr $this->fatal('Upgrade aborted'); 554e3e87e4SAndreas Gohr } 56601d513fSAndreas Gohr $this->helper->downloadTarball() || $this->fatal('Upgrade aborted'); 57601d513fSAndreas Gohr $this->helper->extractTarball() || $this->fatal('Upgrade aborted'); 58601d513fSAndreas Gohr $this->helper->checkPermissions() || $this->fatal('Upgrade aborted'); 594e3e87e4SAndreas Gohr if (!$dryrun) { 60601d513fSAndreas Gohr $this->helper->copyFiles() || $this->fatal('Upgrade aborted'); 61601d513fSAndreas Gohr $this->helper->deleteObsoleteFiles() || $this->fatal('Upgrade aborted'); 624e3e87e4SAndreas Gohr } 63eb7336c4SAndreas Gohr $this->helper->cleanUp(); 644e3e87e4SAndreas Gohr } 654e3e87e4SAndreas Gohr 664e3e87e4SAndreas Gohr /** @inheritDoc */ 674e3e87e4SAndreas Gohr public function log($level, $message, array $context = array()) 684e3e87e4SAndreas Gohr { 694e3e87e4SAndreas Gohr // Log messages are HTML formatted, we need to clean them for console 704e3e87e4SAndreas Gohr $message = strip_tags($message); 714e3e87e4SAndreas Gohr $message = htmlspecialchars_decode($message); 724e3e87e4SAndreas Gohr $message = preg_replace('/\s+/', ' ', $message); 734e3e87e4SAndreas Gohr parent::log($level, $message, $context); 744e3e87e4SAndreas Gohr } 754e3e87e4SAndreas Gohr} 764e3e87e4SAndreas Gohr 77eb7336c4SAndreas Gohr// run the script ourselves if called directly 78eb7336c4SAndreas Gohrif (basename($_SERVER['SCRIPT_NAME']) == 'cli.php') { 79eb7336c4SAndreas Gohr $cli = new cli_plugin_upgrade(); 80eb7336c4SAndreas Gohr $cli->run(); 81eb7336c4SAndreas Gohr} 82