SelfupgradeCommand.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. /**
  3. * @package Grav\Console\Gpm
  4. *
  5. * @copyright Copyright (C) 2015 - 2019 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Console\Gpm;
  9. use Grav\Common\Cache;
  10. use Grav\Common\Filesystem\Folder;
  11. use Grav\Common\GPM\Installer;
  12. use Grav\Common\GPM\Remote\Package;
  13. use Grav\Common\GPM\Response;
  14. use Grav\Common\GPM\Upgrader;
  15. use Grav\Common\Grav;
  16. use Grav\Console\ConsoleCommand;
  17. use Symfony\Component\Console\Input\ArrayInput;
  18. use Symfony\Component\Console\Input\InputOption;
  19. use Symfony\Component\Console\Question\ConfirmationQuestion;
  20. class SelfupgradeCommand extends ConsoleCommand
  21. {
  22. /** @var array */
  23. protected $data;
  24. protected $extensions;
  25. protected $updatable;
  26. /** @var string */
  27. protected $file;
  28. /** @var array */
  29. protected $types = ['plugins', 'themes'];
  30. /** @var string */
  31. private $tmp;
  32. private $upgrader;
  33. /** @var string */
  34. protected $all_yes;
  35. protected $overwrite;
  36. protected function configure()
  37. {
  38. $this
  39. ->setName('self-upgrade')
  40. ->setAliases(['selfupgrade', 'selfupdate'])
  41. ->addOption(
  42. 'force',
  43. 'f',
  44. InputOption::VALUE_NONE,
  45. 'Force re-fetching the data from remote'
  46. )
  47. ->addOption(
  48. 'all-yes',
  49. 'y',
  50. InputOption::VALUE_NONE,
  51. 'Assumes yes (or best approach) instead of prompting'
  52. )
  53. ->addOption(
  54. 'overwrite',
  55. 'o',
  56. InputOption::VALUE_NONE,
  57. 'Option to overwrite packages if they already exist'
  58. )
  59. ->setDescription('Detects and performs an update of Grav itself when available')
  60. ->setHelp('The <info>update</info> command updates Grav itself when a new version is available');
  61. }
  62. protected function serve()
  63. {
  64. $this->upgrader = new Upgrader($this->input->getOption('force'));
  65. $this->all_yes = $this->input->getOption('all-yes');
  66. $this->overwrite = $this->input->getOption('overwrite');
  67. $this->displayGPMRelease();
  68. $update = $this->upgrader->getAssets()['grav-update'];
  69. $local = $this->upgrader->getLocalVersion();
  70. $remote = $this->upgrader->getRemoteVersion();
  71. $release = strftime('%c', strtotime($this->upgrader->getReleaseDate()));
  72. if (!$this->upgrader->meetsRequirements()) {
  73. $this->output->writeln('<red>ATTENTION:</red>');
  74. $this->output->writeln(' Grav has increased the minimum PHP requirement.');
  75. $this->output->writeln(' You are currently running PHP <red>' . phpversion() . '</red>, but PHP <green>' . $this->upgrader->minPHPVersion() . '</green> is required.');
  76. $this->output->writeln(' Additional information: <white>http://getgrav.org/blog/changing-php-requirements</white>');
  77. $this->output->writeln('');
  78. $this->output->writeln('Selfupgrade aborted.');
  79. $this->output->writeln('');
  80. exit;
  81. }
  82. if (!$this->overwrite && !$this->upgrader->isUpgradable()) {
  83. $this->output->writeln("You are already running the latest version of Grav (v{$local}) released on {$release}");
  84. exit;
  85. }
  86. Installer::isValidDestination(GRAV_ROOT . '/system');
  87. if (Installer::IS_LINK === Installer::lastErrorCode()) {
  88. $this->output->writeln('<red>ATTENTION:</red> Grav is symlinked, cannot upgrade, aborting...');
  89. $this->output->writeln('');
  90. $this->output->writeln("You are currently running a symbolically linked Grav v{$local}. Latest available is v{$remote}.");
  91. exit;
  92. }
  93. // not used but preloaded just in case!
  94. new ArrayInput([]);
  95. $questionHelper = $this->getHelper('question');
  96. $this->output->writeln("Grav v<cyan>{$remote}</cyan> is now available [release date: {$release}].");
  97. $this->output->writeln('You are currently using v<cyan>' . GRAV_VERSION . '</cyan>.');
  98. if (!$this->all_yes) {
  99. $question = new ConfirmationQuestion('Would you like to read the changelog before proceeding? [y|N] ',
  100. false);
  101. $answer = $questionHelper->ask($this->input, $this->output, $question);
  102. if ($answer) {
  103. $changelog = $this->upgrader->getChangelog(GRAV_VERSION);
  104. $this->output->writeln('');
  105. foreach ($changelog as $version => $log) {
  106. $title = $version . ' [' . $log['date'] . ']';
  107. $content = preg_replace_callback('/\d\.\s\[\]\(#(.*)\)/', function ($match) {
  108. return "\n" . ucfirst($match[1]) . ':';
  109. }, $log['content']);
  110. $this->output->writeln($title);
  111. $this->output->writeln(str_repeat('-', \strlen($title)));
  112. $this->output->writeln($content);
  113. $this->output->writeln('');
  114. }
  115. $question = new ConfirmationQuestion('Press [ENTER] to continue.', true);
  116. $questionHelper->ask($this->input, $this->output, $question);
  117. }
  118. $question = new ConfirmationQuestion('Would you like to upgrade now? [y|N] ', false);
  119. $answer = $questionHelper->ask($this->input, $this->output, $question);
  120. if (!$answer) {
  121. $this->output->writeln('Aborting...');
  122. exit;
  123. }
  124. }
  125. $this->output->writeln('');
  126. $this->output->writeln("Preparing to upgrade to v<cyan>{$remote}</cyan>..");
  127. $this->output->write(" |- Downloading upgrade [{$this->formatBytes($update['size'])}]... 0%");
  128. $this->file = $this->download($update);
  129. $this->output->write(' |- Installing upgrade... ');
  130. $installation = $this->upgrade();
  131. if (!$installation) {
  132. $this->output->writeln(" '- <red>Installation failed or aborted.</red>");
  133. $this->output->writeln('');
  134. } else {
  135. $this->output->writeln(" '- <green>Success!</green> ");
  136. $this->output->writeln('');
  137. }
  138. // clear cache after successful upgrade
  139. $this->clearCache('all');
  140. }
  141. /**
  142. * @param array $package
  143. *
  144. * @return string
  145. */
  146. private function download($package)
  147. {
  148. $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true);
  149. $this->tmp = $tmp_dir . '/Grav-' . uniqid('', false);
  150. $output = Response::get($package['download'], [], [$this, 'progress']);
  151. Folder::create($this->tmp);
  152. $this->output->write("\x0D");
  153. $this->output->write(" |- Downloading upgrade [{$this->formatBytes($package['size'])}]... 100%");
  154. $this->output->writeln('');
  155. file_put_contents($this->tmp . DS . $package['name'], $output);
  156. return $this->tmp . DS . $package['name'];
  157. }
  158. /**
  159. * @return bool
  160. */
  161. private function upgrade()
  162. {
  163. if ($this->file) {
  164. $folder = Installer::unZip($this->file, $this->tmp . '/zip');
  165. } else {
  166. $folder = false;
  167. }
  168. static::upgradeGrav($this->file, $folder);
  169. $errorCode = Installer::lastErrorCode();
  170. if ($this->tmp) {
  171. Folder::delete($this->tmp);
  172. }
  173. if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) {
  174. $this->output->write("\x0D");
  175. // extra white spaces to clear out the buffer properly
  176. $this->output->writeln(' |- Installing upgrade... <red>error</red> ');
  177. $this->output->writeln(" | '- " . Installer::lastErrorMsg());
  178. return false;
  179. }
  180. $this->output->write("\x0D");
  181. // extra white spaces to clear out the buffer properly
  182. $this->output->writeln(' |- Installing upgrade... <green>ok</green> ');
  183. return true;
  184. }
  185. /**
  186. * @param array $progress
  187. */
  188. public function progress($progress)
  189. {
  190. $this->output->write("\x0D");
  191. $this->output->write(" |- Downloading upgrade [{$this->formatBytes($progress['filesize']) }]... " . str_pad($progress['percent'],
  192. 5, ' ', STR_PAD_LEFT) . '%');
  193. }
  194. /**
  195. * @param int|float $size
  196. * @param int $precision
  197. *
  198. * @return string
  199. */
  200. public function formatBytes($size, $precision = 2)
  201. {
  202. $base = log($size) / log(1024);
  203. $suffixes = array('', 'k', 'M', 'G', 'T');
  204. return round(1024 ** ($base - floor($base)), $precision) . $suffixes[(int)floor($base)];
  205. }
  206. private function upgradeGrav($zip, $folder, $keepFolder = false)
  207. {
  208. static $ignores = [
  209. 'backup',
  210. 'cache',
  211. 'images',
  212. 'logs',
  213. 'tmp',
  214. 'user',
  215. '.htaccess',
  216. 'robots.txt'
  217. ];
  218. if (!is_dir($folder)) {
  219. Installer::setError('Invalid source folder');
  220. }
  221. try {
  222. $script = $folder . '/system/install.php';
  223. /** Install $installer */
  224. if ((file_exists($script) && $install = include $script) && is_callable($install)) {
  225. $install($zip);
  226. } else {
  227. Installer::install(
  228. $zip,
  229. GRAV_ROOT,
  230. ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true, 'ignores' => $ignores],
  231. $folder,
  232. $keepFolder
  233. );
  234. Cache::clearCache();
  235. }
  236. } catch (\Exception $e) {
  237. Installer::setError($e->getMessage());
  238. }
  239. }
  240. }