/** * @return int|null|void */ protected function serve() { $this->gpm = new GPM($this->input->getOption('force')); $this->displayGPMRelease(); $this->destination = realpath($this->input->getOption('destination')); $skip_prompt = $this->input->getOption('all-yes'); if (!Installer::isGravInstance($this->destination)) { $this->output->writeln("<red>ERROR</red>: " . Installer::lastErrorMsg()); exit; } if ($this->input->getOption('plugins') === false and $this->input->getOption('themes') === false) { $list_type_update = ['plugins' => true, 'themes' => true]; } else { $list_type_update['plugins'] = $this->input->getOption('plugins'); $list_type_update['themes'] = $this->input->getOption('themes'); } $this->data = $this->gpm->getUpdatable($list_type_update); $only_packages = array_map('strtolower', $this->input->getArgument('package')); if (!$this->data['total']) { $this->output->writeln("Nothing to update."); exit; } $this->output->write("Found <green>" . $this->gpm->countInstalled() . "</green> extensions installed of which <magenta>" . $this->data['total'] . "</magenta> need updating"); $limit_to = $this->userInputPackages($only_packages); $this->output->writeln(''); unset($this->data['total']); unset($limit_to['total']); // updates review $slugs = []; $index = 0; foreach ($this->data as $packages) { foreach ($packages as $slug => $package) { if (count($limit_to) && !array_key_exists($slug, $limit_to)) { continue; } $this->output->writeln(str_pad($index++ + 1, 2, '0', STR_PAD_LEFT) . ". " . "<cyan>" . str_pad($package->name, 15) . "</cyan> " . "[v<magenta>" . $package->version . "</magenta> ➜ v<green>" . $package->available . "</green>]"); $slugs[] = $slug; } } if (!$skip_prompt) { // prompt to continue $this->output->writeln(""); $questionHelper = $this->getHelper('question'); $question = new ConfirmationQuestion("Continue with the update process? [Y|n] ", true); $answer = $questionHelper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln("Update aborted. Exiting..."); exit; } } // finally update $install_command = $this->getApplication()->find('install'); $args = new ArrayInput(['command' => 'install', 'package' => $slugs, '-f' => $this->input->getOption('force'), '-d' => $this->destination, '-y' => true]); $command_exec = $install_command->run($args, $this->output); if ($command_exec != 0) { $this->output->writeln("<red>Error:</red> An error occurred while trying to install the extensions"); exit; } }
public static function selfupgrade() { $upgrader = new Upgrader(); if (!Installer::isGravInstance(GRAV_ROOT)) { return false; } if (is_link(GRAV_ROOT . DS . 'index.php')) { Installer::setError(Installer::IS_LINK); return false; } if (method_exists($upgrader, 'meetsRequirements') && !$upgrader->meetsRequirements()) { $error = []; $error[] = '<p>Grav has increased the minimum PHP requirement.<br />'; $error[] = 'You are currently running PHP <strong>' . PHP_VERSION . '</strong>'; $error[] = ', but PHP <strong>' . GRAV_PHP_MIN . '</strong> is required.</p>'; $error[] = '<p><a href="http://getgrav.org/blog/changing-php-requirements-to-5.5" class="button button-small secondary">Additional information</a></p>'; Installer::setError(implode("\n", $error)); return false; } $update = $upgrader->getAssets()['grav-update']; $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid(); $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { return false; } return true; }
/** * @return bool */ private function upgrade() { Installer::install($this->file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing upgrade... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing upgrade... <green>ok</green> "); return true; }
/** * @param $package * * @return bool */ private function installPackage($package) { Installer::install($this->file, $this->destination, ['install_path' => $package->install_path]); $errorCode = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <green>ok</green> "); return true; }
public static function uninstall($packages, $options) { $options = array_merge(self::$options, $options); $packages = is_array($packages) ? $packages : [$packages]; $count = count($packages); $packages = array_filter(array_map(function ($p) { if (is_string($p)) { $p = strtolower($p); $plugin = static::GPM()->getInstalledPlugin($p); $p = $plugin ?: static::GPM()->getInstalledTheme($p); } return $p instanceof Package ? $p : false; }, $packages)); if (!$options['skip_invalid'] && $count !== count($packages)) { return false; } foreach ($packages as $package) { $location = self::getGrav()['locator']->findResource($package->package_type . '://' . $package->slug); // Check destination Installer::isValidDestination($location); if (Installer::lastErrorCode() === Installer::IS_LINK && !$options['ignore_symlinks']) { return false; } Installer::uninstall($location); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { return false; } } return true; }
/** * Handles updating Grav * * @return bool True if the action was performed */ public function taskUpdategrav() { require_once __DIR__ . '/gpm.php'; if (!$this->authorizeTask('install grav', ['admin.super'])) { return; } $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); if ($result) { $this->admin->json_response = ['status' => 'success', 'message' => $this->admin->translate('PLUGIN_ADMIN.GRAV_WAS_SUCCESSFULLY_UPDATED_TO') . ' ']; } else { $this->admin->json_response = ['status' => 'error', 'message' => $this->admin->translate('PLUGIN_ADMIN.GRAV_UPDATE_FAILED') . ' <br>' . Installer::lastErrorMsg()]; } return true; }
/** * Handles updating Grav * * @return bool True if the action was performed */ public function taskUpdategrav() { if (!$this->authorizeTask('install grav', ['admin.super'])) { return false; } $gpm = Gpm::GPM(); $version = $gpm->grav->getVersion(); $result = Gpm::selfupgrade(); if ($result) { $this->admin->json_response = ['status' => 'success', 'type' => 'updategrav', 'version' => $version, 'message' => $this->admin->translate('PLUGIN_ADMIN.GRAV_WAS_SUCCESSFULLY_UPDATED_TO') . ' ' . $version]; } else { $this->admin->json_response = ['status' => 'error', 'type' => 'updategrav', 'version' => GRAV_VERSION, 'message' => $this->admin->translate('PLUGIN_ADMIN.GRAV_UPDATE_FAILED') . ' <br>' . Installer::lastErrorMsg()]; } return true; }
public static function selfupgrade() { $upgrader = new Upgrader(); if (!Installer::isGravInstance(GRAV_ROOT)) { return false; } if (is_link(GRAV_ROOT . DS . 'index.php')) { Installer::setError(Installer::IS_LINK); return false; } $update = $upgrader->getAssets()['grav-update']; $tmp = CACHE_DIR . 'tmp/Grav-' . uniqid(); $file = self::_downloadSelfupgrade($update, $tmp); Installer::install($file, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true]); $errorCode = Installer::lastErrorCode(); Folder::delete($tmp); if ($errorCode & (Installer::ZIP_OPEN_ERROR | Installer::ZIP_EXTRACT_ERROR)) { return false; } return true; }
/** * @param $slug * @param $package * * @return bool */ private function checkDestination($slug, $package) { $path = self::getGrav()['locator']->findResource($package->package_type . '://' . $slug); $questionHelper = $this->getHelper('question'); $skipPrompt = $this->input->getOption('all-yes'); Installer::isValidDestination($path); if (Installer::lastErrorCode() == Installer::IS_LINK) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); if ($skipPrompt) { $this->output->writeln(" | '- <yellow>Skipped automatically.</yellow>"); return false; } $question = new ConfirmationQuestion(" | '- Destination has been detected as symlink, delete symbolic link first? [y|N] ", false); $answer = $questionHelper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln(" | '- <red>You decided to not delete the symlink automatically.</red>"); return false; } } $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); return true; }
/** * Install a package * * @param Package $package * @param bool $is_update True if it's an update. False if it's an install * * @return bool */ private function installPackage($package, $is_update = false) { $type = $package->package_type; Installer::install($this->file, $this->destination, ['install_path' => $package->install_path, 'theme' => $type == 'themes', 'is_update' => $is_update]); $error_code = Installer::lastErrorCode(); Folder::delete($this->tmp); if ($error_code) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <red>error</red> "); $this->output->writeln(" | '- " . Installer::lastErrorMsg()); return false; } $message = Installer::getMessage(); if ($message) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- " . $message); } $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Installing package... <green>ok</green> "); return true; }
/** * @return int|null|void */ protected function serve() { $package_file = $this->input->getArgument('package-file'); $helper = $this->getHelper('question'); $question = new ConfirmationQuestion('Are you sure you want to direct-install <cyan>' . $package_file . '</cyan> [y|N] ', false); $answer = $helper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln("exiting..."); $this->output->writeln(''); exit; } $tmp_dir = Grav::instance()['locator']->findResource('tmp://', true, true); $tmp_zip = $tmp_dir . '/Grav-' . uniqid(); $this->output->writeln(""); $this->output->writeln("Preparing to install <cyan>" . $package_file . "</cyan>"); if ($this->isRemote($package_file)) { $zip = $this->downloadPackage($package_file, $tmp_zip); } else { $zip = $this->copyPackage($package_file, $tmp_zip); } if (file_exists($zip)) { $tmp_source = $tmp_dir . '/Grav-' . uniqid(); $this->output->write(" |- Extracting package... "); $extracted = Installer::unZip($zip, $tmp_source); if (!$extracted) { $this->output->write("\r"); $this->output->writeln(" |- Extracting package... <red>failed</red>"); exit; } $this->output->write("\r"); $this->output->writeln(" |- Extracting package... <green>ok</green>"); $type = $this->getPackageType($extracted); if (!$type) { $this->output->writeln(" '- <red>ERROR: Not a valid Grav package</red>"); $this->output->writeln(''); exit; } $blueprint = $this->getBlueprints($extracted); if ($blueprint) { if (isset($blueprint['dependencies'])) { $depencencies = []; foreach ($blueprint['dependencies'] as $dependency) { if (is_array($dependency) && isset($dependency['name'])) { $depencencies[] = $dependency['name']; } else { $depencencies[] = $dependency; } } $this->output->writeln(" |- Dependencies found... <cyan>[" . implode(',', $depencencies) . "]</cyan>"); $question = new ConfirmationQuestion(" | '- Dependencies will not be satisfied. Continue ? [y|N] ", false); $answer = $helper->ask($this->input, $this->output, $question); if (!$answer) { $this->output->writeln("exiting..."); $this->output->writeln(''); exit; } } } if ($type == 'grav') { $this->output->write(" |- Checking destination... "); Installer::isValidDestination(GRAV_ROOT . '/system'); if (Installer::IS_LINK === Installer::lastErrorCode()) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); $this->output->writeln(" '- <red>ERROR: symlinks found...</red> <yellow>" . GRAV_ROOT . "</yellow>"); $this->output->writeln(''); exit; } $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); $this->output->write(" |- Installing package... "); Installer::install($zip, GRAV_ROOT, ['sophisticated' => true, 'overwrite' => true, 'ignore_symlinks' => true], $extracted); } else { $name = $this->getPackageName($extracted); if (!$name) { $this->output->writeln("<red>ERROR: Name could not be determined.</red> Please specify with --name|-n"); $this->output->writeln(''); exit; } $install_path = $this->getInstallPath($type, $name); $is_update = file_exists($install_path); $this->output->write(" |- Checking destination... "); Installer::isValidDestination(GRAV_ROOT . DS . $install_path); if (Installer::lastErrorCode() == Installer::IS_LINK) { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <yellow>symbolic link</yellow>"); $this->output->writeln(" '- <red>ERROR: symlink found...</red> <yellow>" . GRAV_ROOT . DS . $install_path . '</yellow>'); $this->output->writeln(''); exit; } else { $this->output->write("\r"); $this->output->writeln(" |- Checking destination... <green>ok</green>"); } $this->output->write(" |- Installing package... "); Installer::install($zip, GRAV_ROOT, ['install_path' => $install_path, 'theme' => $type == 'theme', 'is_update' => $is_update], $extracted); } Folder::delete($tmp_source); $this->output->write("\r"); if (Installer::lastErrorCode()) { $this->output->writeln(" '- <red>Installation failed or aborted.</red>"); $this->output->writeln(''); } else { $this->output->writeln(" |- Installing package... <green>ok</green>"); $this->output->writeln(" '- <green>Success!</green> "); $this->output->writeln(''); } } else { $this->output->writeln(" '- <red>ERROR: ZIP package could not be found</red>"); } Folder::delete($tmp_zip); // clear cache after successful upgrade $this->clearCache(); return true; }
/** * Check if package exists * * @param $slug * @param $package * @return int */ private function packageExists($slug, $package) { $path = Grav::instance()['locator']->findResource($package->package_type . '://' . $slug); Installer::isValidDestination($path); return Installer::lastErrorCode(); }
/** * Handles updating Grav * * @return bool True is the action was performed */ public function taskUpdategrav() { require_once __DIR__ . '/gpm.php'; if (!$this->authoriseTask('install grav', ['admin.super'])) { return; } $result = \Grav\Plugin\Admin\Gpm::selfupgrade(); if ($result) { $this->admin->json_response = ['status' => 'success', 'message' => 'Grav was successfully updated to ']; } else { $this->admin->json_response = ['status' => 'error', 'message' => 'Grav update failed <br>' . Installer::lastErrorMsg()]; } return true; }