diff options
author | diosmosis <benaka@piwik.pro> | 2015-02-26 07:41:58 +0300 |
---|---|---|
committer | diosmosis <benaka@piwik.pro> | 2015-02-26 07:41:58 +0300 |
commit | ed3917deb266e059f3b25937feba0b442298a231 (patch) | |
tree | 3c30ac230a898299e15fc0bf71bf04745cd2cd71 /plugins/CoreUpdater/Commands | |
parent | 5a830fbc1ba686feaa6e6437ab3fc4666a11fed5 (diff) |
Refs #7276, recreate CLI update logic originally in web controller in core:update command.
Diffstat (limited to 'plugins/CoreUpdater/Commands')
-rw-r--r-- | plugins/CoreUpdater/Commands/Update.php | 221 |
1 files changed, 217 insertions, 4 deletions
diff --git a/plugins/CoreUpdater/Commands/Update.php b/plugins/CoreUpdater/Commands/Update.php index 02f774ec44..6fdc9811e0 100644 --- a/plugins/CoreUpdater/Commands/Update.php +++ b/plugins/CoreUpdater/Commands/Update.php @@ -8,11 +8,15 @@ */ namespace Piwik\Plugins\CoreUpdater\Commands; +use Guzzle\Common\Version; +use Piwik\Config; +use Piwik\DbHelper; use Piwik\Filesystem; +use Piwik\Piwik; use Piwik\Plugin\ConsoleCommand; -use Piwik\Plugins\CoreUpdater\Controller; use Piwik\Plugins\CoreUpdater\NoUpdatesFoundException; use Piwik\Plugins\UserCountry\LocationProvider; +use Piwik\Updater; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; @@ -85,9 +89,218 @@ class Update extends ConsoleCommand { $this->checkAllRequiredOptionsAreNotEmpty($input); - $updateController = new Controller(); - $content = $updateController->runUpdaterAndExit($doDryRun); + $updater = new Updater(); + $componentsWithUpdateFile = $updater->getComponentUpdates(); + if (empty($componentsWithUpdateFile)) { + throw new NoUpdatesFoundException("Everything is already up to date."); + } + + $output->writeln(array( + "", + " *** " . Piwik::translate('CoreUpdater_UpdateTitle') . " ***" + )); + + // handle case of existing database with no tables + if (!DbHelper::isInstalled()) { + $this->handleCoreError($output, Piwik::translate('CoreUpdater_EmptyDatabaseError', Config::getInstance()->database['dbname'])); + return; + } + + $output->writeln(array( + "", + " " . Piwik::translate('CoreUpdater_DatabaseUpgradeRequired'), + "", + " " . Piwik::translate('CoreUpdater_YourDatabaseIsOutOfDate') + )); + + if ($this->isUpdatingCore($componentsWithUpdateFile)) { + $currentVersion = $this->getCurrentVersionForCore($updater); + $output->writeln(array( + "", + " " . Piwik::translate('CoreUpdater_PiwikWillBeUpgradedFromVersionXToVersionY', array($currentVersion, Version::VERSION)) + )); + } + + $pluginsToUpdate = $this->getPluginsToUpdate($componentsWithUpdateFile); + if (!empty($pluginsToUpdate)) { + $output->writeln(array( + "", + " " . Piwik::translate('CoreUpdater_TheFollowingPluginsWillBeUpgradedX', implode(', ', $pluginsToUpdate)) + )); + } + + $dimensionsToUpdate = $this->getDimensionsToUpdate($componentsWithUpdateFile); + if (!empty($dimensionsToUpdate)) { + $output->writeln(array( + "", + " " . Piwik::translate('CoreUpdater_TheFollowingDimensionsWillBeUpgradedX', implode(', ', $dimensionsToUpdate)) + )); + } + + $output->writeln(""); + + if ($doDryRun) { + $this->doDryRun($updater, $output); + } else { + $this->doRealUpdate($updater, $componentsWithUpdateFile, $output); + } + } + + private function doDryRun(Updater $updater, OutputInterface $output) + { + $migrationQueries = $updater->getSqlQueriesToExecute(); + + $output->writeln(array(" *** Note: this is a Dry Run ***", "")); + + foreach ($migrationQueries as $query) { + $output->writeln(" " . $query); + } + + $output->writeln(array("", " *** End of Dry Run ***", "")); + } + + private function doRealUpdate(Updater $updater, $componentsWithUpdateFile, OutputInterface $output) + { + $output->writeln(" " . Piwik::translate('CoreUpdater_TheUpgradeProcessMayTakeAWhilePleaseBePatient')); + + $updaterResult = $updater->updateComponents($componentsWithUpdateFile); + + if (@$updaterResult['coreError']) { + $this->handleCoreError($output, $updaterResult['errors'], $includeDiyHelp = true); + return; + } + + if (!empty($updaterResult['warnings'])) { + $this->outputUpdaterWarnings($output, $updaterResult['warnings']); + } + + if (!empty($updaterResult['errors'])) { + $this->outputUpdaterErrors($output, $updaterResult['errors'], $updaterResult['deactivatedPlugins']); + } + + if (!empty($updaterResult['warnings']) + || !empty($updaterResult['errors']) + ) { + $output->writeln(array( + " " . Piwik::translate('CoreUpdater_HelpMessageIntroductionWhenWarning'), + "", + " * " . $this->getUpdateHelpMessage() + )); + } + } + + private function handleCoreError(OutputInterface $output, $errors, $includeDiyHelp = false) + { + if (!is_array($errors)) { + $errors = array($errors); + } + + $output->writeln(array( + " [X] " . Piwik::translate('CoreUpdater_CriticalErrorDuringTheUpgradeProcess'), + "", + )); - $output->writeln($content); + foreach ($errors as $errorMessage) { + $output->writeln(" * $errorMessage"); + } + + $output->writeln(array( + "", + " " . Piwik::translate('CoreUpdater_HelpMessageIntroductionWhenError'), + "", + " * " . $this->getUpdateHelpMessage() + )); + + if ($includeDiyHelp) { + $output->writeln(array( + " " . Piwik::translate('CoreUpdater_ErrorDIYHelp'), + "* " . Piwik::translate('CoreUpdater_ErrorDIYHelp_1'), + "* " . Piwik::translate('CoreUpdater_ErrorDIYHelp_2'), + "* " . Piwik::translate('CoreUpdater_ErrorDIYHelp_3'), + "* " . Piwik::translate('CoreUpdater_ErrorDIYHelp_4'), + "* " . Piwik::translate('CoreUpdater_ErrorDIYHelp_5') + )); + } + } + + private function outputUpdaterWarnings(OutputInterface $output, $warnings) + { + $output->writeln(array( + "", + " [!] " . Piwik::translate('CoreUpdater_WarningMessages'), + "" + )); + + foreach ($warnings as $message) { + $output->writeln(" * $message"); + } + } + + private function outputUpdaterErrors(OutputInterface $output, $errors, $deactivatedPlugins) + { + $output->writeln(array( + "", + " [X] " . Piwik::translate('CoreUpdater_ErrorDuringPluginsUpdates'), + "" + )); + + foreach ($errors as $message) { + $output->writeln(" * $message"); + } + + if (!empty($deactivatedPlugins)) { + $output->writeln(array( + "", + " [!] " . Piwik::translate('CoreUpdater_WeAutomaticallyDeactivatedTheFollowingPlugins', implode(', ', $deactivatedPlugins)) + )); + } + } + + private function getUpdateHelpMessage() + { + return Piwik::translate('CoreUpdater_HelpMessageContent', array('[',']','\n\n *')); + } + + private function isUpdatingCore($componentsWithUpdateFile) + { + foreach ($componentsWithUpdateFile as $componentName => $updates) { + if ($componentName == 'core') { + return true; + } + } + return false; + } + + private function getCurrentVersionForCore(Updater $updater) + { + $currentVersion = $updater->getCurrentComponentVersion('core'); + if ($currentVersion === false) { + $currentVersion = "<= 0.2.9"; + } + return $currentVersion; + } + + private function getPluginsToUpdate($componentsWithUpdateFile) + { + $plugins = array(); + foreach ($componentsWithUpdateFile as $componentName => $updates) { + if ($componentName !== 'core' + && 0 !== strpos($componentName, 'log_') + ) { + $plugins[] = $componentName; + } + } + return $plugins; + } + + private function getDimensionsToUpdate($componentsWithUpdateFile) + { + $dimensions = array(); + foreach ($componentsWithUpdateFile as $componentName => $updates) { + if (0 === strpos($componentName, 'log_')) { + $dimensions[] = $componentName; + } + } + return $dimensions; } }
\ No newline at end of file |