[TASK] Deprecate unused methods of VersionNumberUtility
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Utility / VersionNumberUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Class with helper functions for version number handling
19 */
20 class VersionNumberUtility
21 {
22 /**
23 * Returns an integer from a three part version number, eg '4.12.3' -> 4012003
24 *
25 * @param string $versionNumber Version number on format x.x.x
26 * @return int Integer version of version number (where each part can count to 999)
27 */
28 public static function convertVersionNumberToInteger($versionNumber)
29 {
30 $versionParts = explode('.', $versionNumber);
31 $version = $versionParts[0];
32 for ($i = 1; $i < 3; $i++) {
33 if (!empty($versionParts[$i])) {
34 $version .= str_pad((int)$versionParts[$i], 3, '0', STR_PAD_LEFT);
35 } else {
36 $version .= '000';
37 }
38 }
39 return (int)$version;
40 }
41
42 /**
43 * Returns the three part version number (string) from an integer, eg 4012003 -> '4.12.3'
44 *
45 * @param int $versionInteger Integer representation of version number
46 * @return string Version number as format x.x.x
47 * @throws \InvalidArgumentException if $versionInteger is not an integer
48 * @deprecated
49 */
50 public static function convertIntegerToVersionNumber($versionInteger)
51 {
52 trigger_error('Method ' . __METHOD__ . ' will be removed in TYPO3 11.0', E_USER_DEPRECATED);
53
54 if (!is_int($versionInteger)) {
55 throw new \InvalidArgumentException(\TYPO3\CMS\Core\Utility\VersionNumberUtility::class . '::convertIntegerToVersionNumber() supports an integer argument only!', 1334072223);
56 }
57 $versionString = str_pad($versionInteger, 9, '0', STR_PAD_LEFT);
58 $parts = [
59 substr($versionString, 0, 3),
60 substr($versionString, 3, 3),
61 substr($versionString, 6, 3)
62 ];
63 return (int)$parts[0] . '.' . (int)$parts[1] . '.' . (int)$parts[2];
64 }
65
66 /**
67 * Splits a version range into an array.
68 *
69 * If a single version number is given, it is considered a minimum value.
70 * If a dash is found, the numbers left and right are considered as minimum and maximum. Empty values are allowed.
71 * If no version can be parsed "0.0.0" — "0.0.0" is the result
72 *
73 * @param string $version A string with a version range.
74 * @return array
75 * @deprecated
76 */
77 public static function splitVersionRange($version)
78 {
79 trigger_error('Method ' . __METHOD__ . ' will be removed in TYPO3 11.0', E_USER_DEPRECATED);
80
81 $versionRange = [];
82 if (strstr($version, '-')) {
83 $versionRange = explode('-', $version, 2);
84 } else {
85 $versionRange[0] = $version;
86 $versionRange[1] = '';
87 }
88 if (!$versionRange[0]) {
89 $versionRange[0] = '0.0.0';
90 }
91 if (!$versionRange[1]) {
92 $versionRange[1] = '0.0.0';
93 }
94 return $versionRange;
95 }
96
97 /**
98 * Removes -dev -alpha -beta -RC states (also without '-' prefix) from a version number
99 * and replaces them by .0 and normalizes to a three part version number
100 *
101 * @return string
102 */
103 public static function getNumericTypo3Version()
104 {
105 $t3version = static::getCurrentTypo3Version();
106 $t3version = preg_replace('/-?(dev|alpha|beta|RC).*$/', '', $t3version);
107 $parts = GeneralUtility::intExplode('.', $t3version . '..');
108 $t3version = MathUtility::forceIntegerInRange($parts[0], 0, 999) . '.' .
109 MathUtility::forceIntegerInRange($parts[1], 0, 999) . '.' .
110 MathUtility::forceIntegerInRange($parts[2], 0, 999);
111 return $t3version;
112 }
113
114 /**
115 * Wrapper function for TYPO3_version constant to make functions using
116 * the constant unit testable
117 *
118 * @return string
119 */
120 public static function getCurrentTypo3Version()
121 {
122 return TYPO3_version;
123 }
124
125 /**
126 * This function converts version range strings (like '4.2.0-4.4.99') to an array
127 * (like array('4.2.0', '4.4.99'). It also forces each version part to be between
128 * 0 and 999
129 *
130 * @param string $versionsString
131 * @return array
132 */
133 public static function convertVersionsStringToVersionNumbers($versionsString)
134 {
135 $versions = GeneralUtility::trimExplode('-', $versionsString);
136 $versionsCount = count($versions);
137 for ($i = 0; $i < $versionsCount; $i++) {
138 $cleanedVersion = GeneralUtility::trimExplode('.', $versions[$i]);
139 $cleanedVersionCount = count($cleanedVersion);
140 for ($j = 0; $j < $cleanedVersionCount; $j++) {
141 $cleanedVersion[$j] = MathUtility::forceIntegerInRange($cleanedVersion[$j], 0, 999);
142 }
143 $cleanedVersionString = implode('.', $cleanedVersion);
144 if (static::convertVersionNumberToInteger($cleanedVersionString) === 0) {
145 $cleanedVersionString = '';
146 }
147 $versions[$i] = $cleanedVersionString;
148 }
149 return $versions;
150 }
151
152 /**
153 * Parses the version number x.x.x and returns an array with the various parts.
154 * It also forces each … 0 to 999
155 *
156 * @param string $version Version code, x.x.x
157 * @return array
158 */
159 public static function convertVersionStringToArray($version)
160 {
161 $parts = GeneralUtility::intExplode('.', $version . '..');
162 $parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
163 $parts[1] = MathUtility::forceIntegerInRange($parts[1], 0, 999);
164 $parts[2] = MathUtility::forceIntegerInRange($parts[2], 0, 999);
165 $result = [];
166 $result['version'] = $parts[0] . '.' . $parts[1] . '.' . $parts[2];
167 $result['version_int'] = (int)($parts[0] * 1000000 + $parts[1] * 1000 + $parts[2]);
168 $result['version_main'] = $parts[0];
169 $result['version_sub'] = $parts[1];
170 $result['version_dev'] = $parts[2];
171 return $result;
172 }
173
174 /**
175 * Method to raise a version number
176 *
177 * @param string $raise one of "main", "sub", "dev" - the version part to raise by one
178 * @param string $version (like 4.1.20)
179 * @return string
180 * @throws \TYPO3\CMS\Core\Exception
181 * @deprecated
182 */
183 public static function raiseVersionNumber($raise, $version)
184 {
185 trigger_error('Method ' . __METHOD__ . ' will be removed in TYPO3 11.0', E_USER_DEPRECATED);
186
187 if (!in_array($raise, ['main', 'sub', 'dev'])) {
188 throw new \TYPO3\CMS\Core\Exception('RaiseVersionNumber expects one of "main", "sub" or "dev".', 1342639555);
189 }
190 $parts = GeneralUtility::intExplode('.', $version . '..');
191 $parts[0] = MathUtility::forceIntegerInRange($parts[0], 0, 999);
192 $parts[1] = MathUtility::forceIntegerInRange($parts[1], 0, 999);
193 $parts[2] = MathUtility::forceIntegerInRange($parts[2], 0, 999);
194 switch ((string)$raise) {
195 case 'main':
196 $parts[0]++;
197 $parts[1] = 0;
198 $parts[2] = 0;
199 break;
200 case 'sub':
201 $parts[1]++;
202 $parts[2] = 0;
203 break;
204 case 'dev':
205 $parts[2]++;
206 break;
207 }
208 return $parts[0] . '.' . $parts[1] . '.' . $parts[2];
209 }
210 }