-
Notifications
You must be signed in to change notification settings - Fork 18
/
backdrop_pm.drush.inc
511 lines (479 loc) · 20.3 KB
/
backdrop_pm.drush.inc
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
<?php
/**
* @file
* Package management Drush commands for Backdrop.
*/
/**
* Implements hook_drush_command().
*/
function backdrop_pm_drush_command() {
$items = array();
$items['backdrop-pm-download'] = array(
'description' => 'Download Backdrop CMS contrib modules.',
'callback' => 'backdrop_command_pm_download',
'hidden' => TRUE,
'arguments' => array(
'module-name' => array('description' => 'The name of the module(s) you would like to download.'),
),
'options' => array(
'select' => 'Select the version of the module.',
'version' => 'Specify the tagged version of the module that you want. (This overrides the --select option.',
),
'required-arguments' => TRUE,
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_SITE,
);
$items['backdrop-pm-update'] = array(
'description' => 'Update Backdrop core or contrib modules.',
'callback' => 'backdrop_command_pm_update',
'arguments' => array(
'module-name' => array(
'description' => 'The name of the module(s) you would like to update.',
),
),
// TODO: show available updates and allow for selection of which to apply.
// 'options' => array(
// 'show' => "Select from available updates:"
// ),
'aliases' => array('up'),
'required-arguments' => TRUE,
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_SITE,
);
return $items;
}
/**
* Command callback to download a Backdrop CMS contrib project.
*/
function backdrop_command_pm_download() {
$package_handler = backdrop_command_pm_update_validate();
if (!$package_handler) {
return;
}
$projects = func_get_args();
$options['select'] = drush_get_option('select', '');
$options['version'] = drush_get_option('version', '');
if (!empty($options['select']) || !empty($options['version'])) {
// Get available releases.
$tags = backdrop_pm_get_tags(
"https://updates.backdropcms.org/release-history/$projects[0]/1.x"
);
$project_path = backdrop_pm_get_path($tags);
if (empty($options['version'])) {
drush_print_r("\t\033[32mChoose one of the available releases for $projects[0] \033[0m\n");
for ($i = 0; $i < count($tags->releases->release); $i++) {
$version = (string) $tags->releases->release[$i]->version;
drush_print_r("\t\t [$i] \t:\t " . $version);
}
// Get users's menu selection.
$handle = fopen("php://stdin", "r");
print ("\n\tSelect one [0]: ");
print ("\033[0m");
$selection = fgets($handle);
$selection = trim($selection);
// Default to latest release if user just hits enter.
if (empty($selection)) {
$selection = 0;
}
$sel_url = $tags->releases->release[(int) $selection]->download_link;
}
else {
$releases = (array) $tags->releases;
// Check how many releases we have. One release requires special handling.
$num_releases = count($releases['release']);
if ($num_releases > 1 && gettype($releases['release']) == 'array') {
foreach ((array) $releases['release'] as $release) {
if ((string) $release->version == $options['version']) {
$sel_url = (string) $release->download_link;
}
}
}
elseif (gettype($releases['release']) == 'object') {
$sel_url = (string) $releases['release']->download_link;
}
// If the $sel_url is still empty we will not be able to get the project.
if (empty($sel_url)) {
drush_print_r('You must specify a valid version string. Project ' . $projects[0] . ' does not have a version: ' . $options['version'] . '.');
return;
}
}
// Download the version of the project user specified.
exec(
"wget --quiet -O $project_path/$projects[0].zip $sel_url"
);
// Extract the zip file.
exec(
"unzip $project_path/$projects[0].zip -d $project_path"
);
// Remove the zip file.
exec(
"rm $project_path/$projects[0].zip"
);
backdrop_pm_dl_outcome($project_path, $projects[0]);
}
else {
foreach ($projects as $project) {
if ($project != 'backdrop') {
$url = backdrop_pm_get_from_github(
"https://github.com/backdrop-contrib/$project/releases/latest"
);
// Get the release info from backdropcms.org.
$tags = backdrop_pm_get_tags(
"https://updates.backdropcms.org/release-history/$project/1.x"
);
$project_path = backdrop_pm_get_path($tags);
$latest = explode('/', $url);
$latest = array_reverse($latest);
// Build the path to location to download the module.
$module_install_location = $project_path . '/' . $project;
// Build the download URL.
$download_url = "https://github.com/backdrop-contrib/$project/releases/download/$latest[0]/$project.zip";
// Get the HTTP/1.1 response to check against.
$response = get_headers($download_url);
// If the http response is 404 gracefully bow out; offer user the dev
// version?
if (strpos($response[0], '404') !== FALSE) {
drush_print_r(
dt("\n\t\e[31mError\e[0m: There is no release located at $download_url." .
"\n\tTry visiting the \e[1m$project\e[0m page directly: https://github.com/backdrop-contrib/$project\n"
)
);
continue;
}
if (is_dir($module_install_location)) {
drush_log(dt('Module is already installed ... exiting without re-writing module.'), 'error');
continue;
}
exec(
"wget --quiet -O $project_path/$project.zip $download_url"
);
// Extract the zip file.
exec(
"unzip $project_path/$project.zip -d $project_path"
);
// Remove the zip file.
exec(
"rm $project_path/$project.zip"
);
backdrop_pm_dl_outcome($module_install_location, $project);
}
}
}
}
/**
* Helper function for backdrop_command_pm_download().
*
* Gets the url for the github repo of the contrib module.
*/
function backdrop_pm_get_from_github($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_exec($ch);
$curl_info = curl_getinfo($ch);
$redirect_url = "$curl_info[redirect_url]";
curl_close($ch);
return $redirect_url;
}
/**
* Helper function for --select option for download command.
*
* @param string $url
* Path to tags file.
*/
function backdrop_pm_get_tags($url) {
$xml = simplexml_load_file($url);
return $xml;
}
/**
* Get the path to where to store the module, theme, or layout.
*
* @param Object $tags
* xml object.
*/
function backdrop_pm_get_path($tags) {
$type = (string) $tags->type;
if (!empty(drush_get_context('DRUSH_BACKDROP_SITE_ROOT'))) {
$site_root = realpath(BACKDROP_ROOT . '/' . drush_get_context('DRUSH_BACKDROP_SITE_ROOT'));
}
else {
$site_root = BACKDROP_ROOT;
}
if ($type == 'project_module') {
if (file_exists($site_root . '/modules/contrib')) {
$project_path = $site_root . '/modules/contrib';
}
else {
$project_path = $site_root . '/modules';
}
}
elseif ($type == 'project_theme') {
$project_path = $site_root . '/themes';
}
elseif ($type == 'project_layout') {
$project_path = $site_root . '/layouts';
}
return $project_path;
}
/**
* Determine success or failure of drush dl command.
*
* @param string $module_install_location
* String directory path where module should end up.
*
* @param string $project
* Project name.
*/
function backdrop_pm_dl_outcome($module_install_location, $project) {
if (file_exists($module_install_location)) {
print "\n\t\033[32mSuccess: \033[0m Project $project downloaded to $module_install_location.\n\n";
}
else {
print "\n\t\033[31mError: \033[0m Project $project could not be found.\n\n";
}
}
/**
* Command pm-update validate function.
*/
function backdrop_command_pm_update_validate() {
// Check that wget command exists. Disable possible output.
$debug = drush_get_context('DRUSH_DEBUG');
drush_set_context('DRUSH_DEBUG', FALSE);
$success = drush_shell_exec('wget --version');
drush_set_context('DRUSH_DEBUG', $debug);
if (!$success) {
return drush_set_error(
'DRUSH_SHELL_COMMAND_NOT_FOUND',
dt("\n\t\033[1mwget\033[0m executable not found.
Downloading projects with drush depends on wget consider installing wget on your system.\n")
);
}
return TRUE;
}
/**
* Command callback for pm-update.
*/
function backdrop_command_pm_update() {
// Call validate function to check for required curl or wget.
$package = backdrop_command_pm_update_validate();
// If neither curl or wget are available bail.
if (!$package) {
return;
}
// Grab the transliteration.inc file so we can sanitize $site_name string.
require_once 'core/includes/transliteration.inc';
$projects = func_get_args();
foreach ($projects as $project) {
$project = strtolower($project);
$project = rtrim($project, '/');
$backdrop_root = BACKDROP_ROOT;
if ($project === 'backdrop' || $project == 'core') {
// Get current Backdrop version info.
$current_version = BACKDROP_VERSION;
$parse_version = explode('.', $current_version);
$current_version_major = $parse_version[0];
$current_version_minor = $parse_version[1];
$current_version_patch = $parse_version[2];
// Get proposed Backdrop version info.
$xml = backdrop_pm_get_tags("https://updates.backdropcms.org/release-history/backdrop/1.x");
$download_link = $xml->releases->release->download_link;
$proposed_version = $xml->releases->release->version;
$proposed_version_major = $xml->releases->release->version_major;
$proposed_version_minor = $xml->releases->release->version_minor;
$proposed_version_patch = $xml->releases->release->version_patch;
if ($current_version == $proposed_version) {
drush_print_r("\n\t\tBackdrop is up to date :).\n");
}
elseif ($current_version_major < $proposed_version_major) {
drush_print_r("\n\t\tYou can not use drush up to move between Major Versions of Backdrop.");
}
elseif (
(
$current_version_major == $proposed_version_major &&
$current_version_minor == $proposed_version_minor &&
$current_version_patch < $proposed_version_patch
) ||
(
$current_version_major == $proposed_version_major &&
$current_version_minor < $proposed_version_minor
)
) {
drush_print_r("\n\t\tCurrent \033[1mBackdrop\033[0m Version:\t$current_version");
drush_print_r("\t\tProposed \033[1mBackdrop\033[0m Version:\t$proposed_version\n");
drush_print_r("\tCode updates will be made to Backdrop core.\n");
drush_print_r("\t\e[33mWARNING\e[0m: Updating core will discard any");
drush_print_r("\tmodifications made to Backdrop core files. If you have made any");
drush_print_r("\tmodifications to these files, please back them up before updating so that");
drush_print_r("\tyou can re-create your modifications in the updated version of the file.\n\n");
drush_print_r("\tNote: Updating core can potentially break your site.");
drush_print_r("\tIt is NOT recommended to update production sites without prior testing.\n\n");
$answer = drush_prompt("\t\e[34mDo you wish to continue?\e[0m (y or n)");
$answer = strtolower($answer);
if ($answer == 'y' || $answer == 'yes') {
$users_home_directory = $_SERVER['HOME'];
$site_name = strtolower(transliteration_clean_filename(config_get('system.core', 'site_name')));
$timestamp = date_timestamp_get(date_create());
drush_print_r("\tBacking up current core folder to $users_home_directory/drush-backups/$site_name/$timestamp/core-$current_version");
exec("mkdir -p $users_home_directory/.drush/drush-tmp");
if (!file_exists("$users_home_directory/.drush/drush-tmp/")) {
drush_print_r("\t\e[31mFailed\e[0m: The drush-tmp directory could not be created.\n\n");
return FALSE;
}
exec("wget --quiet -O $users_home_directory/.drush/drush-tmp/backdrop.zip $download_link");
exec("unzip $users_home_directory/.drush/drush-tmp/backdrop.zip -d $users_home_directory/.drush/drush-tmp");
foreach (glob("$users_home_directory/.drush/drush-tmp/backdrop-backdrop*") as $glob) {
rename($glob, "$users_home_directory/.drush/drush-tmp/backdrop");
}
if (file_exists("$users_home_directory/.drush/drush-tmp/backdrop")) {
// Backup current core directory.
exec("mkdir -p $users_home_directory/drush-backups/$site_name/$timestamp/");
exec("mv $backdrop_root/core $users_home_directory/drush-backups/$site_name/$timestamp/core-$current_version");
exec("mv $users_home_directory/.drush/drush-tmp/backdrop/core $backdrop_root/core");
exec("rm -r $users_home_directory/.drush/drush-tmp");
}
else {
drush_print_r("\t\e[031mError\e[0m: New version of backdrop could not be found.");
return FALSE;
}
drush_print_r("\n\tYou can retrieve your old core files, should you need
to from: $users_home_directory/drush-backups/$site_name/$timestamp/core-$current_version");
if (file_exists("$backdrop_root/core")) {
drush_print_r("\t\e[32mSuccess\033[0m: Backdrop updated to $proposed_version.");
}
else {
drush_print_r("\t\033[31mFailed\033[0m: Upgrade failed.");
}
}
else {
drush_print_r("\tBackdrop core update cancelled.");
}
}
else {
drush_print_r("\n\tNo Backdrop core updates available. Your Backdrop is up to date!\n");
}
}
elseif (!empty($project)) {
if (!empty(drush_get_context('DRUSH_BACKDROP_SITE_ROOT'))) {
$site_root = realpath($backdrop_root . '/' . drush_get_context('DRUSH_BACKDROP_SITE_ROOT'));
}
else {
$site_root = $backdrop_root;
}
if (file_exists("$site_root/modules/contrib")) {
$module_path = "$site_root/modules/contrib";
}
else {
$module_path = "$site_root/modules";
}
$ls = scandir($module_path);
if (in_array($project, $ls)) {
$info = file("$module_path/$project/$project.info", FILE_IGNORE_NEW_LINES);
foreach ($info as $i) {
if (strpos($i, 'version =') !== FALSE) {
$module_version_raw = $i;
}
}
if (!$module_version_raw) {
drush_print_r("\tCurrent module version of \033[1m$project\033[0m could not be determined.");
drush_print_r("\tThis is usually because the module doesn't have an official release.");
}
$module_version_arr = explode(' = ', $module_version_raw);
$module_full_version = trim($module_version_arr[1]);
if (strpos($module_full_version, '-') !== FALSE) {
$module_sem_version_arr = explode('-', $module_version_arr[1]);
$module_sem_version = trim($module_sem_version_arr[1]);
$module_core_compat = trim($module_sem_version_arr[0]);
}
else {
$module_sem_version = $module_full_version;
}
$module_sem_version_parts = explode('.', $module_sem_version);
$module_version_major = $module_sem_version_parts[0];
$module_version_minor = $module_sem_version_parts[1];
$module_version_patch = $module_sem_version_parts[2];
// Get proposed Backdrop version info.
$xml = backdrop_pm_get_tags("https://updates.backdropcms.org/release-history/$project/1.x");
$download_link = $xml->releases->release->download_link;
$proposed_version = $xml->releases->release->version;
$proposed_version_major = $xml->releases->release->version_major;
$proposed_version_minor = $xml->releases->release->version_minor;
$proposed_version_patch = $xml->releases->release->version_patch;
// Check if proposed version has prepended Backrop compatibility string.
if (strpos($proposed_version, "-") !== FALSE) {
$proposed_version_array = explode('-', $proposed_version);
$proposed_sem_version = $proposed_version_array[1];
}
if (empty($proposed_version)) {
drush_print_r("\tThere are no official releases for \033[1m$project\033[0m.
Upgrading is not possible.
");
}
elseif ($module_sem_version == $proposed_sem_version) {
drush_print_r("\n\t\tProject \033[1m$project\033[0m is up to date :).\n");
}
elseif (
(
$module_version_major == $proposed_version_major &&
$module_version_minor == $proposed_version_minor &&
$module_version_patch < $proposed_version_patch
) ||
(
$module_version_major == $proposed_version_major &&
$module_version_minor < $proposed_version_minor
)
) {
drush_print_r("\n\t\tCurrent \033[1m$project\033[0m Version:\t$module_full_version");
drush_print_r("\t\tProposed \033[1m$project\033[0m Version:\t$proposed_version\n");
drush_print_r("\t\e[33mWARNING\033[0m:\n
Updating \033[1m$project\033[0m will discard anymodifications made to
\033[1m$project\033[0m files. If you have made any modifications to
these files, please back them up before updating so that you can
re-create your modifications in the updated version of the file.\n\n");
drush_print_r("\tNote: Updating modules can potentially break your site.");
drush_print_r("\tIt is NOT recommended to update production sites without prior testing.\n\n");
$answer = drush_prompt("\t\e[34mDo you wish to continue?\e[0m (y or n)");
$answer = strtolower($answer);
if ($answer == 'y' || $answer == 'yes') {
$users_home_directory = $_SERVER['HOME'];
$site_name = strtolower(transliteration_clean_filename(config_get('system.core', 'site_name')));
$timestamp = date_timestamp_get(date_create());
exec("mkdir -p $users_home_directory/drush-backups/$site_name/$timestamp/modules/");
drush_print_r("\t Backing up current \033[1m$project\033[0m folder to
$users_home_directory/drush-backups/$site_name/$timestamp/modules/$project-$module_full_version");
exec("mv $module_path/$project $users_home_directory/drush-backups/$site_name/$timestamp/modules/$project-$module_full_version");
exec("mkdir -p $users_home_directory/.drush/drush-tmp");
exec("wget --quiet -O $users_home_directory/.drush/drush-tmp/$project.zip $download_link");
exec("unzip $users_home_directory/.drush/drush-tmp/$project.zip -d $users_home_directory/.drush/drush-tmp");
foreach (glob("$users_home_directory/.drush/drush-tmp/backdrop-contrib-$project-*") as $glob) {
rename($glob, "$users_home_directory/.drush/drush-tmp/$project");
}
exec("mv $users_home_directory/.drush/drush-tmp/$project $module_path/$project");
exec("rm -r $users_home_directory/.drush/drush-tmp");
drush_print_r("\n\tYou can retrieve your old \033[1m$project\033[0m files, should you need
to, from: $users_home_directory/drush-backups/$site_name/$timestamp/modules/$project-$module_full_version");
if (file_exists("$module_path/$project")) {
drush_print_r("\t\e[32mSuccess\033[0m: Project \033[1m$project\033[0m updated to $proposed_version.");
}
else {
drush_print_r("\t\033[31mFailed\033[0m: Project $project upgrade failed.");
}
}
else {
drush_print_r("\tBackdrop core update cancelled.");
}
}
}
else {
drush_print_r("\n\t\e[33mWARNING\033[0m: There is no project \e[1m$project\033[0m installed on your backdrop site.");
drush_print_r("\tNo action taken.\n");
}
}
else {
drush_print_r("\n\tYou must specify a project to update. For example:\n");
drush_print_r("\t\tdrush up project_name");
drush_print_r("\n\tWhere project_name is the machine name of the project like webform or devel etc.\n");
}
}
// Run db updates and clear cache.
backdrop_command_updatedb();
backdrop_flush_all_caches();
}