Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/lib/upgrade.php (1 issue)

1
<?php
2
/**
3
 * Elgg upgrade library.
4
 * Contains code for handling versioning and upgrades.
5
 *
6
 * @package    Elgg.Core
7
 * @subpackage Upgrade
8
 */
9
10
/**
11
 * Returns the version of the upgrade filename.
12
 *
13
 * @param string $filename The upgrade filename. No full path.
14
 * @return int|false
15
 * @since 1.8.0
16
 * @todo used by elgg_get_upgrade_files
17
 */
18
function elgg_get_upgrade_file_version($filename) {
19 2
	preg_match('/^([0-9]{10})([\.a-z0-9-_]+)?\.(php)$/i', $filename, $matches);
20
21 2
	if (isset($matches[1])) {
22
		return (int) $matches[1];
23
	}
24
25 2
	return false;
26
}
27
28
/**
29
 * Returns a list of upgrade files relative to the $upgrade_path dir.
30
 *
31
 * @param string $upgrade_path The directory that has upgrade scripts
32
 * @return array|false
33
 * @access private
34
 *
35
 * @todo the wire and groups plugins and the installer are using this
36
 */
37
function elgg_get_upgrade_files($upgrade_path = null) {
38 2
	if (!$upgrade_path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $upgrade_path of type null|string is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
39
		$upgrade_path = elgg_get_engine_path() . '/lib/upgrades/';
40
	}
41 2
	$upgrade_path = \Elgg\Project\Paths::sanitize($upgrade_path);
42 2
	$handle = opendir($upgrade_path);
43
44 2
	if (!$handle) {
45
		return false;
46
	}
47
48 2
	$upgrade_files = [];
49
50 2
	while ($upgrade_file = readdir($handle)) {
51
		// make sure this is a well formed upgrade.
52 2
		if (is_dir($upgrade_path . '$upgrade_file')) {
53
			continue;
54
		}
55 2
		$upgrade_version = elgg_get_upgrade_file_version($upgrade_file);
56 2
		if (!$upgrade_version) {
57 2
			continue;
58
		}
59
		$upgrade_files[] = $upgrade_file;
60
	}
61
62 2
	sort($upgrade_files);
63
64 2
	return $upgrade_files;
65
}
66
67
/**
68
 * Unlocks upgrade.
69
 *
70
 * @todo the hack in the 2011010101 upgrade requires this
71
 *
72
 * @return void
73
 *
74
 * @access private
75
 */
76
function _elgg_upgrade_unlock() {
77
	$prefix = _elgg_config()->dbprefix;
78
	delete_data("drop table {$prefix}upgrade_lock");
79
	elgg_log('Upgrade unlocked.', 'NOTICE');
80
}
81