Repository navigation
Expand file tree
/
Copy pathutils.php
More file actions
336 lines (266 loc) · 7.52 KB
/
Copy pathutils.php
File metadata and controls
336 lines (266 loc) · 7.52 KB
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
<?php
// Utilities that do NOT depend on WordPress code.
namespace WP_CLI\Utils;
use \WP_CLI\Dispatcher;
function load_dependencies() {
$vendor_paths = array(
WP_CLI_ROOT . '../../../../vendor', // part of a larger project
WP_CLI_ROOT . '../vendor', // top-level project
);
$has_autoload = false;
foreach ( $vendor_paths as $vendor_path ) {
if ( file_exists( $vendor_path . '/autoload.php' ) ) {
require $vendor_path . '/autoload.php';
include $vendor_path . '/wp-cli/php-cli-tools/lib/cli/cli.php';
$has_autoload = true;
break;
}
}
if ( !$has_autoload ) {
include WP_CLI_ROOT . 'php-cli-tools/lib/cli/cli.php';
\cli\register_autoload();
include WP_CLI_ROOT . 'mustache/src/Mustache/Autoloader.php';
\Mustache_Autoloader::register();
register_autoload();
}
include WP_CLI_ROOT . 'Spyc.php';
}
function register_autoload() {
spl_autoload_register( function($class) {
// Only attempt to load classes in our namespace
if ( 0 !== strpos( $class, 'WP_CLI\\' ) ) {
return;
}
$path = WP_CLI_ROOT . str_replace( '\\', DIRECTORY_SEPARATOR, $class ) . '.php';
if ( is_file( $path ) ) {
require_once $path;
}
} );
}
function get_config_spec() {
$spec = include __DIR__ . '/config-spec.php';
$defaults = array(
'runtime' => false,
'file' => false,
'synopsis' => '',
'default' => null,
);
foreach ( $spec as &$option ) {
$option = array_merge( $defaults, $option );
}
return $spec;
}
/**
* Splits $argv into positional and associative arguments.
*
* @param string
* @return array
*/
function parse_args( $arguments ) {
$regular_args = array();
$assoc_args = array();
foreach ( $arguments as $arg ) {
if ( preg_match( '|^--([^=]+)$|', $arg, $matches ) ) {
$assoc_args[ $matches[1] ] = true;
} elseif ( preg_match( '|^--([^=]+)=(.+)|', $arg, $matches ) ) {
$assoc_args[ $matches[1] ] = $matches[2];
} else {
$regular_args[] = $arg;
}
}
return array( $regular_args, $assoc_args );
}
/**
* Composes positional arguments into a command string.
*
* @param array
* @return string
*/
function args_to_str( $args ) {
return ' ' . implode( ' ', array_map( 'escapeshellarg', $args ) );
}
/**
* Composes associative arguments into a command string.
*
* @param array
* @return string
*/
function assoc_args_to_str( $assoc_args ) {
$str = '';
foreach ( $assoc_args as $key => $value ) {
if ( true === $value )
$str .= " --$key";
else
$str .= " --$key=" . escapeshellarg( $value );
}
return $str;
}
function get_command_file( $command ) {
$path = WP_CLI_ROOT . "/commands/$command.php";
if ( !is_readable( $path ) ) {
return false;
}
return $path;
}
/**
* Sets the appropriate $_SERVER keys based on a given string
*
* @param string $url The URL
*/
function set_url_params( $url ) {
$url_parts = parse_url( $url );
if ( !isset( $url_parts['scheme'] ) ) {
$url_parts = parse_url( 'http://' . $url );
}
$f = function( $key ) use ( $url_parts ) {
return isset( $url_parts[ $key ] ) ? $url_parts[ $key ] : '';
};
$_SERVER['HTTP_HOST'] = $f('host');
$_SERVER['REQUEST_URI'] = $f('path') . ( isset( $url_parts['query'] ) ? '?' . $url_parts['query'] : '' );
$_SERVER['REQUEST_URL'] = $f('path');
$_SERVER['QUERY_STRING'] = $f('query');
$_SERVER['SERVER_NAME'] = substr($_SERVER['HTTP_HOST'], 0, strrpos($_SERVER['HTTP_HOST'], '.'));
$_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.0';
$_SERVER['HTTP_USER_AGENT'] = '';
$_SERVER['REQUEST_METHOD'] = 'GET';
}
function locate_wp_config() {
static $path;
if ( null === $path ) {
if ( file_exists( ABSPATH . 'wp-config.php' ) )
$path = ABSPATH . 'wp-config.php';
elseif ( file_exists( ABSPATH . '../wp-config.php' ) && ! file_exists( ABSPATH . '/../wp-settings.php' ) )
$path = ABSPATH . '../wp-config.php';
else
$path = false;
if ( $path )
$path = realpath( $path );
}
return $path;
}
/**
* Take a serialised array and unserialise it replacing elements as needed and
* unserialising any subordinate arrays and performing the replace on those too.
*
* @source https://github.com/interconnectit/Search-Replace-DB
*
* @param string $from String we're looking to replace.
* @param string $to What we want it to be replaced with
* @param array $data Used to pass any subordinate arrays back to in.
* @param bool $serialised Does the array passed via $data need serialising.
*
* @return array The original array with all elements replaced as needed.
*/
function recursive_unserialize_replace( $from = '', $to = '', $data = '', $serialised = false ) {
// some unseriliased data cannot be re-serialised eg. SimpleXMLElements
try {
if ( is_string( $data ) && ( $unserialized = @unserialize( $data ) ) !== false ) {
$data = recursive_unserialize_replace( $from, $to, $unserialized, true );
}
elseif ( is_array( $data ) ) {
$_tmp = array( );
foreach ( $data as $key => $value ) {
$_tmp[ $key ] = recursive_unserialize_replace( $from, $to, $value, false );
}
$data = $_tmp;
unset( $_tmp );
}
// Submitted by Tina Matter
elseif ( is_object( $data ) ) {
$dataClass = get_class( $data );
$_tmp = new $dataClass( );
foreach ( $data as $key => $value ) {
$_tmp->$key = recursive_unserialize_replace( $from, $to, $value, false );
}
$data = $_tmp;
unset( $_tmp );
}
else {
if ( is_string( $data ) )
$data = str_replace( $from, $to, $data );
}
if ( $serialised )
return serialize( $data );
} catch( Exception $error ) {
}
return $data;
}
/**
* Output items in a table, JSON, or CSV
*
* @param string $format Format to use: 'table', 'json', 'csv'
* @param array $fields Named fields for each item of data
* @param array $items Data to output
*/
function format_items( $format, $fields, $items ) {
switch ( $format ) {
case 'table':
$table = new \cli\Table();
$table->setHeaders( $fields );
foreach ( $items as $item ) {
$line = array();
foreach ( $fields as $field ) {
$line[] = $item->$field;
}
$table->addRow( $line );
}
$table->display();
break;
case 'csv':
case 'json':
$output_items = array();
foreach( $items as $item ) {
$output_item = new \stdClass;
foreach( $fields as $field ) {
$output_item->$field = $item->$field;
}
$output_items[] = $output_item;
}
if ( 'json' == $format )
echo json_encode( $output_items );
else
output_csv( $output_items, $fields );
break;
}
}
/**
* Output data as CSV
*
* @param array $rows Array of rows to output
* @param array $headers List of CSV columns (optional)
*/
function output_csv( $rows, $headers = array() ) {
// Prepare the headers if they were specified
if ( ! empty( $headers ) )
fputcsv( STDOUT, $headers );
foreach ( $rows as $row ) {
$row = (array) $row;
if ( ! empty( $headers ) ) {
$build_row = array();
foreach ( $headers as $key ) {
$build_row[] = $row[ $key ];
}
$row = $build_row;
}
fputcsv( STDOUT, $row );
}
}
/**
* Launch system's $EDITOR to edit text
*
* @param str $content Text to edit (eg post content)
* @return str|bool Edited text, if file is saved from editor
* False, if no change to file
*/
function launch_editor_for_input( $input, $title = 'WP-CLI' ) {
$tmpfile = wp_tempnam( $title );
if ( !$tmpfile )
\WP_CLI::error( 'Error creating temporary file.' );
file_put_contents( $tmpfile, $input );
\WP_CLI::launch( "\${EDITOR:-vi} '$tmpfile'" );
$output = file_get_contents( $tmpfile );
unlink( $tmpfile );
if ( $output === $input )
return false;
return $output;
}