cli.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. use splitbrain\phpcli\Options;
  3. require_once __DIR__ . '/vendor/autoload.php';
  4. /**
  5. * DokuWiki Plugin upgrade (CLI Component)
  6. *
  7. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  8. * @author Andreas Gohr <andi@splitbrain.org>
  9. */
  10. class cli_plugin_upgrade extends DokuWiki_CLI_Plugin
  11. {
  12. protected $logdefault = 'notice';
  13. protected $helper;
  14. /**
  15. * initialize
  16. */
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->helper = new helper_plugin_upgrade();
  21. $this->helper->setLogger($this);
  22. }
  23. /** @inheritDoc */
  24. protected function setup(Options $options)
  25. {
  26. $options->setHelp(
  27. 'This tool will upgrade your wiki to the newest release. It will automatically check file permissions '.
  28. 'and download the required tarball. Internet access is required.'
  29. );
  30. $options->registerArgument('check|run', 'Either only check if an update can be done or do it', 'true');
  31. $options->registerOption('ignoreversions', 'Ignore the version check results and continue anyway', 'i');
  32. }
  33. /** @inheritDoc */
  34. protected function main(Options $options)
  35. {
  36. $arguments = $options->getArgs();
  37. if ($arguments[0] === 'check') {
  38. $dryrun = true;
  39. } elseif ($arguments[0] === 'run') {
  40. $dryrun = false;
  41. } else {
  42. $this->fatal('Unknown command');
  43. }
  44. if (!$this->helper->checkVersions() && !$options->getOpt('ignoreversions')) {
  45. $this->fatal('Upgrade aborted');
  46. }
  47. $this->helper->downloadTarball() || $this->fatal('Upgrade aborted');
  48. $this->helper->extractTarball() || $this->fatal('Upgrade aborted');
  49. $this->helper->checkPermissions() || $this->fatal('Upgrade aborted');
  50. if (!$dryrun) {
  51. $this->helper->copyFiles() || $this->fatal('Upgrade aborted');
  52. $this->helper->deleteObsoleteFiles() || $this->fatal('Upgrade aborted');
  53. }
  54. $this->helper->cleanUp();
  55. }
  56. /** @inheritDoc */
  57. public function log($level, $message, array $context = array())
  58. {
  59. // Log messages are HTML formatted, we need to clean them for console
  60. $message = strip_tags($message);
  61. $message = htmlspecialchars_decode($message);
  62. $message = preg_replace('/\s+/', ' ', $message);
  63. parent::log($level, $message, $context);
  64. }
  65. }
  66. // run the script ourselves if called directly
  67. if(basename($_SERVER['SCRIPT_NAME']) == 'cli.php') {
  68. $cli = new cli_plugin_upgrade();
  69. $cli->run();
  70. }