Dimension
5 years ago
API.php
5 years ago
AggregatedMetric.php
5 years ago
ArchivedMetric.php
5 years ago
Archiver.php
5 years ago
Categories.php
5 years ago
ComponentFactory.php
5 years ago
ComputedMetric.php
5 years ago
ConsoleCommand.php
5 years ago
Controller.php
5 years ago
ControllerAdmin.php
5 years ago
Dependency.php
5 years ago
LogTablesProvider.php
5 years ago
Manager.php
5 years ago
Menu.php
5 years ago
MetadataLoader.php
5 years ago
Metric.php
5 years ago
PluginException.php
5 years ago
ProcessedMetric.php
5 years ago
ReleaseChannels.php
5 years ago
Report.php
5 years ago
ReportsProvider.php
5 years ago
RequestProcessors.php
5 years ago
Segment.php
5 years ago
SettingsProvider.php
5 years ago
Tasks.php
5 years ago
ThemeStyles.php
5 years ago
ViewDataTable.php
5 years ago
Visualization.php
5 years ago
WidgetsProvider.php
5 years ago
Dependency.php
201 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Matomo - free/libre analytics platform |
| 4 | * |
| 5 | * @link https://matomo.org |
| 6 | * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later |
| 7 | * |
| 8 | */ |
| 9 | namespace Piwik\Plugin; |
| 10 | |
| 11 | use Composer\Semver\VersionParser; |
| 12 | use Piwik\Plugin\Manager as PluginManager; |
| 13 | use Piwik\Plugins\Marketplace\Environment; |
| 14 | use Piwik\Version; |
| 15 | |
| 16 | /** |
| 17 | * |
| 18 | */ |
| 19 | class Dependency |
| 20 | { |
| 21 | private $piwikVersion; |
| 22 | private $phpVersion; |
| 23 | |
| 24 | public function __construct() |
| 25 | { |
| 26 | $this->setPiwikVersion(Version::VERSION); |
| 27 | $this->setPhpVersion(PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION . '.' . PHP_RELEASE_VERSION); |
| 28 | } |
| 29 | |
| 30 | public function setEnvironment(Environment $environment) |
| 31 | { |
| 32 | $this->setPiwikVersion($environment->getPiwikVersion()); |
| 33 | $this->setPhpVersion($environment->getPhpVersion()); |
| 34 | } |
| 35 | |
| 36 | public function getMissingDependencies($requires) |
| 37 | { |
| 38 | $missingRequirements = array(); |
| 39 | |
| 40 | if (empty($requires)) { |
| 41 | return $missingRequirements; |
| 42 | } |
| 43 | |
| 44 | foreach ($requires as $name => $requiredVersion) { |
| 45 | $currentVersion = $this->getCurrentVersion($name); |
| 46 | |
| 47 | if (in_array(strtolower($name), ['piwik', 'matomo'])) { |
| 48 | $requiredVersion = $this->markPluginsWithoutUpperBoundMatomoRequirementAsIncompatible($requiredVersion); |
| 49 | } |
| 50 | |
| 51 | $missingVersions = $this->getMissingVersions($currentVersion, $requiredVersion); |
| 52 | |
| 53 | if (!empty($missingVersions)) { |
| 54 | $missingRequirements[] = array( |
| 55 | 'requirement' => $name, |
| 56 | 'actualVersion' => $currentVersion, |
| 57 | 'requiredVersion' => $requiredVersion, |
| 58 | 'causedBy' => implode(', ', $missingVersions) |
| 59 | ); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return $missingRequirements; |
| 64 | } |
| 65 | |
| 66 | public function getMissingVersions($currentVersion, $requiredVersion) |
| 67 | { |
| 68 | $currentVersion = trim($currentVersion); |
| 69 | |
| 70 | $missingVersions = array(); |
| 71 | |
| 72 | if (empty($currentVersion)) { |
| 73 | if (!empty($requiredVersion)) { |
| 74 | $missingVersions[] = (string) $requiredVersion; |
| 75 | } |
| 76 | |
| 77 | return $missingVersions; |
| 78 | } |
| 79 | |
| 80 | $requiredVersion = $this->makeVersionBackwardsCompatibleIfNoComparisonDefined($requiredVersion); |
| 81 | |
| 82 | $version = new VersionParser(); |
| 83 | $constraintsExisting = $version->parseConstraints($currentVersion); |
| 84 | |
| 85 | $requiredVersions = explode(',', (string) $requiredVersion); |
| 86 | |
| 87 | foreach ($requiredVersions as $required) { |
| 88 | $required = trim($required); |
| 89 | |
| 90 | if (empty($required)) { |
| 91 | continue; |
| 92 | } |
| 93 | |
| 94 | $required = $this->makeVersionBackwardsCompatibleIfNoComparisonDefined($required); |
| 95 | $constraintRequired = $version->parseConstraints($required); |
| 96 | |
| 97 | if (!$constraintRequired->matches($constraintsExisting)) { |
| 98 | $missingVersions[] = $required; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | return $missingVersions; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Upon Matomo 4 we require a lower and upper version bound for Matomo to be set in plugin.json |
| 107 | * If that is not the case we assume the plugin not to be compatible with Matomo 4 |
| 108 | * |
| 109 | * @param string $requiredVersion |
| 110 | * @return string |
| 111 | */ |
| 112 | private function markPluginsWithoutUpperBoundMatomoRequirementAsIncompatible($requiredVersion) |
| 113 | { |
| 114 | if (strpos($requiredVersion, ',') !== false) { |
| 115 | return $requiredVersion; |
| 116 | } |
| 117 | |
| 118 | $minVersion = str_replace(array('>', '=', '<', '!', '~', '^'), '', $requiredVersion); |
| 119 | if (preg_match("/^\<=?\d/", $requiredVersion)) { |
| 120 | $upperLimit = '>=' . $minVersion[0] . '.0.0-b1,' . $requiredVersion; |
| 121 | } else if (!empty($minVersion) && is_numeric($minVersion[0])) { |
| 122 | $upperLimit = $requiredVersion . ',<' . ($minVersion[0] + 1) . '.0.0-b1'; |
| 123 | } else { |
| 124 | $upperLimit = '>=4.0.0-b1,<5.0.0-b1'; |
| 125 | } |
| 126 | |
| 127 | return $upperLimit; |
| 128 | } |
| 129 | |
| 130 | private function makeVersionBackwardsCompatibleIfNoComparisonDefined($version) |
| 131 | { |
| 132 | if (!empty($version) && preg_match('/^(\d+)\.(\d+)/', $version)) { |
| 133 | // TODO: we should remove this from piwik 3. To stay BC we add >= if no >= is defined yet |
| 134 | $version = '>=' . $version; |
| 135 | } |
| 136 | |
| 137 | return $version; |
| 138 | } |
| 139 | |
| 140 | public function setPiwikVersion($piwikVersion) |
| 141 | { |
| 142 | $this->piwikVersion = $piwikVersion; |
| 143 | } |
| 144 | |
| 145 | public function setPhpVersion($phpVersion) |
| 146 | { |
| 147 | $this->phpVersion = $phpVersion; |
| 148 | } |
| 149 | |
| 150 | public function hasDependencyToDisabledPlugin($requires) |
| 151 | { |
| 152 | if (empty($requires)) { |
| 153 | return false; |
| 154 | } |
| 155 | |
| 156 | foreach ($requires as $name => $requiredVersion) { |
| 157 | $nameLower = strtolower($name); |
| 158 | $isPluginRequire = !in_array($nameLower, array('piwik', 'php', 'matomo')); |
| 159 | if ($isPluginRequire) { |
| 160 | // we do not check version, only whether it's activated. Everything that is not piwik or php is assumed |
| 161 | // a plugin so far. |
| 162 | if (!PluginManager::getInstance()->isPluginActivated($name)) { |
| 163 | return true; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | private function getCurrentVersion($name) |
| 172 | { |
| 173 | switch (strtolower($name)) { |
| 174 | case 'matomo': |
| 175 | case 'piwik': |
| 176 | return $this->piwikVersion; |
| 177 | case 'php': |
| 178 | return $this->phpVersion; |
| 179 | default: |
| 180 | try { |
| 181 | $pluginNames = PluginManager::getAllPluginsNames(); |
| 182 | |
| 183 | if (!in_array($name, $pluginNames) || !PluginManager::getInstance()->isPluginLoaded($name)) { |
| 184 | return ''; |
| 185 | } |
| 186 | |
| 187 | $plugin = PluginManager::getInstance()->loadPlugin(ucfirst($name)); |
| 188 | |
| 189 | if (!empty($plugin)) { |
| 190 | return $plugin->getVersion(); |
| 191 | } |
| 192 | } catch (\Exception $e) { |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | return ''; |
| 197 | } |
| 198 | |
| 199 | |
| 200 | } |
| 201 |