Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| common_ext_UpdateExtensions | |
0.00% |
0 / 76 |
|
0.00% |
0 / 4 |
306 | |
0.00% |
0 / 1 |
| __invoke | |
0.00% |
0 / 29 |
|
0.00% |
0 / 1 |
42 | |||
| updateExtension | |
0.00% |
0 / 38 |
|
0.00% |
0 / 1 |
72 | |||
| getMissingExtensions | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| getExtensionManager | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2015 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
| 19 | * |
| 20 | */ |
| 21 | |
| 22 | declare(strict_types=1); |
| 23 | |
| 24 | use oat\oatbox\action\Action; |
| 25 | use oat\oatbox\log\LoggerAwareTrait; |
| 26 | use common_report_Report as Report; |
| 27 | use Zend\ServiceManager\ServiceLocatorAwareInterface; |
| 28 | use Zend\ServiceManager\ServiceLocatorAwareTrait; |
| 29 | use Psr\Log\LoggerAwareInterface; |
| 30 | use oat\oatbox\cache\SimpleCache; |
| 31 | |
| 32 | /** |
| 33 | * Run the extension updater |
| 34 | * |
| 35 | * @access public |
| 36 | * @package generis |
| 37 | * @see @license GNU General Public (GPL) Version 2 http://www.opensource.org/licenses/gpl-2.0.php |
| 38 | */ |
| 39 | class common_ext_UpdateExtensions implements Action, ServiceLocatorAwareInterface, LoggerAwareInterface |
| 40 | { |
| 41 | use ServiceLocatorAwareTrait; |
| 42 | use LoggerAwareTrait; |
| 43 | |
| 44 | /** |
| 45 | * (non-PHPdoc) |
| 46 | * @see \oat\oatbox\action\Action::__invoke() |
| 47 | */ |
| 48 | public function __invoke($params) |
| 49 | { |
| 50 | $extManager = $this->getExtensionManager(); |
| 51 | $merged = array_merge( |
| 52 | $extManager->getInstalledExtensions(), |
| 53 | $this->getMissingExtensions() |
| 54 | ); |
| 55 | |
| 56 | $sorted = \helpers_ExtensionHelper::sortByDependencies($merged); |
| 57 | $report = new Report(Report::TYPE_INFO, 'Running extension update'); |
| 58 | |
| 59 | foreach ($sorted as $ext) { |
| 60 | try { |
| 61 | if (!$extManager->isInstalled($ext->getId())) { |
| 62 | $installer = new \tao_install_ExtensionInstaller($ext); |
| 63 | $installer->install(); |
| 64 | $report->add(new Report(Report::TYPE_SUCCESS, 'Installed ' . $ext->getName())); |
| 65 | } else { |
| 66 | $report->add($this->updateExtension($ext)); |
| 67 | } |
| 68 | } catch (common_ext_MissingExtensionException $ex) { |
| 69 | $report->add(new Report(Report::TYPE_ERROR, $ex->getMessage())); |
| 70 | break; |
| 71 | } catch (common_ext_OutdatedVersionException $ex) { |
| 72 | $report->add(new Report(Report::TYPE_ERROR, $ex->getMessage())); |
| 73 | break; |
| 74 | } catch (Exception $e) { |
| 75 | $this->logError( |
| 76 | 'Exception during update of ' . $ext->getId() . ': ' . get_class($e) . ' "' . $e->getMessage() . '"' |
| 77 | ); |
| 78 | $report->setType(Report::TYPE_ERROR); |
| 79 | $report->setMessage('Update failed'); |
| 80 | $report->add(new Report(Report::TYPE_ERROR, 'Exception during update of ' . $ext->getId() . '.')); |
| 81 | break; |
| 82 | } |
| 83 | } |
| 84 | $this->logInfo(helpers_Report::renderToCommandline($report, false)); |
| 85 | return $report; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Update a specific extension |
| 90 | * |
| 91 | * @param common_ext_Extension $ext |
| 92 | * @return Report |
| 93 | * @throws common_exception_Error |
| 94 | * @throws common_ext_ManifestNotFoundException |
| 95 | * @throws common_ext_MissingExtensionException |
| 96 | * @throws common_ext_OutdatedVersionException |
| 97 | */ |
| 98 | protected function updateExtension(common_ext_Extension $ext) |
| 99 | { |
| 100 | helpers_ExtensionHelper::checkRequiredExtensions($ext); |
| 101 | $installed = $this->getExtensionManager()->getInstalledVersion($ext->getId()); |
| 102 | $codeVersion = $ext->getVersion(); |
| 103 | if ($installed !== $codeVersion) { |
| 104 | $report = new Report( |
| 105 | Report::TYPE_INFO, |
| 106 | $ext->getName() . ' requires update from ' . $installed . ' to ' . $codeVersion |
| 107 | ); |
| 108 | try { |
| 109 | $updater = $ext->getUpdater(); |
| 110 | $returnedVersion = $updater->update($installed); |
| 111 | $currentVersion = $this->getExtensionManager()->getInstalledVersion($ext->getId()); |
| 112 | |
| 113 | if (!is_null($returnedVersion) && $returnedVersion != $currentVersion) { |
| 114 | $this->getExtensionManager()->updateVersion($ext, $returnedVersion); |
| 115 | $report->add(new Report(Report::TYPE_WARNING, 'Manually saved extension version')); |
| 116 | $currentVersion = $returnedVersion; |
| 117 | } |
| 118 | |
| 119 | if ($currentVersion === $codeVersion) { |
| 120 | $versionReport = new Report( |
| 121 | Report::TYPE_SUCCESS, |
| 122 | sprintf('Successfully updated %s to %s', $ext->getName(), $currentVersion) |
| 123 | ); |
| 124 | } else { |
| 125 | $versionReport = new Report( |
| 126 | Report::TYPE_WARNING, |
| 127 | sprintf('Update of %s exited with version %s', $ext->getName(), $currentVersion) |
| 128 | ); |
| 129 | } |
| 130 | |
| 131 | foreach ($updater->getReports() as $updaterReport) { |
| 132 | $versionReport->add($updaterReport); |
| 133 | } |
| 134 | |
| 135 | $report->add($versionReport); |
| 136 | |
| 137 | $this->getServiceLocator()->get(SimpleCache::SERVICE_ID)->clear(); |
| 138 | } catch (common_ext_UpdaterNotFoundException $e) { |
| 139 | $this->getExtensionManager()->updateVersion($ext, $codeVersion); |
| 140 | $versionReport = Report::createSuccess( |
| 141 | sprintf('Successfully updated %s to %s', $ext->getName(), $codeVersion) |
| 142 | ); |
| 143 | $report->add($versionReport); |
| 144 | } catch (common_ext_ManifestException $e) { |
| 145 | $report = new Report(Report::TYPE_WARNING, $e->getMessage()); |
| 146 | } |
| 147 | } else { |
| 148 | $report = new Report(Report::TYPE_INFO, $ext->getName() . ' already up to date'); |
| 149 | } |
| 150 | return $report; |
| 151 | } |
| 152 | |
| 153 | protected function getMissingExtensions() |
| 154 | { |
| 155 | $missingId = \helpers_ExtensionHelper::getMissingExtensionIds( |
| 156 | $this->getExtensionManager()->getInstalledExtensions() |
| 157 | ); |
| 158 | |
| 159 | $missingExt = []; |
| 160 | foreach ($missingId as $extId) { |
| 161 | $ext = $this->getExtensionManager()->getExtensionById($extId); |
| 162 | $missingExt[$extId] = $ext; |
| 163 | } |
| 164 | return $missingExt; |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * @return common_ext_ExtensionsManager |
| 169 | */ |
| 170 | private function getExtensionManager() |
| 171 | { |
| 172 | return $this->getServiceLocator()->get(common_ext_ExtensionsManager::SERVICE_ID); |
| 173 | } |
| 174 | } |