aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/version.php
blob: 3728fb8ec9cb5306200b8a56fdff43b6956f89fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
 * Elgg version library.
 * Contains code for handling versioning and upgrades.
 *
 * @package Elgg
 * @subpackage Core
 * @link http://elgg.org/
 */

/**
 * Run any php upgrade scripts which are required
 *
 * @param unknown_type $version
 */
function upgrade_code($version) {
	global $CONFIG;

	// Elgg and its database must be installed to upgrade it!
	if (!is_db_installed() || !is_installed()) {
		return false;
	}

	$version = (int) $version;

	if ($handle = opendir($CONFIG->path . 'engine/lib/upgrades/')) {
		$upgrades = array();

		while ($updatefile = readdir($handle)) {
			// Look for upgrades and add to upgrades list
			if (!is_dir($CONFIG->path . 'engine/lib/upgrades/' . $updatefile)) {
				if (preg_match('/([0-9]*)\.php/',$updatefile,$matches)) {
					$core_version = (int) $matches[1];
					if ($core_version > $version) {
						$upgrades[] = $updatefile;
					}
				}
			}
		}

		// Sort and execute
		asort($upgrades);
		if (sizeof($upgrades) > 0) {
			foreach($upgrades as $upgrade) {
				try {
					include($CONFIG->path . 'engine/lib/upgrades/' . $upgrade);
				} catch (Exception $e) {
					error_log($e->getmessage());
				}
			}
		}

		return true;
	}

	return false;
}

/**
 * Get the current version information
 *
 * @param true|false $humanreadable Whether to return a human readable version (default: false)
 * @return string|false Depending on success
 */
function get_version($humanreadable = false) {
	global $CONFIG;

	if (include($CONFIG->path . "version.php")) {
		if (!$humanreadable) return $version;
		return $release;
	}

	return false;
}

/**
 * Determines whether or not the database needs to be upgraded.
 *
 * @return true|false Depending on whether or not the db version matches the code version
 */
function version_upgrade_check() {
	$dbversion = (int) datalist_get('version');
	$version = get_version();

	if ($version > $dbversion) {
		return true;
	}

	return false;
}

/**
 * Upgrades Elgg
 *
 */
function version_upgrade() {
	$dbversion = (int) datalist_get('version');

	// Upgrade database
	db_upgrade($dbversion);
	system_message(elgg_echo('upgrade:db'));

	// Upgrade core
	if (upgrade_code($dbversion)) {
		system_message(elgg_echo('upgrade:core'));
	}

	// Now we trigger an event to give the option for plugins to do something
	$upgrade_details = stdClass;
	$upgrade_details->from = $dbversion;
	$upgrade_details->to = get_version();

	trigger_elgg_event('upgrade', 'upgrade', $upgrade_details);

	// Update the version
	datalist_set('version', get_version());
}