/
cli.php
359 lines (301 loc) · 9.82 KB
/
cli.php
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
<?php
use \Composer\Semver\Comparator;
use \WP_CLI\Dispatcher;
use \WP_CLI\Utils;
/**
* Get information about WP-CLI itself.
*
* @when before_wp_load
*/
class CLI_Command extends WP_CLI_Command {
private function command_to_array( $command ) {
$dump = array(
'name' => $command->get_name(),
'description' => $command->get_shortdesc(),
'longdesc' => $command->get_longdesc(),
);
foreach ( $command->get_subcommands() as $subcommand ) {
$dump['subcommands'][] = self::command_to_array( $subcommand );
}
if ( empty( $dump['subcommands'] ) ) {
$dump['synopsis'] = (string) $command->get_synopsis();
}
return $dump;
}
/**
* Print WP-CLI version.
*/
public function version() {
WP_CLI::line( 'WP-CLI ' . WP_CLI_VERSION );
}
/**
* Print various data about the CLI environment.
*
* ## OPTIONS
*
* [--format=<format>]
* : Accepted values: json
*/
public function info( $_, $assoc_args ) {
$php_bin = WP_CLI::get_php_binary();
$runner = WP_CLI::get_runner();
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ) === 'json' ) {
$info = array(
'php_binary_path' => $php_bin,
'global_config_path' => $runner->global_config_path,
'project_config_path' => $runner->project_config_path,
'wp_cli_dir_path' => WP_CLI_ROOT,
'wp_cli_packages_dir_path' => $runner->get_packages_dir_path(),
'wp_cli_version' => WP_CLI_VERSION,
);
WP_CLI::line( json_encode( $info ) );
} else {
WP_CLI::line( "PHP binary:\t" . $php_bin );
WP_CLI::line( "PHP version:\t" . PHP_VERSION );
WP_CLI::line( "php.ini used:\t" . get_cfg_var( 'cfg_file_path' ) );
WP_CLI::line( "WP-CLI root dir:\t" . WP_CLI_ROOT );
WP_CLI::line( "WP-CLI packages dir:\t" . $runner->get_packages_dir_path() );
WP_CLI::line( "WP-CLI global config:\t" . $runner->global_config_path );
WP_CLI::line( "WP-CLI project config:\t" . $runner->project_config_path );
WP_CLI::line( "WP-CLI version:\t" . WP_CLI_VERSION );
}
}
/**
* Check for update via Github API. Returns the available versions if there are updates, or empty if no update available.
*
* ## OPTIONS
*
* [--patch]
* : Only list patch updates
*
* [--minor]
* : Only list minor updates
*
* [--major]
* : Only list major updates
*
* [--field=<field>]
* : Prints the value of a single field for each update.
*
* [--fields=<fields>]
* : Limit the output to specific object fields. Defaults to version,update_type,package_url.
*
* [--format=<format>]
* : Accepted values: table, csv, json, count. Default: table
*
* @subcommand check-update
*/
public function check_update( $_, $assoc_args ) {
$updates = $this->get_updates( $assoc_args );
if ( $updates ) {
$formatter = new \WP_CLI\Formatter(
$assoc_args,
array( 'version', 'update_type', 'package_url' )
);
$formatter->display_items( $updates );
} else if ( empty( $assoc_args['format'] ) || 'table' == $assoc_args['format'] ) {
$update_type = $this->get_update_type_str( $assoc_args );
WP_CLI::success( "WP-CLI is at the latest{$update_type}version." );
}
}
/**
* Fetch most recent update matching the requirements. Returns the available versions if there are updates, or empty if no update available.
*
* ## OPTIONS
*
* [--patch]
* : Only perform patch updates
*
* [--minor]
* : Only perform minor updates
*
* [--major]
* : Only perform major updates
*
* [--nightly]
* : Update to the latest built version of the master branch. Potentially unstable.
*
* [--yes]
* : Do not prompt for confirmation
*/
public function update( $_, $assoc_args ) {
if ( ! Utils\inside_phar() ) {
WP_CLI::error( "You can only self-update Phar files." );
}
$old_phar = realpath( $_SERVER['argv'][0] );
if ( ! is_writable( $old_phar ) ) {
WP_CLI::error( sprintf( "%s is not writable by current user", $old_phar ) );
} else if ( ! is_writeable( dirname( $old_phar ) ) ) {
WP_CLI::error( sprintf( "%s is not writable by current user", dirname( $old_phar ) ) );
}
if ( isset( $assoc_args['nightly'] ) ) {
WP_CLI::confirm( sprintf( 'You have version %s. Would you like to update to the latest nightly?', WP_CLI_VERSION ), $assoc_args );
$download_url = 'https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli-nightly.phar';
} else {
$updates = $this->get_updates( $assoc_args );
if ( empty( $updates ) ) {
$update_type = $this->get_update_type_str( $assoc_args );
WP_CLI::success( "WP-CLI is at the latest{$update_type}version." );
exit(0);
}
$newest = $updates[0];
WP_CLI::confirm( sprintf( 'You have version %s. Would you like to update to %s?', WP_CLI_VERSION, $newest['version'] ), $assoc_args );
$download_url = $newest['package_url'];
}
WP_CLI::log( sprintf( 'Downloading from %s...', $download_url ) );
$temp = \WP_CLI\Utils\get_temp_dir() . uniqid('wp_') . '.phar';
$headers = array();
$options = array(
'timeout' => 600, // 10 minutes ought to be enough for everybody
'filename' => $temp
);
Utils\http_request( 'GET', $download_url, null, $headers, $options );
$allow_root = WP_CLI::get_runner()->config['allow-root'] ? '--allow-root' : '';
$php_binary = WP_CLI::get_php_binary();
$process = WP_CLI\Process::create( "{$php_binary} $temp --info {$allow_root}" );
$result = $process->run();
if ( 0 !== $result->return_code || false === stripos( $result->stdout, 'WP-CLI version:' ) ) {
$multi_line = explode( PHP_EOL, $result->stderr );
WP_CLI::error_multi_line( $multi_line );
WP_CLI::error( 'The downloaded PHAR is broken, try running wp cli update again.' );
}
WP_CLI::log( 'New version works. Proceeding to replace.' );
$mode = fileperms( $old_phar ) & 511;
if ( false === @chmod( $temp, $mode ) ) {
WP_CLI::error( sprintf( "Cannot chmod %s", $temp ) );
}
class_exists( '\cli\Colors' ); // this autoloads \cli\Colors - after we move the file we no longer have access to this class
if ( false === @rename( $temp, $old_phar ) ) {
WP_CLI::error( sprintf( "Cannot move %s to %s", $temp, $old_phar ) );
}
if ( isset( $assoc_args['nightly'] ) ) {
$updated_version = 'the latest nightly release';
} else {
$updated_version = $newest['version'];
}
WP_CLI::success( sprintf( 'Updated WP-CLI to %s', $updated_version ) );
}
/**
* Returns update information
*/
private function get_updates( $assoc_args ) {
$url = 'https://api.github.com/repos/wp-cli/wp-cli/releases';
$options = array(
'timeout' => 30
);
$headers = array(
'Accept' => 'application/json'
);
$response = Utils\http_request( 'GET', $url, $headers, $options );
if ( ! $response->success || 200 !== $response->status_code ) {
WP_CLI::error( sprintf( "Failed to get latest version (HTTP code %d)", $response->status_code ) );
}
$release_data = json_decode( $response->body );
$updates = array(
'major' => false,
'minor' => false,
'patch' => false,
);
foreach ( $release_data as $release ) {
// get rid of leading "v" if there is one set
$release_version = $release->tag_name;
if ( 'v' === substr( $release_version, 0, 1 ) ) {
$release_version = ltrim( $release_version, 'v' );
}
$update_type = Utils\get_named_sem_ver( $release_version, WP_CLI_VERSION );
if ( ! $update_type ) {
continue;
}
if ( ! empty( $updates[ $update_type ] ) && ! Comparator::greaterThan( $release_version, $updates[ $update_type ]['version'] ) ) {
continue;
}
$updates[ $update_type ] = array(
'version' => $release_version,
'update_type' => $update_type,
'package_url' => $release->assets[0]->browser_download_url
);
}
foreach( $updates as $type => $value ) {
if ( empty( $value ) ) {
unset( $updates[ $type ] );
}
}
foreach( array( 'major', 'minor', 'patch' ) as $type ) {
if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, $type ) ) {
return ! empty( $updates[ $type ] ) ? array( $updates[ $type ] ) : false;
}
}
return array_values( $updates );
}
/**
* Dump the list of global parameters, as JSON or in var_export format.
*
* ## OPTIONS
*
* [--with-values]
* : Display current values also.
*
* [--format=<format>]
* : Accepted values: var_export, json. Default: json.
*
* @subcommand param-dump
*/
function param_dump( $_, $assoc_args ) {
$spec = \WP_CLI::get_configurator()->get_spec();
if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'with-values' ) ) {
$config = \WP_CLI::get_configurator()->to_array();
// Copy current config values to $spec
foreach ( $spec as $key => $value ) {
if ( isset( $config[0][$key] ) ) {
$current = $config[0][$key];
} else {
$current = NULL;
}
$spec[$key]['current'] = $current;
}
}
if ( 'var_export' === \WP_CLI\Utils\get_flag_value( $assoc_args, 'format' ) ) {
var_export( $spec );
} else {
echo json_encode( $spec );
}
}
/**
* Dump the list of installed commands, as JSON.
*
* @subcommand cmd-dump
*/
public function cmd_dump() {
echo json_encode( self::command_to_array( WP_CLI::get_root_command() ) );
}
/**
* Generate tab completion strings.
*
* ## OPTIONS
*
* --line=<line>
* : The current command line to be executed
*
* --point=<point>
* : The index to the current cursor position relative to the beginning of the command
*/
public function completions( $_, $assoc_args ) {
$line = substr( $assoc_args['line'], 0, $assoc_args['point'] );
$compl = new \WP_CLI\Completions( $line );
$compl->render();
}
/**
* Get a string representing the type of update being checked for
*/
private function get_update_type_str( $assoc_args ) {
$update_type = ' ';
foreach( array( 'major', 'minor', 'patch' ) as $type ) {
if ( true === \WP_CLI\Utils\get_flag_value( $assoc_args, $type ) ) {
$update_type = ' ' . $type . ' ';
break;
}
}
return $update_type;
}
}
WP_CLI::add_command( 'cli', 'CLI_Command' );