Newer
Older
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
<?php
/**
* Upgrade the CiviCRM plugin files and database.
*
* ## EXAMPLES
*
* # Update to the version of CiviCRM in the supplied archive.
* $ wp civicrm upgrade --zipfile=~/civicrm-5.57.1-wordpress.zip
*
* @since 5.69
*/
class CLI_Tools_CiviCRM_Command_Upgrade extends CLI_Tools_CiviCRM_Command {
/**
* Upgrade the CiviCRM plugin files and database. Deprecated: use `wp civicrm core update` instead.
*
* ## OPTIONS
*
* [--zipfile=<zipfile>]
* : Path to your CiviCRM zip file.
*
* [--tarfile=<tarfile>]
* : Path to your CiviCRM .tar.gz file. Not currently available.
*
* [--backup-dir=<backup-dir>]
* : Path to your CiviCRM backup directory. Default is one level above ABSPATH.
*
* [--v]
* : Run the upgrade queue with verbose output.
*
* [--vv]
* : Run the upgrade queue with extra verbose output.
*
* [--yes]
* : Answer yes to the confirmation messages.
*
* ## EXAMPLES
*
* # Update to the version of CiviCRM in the supplied archive.
* $ wp civicrm upgrade --zipfile=~/civicrm-5.57.1-wordpress.zip
*
* @since 5.69
*
* @param array $args The WP-CLI positional arguments.
* @param array $assoc_args The WP-CLI associative arguments.
*/
public function __invoke($args, $assoc_args) {
WP_CLI::log(WP_CLI::colorize('%CDeprecated command:%n %cuse `wp civicrm core update` instead.%n'));
// Grab associative arguments.
$zipfile = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'zipfile', '');
$tarfile = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'l10n-tarfile', '');
$backup_root_dir = (string) \WP_CLI\Utils\get_flag_value($assoc_args, 'backup-dir', '');
$v = (bool) \WP_CLI\Utils\get_flag_value($assoc_args, 'v', FALSE);
$vv = (bool) \WP_CLI\Utils\get_flag_value($assoc_args, 'vv', FALSE);
$yes = (bool) \WP_CLI\Utils\get_flag_value($assoc_args, 'yes', FALSE);
// Bail when .tar.gz archive is specified.
if (!empty($tarfile)) {
WP_CLI::error('CiviCRM .tar.gz archives are not supported.');
}
// Bail when no .zip archive is specified.
if (empty($zipfile)) {
WP_CLI::error('You must supply a CiviCRM zip archive.');
}
// Choose higher verbosity when both are specified.
if (!empty($v) && !empty($vv)) {
$v = FALSE;
}
// ----------------------------------------------------------------------------
// We can't use "wp civicrm core backup" because of its new backup schema.
// ----------------------------------------------------------------------------
// Build backup directory when not specified.
if (empty($backup_root_dir)) {
$backup_root_dir = trailingslashit(dirname(ABSPATH)) . 'backup';
}
// Maybe create destination directory.
$backup_root_dir = untrailingslashit($backup_root_dir);
if (!is_dir($backup_root_dir)) {
if (!is_writable(dirname($backup_root_dir))) {
WP_CLI::error("Insufficient permission to create directory '{$backup_root_dir}'.");
}
WP_CLI::log("Creating directory '{$backup_root_dir}'.");
// Recursively create directory.
if (!@mkdir($backup_root_dir, 0777, TRUE)) {
$error = error_get_last();
WP_CLI::error("Failed to create directory '{$backup_root_dir}': {$error['message']}.");
}
}
// Sanity check.
if (!is_writable($backup_root_dir)) {
WP_CLI::error("'{$backup_root_dir}' is not writable by current user.");
}
// Get the path to the CiviCRM plugin directory.
$plugin_path = $this->plugin_path_get();
// Build backup filename and path.
$date = date('YmdHis');
$filename = 'civicrm';
$backup_working_dir = trailingslashit($backup_root_dir) . trailingslashit('plugins') . $date;
$backup_sql_file = trailingslashit($backup_working_dir) . $filename . '.sql';
$backup_plugin_path = trailingslashit($backup_working_dir) . $filename;
WP_CLI::log('');
WP_CLI::log('The upgrade process involves:');
WP_CLI::log(sprintf('1. Backing up database as => %s', $backup_sql_file));
WP_CLI::log(sprintf('2. Backing up current CiviCRM code as => %s', $backup_plugin_path));
WP_CLI::log(sprintf('3. Unpacking zipfile to => %s', $plugin_path));
WP_CLI::log('4. Executing "civicrm/upgrade?reset=1" just as a browser would.');
WP_CLI::log('');
// Let's give folks a chance to exit now.
WP_CLI::confirm(WP_CLI::colorize('%GDo you want to continue?%n'), $assoc_args);
// ----------------------------------------------------------------------------
// Backup procedure.
// ----------------------------------------------------------------------------
// Create working backup directory.
if (!@mkdir($backup_working_dir, 0777, TRUE)) {
$error = error_get_last();
WP_CLI::error("Failed to create directory '{$backup_working_dir}': {$error['message']}.");
}
// Use "wp civicrm sql-dump" to dump database.
$options = ['launch' => FALSE, 'return' => FALSE];
WP_CLI::runcommand("civicrm sql-dump --result-file={$backup_sql_file}", $options);
WP_CLI::success('1. Database backed up.');
// Move existing CiviCRM plugin directory to backup directory.
if (!@rename($plugin_path, $backup_plugin_path)) {
$error = error_get_last();
WP_CLI::error(sprintf('Failed to backup CiviCRM project directory %s to %s: %s', $project_path, $backup_plugin_path, $error['message']));
}
WP_CLI::log('');
WP_CLI::success('2. Code backed up.');
// ----------------------------------------------------------------------------
// Subsequent commands can remain the same.
// ----------------------------------------------------------------------------
// Use "wp civicrm core update" to upgrade CiviCRM.
$command = 'civicrm core update --zipfile=' . $zipfile . (empty($yes) ? '' : ' --yes');
$options = ['launch' => FALSE, 'return' => FALSE];
WP_CLI::runcommand($command, $options);
// Use "wp civicrm core update-db" to upgrade the CiviCRM database.
$command = 'civicrm core update-db' . (empty($v) ? '' : ' --v') . (empty($vv) ? '' : ' --vv') . (empty($yes) ? '' : ' --yes');
$options = ['launch' => TRUE, 'return' => FALSE];
WP_CLI::runcommand($command, $options);
}
}