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