forked from Automattic/vip-go-ci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.php
879 lines (788 loc) · 18.6 KB
/
misc.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
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
<?php
/**
* Misc functions for vip-go-ci.
*
* @package Automattic/vip-go-ci
*/
declare(strict_types=1);
/**
* Set how to deal with errors:
* Report all errors, and display them.
*
* @return void
*/
function vipgoci_set_php_error_reporting() :void {
ini_set( 'error_log', '' ); // phpcs:ignore WordPress.PHP.IniSet
error_reporting( E_ALL );
ini_set( 'display_errors', 'on' ); // phpcs:ignore WordPress.PHP.IniSet
}
/**
* Set up to alarm when maximum execution time of
* vip-go-ci is reached. Will call exit() when
* alarm goes off.
*
* @param int $max_exec_time Maximum execution time in seconds.
* @param string $commit_identifier Identifier for the commit.
*
* @return void
*/
function vipgoci_set_maximum_exec_time(
int $max_exec_time = 600,
string $commit_identifier = ''
) :void {
static $has_been_invoked = false;
/*
* Ensure the function is only called
* once per run.
*/
if ( true === $has_been_invoked ) {
vipgoci_log(
'Cannot set maximum execution time twice, ignoring'
);
return;
}
$has_been_invoked = true;
/*
* Enable async signal handlers
*/
pcntl_async_signals( true );
vipgoci_log(
'Setting maximum execution time',
array(
'max_exec_time' => $max_exec_time,
)
);
/*
* Set up signal handler.
*/
vipgoci_log(
'Setting up alarm signal handler and setting up alarm',
);
/*
* Handle signals for SIGALRM only;
* log and call exit().
*/
pcntl_signal(
SIGALRM,
function ( $signo ) use ( $commit_identifier ) {
if ( SIGALRM === $signo ) {
vipgoci_sysexit(
'Maximum execution time reached ' .
( empty( $commit_identifier ) ?
'' :
'(' . $commit_identifier . ').' ),
array(),
VIPGOCI_EXIT_EXEC_TIME,
true // log to IRC.
);
}
}
);
/*
* Send alarm after max-exec-time
*/
pcntl_alarm( $max_exec_time );
}
/**
* Add value to array, if not found there already.
*
* @param array $arr Target array.
* @param mixed $value Value to add, if not already exists in array.
* @param bool $strict If to perform strict comparison, true by default.
*
* @return void
*/
function vipgoci_array_push_uniquely(
array &$arr,
mixed $value,
bool $strict = true
) :void {
if ( false === in_array(
$value,
$arr, // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
$strict
) ) {
$arr[] = $value;
}
}
/**
* Check if a particular set of fields exist
* in a target array and if their values match a set
* given. Will return an array describing
* which items of the array contain all the fields
* and the matching values.
*
* Example:
* $fields_arr = array(
* 'a' => 920,
* 'b' => 700,
* );
*
* $data_arr = array(
* array(
* 'a' => 920,
* 'b' => 500,
* 'c' => 0,
* 'd' => 1,
* ...
* ),
* array(
* 'a' => 920,
* 'b' => 700,
* 'c' => 0,
* 'd' => 2,
* ...
* ),
* );
*
* $res = vipgoci_find_fields_in_array(
* $fields_arr,
* $data_arr
* );
*
* $res will be:
* array(
* 0 => false,
* 1 => true,
* );
*
* ***
*
* @param array $fields_arr Associative array (see example above).
* @param array $data_arr Array of associative arrays (see example above).
*
* @return array Array of keys and values, values true or false.
*/
function vipgoci_find_fields_in_array(
array $fields_arr,
array $data_arr
) :array {
$res_arr = array();
$data_arr_cnt = count( $data_arr );
for (
$data_item_cnt = 0;
$data_item_cnt < $data_arr_cnt;
$data_item_cnt++
) {
$res_arr[ $data_item_cnt ] = 0;
foreach ( $fields_arr as $field_name => $field_values ) {
if ( ! array_key_exists( $field_name, $data_arr[ $data_item_cnt ] ) ) {
continue;
}
foreach ( $field_values as $field_value_item ) {
if ( $data_arr[ $data_item_cnt ][ $field_name ] === $field_value_item ) {
$res_arr[ $data_item_cnt ]++;
/*
* Once we find a match, stop searching.
* This is to safeguard against any kind of
* multiple matches (which though are nearly
* impossible).
*/
break;
}
}
}
$res_arr[ $data_item_cnt ] = (
count( array_keys( $fields_arr ) )
===
$res_arr[ $data_item_cnt ]
);
}
return $res_arr;
}
/**
* Check if any of the strings in $arr_items is a substring
* of $str. Comparison is case insensitive.
*
* @param array $arr_items Array of items to check against $str.
* @param string $str String to use to check.
* @param bool $start_only If to match at beginning of comparison string only.
*
* @return bool True when substring is found, else false.
*/
function vipgoci_string_found_in_substrings_array(
array $arr_items,
string $str,
bool $start_only = false
) :bool {
$found = false;
foreach ( $arr_items as $arr_item ) {
if ( true === $start_only ) {
if ( 0 === stripos( $arr_item, $str ) ) {
$found = true;
break;
}
} else {
if ( false !== stripos( $arr_item, $str ) ) {
$found = true;
break;
}
}
}
return $found;
}
/**
* Convert a string that contains "true", "false" or
* "null" to a variable of that type.
*
* @param string $str String to convert.
*
* @return string|bool|null String, bool (true or false) or null.
*/
function vipgoci_convert_string_to_type(
string $str
) :string|bool|null {
switch ( $str ) {
case 'true':
$ret = true;
break;
case 'false':
$ret = false;
break;
case 'null':
$ret = null;
break;
default:
$ret = $str;
break;
}
return $ret;
}
/**
* Will return beginning of a string, or if not a string, the item
* unchanged. Useful for example in logging, when it is not
* suitable to log very long strings.
*
* @param mixed $data_for_preview String to shorten and return. If not a string, will return item unchanged.
* @param int $preview_length Length of string to return.
*
* @return mixed Shortened string, else unchanged data.
*/
function vipgoci_preview_string(
mixed $data_for_preview,
int $preview_length = 100
): mixed {
if ( ! is_string( $data_for_preview ) ) {
return $data_for_preview;
}
return substr(
$data_for_preview,
0,
$preview_length
);
}
/**
* Round items in an array to a certain precision, return
* new array with results. Essentially a wrapper around the
* PHP round() function.
*
* @param array $arr Array to process.
* @param int $precision Precision to use.
* @param int $mode Mode for round().
*
* @return array Array of items rounded to precision.
*/
function vipgoci_round_array_items(
array $arr,
int $precision = 0,
int $mode = PHP_ROUND_HALF_UP
): array {
return array_map(
function( $item ) use ( $precision, $mode ) {
return round( $item, $precision, $mode );
},
$arr
);
}
/**
* Create a temporary file, and return the
* full-path to the file.
*
* @param string $file_name_prefix Temporary file name prefix.
* @param null|string $file_name_extension File extention of temporary file, null for no extension.
* @param string $file_contents Contents of resulting file.
*
* @return string Path to the file
*/
function vipgoci_save_temp_file(
string $file_name_prefix,
null|string $file_name_extension = null,
string $file_contents = ''
) :string {
// Determine name for temporary-file.
$temp_file_save_status = tempnam(
sys_get_temp_dir(),
$file_name_prefix
);
$temp_file_name = $temp_file_save_status;
/*
* If temporary file should have an extension,
* make that happen by renaming the currently existing
* file.
*/
if (
( null !== $file_name_extension ) &&
( false !== $temp_file_name )
) {
$temp_file_name_old = $temp_file_name;
$temp_file_name .= '.' . $file_name_extension;
if ( true !== rename(
$temp_file_name_old,
$temp_file_name
) ) {
vipgoci_sysexit(
'Unable to rename temporary file',
array(
'temp_file_name_old' => $temp_file_name_old,
'temp_file_name_new' => $temp_file_name,
),
VIPGOCI_EXIT_SYSTEM_PROBLEM
);
}
unset( $temp_file_name_old );
}
if ( false !== $temp_file_name ) {
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'save_temp_file' );
$temp_file_save_status = file_put_contents(
$temp_file_name,
$file_contents
);
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'save_temp_file' );
}
// Detect possible errors when saving the temporary file.
if ( false === $temp_file_save_status ) {
vipgoci_sysexit(
'Could not save file to disk, got ' .
'an error. Exiting...',
array(
'temp_file_name' => $temp_file_name,
),
VIPGOCI_EXIT_SYSTEM_PROBLEM
);
}
return $temp_file_name;
}
/**
* Determine file-extension of a particular file,
* and return it in lowercase. If it can not be
* determined, return null.
*
* @param string $file_name File name whose file-extention to get.
*
* @return string File extension.
*/
function vipgoci_file_extension_get(
string $file_name
) :string|null {
$file_extension = pathinfo(
$file_name,
PATHINFO_EXTENSION
);
if ( empty( $file_extension ) ) {
return null;
}
$file_extension = strtolower(
$file_extension
);
return $file_extension;
}
/**
* Check if directory $dir matches one of the directories
* found in $files_arr.
*
* @param array $files_arr List of paths to files.
* @param string $dir_path Directory path to check if matches $files_arr.
*
* @return bool True when directory is found, else false.
*/
function vipgoci_directory_found_in_file_list(
array $files_arr,
string $dir_path
) :bool {
/*
* We get list of file paths; convert to
* directory paths.
*/
$dirs_arr = array_unique(
array_map(
'dirname',
$files_arr
)
);
$res = array_filter(
$dirs_arr,
function ( $item ) use ( $dir_path ) {
if ( $item === $dir_path ) {
return true;
} else {
return str_starts_with(
$item,
$dir_path . '/'
);
}
}
);
return ( ! empty( $res ) );
}
/**
* Get "base" path of target directory along with the
* directory-name itself, skip any sub-directories.
*
* @param string $base_dir_path Base directory.
* @param string $target_dir_path Target directory - should include the base directory.
*
* @return null|string Returns null on failure, string with path on success.
* For example, when called with these parameters:
* - $base_dir = 'plugins';
* - $target_dir = 'plugins/my-plugin/dir1/subdir2';
* The function will return 'plugins/my-plugin'.
*/
function vipgoci_directory_path_get_dir_and_include_base(
string $base_dir_path,
string $target_dir_path
) :null|string {
// Trim directory paths.
$base_dir_path = trim( $base_dir_path, '/' );
$target_dir_path = trim( $target_dir_path, '/' );
// Ensure $target_dir starts with $base_dir.
if ( false === str_starts_with(
$target_dir_path,
$base_dir_path . '/'
) ) {
return null;
}
/*
* Count "/" in paths.
*/
$base_dir_path_slashes = substr_count(
$base_dir_path,
'/'
);
$target_dir_path_slashes = substr_count(
$target_dir_path,
'/'
);
// Do a sanity check.
if ( $target_dir_path_slashes < $base_dir_path_slashes ) {
vipgoci_sysexit(
'Internal error: $target_dir_path_slashes < $base_dir_path_slashes',
array(
'base_dir_path' => $base_dir_path,
'base_dir_path_slashes' => $base_dir_path_slashes,
'target_dir_path' => $target_dir_path,
'target_dir_path_slashes' => $target_dir_path_slashes,
),
VIPGOCI_EXIT_INTERNAL_ERROR
);
}
if ( 0 === $target_dir_path_slashes ) {
// No "/" found, return with error.
return null;
} elseif ( ( $target_dir_path_slashes - 1 ) <= $base_dir_path_slashes ) {
// Nothing to do.
return $target_dir_path;
} elseif ( ( $target_dir_path_slashes - 1 ) > $base_dir_path_slashes ) {
// Ensure we return base and plugin directory.
return dirname(
$target_dir_path,
( ( $target_dir_path_slashes - 1 ) - $base_dir_path_slashes )
);
}
}
/**
* Determine if the presented file has an
* allowable file-ending, if the file presented
* is in a directory that can be skipped or included
* for scanning.
*
* @param string $filename File name; is expected to be a relative path to the git-repository root.
* @param null|array $filter Filter to apply.
*
* @return bool False when file does not match filter criteria, else true.
*/
function vipgoci_filter_file_path(
string $filename,
null|array $filter
) :bool {
$file_info_extension = vipgoci_file_extension_get(
$filename
);
$file_dirs = pathinfo(
$filename,
PATHINFO_DIRNAME
);
/*
* If the file does not have an acceptable
* file-extension, flag it.
*/
$file_ext_match =
( null !== $filter ) &&
( isset( $filter['file_extensions'] ) ) &&
( ! in_array(
$file_info_extension,
$filter['file_extensions'],
true
) );
/*
* If path to the file contains any non-acceptable
* directory-names, skip it.
*/
$file_folders_match = false;
if (
( null !== $filter ) &&
( isset( $filter['skip_folders'] ) )
) {
/*
* Loop through all skip-folders.
*/
foreach (
$filter['skip_folders'] as $tmp_skip_folder_item
) {
/*
* Note: All 'skip_folder' options should lack '/' at the
* end and beginning.
*
* $filename we expect to be a relative path.
*/
$file_folders_match = strpos(
$filename,
$tmp_skip_folder_item . '/'
);
/*
* Check if the file matches any of the folders
* that are to be skipped -- note that we enforce
* that the folder has to be at the root of the
* path to be a match.
*/
if (
( false !== $file_folders_match ) &&
( is_numeric( $file_folders_match ) ) &&
( 0 === $file_folders_match )
) {
$file_folders_match = true;
break;
}
}
} elseif (
( null !== $filter ) &&
( isset( $filter['include_folders'] ) )
) {
/*
* Loop through all include-folders specified.
*/
foreach (
$filter['include_folders'] as $tmp_include_folder_item
) {
/*
* Note: All 'include_folders' options should lack '/' at the
* end and beginning.
*
* $filename we expect to be a relative path.
*/
$file_folders_match = strpos(
$filename,
$tmp_include_folder_item . '/'
);
/*
* If it's not a match, then that folder is to be skipped.
*
* There can only be 1 match with the filename so the
* moment that happens, we break out.
*/
if (
( false !== $file_folders_match ) &&
( is_numeric( $file_folders_match ) )
) {
$file_folders_match = false;
break;
} else {
$file_folders_match = true;
}
}
}
/*
* Do the actual skipping of file,
* if either of the conditions are fulfiled.
*/
if (
( true === $file_ext_match ) ||
( true === $file_folders_match )
) {
vipgoci_log(
'Skipping file that does not seem to be a file ' .
'matching filter-criteria',
array(
'filename' => $filename,
'filter' => $filter,
'matches' => array(
'file_ext_match' => $file_ext_match,
'file_folders_match' => $file_folders_match,
),
),
2
);
return false;
}
return true;
}
/**
* Recursively scan the git repository,
* returning list of files that exist in
* it, making sure to filter the result
*
* @param string $path Path to scan.
* @param bool $process_subdirectories If to process subdirectories.
* @param null|array $filter Filter to apply.
* @param null|string $base_path Internal only, should be null normally.
*
* @return array Array of files found.
*/
function vipgoci_scandir_git_repo(
string $path,
bool $process_subdirectories = true,
null|array $filter = null,
null|string $base_path = null
) :array {
$result = array();
vipgoci_log(
'Fetching git-tree using scandir()',
array(
'path' => $path,
'filter' => $filter,
'base_path' => $base_path,
),
2
);
/*
* If no base path is given,
* use $path. This will be used
* when making sure we do not
* accidentally filter by the filesystem
* outside of the git-repository (see below).
*/
if ( null === $base_path ) {
$base_path = $path;
}
vipgoci_runtime_measure( VIPGOCI_RUNTIME_START, 'git_repo_scandir' );
$cdir = scandir( $path );
vipgoci_runtime_measure( VIPGOCI_RUNTIME_STOP, 'git_repo_scandir' );
foreach ( $cdir as $key => $value ) {
if ( in_array(
$value,
array( '.', '..', '.git' ),
true
) ) {
// Skip '.' and '..'.
continue;
}
if ( is_dir(
$path . DIRECTORY_SEPARATOR . $value
) ) {
if ( false === $process_subdirectories ) {
// Not supposed to process subdirectories, skip.
continue;
}
/*
* A directory, traverse into, get files,
* amend the results
*/
$tmp_result = vipgoci_scandir_git_repo(
$path . DIRECTORY_SEPARATOR . $value,
$process_subdirectories,
$filter,
$base_path
);
foreach ( $tmp_result as $tmp_result_item ) {
$result[] = $value .
DIRECTORY_SEPARATOR .
$tmp_result_item;
}
continue;
}
/*
* Filter out files not with desired line-ending
* or are located in directories that should be
* ignored.
*/
if ( null !== $filter ) {
/*
* Remove the portion of the path
* that leads to the git repository,
* as we only want to filter by files in the
* git repository it self here. This is to
* make sure "skip_folders" filtering works
* correctly and does not accidentally take into
* consideration the path leading to the git repository.
*/
$file_path_without_git_repo = substr(
$path . DIRECTORY_SEPARATOR . $value,
strlen( $base_path ) + 1 // Relative path.
);
if ( false === vipgoci_filter_file_path(
$file_path_without_git_repo,
$filter
) ) {
continue;
}
}
// Not a directory, passed filter, save in array.
$result[] = $value;
}
return $result;
}
/**
* Sanitize a string, removing any whitespace-characters
* from the beginning and end, and transform to lowercase.
*
* @param string $str String to sanitize.
*
* @return string Sanitized string.
*/
function vipgoci_sanitize_string(
string $str
) :string {
return strtolower(
trim(
$str
)
);
}
/**
* Sanitize path, remove any of the specified prefixes
* if exist.
*
* @param string $path Path to process.
* @param array $prefixes Prefixes to remove from path.
*
* @return string Sanitized path, prefix removed if found.
*/
function vipgoci_sanitize_path_prefix(
string $path,
array $prefixes
): string {
foreach ( $prefixes as $prefix ) {
if ( 0 === strpos( $path, $prefix ) ) {
$path = substr(
$path,
strlen( $prefix )
);
break;
}
}
return $path;
}
/**
* Ensure string provided is in slug-format.
*
* @param string $str String to check.
*
* @return bool True when string is a valid slug, false otherwise.
*/
function vipgoci_validate_slug(
string $str
): bool {
return 1 === preg_match(
'/^[a-z0-9]+(-?[a-z0-9]+)*$/i',
$str
);
}