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

engine/lib/upgrades/create_upgrade.php (1 issue)

1
<?php
2
/**
3
 * Creates an upgrade file for Elgg.
4
 *
5
 * Run this from the command line:
6
 * 	php create_upgrade.php upgrade_name
7
 */
8
9
error_reporting(E_ALL);
10
11
// only allow from the command line.
12
if (php_sapi_name() != 'cli') {
13
	die('Upgrades can only be created from the command line.');
14
}
15
16
if (count($argv) < 2) {
17
	elgg_create_upgrade_show_usage('No upgrade name.');
18
}
19
20
$name = $argv[1];
21
22
if (strlen($name) > 24) {
23
	elgg_create_upgrade_show_usage('Upgrade names cannot be longer than 24 characters.');
24
}
25
26
require_once '../../../version.php';
27
$upgrade_path = dirname(__FILE__);
28
29
$upgrade_name = strtolower($name);
30
$upgrade_name = str_replace(array(' ', '-'), '_', $upgrade_name);
31
$upgrade_release = str_replace(array(' ', '-'), '_', $release);
32
$time = time();
33
$upgrade_rnd = substr(md5($time), 0, 16);
34
$upgrade_date = date('Ymd', $time);
35
36
// determine the inc count
37
$upgrade_inc = 0;
38
$files = elgg_get_file_list($upgrade_path);
39
sort($files);
40
41
foreach ($files as $filename) {
42
	$filename = basename($filename);
43
	$date = (int)substr($filename, 0, 8);
44
	$inc = (int)substr($filename, 8, 2);
45
46
	if ($upgrade_date == $date) {
47
		if ($inc >= $upgrade_inc) {
48
			$upgrade_inc = $inc + 1;
49
		}
50
	}
51
}
52
53
// zero-pad
54
// if there are more than 10 upgrades in a day, someone needs talking to.
55
if ($upgrade_inc < 10) {
56
	$upgrade_inc = "0$upgrade_inc";
57
}
58
59
$upgrade_version = $upgrade_date . $upgrade_inc;
60
61
// make filename
62
if (substr($release, 0, 3) == '1.7') {
63
	// 1.7 upgrades are YYYYMMDDXX
64
	$upgrade_name = $upgrade_version . '.php';
65
} else {
66
	// 1.8+ upgrades are YYYYMMDDXX-release-friendly_name-rnd
67
	$upgrade_name = $upgrade_version . "-$upgrade_release-$name-$upgrade_rnd.php";
68
}
69
70
$upgrade_file = $upgrade_path . '/' . $upgrade_name;
71
72
if (is_file($upgrade_file)) {
73
	elgg_create_upgrade_show_usage("Upgrade file $upgrade_file already exists. This script has failed you.");
74
}
75
76
$upgrade_code = <<<___UPGRADE
77
<?php
78
/**
79
 * Elgg $release upgrade $upgrade_version
80
 * $name
81
 *
82
 * Upgrade script will always run after ALL database migrations are complete.
83
 * 
84
 * Do not use upgrade scripts for database schema migrations, use phinx instead. See docs for instructions.
85
 * 
86
 * Do not use upgrade script for long-running scripts, use async upgrades instead. See docs for instructions. 
87
 */
88
89
// upgrade code here.
90
91
___UPGRADE;
92
93
$h = fopen($upgrade_file, 'wb');
94
95
if (!$h) {
96
	die("Could not open file $upgrade_file");
97
}
98
99
if (!fwrite($h, $upgrade_code)) {
100
	die("Could not write to $upgrade_file");
101
} else {
102
	elgg_set_version_dot_php_version($upgrade_version);
103
	echo <<<___MSG
104
105
Created upgrade file and updated version.php.
106
107
Upgrade file: $upgrade_name
108
Version:      $upgrade_version
109
110
___MSG;
111
}
112
113
fclose($h);
114
115
/**
116
 * Updates the version number in elgg/version.php
117
 *
118
 * @param string $version
119
 * @return boolean
120
 */
121
function elgg_set_version_dot_php_version($version) {
122
	$file = '../../../version.php';
123
	$h = fopen($file, 'r+b');
124
125
	if (!$h) {
126
		return false;
127
	}
128
129
	$out = '';
130
131
	while (($line = fgets($h)) !== false) {
132
		$find = "/\\\$version[ ]?=[ ]?[0-9]{10};/";
133
		$replace = "\$version = $version;";
134
		$out .= preg_replace($find, $replace, $line);
135
	}
136
137
	rewind($h);
138
139
	fwrite($h, $out);
140
	fclose($h);
141
	return true;
142
}
143
144
/**
145
 * Shows the usage for the create_upgrade script and dies().
146
 *
147
 * @param string $msg Optional message to display
148
 * @return void
149
 */
150
function elgg_create_upgrade_show_usage($msg = '') {
151
	$text = <<<___MSG
152
$msg
153
154
Example:
155
	php create_upgrade.php my_upgrade
156
157
___MSG;
158
159
	die($text);
160
}
161
162
/**
163
 * C&P'd helpers from core to avoid pulling in everything.
164
 */
165
166
/**
167
 * Returns a list of files in $directory.
168
 *
169
 * Only returns files.  Does not recurse into subdirs.
170
 *
171
 * @param string $directory  Directory to look in
172
 * @param array  $exceptions Array of filenames to ignore
173
 * @param array  $list       Array of files to append to
174
 * @param mixed  $extensions Array of extensions to allow, NULL for all. Use a dot: array('.php').
175
 *
176
 * @return array Filenames in $directory, in the form $directory/filename.
177
 */
178
function elgg_get_file_list($directory, $exceptions = array(), $list = array(),
179
$extensions = NULL) {
180
181
	$directory = \Elgg\Project\Paths::sanitize($directory);
182
	if ($handle = opendir($directory)) {
183
		while (($file = readdir($handle)) !== FALSE) {
184
			if (!is_file($directory . $file) || in_array($file, $exceptions)) {
185
				continue;
186
			}
187
188
			if (is_array($extensions)) {
189
				if (in_array(strrchr($file, '.'), $extensions)) {
190
					$list[] = $directory . $file;
191
				}
192
			} else {
193
				$list[] = $directory . $file;
194
			}
195
		}
196
		closedir($handle);
197
	}
198
199
	return $list;
200
}
201
202
/**
203
 * Sanitise file paths ensuring that they begin and end with slashes etc.
204
 *
205
 * @param string $path         The path
206
 * @param bool   $append_slash Add tailing slash
207
 *
208
 * @return string
209
 */
210
function \Elgg\Project\Paths::sanitize($path, $append_slash = TRUE) {
0 ignored issues
show
A parse error occurred: Syntax error, unexpected T_NS_SEPARATOR, expecting T_STRING or '(' on line 210 at column 9
Loading history...
211
	// Convert to correct UNIX paths
212
	$path = str_replace('\\', '/', $path);
213
	$path = str_replace('../', '/', $path);
214
	// replace // with / except when preceeded by :
215
	$path = preg_replace("/([^:])\/\//", "$1/", $path);
216
217
	// Sort trailing slash
218
	$path = trim($path);
219
	// rtrim defaults plus /
220
	$path = rtrim($path, " \n\t\0\x0B/");
221
222
	if ($append_slash) {
223
		$path = $path . '/';
224
	}
225
226
	return $path;
227
}