upgrade.php ➔ elgg_get_upgrade_file_version()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 9
loc 9
ccs 0
cts 4
cp 0
crap 6
rs 9.9666
c 0
b 0
f 0
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
 * Saves the processed upgrades to a dataset.
12
 *
13
 * @param array $processed_upgrades An array of processed upgrade filenames
14
 *                                  (not the path, just the file)
15
 * @return bool
16
 * @access private
17
 *
18
 * @todo this is still required because of the hack in the 2011010101 upgrade
19
 */
20
function elgg_set_processed_upgrades(array $processed_upgrades) {
21
	$processed_upgrades = array_unique($processed_upgrades);
22
	return datalist_set('processed_upgrades', serialize($processed_upgrades));
23
}
24
25
/**
26
 * Returns the version of the upgrade filename.
27
 *
28
 * @param string $filename The upgrade filename. No full path.
29
 * @return int|false
30
 * @since 1.8.0
31
 * @access private
32
 * @todo used by elgg_get_upgrade_files
33
 */
34 View Code Duplication
function elgg_get_upgrade_file_version($filename) {
35
	preg_match('/^([0-9]{10})([\.a-z0-9-_]+)?\.(php)$/i', $filename, $matches);
36
37
	if (isset($matches[1])) {
38
		return (int) $matches[1];
39
	}
40
41
	return false;
42
}
43
44
/**
45
 * Returns a list of upgrade files relative to the $upgrade_path dir.
46
 *
47
 * @param string $upgrade_path The directory that has upgrade scripts
48
 * @return array|false
49
 * @access private
50
 *
51
 * @todo the wire and groups plugins and the installer are using this
52
 */
53
function elgg_get_upgrade_files($upgrade_path = null) {
54
	if (!$upgrade_path) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $upgrade_path of type string|null 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...
55
		$upgrade_path = elgg_get_root_path() . 'engine/lib/upgrades/';
56
	}
57
	$upgrade_path = sanitise_filepath($upgrade_path);
58
	$handle = opendir($upgrade_path);
59
60
	if (!$handle) {
61
		return false;
62
	}
63
64
	$upgrade_files = array();
65
66 View Code Duplication
	while ($upgrade_file = readdir($handle)) {
67
		// make sure this is a well formed upgrade.
68
		if (is_dir($upgrade_path . '$upgrade_file')) {
69
			continue;
70
		}
71
		$upgrade_version = elgg_get_upgrade_file_version($upgrade_file);
72
		if (!$upgrade_version) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $upgrade_version of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. 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 integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
73
			continue;
74
		}
75
		$upgrade_files[] = $upgrade_file;
76
	}
77
78
	sort($upgrade_files);
79
80
	return $upgrade_files;
81
}
82
83
/**
84
 * Unlocks upgrade.
85
 *
86
 * @access private
87
 *
88
 * @todo the hack in the 2011010101 upgrade requires this
89
 */
90
function _elgg_upgrade_unlock() {
91
	global $CONFIG;
92
	delete_data("drop table {$CONFIG->dbprefix}upgrade_lock");
93
	elgg_log('Upgrade unlocked.', 'NOTICE');
94
}
95