-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-validate-sql-wpcli.php
620 lines (533 loc) · 18.6 KB
/
class-validate-sql-wpcli.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
/**
* WP CLI command that takes a .sql file and validate multiple rules before importing.
*
* @package validate-sql
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Check if the .sql file is ready to be imported.
*/
class Validate_SQL_WPCLI {
/**
* DROP TABLE statements that are found in the SQL file.
*
* @see get_drop_tables()
* @var array
*/
private $drop_tables = array(
'wp' => array(),
'nonwp' => array(),
);
/**
* CREATE TABLE statements that are found in the SQL file.
*
* @see create_drop_tables()
* @var array
*/
private $create_tables = array(
'wp' => array(),
'nonwp' => array(),
);
/**
* Charsets that are found in the SQL file.
*
* @see get_charset()
* @var array
*/
private $charsets = array();
/**
* CREATE or DELETE DATABASE statements that are found in the SQL file.
*
* @see get_database_statements()
* @var array
*/
private $database_statements = array();
/**
* Entries from wp_options that are found in the SQL file.
*
* @see validate_options_entries()
* @var array
*/
private $options_entries = array();
/**
* Entries from wp_blogs that are found in the SQL file.
*
* @see get_blogs()
* @var array
*/
private $blogs_entries = array();
/**
* The list of the core tables used by WordPress.
*
* @var array
*/
private $core_tables = array(
'wp_commentmeta',
'wp_comments',
'wp_links',
'wp_options',
'wp_postmeta',
'wp_posts',
'wp_terms',
'wp_termmeta',
'wp_term_relationships',
'wp_term_taxonomy',
'wp_usermeta',
'wp_users',
);
/**
* The list of the multisite tables used by WordPress.
*
* @var array
*/
private $multisite_tables = array(
'wp_blogs',
'wp_blogmeta',
'wp_blog_versions',
'wp_registration_log',
'wp_signups',
'wp_site',
'wp_sitemeta',
);
/**
* CLI command that takes a .sql file (required) and validate multiple rules before importing.
*
* @synopsis --file=<SQLFILE>
*/
public function __invoke( $args, $assoc_args ) {
$archive_extensions = array( 'gz', 'zip' );
if ( ! isset( $assoc_args['file'] ) ) {
WP_CLI::error( 'Must have --file=my_sql_file attached' );
} else {
$path = $assoc_args['file'];
$ext = pathinfo( $path, PATHINFO_EXTENSION );
if ( 'sql' === $ext ) {
$this->validate_sql_file( $path );
} elseif ( in_array( $ext, $archive_extensions ) ) {
$archive = new PharData( $path );
$sql_count = $this->count_sql( $archive );
if ( $sql_count > 1 ) {
WP_CLI::error( 'There are more than one .sql file in the archive. Please provide only one .sql file.' );
} elseif ( 0 === $sql_count ) {
WP_CLI::error( 'There is no .sql file in the archive.' );
} else {
// Extract the SQL file and return the path.
WP_CLI::line( 'Extracting the archive...' );
$path = $this->extract_sql( $archive );
$this->validate_sql_file( $path );
}
} else {
WP_CLI::error( 'Please provide a .sql file or an archive (gz, or zip) with one .sql file inside.' );
}
}
}
/**
* Count how many sql files are in the archive.
*
* @param string $archive Path to the archive containing SQL file.
*/
private function count_sql( $archive ) {
$sql_count = 0;
foreach ( $archive as $file ) {
$ext = pathinfo( $file, PATHINFO_EXTENSION );
if ( 'sql' === $ext ) {
$sql_count++;
}
}
return $sql_count;
}
/**
* Extract the SQL in the archive.
*
* @param string $archive Path to the archive containing SQL file.
*/
private function extract_sql( $archive ) {
foreach ( $archive as $file ) {
$ext = pathinfo( $file, PATHINFO_EXTENSION );
if ( 'sql' === $ext ) {
try {
$archive->extractTo( '.', basename( $file ) );
} catch ( Exception $e ) {
WP_CLI::error( 'We are unable to extract the archive. ' . $e->getMessage() );
}
}
}
return $file;
}
/**
* Remove all comments from the SQL file
*
* @param string $sql_file The SQL data.
*
* return string $sql_file The SQl data without any comments.
*/
private function remove_comment( $sql_file ) {
$sql_file = preg_replace( '/^#.*$/', '', $sql_file );
// Remove /*! */; comments.
$sql_file = preg_replace( '#/\*!(.|[\r\n])*?\*/;#', '', $sql_file );
// Remove /* */ comments.
$sql_file = preg_replace( '#/\*(.|[\r\n])*?\*/#', '', $sql_file );
// Remove # style comments.
$sql_file = preg_replace( '/\n{2,}/', '', preg_replace( '/^#.*$/m', '', $sql_file ) );
return $sql_file;
}
/**
* Transform values of an INSERT statement into an array of entries
*
* @param string $query The SQL query.
* @param string $table The table we want to get the values from.
*
* return array $blogs_array The entries of the specific table as an array or an empty array.
*/
private function insert_to_array( $query, $table ) {
$blogs_array = array();
if ( preg_match( '/INSERT INTO.*' . $table . '/i', $query ) ) {
preg_match_all( '/\((?>[^)(]+|(?R))*+\)/', $query, $matches );
$indexes = preg_replace( '/[(\'" `)]/', '', $matches[0][0] );
$indexes = explode( ',', $indexes );
unset( $matches[0][0] );
$blogs_array_size = count( $blogs_array );
foreach ( $matches[0] as $key => $match ) {
$entry = trim( $match, '/[()]/' );
$entry = explode( ',', $entry );
foreach ( $indexes as $index_key => $index ) {
if ( isset( $entry[ $index_key ] ) ) {
$blogs_array[ $blogs_array_size + $key ][ $index ] = trim( $entry[ $index_key ], '/[\'" `]/' );
} else {
$blogs_array[ $blogs_array_size + $key ][ $index ] = null;
}
}
}
}
return $blogs_array;
}
/**
* Process the SQL file trought multiple validation steps.
*
* @param string $file Path to SQL file.
* @param string $delimiter The delimiter used in the SQL file, default ";".
*/
private function validate_sql_file( $file, $delimiter = ';' ) {
set_time_limit( 0 );
if ( is_file( $file ) === true ) {
$file = fopen( $file, 'r' );
if ( is_resource( $file ) === true ) {
$query = array();
while ( feof( $file ) === false ) {
$query[] = fgets( $file );
if ( preg_match( '/' . preg_quote( $delimiter, '/' ) . '$/m', end( $query ) ) ) {
$query = trim( implode( '', $query ) );
$query = $this->remove_comment( $query );
$this->get_drop_tables( $query );
$this->get_create_tables( $query );
$this->get_charset( $query );
$this->get_database_statements( $query );
$this->get_options( $query );
$this->get_blogs( $query );
while ( ob_get_level() > 0 ) {
ob_end_flush();
}
flush();
}
if ( is_string( $query ) === true ) {
$query = array();
}
}
$this->validate_prefix( $this->drop_tables, $this->create_tables );
$this->validate_matching_drop( $this->drop_tables, $this->create_tables );
$this->validate_charset( $this->charsets );
$this->validate_drop_table( $this->drop_tables );
$this->validate_create_table( $this->create_tables );
$this->validate_database_statements( $this->database_statements );
$this->validate_options_entries( $this->options_entries );
WP_CLI::line( '' );
WP_CLI::confirm( WP_CLI::colorize( '%yIs the provided database for a multisite WordPress?%n' ) );
$this->validate_drop_table_multisite( $this->drop_tables );
$this->validate_create_table_multisite( $this->create_tables );
$this->validate_blogs_entries( $this->blogs_entries );
return fclose( $file );
}
} else {
WP_CLI::error( "We can't find the SQL file" );
}
}
/**
* Check the drop tables that are set up in the SQL file.
*
* @param string $sql_file The SQL data.
*/
private function get_drop_tables( $sql_file ) {
// Check for tables with wp_ prefix.
preg_match( '/DROP TABLE (IF EXISTS )?([`\'"]?.*wp_.*[`\'"]?);/', $sql_file, $wp_match );
if ( isset( $wp_match[2] ) && ! empty( $wp_match[2] ) ) {
$this->drop_tables['wp'][] = trim( $wp_match[2], '/[\'" `]/' );
}
// Check for tables with prefix that is not wp_.
preg_match( '/DROP TABLE (IF EXISTS )?([`\'"]?(?!.*wp_).*[`\'"]?);/', $sql_file, $nonwp_match );
if ( isset( $nonwp_match[2] ) && ! empty( $nonwp_match[2] ) ) {
$this->drop_tables['nonwp'][] = trim( $nonwp_match[2], '/[\'" `]/' );
}
}
/**
* Check the create tables that are set up in the SQL file.
*
* @param string $sql_file The SQL data.
*/
private function get_create_tables( $sql_file ) {
// Check for tables with wp_ prefix.
preg_match( '/CREATE TABLE ([`\'"]?.*wp_.*[`\'"]?) \(/', $sql_file, $wp_match );
if ( isset( $wp_match[1] ) && ! empty( $wp_match[1] ) ) {
$this->create_tables['wp'][] = trim( $wp_match[1], '/[\'" `]/' );
}
// Check for tables with prefix that is not wp_.
preg_match( '/CREATE TABLE ([`\'"]?(?!.*wp_).*[`\'"]?) \(/', $sql_file, $nonwp_match );
if ( isset( $nonwp_match[1] ) && ! empty( $nonwp_match[1] ) ) {
$this->create_tables['nonwp'][] = trim( $nonwp_match[1], '/[\'" `]/' );
}
}
/**
* Check for the charset and display warnings if not set to UTF8MB4.
*
* @param string $sql_file The SQL data.
*/
private function get_charset( $sql_file ) {
if ( preg_match( '/CHARSET(=| SET )(\w*)/', $sql_file, $match ) ) {
$this->charsets[] = $match[2];
}
}
/**
* Display a warning if there is a CREATE or DROP DATABASE statement.
*
* @param string $sql_file The SQL data.
*/
private function get_database_statements( $sql_file ) {
if ( preg_match( '/^CREATE DATABASE/i', $sql_file ) ) {
$this->database_statements[] = $sql_file;
}
if ( preg_match( '/^DROP DATABASE/i', $sql_file ) ) {
$this->database_statements[] = $sql_file;
}
}
/**
* Fill the $options_entries array with the values of wp_options table.
*
* @param string $query The SQL data.
*/
private function get_options( $query ) {
if ( $this->insert_to_array( $query, 'wp_options' ) ) {
$this->options_entries = array_merge( $this->options_entries, $this->insert_to_array( $query, 'wp_options' ) );
}
}
/**
* Fill the $blogs_entries array with the values of wp_options table.
*
* @param string $query The SQL data.
*/
private function get_blogs( $query ) {
if ( $this->insert_to_array( $query, 'wp_blogs' ) ) {
$this->blogs_entries = array_merge( $this->blogs_entries, $this->insert_to_array( $query, 'wp_blogs' ) );
}
}
/**
* Check the prefix that are set up in the SQL file.
*
* @param array $drop_tables All tables that have a DROP TABLE statement.
* @param array $create_tables All tables that have a CREATE TABLE statement.
*/
private function validate_prefix( $drop_tables, $create_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking for table prefix...' );
if ( count( $drop_tables['nonwp'] ) > 0 ) {
WP_CLI::warning( 'We have found some DROP TABLE statements with a custom prefix.' );
WP_CLI::error_multi_line( $drop_tables['nonwp'] );
}
if ( count( $create_tables['nonwp'] ) > 0 ) {
WP_CLI::warning( 'We have found some CREATE TABLE statements with a custom prefix.' );
WP_CLI::error_multi_line( $create_tables['nonwp'] );
}
}
/**
* Check the prefix that are set up in the SQL file.
*
* @param array $drop_tables All tables that have a DROP TABLE statement.
* @param array $create_tables All tables that have a CREATE TABLE statement.
*/
private function validate_matching_drop( $drop_tables, $create_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking if all CREATE TABLE statements have a matching DROP TABLE statement...' );
$matching_error = 0;
foreach ( $create_tables['wp'] as $create_table ) {
if ( ! in_array( $create_table, $drop_tables['wp'] ) ) {
WP_CLI::warning( 'There is a missing drop statement for ' . $create_table );
$matching_error++;
}
}
foreach ( $create_tables['nonwp'] as $create_table ) {
if ( ! in_array( $create_table, $drop_tables['nonwp'] ) ) {
WP_CLI::warning( 'There is a missing drop statement for ' . $create_table );
$matching_error++;
}
}
if ( 0 === $matching_error ) {
WP_CLI::success( 'We have found a matching DROP TABLE statement for each table.' );
}
}
/**
* Check for the charset and display warnings if not set to UTF8MB4.
*
* @param array $charsets Charsets found in the SQL file.
*/
private function validate_charset( $charsets ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking for charset...' );
if ( count( $charsets ) > 0 ) {
if ( ! empty( preg_grep( '/(utf8mb4|latin1|^utf8$)/i', $charsets ) ) ) {
if ( ! empty( preg_grep( '/utf8mb4/i', $charsets ) ) ) {
WP_CLI::success( 'We have found some UTF8MB4 charsets' );
}
if ( ! empty( preg_grep( '/(latin1|^utf8$)/i', $charsets ) ) ) {
WP_CLI::warning( 'We have found some latin1 or UTF8 charsets that should be converted to UTF8MB4' );
}
}
if ( ! empty( preg_grep( '/^(?!.*(utf8mb4|latin1|^utf8$)).*/i', $charsets ) ) ) {
WP_CLI::warning( 'We have found some custom charset, please check your SQL file' );
}
}
}
/**
* Check if there is DROP TABLE statements
*
* @param array $drop_tables Tables with a drop statement found in the file.
*/
private function validate_drop_table( $drop_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Validating WP core DROP TABLE statements...' );
if ( count( $drop_tables['wp'] ) ) {
if ( array_diff( $this->core_tables, $drop_tables['wp'] ) ) {
WP_CLI::warning( 'Missing core drop statement: ' );
WP_CLI::error_multi_line( array_diff( $this->core_tables, $drop_tables['wp'] ) );
} else {
WP_CLI::success( 'We have found all required DROP TABLE statements for core tables' );
}
} else {
WP_CLI::warning( 'There is no DROP TABLE statement for all core tables' );
}
}
/**
* Check if there is CREATE TABLE statements.
*
* @param array $create_tables Tables with a create statement found in the file.
*/
private function validate_create_table( $create_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Validating WP core CREATE TABLE statements...' );
if ( count( $create_tables['wp'] ) ) {
if ( array_diff( $this->core_tables, $create_tables['wp'] ) ) {
WP_CLI::warning( 'Missing core create statement: ' );
WP_CLI::error_multi_line( array_diff( $this->core_tables, $create_tables['wp'] ) );
} else {
WP_CLI::success( 'We have found all required CREATE TABLE statements for core tables' );
}
} else {
WP_CLI::warning( 'There is no CREATE TABLE statement for all core tables' );
}
}
/**
* Display a warning if there is a CREATE or DROP DATABASE statement.
*
* @param array $database_statements Database statements found in the sql file.
*/
private function validate_database_statements( $database_statements ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking for CREATE or DROP DATABASE statements...' );
if ( count( $database_statements ) > 0 ) {
WP_CLI::warning( 'We have found some unwanted statemnents: ' );
WP_CLI::error_multi_line( $database_statements );
} else {
WP_CLI::success( 'There is no DATABASE statements' );
}
}
/**
* Check if there is DROP TABLE statements on a multisite SQL import
*
* @param array $drop_tables Tables with a drop statement found in the file.
*/
private function validate_drop_table_multisite( $drop_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Validating WP multisite DROP TABLE statements...' );
if ( count( $drop_tables['wp'] ) ) {
if ( array_diff( $this->multisite_tables, $drop_tables['wp'] ) ) {
WP_CLI::warning( 'Missing multisite drop statement: ' );
WP_CLI::error_multi_line( array_diff( $this->multisite_tables, $drop_tables['wp'] ) );
} else {
WP_CLI::success( 'We have found all required DROP TABLE statements for multisite tables' );
}
} else {
WP_CLI::warning( 'There is no DROP TABLE statement for all multisite tables' );
}
}
/**
* Check if there is CREATE TABLE statements.
*
* @param array $create_tables Tables with a create statement found in the file.
*/
private function validate_create_table_multisite( $create_tables ) {
WP_CLI::line( '' );
WP_CLI::line( 'Validating WP multisite CREATE TABLE statements...' );
if ( count( $create_tables['wp'] ) ) {
if ( array_diff( $this->multisite_tables, $create_tables['wp'] ) ) {
WP_CLI::warning( 'Missing multisite create statement: ' );
WP_CLI::error_multi_line( array_diff( $this->multisite_tables, $create_tables['wp'] ) );
} else {
WP_CLI::success( 'We have found all required CREATE TABLE statements for multisite tables' );
}
} else {
WP_CLI::warning( 'There is no create statement for multisite tables' );
}
}
/**
* Check entries of the wp_blogs table.
*
* @param array $blogs_entries The entries of the wp_blogs table as an array.
*/
private function validate_blogs_entries( $blogs_entries ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking for wp_blogs table...' );
$lookfor = array( 'blog_id', 'site_id', 'domain', 'path' );
// display tables.
WP_CLI::line( 'We have found ' . count( $blogs_entries ) . ' entries.' );
WP_CLI\Utils\format_items( 'table', $blogs_entries, $lookfor );
foreach ( $blogs_entries as $entry ) {
if ( ! isset( $entry['domain'] ) || '' === $entry['domain'] ) {
WP_CLI::warning( 'No domain set up for blog_id ' . $entry['blog_id'] );
}
if ( ! isset( $entry['path'] ) || '' === $entry['path'] ) {
WP_CLI::warning( 'No path set up for blog_id ' . $entry['blog_id'] );
}
}
}
/**
* Check siteurl and home of the wp_otions table.
*
* @param array $options_entries The entries of the wp_options table as an array.
*/
private function validate_options_entries( $options_entries ) {
WP_CLI::line( '' );
WP_CLI::line( 'Checking for siteurl and home options...' );
$lookfor = array( 'option_name', 'option_value' );
if ( ! empty( $options_entries ) ) {
foreach ( $options_entries as $key => $entry ) {
if ( 'siteurl' !== $entry['option_name'] && 'home' !== $entry['option_name'] ) {
unset( $options_entries[ $key ] );
}
}
WP_CLI::line( 'We have found ' . count( $options_entries ) . ' entries.' );
WP_CLI\Utils\format_items( 'table', $options_entries, $lookfor );
} else {
WP_CLI::warning( 'Unable to find the wp_options table.' );
}
}
}
WP_CLI::add_command( 'validate-sql', 'Validate_SQL_WPCLI' );
}