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