aboutsummaryrefslogtreecommitdiff
path: root/engine/lib/upgrades/create_upgrade.php
blob: 3652e18a2d6fd35b874e7150254ea433deae52ad (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
<?php
/**
 * Creates an upgrade file for Elgg.
 *
 * Run this from the command line:
 * 	php create_upgrade.php upgrade_name
 */

error_reporting(E_NOTICE);

// only allow from the command line.
if (php_sapi_name() != 'cli') {
	die('Upgrades can only be created from the command line.');
}

if (count($argv) < 2) {
	elgg_create_upgrade_show_usage('No upgrade name.');
}

$name = $argv[1];

if (strlen($name) > 24) {
	elgg_create_upgrade_show_usage('Upgrade names cannot be longer than 24 characters.');
}

require_once '../../../version.php';
require_once '../elgglib.php';
$upgrade_path = dirname(__FILE__);

$upgrade_name = strtolower($name);
$upgrade_name = str_replace(array(' ', '-'), '_', $upgrade_name);
$upgrade_release = str_replace(array(' ', '-'), '_', $release);
$time = time();
$upgrade_rnd = substr(md5($time), 0, 16);
$upgrade_date = date('Ymd', $time);

// determine the inc count
$upgrade_inc = 0;
$files = elgg_get_file_list($upgrade_path);
sort($files);

foreach ($files as $filename) {
	$filename = basename($filename);
	$date = (int)substr($filename, 0, 8);
	$inc = (int)substr($filename, 8, 2);

	if ($upgrade_date == $date) {
		if ($inc >= $upgrade_inc) {
			$upgrade_inc = $inc + 1;
		}
	}
}

// zero-pad
// if there are more than 10 upgrades in a day, someone needs talking to.
if ($upgrade_inc < 10) {
	$upgrade_inc = "0$upgrade_inc";
}

$upgrade_version = $upgrade_date . $upgrade_inc;

// make filename
if (substr($release, 0, 3) == '1.7') {
	// 1.7 upgrades are YYYYMMDDXX
	$upgrade_name = $upgrade_version . '.php';
} else {
	// 1.8+ upgrades are YYYYMMDDXX-release-friendly_name-rnd
	$upgrade_name = $upgrade_version . "-$upgrade_release-$name-$upgrade_rnd.php";
}

$upgrade_file = $upgrade_path . '/' . $upgrade_name;

if (is_file($upgrade_file)) {
	elgg_create_upgrade_show_usage("Upgrade file $upgrade_file already exists. This script has failed you.");
}

$upgrade_code = <<<___UPGRADE
<?php
/**
 * Elgg $release upgrade $upgrade_version
 * $name
 *
 * Description
 */

// upgrade code here.

___UPGRADE;

$h = fopen($upgrade_file, 'wb');

if (!$h) {
	die("Could not open file $upgrade_file");
}

if (!fputs($h, $upgrade_code)) {
	die("Could not write to $upgrade_file");
} else {
	elgg_set_version_dot_php_version($upgrade_version);
	echo <<<___MSG

Created upgrade file and updated version.php.

Upgrade file: $upgrade_name
Version:      $upgrade_version

___MSG;
}

fclose($h);


function elgg_set_version_dot_php_version($version) {
	$file = '../../../version.php';
	$h = fopen($file, 'r+b');

	if (!$h) {
		return false;
	}

	$out = '';

	while (($line = fgets($h)) !== false) {
		$find = "/\\\$version[ ]?=[ ]?[0-9]{10};/";
		$replace = "\$version = $version;";
		$out .= preg_replace($find, $replace, $line);
	}

	rewind($h);

	fputs($h, $out);
	fclose($h);
}

/**
 * Shows the usage for the create_upgrade script and dies().
 *
 * @param string $msg Optional message to display
 * @return void
 */
function elgg_create_upgrade_show_usage($msg = '') {
	$text = <<<___MSG
$msg

Example:
	php create_upgrade.php my_upgrade

___MSG;

	die($text);
}