/** * @param $onlyPackages * * @return array */ private function userInputPackages($onlyPackages) { $found = ['total' => 0]; $ignore = []; if (!count($onlyPackages)) { $this->output->writeln(''); } else { foreach ($onlyPackages as $onlyPackage) { $find = $this->gpm->findPackage($onlyPackage); if (!$find || !$this->gpm->isUpdatable($find->slug)) { $name = isset($find->slug) ? $find->slug : $onlyPackage; $ignore[$name] = $name; } else { $found[$find->slug] = $find; $found['total']++; } } if ($found['total']) { $list = $found; unset($list['total']); $list = array_keys($list); if ($found['total'] !== $this->data['total']) { $this->output->write(", only <magenta>" . $found['total'] . "</magenta> will be updated"); } $this->output->writeln(''); $this->output->writeln("Limiting updates for only <cyan>" . implode('</cyan>, <cyan>', $list) . "</cyan>"); } if (count($ignore)) { $this->output->writeln("Packages not found or not requiring updates: <red>" . implode('</red>, <red>', $ignore) . "</red>"); } } return $found; }
/** * Get an array of dependencies needed to be installed or updated for a list of packages * to be installed. * * @param array $packages The packages slugs * * @return array|bool */ public function getDependenciesNeededToInstall($packages) { $gpm = $this->gpm(); if (!$gpm) { return false; } $dependencies = $this->gpm->getDependencies($packages); return $dependencies; }
/** * Given a $dependencies list, filters their type according to $type and * shows $message prior to listing them to the user. Then asks the user a confirmation prior * to installing them. * * @param array $dependencies The dependencies array * @param string $type The type of dependency to show: install, update, ignore * @param string $message A message to be shown prior to listing the dependencies * @param bool $required A flag that determines if the installation is required or optional * * @throws \Exception */ public function installDependencies($dependencies, $type, $message, $required = true) { $packages = array_filter($dependencies, function ($action) use($type) { return $action === $type; }); if (count($packages) > 0) { $this->output->writeln($message); foreach ($packages as $dependencyName => $dependencyVersion) { $this->output->writeln(" |- Package <cyan>" . $dependencyName . "</cyan>"); } $this->output->writeln(""); $helper = $this->getHelper('question'); if ($type == 'install') { $questionAction = 'Install'; } else { $questionAction = 'Update'; } if (count($packages) == 1) { $questionArticle = 'this'; } else { $questionArticle = 'these'; } if (count($packages) == 1) { $questionNoun = 'package'; } else { $questionNoun = 'packages'; } $question = new ConfirmationQuestion("{$questionAction} {$questionArticle} {$questionNoun}? [Y|n] ", true); $answer = $this->all_yes ? true : $helper->ask($this->input, $this->output, $question); if ($answer) { foreach ($packages as $dependencyName => $dependencyVersion) { $package = $this->gpm->findPackage($dependencyName); $this->processPackage($package, true, $type == 'update' ? true : false); } $this->output->writeln(''); } else { if ($required) { throw new \Exception(); } } } }
/** * Handles getting GPM updates */ public function onTaskGPM() { $task = 'GPM'; if (!$this->admin->authorize(['admin.maintenance', 'admin.super'])) { $this->admin->json_response = ['status' => 'unauthorized', 'message' => $this->admin->translate('PLUGIN_ADMIN.INSUFFICIENT_PERMISSIONS_FOR_TASK') . ' ' . $task . '.']; return false; } $action = $_POST['action']; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates $flush = isset($_POST['flush']) && $_POST['flush'] == true ? true : false; if (isset($this->grav['session'])) { $this->grav['session']->close(); } try { $gpm = new GPM($flush); switch ($action) { case 'getUpdates': $resources_updates = $gpm->getUpdatable(); if ($gpm->grav != null) { $grav_updates = ["isUpdatable" => $gpm->grav->isUpdatable(), "assets" => $gpm->grav->getAssets(), "version" => GRAV_VERSION, "available" => $gpm->grav->getVersion(), "date" => $gpm->grav->getDate(), "isSymlink" => $gpm->grav->isSymlink()]; echo json_encode(["status" => "success", "payload" => ["resources" => $resources_updates, "grav" => $grav_updates, "installed" => $gpm->countInstalled(), 'flushed' => $flush]]); } else { echo json_encode(["status" => "error", "message" => "Cannot connect to the GPM"]); } break; } } catch (\Exception $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } exit; }
/** * Handles getting GPM updates */ public function onTaskGPM() { $action = $_POST['action']; // getUpdatable | getUpdatablePlugins | getUpdatableThemes | gravUpdates $flush = isset($_POST['flush']) && $_POST['flush'] == true ? true : false; if (isset($this->grav['session'])) { $this->grav['session']->close(); } try { $gpm = new GPM($flush); switch ($action) { case 'getUpdates': $resources_updates = $gpm->getUpdatable(); $grav_updates = ["isUpdatable" => $gpm->grav->isUpdatable(), "assets" => $gpm->grav->getAssets(), "version" => GRAV_VERSION, "available" => $gpm->grav->getVersion(), "date" => $gpm->grav->getDate(), "isSymlink" => $gpm->grav->isSymlink()]; echo json_encode(["status" => "success", "payload" => ["resources" => $resources_updates, "grav" => $grav_updates, "installed" => $gpm->countInstalled(), 'flushed' => $flush]]); break; } } catch (\Exception $e) { echo json_encode(["status" => "error", "message" => $e->getMessage()]); } exit; }
/** * @param $slug * @param $package * * @return bool */ private function uninstallPackage($slug, $package, $is_dependency = false) { if (!$slug) { return false; } //check if there are packages that have this as a dependency. Abort and show list $dependent_packages = $this->gpm->getPackagesThatDependOnPackage($slug); if (count($dependent_packages) > ($is_dependency ? 1 : 0)) { $this->output->writeln(''); $this->output->writeln(''); $this->output->writeln("<red>Uninstallation failed.</red>"); $this->output->writeln(''); if (count($dependent_packages) > ($is_dependency ? 2 : 1)) { $this->output->writeln("The installed packages <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first."); } else { $this->output->writeln("The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove it first."); } $this->output->writeln(''); return false; } $locator = Grav::instance()['locator']; $path = $locator->findResource($package->package_type . '://' . $slug); Installer::uninstall($path); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { $this->output->write("\r"); // extra white spaces to clear out the buffer properly $this->output->writeln(" |- Uninstalling 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(" |- Uninstalling package... <green>ok</green> "); if (isset($package->dependencies)) { $questionHelper = $this->getHelper('question'); foreach ($package->dependencies as $dependency) { if (is_array($dependency)) { $dependency = $dependency['name']; } if ($dependency === 'grav') { continue; } $dependencyPackage = $this->gpm->findPackage($dependency); $question = new ConfirmationQuestion(" | '- Delete dependency <cyan>" . $dependency . "</cyan> too? [y|N] ", false); $answer = $questionHelper->ask($this->input, $this->output, $question); if ($answer) { $this->output->writeln(" | '- <red>You decided to delete " . $dependency . ".</red>"); $uninstall = $this->uninstallPackage($dependency, $dependencyPackage, true); if (!$uninstall) { $this->output->writeln(" '- <red>Uninstallation failed or aborted.</red>"); $this->output->writeln(''); } else { $this->output->writeln(" '- <green>Success!</green> "); $this->output->writeln(''); } } } } return true; }
/** * @param $slug * @param $package * * @return bool */ private function uninstallPackage($slug, $package, $is_dependency = false) { if (!$slug) { return false; } //check if there are packages that have this as a dependency. Abort and show list $dependent_packages = $this->gpm->getPackagesThatDependOnPackage($slug); if (count($dependent_packages) > ($is_dependency ? 1 : 0)) { $this->output->writeln(''); $this->output->writeln(''); $this->output->writeln("<red>Uninstallation failed.</red>"); $this->output->writeln(''); if (count($dependent_packages) > ($is_dependency ? 2 : 1)) { $this->output->writeln("The installed packages <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove those first."); } else { $this->output->writeln("The installed package <cyan>" . implode('</cyan>, <cyan>', $dependent_packages) . "</cyan> depends on this package. Please remove it first."); } $this->output->writeln(''); return false; } if (isset($package->dependencies)) { $dependencies = $package->dependencies; if ($is_dependency) { foreach ($dependencies as $key => $dependency) { if (in_array($dependency['name'], $this->dependencies)) { unset($dependencies[$key]); } } } else { if (count($dependencies) > 0) { $this->output->writeln(' `- Dependencies found...'); $this->output->writeln(''); } } $questionHelper = $this->getHelper('question'); foreach ($dependencies as $dependency) { $this->dependencies[] = $dependency['name']; if (is_array($dependency)) { $dependency = $dependency['name']; } if ($dependency === 'grav') { continue; } $dependencyPackage = $this->gpm->findPackage($dependency); $dependency_exists = $this->packageExists($dependency, $dependencyPackage); if ($dependency_exists == Installer::EXISTS) { $this->output->writeln("A dependency on <cyan>" . $dependencyPackage->name . "</cyan> [v" . $dependencyPackage->version . "] was found"); $question = new ConfirmationQuestion(" |- Uninstall <cyan>" . $dependencyPackage->name . "</cyan>? [y|N] ", false); $answer = $this->all_yes ? true : $questionHelper->ask($this->input, $this->output, $question); if ($answer) { $uninstall = $this->uninstallPackage($dependency, $dependencyPackage, true); if (!$uninstall) { $this->output->writeln(" '- <red>Uninstallation failed or aborted.</red>"); } else { $this->output->writeln(" '- <green>Success!</green> "); } $this->output->writeln(''); } else { $this->output->writeln(" '- <yellow>You decided not to uninstall " . $dependencyPackage->name . ".</yellow>"); $this->output->writeln(''); } } } } $locator = Grav::instance()['locator']; $path = $locator->findResource($package->package_type . '://' . $slug); Installer::uninstall($path); $errorCode = Installer::lastErrorCode(); if ($errorCode && $errorCode !== Installer::IS_LINK && $errorCode !== Installer::EXISTS) { $this->output->writeln(" |- Uninstalling " . $package->name . " package... <red>error</red> "); $this->output->writeln(" | '- <yellow>" . Installer::lastErrorMsg() . "</yellow>"); return false; } $message = Installer::getMessage(); if ($message) { $this->output->writeln(" |- " . $message); } if (!$is_dependency && $this->dependencies) { $this->output->writeln("Finishing up uninstalling <cyan>" . $package->name . "</cyan>"); } $this->output->writeln(" |- Uninstalling " . $package->name . " package... <green>ok</green> "); return true; }