aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/upgrade.php
blob: 0cc1e64dc45cb290596d697479d15a3847973d12 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
<?php
/**
 * Elgg upgrade library.
 * Contains code for handling versioning and upgrades.
 *
 * @package Elgg.Core
 * @subpackage Upgrade
 */

/**
 * Run any php upgrade scripts which are required
 *
 * @param int  $version Version upgrading from.
 * @param bool $quiet   Suppress errors.  Don't use this.
 *
 * @return bool
 * @access private
 */
function upgrade_code($version, $quiet = FALSE) {
	// do not remove - upgrade scripts depend on this
	global $CONFIG;
	
	$version = (int) $version;
	$upgrade_path = elgg_get_config('path') . 'engine/lib/upgrades/';
	$processed_upgrades = elgg_get_processed_upgrades();

	// upgrading from 1.7 to 1.8. Need to bootstrap.
	if (!$processed_upgrades) {
		elgg_upgrade_bootstrap_17_to_18();

		// grab accurate processed upgrades
		$processed_upgrades = elgg_get_processed_upgrades();
	}

	$upgrade_files = elgg_get_upgrade_files($upgrade_path);

	if ($upgrade_files === false) {
		return false;
	}

	$upgrades = elgg_get_unprocessed_upgrades($upgrade_files, $processed_upgrades);

	// Sort and execute
	sort($upgrades);

	foreach ($upgrades as $upgrade) {
		$upgrade_version = elgg_get_upgrade_file_version($upgrade);
		$success = true;

		// hide all errors.
		if ($quiet) {
			// hide include errors as well as any exceptions that might happen
			try {
				if (!@include("$upgrade_path/$upgrade")) {
					$success = false;
					error_log("Could not include $upgrade_path/$upgrade");
				}
			} catch (Exception $e) {
				$success = false;
				error_log($e->getmessage());
			}
		} else {
			if (!include("$upgrade_path/$upgrade")) {
				$success = false;
				error_log("Could not include $upgrade_path/$upgrade");
			}
		}

		if ($success) {
			// incrementally set upgrade so we know where to start if something fails.
			$processed_upgrades[] = $upgrade;

			// don't set the version to a lower number in instances where an upgrade
			// has been merged from a lower version of Elgg
			if ($upgrade_version > $version) {
				datalist_set('version', $upgrade_version);
			}

			elgg_set_processed_upgrades($processed_upgrades);
		} else {
			return false;
		}
	}

	return true;
}

/**
 * Saves the processed upgrades to a dataset.
 *
 * @param array $processed_upgrades An array of processed upgrade filenames
 *                                  (not the path, just the file)
 * @return bool
 * @access private
 */
function elgg_set_processed_upgrades(array $processed_upgrades) {
	$processed_upgrades = array_unique($processed_upgrades);
	return datalist_set('processed_upgrades', serialize($processed_upgrades));
}

/**
 * Gets a list of processes upgrades
 *
 * @return mixed Array of processed upgrade filenames or false
 * @access private
 */
function elgg_get_processed_upgrades() {
	$upgrades = datalist_get('processed_upgrades');
	$unserialized = unserialize($upgrades);
	return $unserialized;
}

/**
 * Returns the version of the upgrade filename.
 *
 * @param string $filename The upgrade filename. No full path.
 * @return int|false
 * @since 1.8.0
 * @access private
 */
function elgg_get_upgrade_file_version($filename) {
	preg_match('/^([0-9]{10})([\.a-z0-9-_]+)?\.(php)$/i', $filename, $matches);

	if (isset($matches[1])) {
		return (int) $matches[1];
	}

	return false;
}

/**
 * Returns a list of upgrade files relative to the $upgrade_path dir.
 *
 * @param string $upgrade_path The up
 * @return array|false
 * @access private
 */
function elgg_get_upgrade_files($upgrade_path = null) {
	if (!$upgrade_path) {
		$upgrade_path = elgg_get_config('path') . 'engine/lib/upgrades/';
	}
	$upgrade_path = sanitise_filepath($upgrade_path);
	$handle = opendir($upgrade_path);

	if (!$handle) {
		return false;
	}

	$upgrade_files = array();

	while ($upgrade_file = readdir($handle)) {
		// make sure this is a wellformed upgrade.
		if (is_dir($upgrade_path . '$upgrade_file')) {
			continue;
		}
		$upgrade_version = elgg_get_upgrade_file_version($upgrade_file);
		if (!$upgrade_version) {
			continue;
		}
		$upgrade_files[] = $upgrade_file;
	}

	sort($upgrade_files);

	return $upgrade_files;
}

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

	static $version, $release;

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

	return false;
}

/**
 * Checks if any upgrades need to be run.
 *
 * @param null|array $upgrade_files      Optional upgrade files
 * @param null|array $processed_upgrades Optional processed upgrades
 *
 * @return array
 * @access private
 */
function elgg_get_unprocessed_upgrades($upgrade_files = null, $processed_upgrades = null) {
	if ($upgrade_files === null) {
		$upgrade_files = elgg_get_upgrade_files();
	}

	if ($processed_upgrades === null) {
		$processed_upgrades = unserialize(datalist_get('processed_upgrades'));
		if (!is_array($processed_upgrades)) {
			$processed_upgrades = array();
		}
	}

	$unprocessed = array_diff($upgrade_files, $processed_upgrades);
	return $unprocessed;
}

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

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

	return FALSE;
}

/**
 * Upgrades Elgg Database and code
 *
 * @return bool
 * @access private
 */
function version_upgrade() {
	// It's possible large upgrades could exceed the max execution time.
	set_time_limit(0);

	$dbversion = (int) datalist_get('version');

	// No version number? Oh snap...this is an upgrade from a clean installation < 1.7.
	// Run all upgrades without error reporting and hope for the best.
	// See http://trac.elgg.org/elgg/ticket/1432 for more.
	$quiet = !$dbversion;

	// Note: Database upgrades are deprecated as of 1.8.  Use code upgrades.  See #1433
	if (db_upgrade($dbversion, '', $quiet)) {
		system_message(elgg_echo('upgrade:db'));
	}

	if (upgrade_code($dbversion, $quiet)) {
		system_message(elgg_echo('upgrade:core'));

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

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

		return true;
	}

	return false;
}

/**
 * Boot straps into 1.8 upgrade system from 1.7
 *
 * This runs all the 1.7 upgrades, then sets the processed_upgrades to all existing 1.7 upgrades.
 * Control is then passed back to the main upgrade function which detects and runs the
 * 1.8 upgrades, regardless of filename convention.
 *
 * @return bool
 * @access private
 */
function elgg_upgrade_bootstrap_17_to_18() {
	$db_version = (int) datalist_get('version');

	// the 1.8 upgrades before the upgrade system change that are interspersed with 1.7 upgrades.
	$upgrades_18 = array(
		'2010111501.php',
		'2010121601.php',
		'2010121602.php',
		'2010121701.php',
		'2010123101.php',
		'2011010101.php',
	);

	$upgrade_files = elgg_get_upgrade_files();
	$processed_upgrades = array();

	foreach ($upgrade_files as $upgrade_file) {
		// ignore if not in 1.7 format or if it's a 1.8 upgrade
		if (in_array($upgrade_file, $upgrades_18) || !preg_match("/[0-9]{10}\.php/", $upgrade_file)) {
			continue;
		}

		$upgrade_version = elgg_get_upgrade_file_version($upgrade_file);

		// this has already been run in a previous 1.7.X -> 1.7.X upgrade
		if ($upgrade_version < $db_version) {
			$processed_upgrades[] = $upgrade_file;
		}
	}

	return elgg_set_processed_upgrades($processed_upgrades);
}

/**
 * Creates a table {prefix}upgrade_lock that is used as a mutex for upgrades.
 *
 * @see _elgg_upgrade_lock()
 *
 * @return bool
 * @access private
 */
function _elgg_upgrade_lock() {
	global $CONFIG;
	
	if (!_elgg_upgrade_is_locked()) {
		// lock it
		insert_data("create table {$CONFIG->dbprefix}upgrade_lock (id INT)");
		elgg_log('Locked for upgrade.', 'NOTICE');
		return true;
	}
	
	elgg_log('Cannot lock for upgrade: already locked.', 'WARNING');
	return false;
}

/**
 * Unlocks upgrade.
 *
 * @see _elgg_upgrade_lock()
 *
 * @access private
 */
function _elgg_upgrade_unlock() {
	global $CONFIG;
	delete_data("drop table {$CONFIG->dbprefix}upgrade_lock");
	elgg_log('Upgrade unlocked.', 'NOTICE');
}

/**
 * Checks if upgrade is locked
 *
 * @return bool
 * @access private
 */
function _elgg_upgrade_is_locked() {
	global $CONFIG;

	$is_locked = count(get_data("show tables like '{$CONFIG->dbprefix}upgrade_lock'"));

	// @todo why?
	_elgg_invalidate_query_cache();

	return $is_locked;
}