freenetis-github/application/libraries/Version.php @ b8a40ec0
8baed187 | Michal Kliment | <?php defined('SYSPATH') or die('No direct script access.');
|
|
/*
|
|||
* This file is part of open source system FreenetIS
|
|||
* and it is released under GPLv3 licence.
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * More info about licence can be found:
|
|
* http://www.gnu.org/licenses/gpl-3.0.html
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * More info about project can be found:
|
|
* http://www.freenetis.org/
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | */
|
|
/**
|
|||
* Class for versioning of FreenetIS. Manages source code and database versions
|
|||
* and it performs database upgrade.
|
|||
*
|
|||
* @author Ondrej Fibich
|
|||
* @see Controller
|
|||
*/
|
|||
class Version
|
|||
{
|
|||
/**
|
|||
18ac9009 | Ondřej Fibich | * Regex for valitadation of versions
|
|
8baed187 | Michal Kliment | */
|
|
18ac9009 | Ondřej Fibich | const VERSION_REGEX = '^(((\d|[1-9][0-9]*)\.(\d|[1-9][0-9]*)\.(\d|[1-9][0-9]*))(~(alpha|beta|dev|rc)([1-9][0-9]*)?)?)$';
|
|
8baed187 | Michal Kliment | /**
|
|
* Gets version of Freenetis code
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @return string Version
|
|
*/
|
|||
public static function get_version()
|
|||
{
|
|||
if (!defined('FREENETIS_VERSION'))
|
|||
{
|
|||
require 'version.php';
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | if (!self::is_valid_version(FREENETIS_VERSION))
|
|
{
|
|||
throw new ErrorException('Wrong version format in version.php');
|
|||
}
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | return FREENETIS_VERSION;
|
|
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | /**
|
|
* Gets version of Freenetis database
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @param $cache May be value returned from cache [optional]
|
|
* @return string Version
|
|||
*/
|
|||
public static function get_db_version($cache = TRUE)
|
|||
{
|
|||
return Settings::get('db_schema_version', $cache);
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | /**
|
|
* Check if the version of database is equal
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @return boolean true if versions are equal
|
|
*/
|
|||
public static function is_db_up_to_date()
|
|||
{
|
|||
// old type of DB upgrades? => always false
|
|||
if (is_numeric(self::get_db_version(FALSE)))
|
|||
{
|
|||
18ac9009 | Ondřej Fibich | return FALSE;
|
|
8baed187 | Michal Kliment | }
|
|
// new type
|
|||
return (self::fn_version_compare() == 0);
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
/**
|
|||
* Is the current database version one of given versions?
|
|||
*
|
|||
* @param array|string $versions
|
|||
* @return boolean
|
|||
*/
|
|||
public static function is_db_version_in($versions)
|
|||
{
|
|||
if (empty($versions))
|
|||
{
|
|||
return FALSE;
|
|||
}
|
|||
if (!is_array($versions))
|
|||
{
|
|||
$versions = array($versions);
|
|||
}
|
|||
// listed => equivalent
|
|||
return in_array(self::get_db_version(), $versions);
|
|||
}
|
|||
8baed187 | Michal Kliment | /**
|
|
* Compares version of FreenetIS and its database
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @return integer -1 if the FreenetIS version is lower than the database
|
|
* version, 0 if they are equal, and 1 if the second is lower
|
|||
*/
|
|||
18ac9009 | Ondřej Fibich | public static function fn_version_compare()
|
|
8baed187 | Michal Kliment | {
|
|
return self::compare(self::get_version(), self::get_db_version());
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | /**
|
|
* Checks if given version is in correct format
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @param string $version Version to check
|
|
* @return boolean true if it is, false otherwise
|
|||
*/
|
|||
18ac9009 | Ondřej Fibich | public static function is_valid_version($version)
|
|
8baed187 | Michal Kliment | {
|
|
18ac9009 | Ondřej Fibich | return (mb_ereg(self::VERSION_REGEX, $version) > 0);
|
|
8baed187 | Michal Kliment | }
|
|
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | /**
|
|
* Compares two valid versions of FreenetIS
|
|||
18ac9009 | Ondřej Fibich | *
|
|
8baed187 | Michal Kliment | * @param string $version1
|
|
* @param string $version2
|
|||
* @return integer -1 if the first version is lower than the second
|
|||
* version, 0 if they are equal, and 1 if the second is lower
|
|||
* @throws InvalidArgumentException On invalid version
|
|||
*/
|
|||
public static function compare($version1, $version2)
|
|||
{
|
|||
if (!self::is_valid_version($version1))
|
|||
{
|
|||
throw new InvalidArgumentException('Wrong version1 format: ' . $version1);
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | if (!self::is_valid_version($version2))
|
|
{
|
|||
throw new InvalidArgumentException('Wrong version2 format: ' . $version2);
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | $version1_parts = explode('~', $version1);
|
|
$version2_parts = explode('~', $version2);
|
|||
$cmp = 0;
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | if (($cmp = version_compare($version1_parts[0], $version2_parts[0])) == 0)
|
|
{
|
|||
if (!isset($version1_parts[1]) && !isset($version2_parts[1]))
|
|||
{
|
|||
return 0;
|
|||
}
|
|||
else if (!isset($version1_parts[1]))
|
|||
{
|
|||
return 1;
|
|||
}
|
|||
else if (!isset($version2_parts[1]))
|
|||
{
|
|||
return -1;
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | $order = array('dev', 'alpha', 'beta', 'rc');
|
|
$order1 = NULL;
|
|||
$order2 = NULL;
|
|||
$i = 0;
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | foreach ($order as $type)
|
|
{
|
|||
if (text::starts_with($version1_parts[1], $type))
|
|||
{
|
|||
$order1 = $i;
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | if (text::starts_with($version2_parts[1], $type))
|
|
{
|
|||
$order2 = $i;
|
|||
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | $i++;
|
|
}
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | if (($cmp = $order1 - $order2) == 0)
|
|
{
|
|||
$number1 = substr($version1_parts[1], mb_strlen($order[$order1]));
|
|||
$number2 = substr($version2_parts[1], mb_strlen($order[$order2]));
|
|||
18ac9009 | Ondřej Fibich | ||
8baed187 | Michal Kliment | return intval($number1) - intval($number2);
|
|
}
|
|||
}
|
|||
c478920d | Ondřej Fibich | ||
18ac9009 | Ondřej Fibich | return $cmp;
|
|
c478920d | Ondřej Fibich | }
|
|
}
|