-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathsummarize-scan.php
executable file
·77 lines (65 loc) · 2.04 KB
/
summarize-scan.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
#!/usr/bin/env php
<?php
$handle = fopen( $argv[1], 'r' ) or die;
$scan_info = array();
$current_plugin = '';
$current_count = 0;
$max_name_length = 0;
function save_current_plugin_info() {
global $scan_info, $current_plugin, $current_count, $max_name_length;
if ( $current_count > 0 ) {
array_push( $scan_info, array(
'plugin_name' => $current_plugin,
'matches' => $current_count,
) );
$current_count = 0;
$max_name_length = max( strlen( $current_plugin ), $max_name_length );
}
}
function get_http_response_code( $url ) {
$headers = get_headers( $url);
return substr( $headers[0], 9, 3 );
}
while ( ( $line = fgets( $handle ) ) !== false ) {
if ( preg_match( '#^(plugins/)?([^/]+)/#', $line, $match ) ) {
$plugin = $match[2];
if ( $plugin !== $current_plugin ) {
save_current_plugin_info();
$current_plugin = $plugin;
}
$current_count++;
}
}
fclose( $handle );
save_current_plugin_info();
$num_results = count( $scan_info );
fwrite( STDERR, sprintf(
"%d matching plugin%s\n",
$num_results,
( $num_results === 1 ? '' : 's' )
) );
echo 'Matches ' . str_pad( 'Plugin', $max_name_length - 3 ) . "Active installs\n";
echo '======= ' . str_pad( '======', $max_name_length - 3 ) . "===============\n";
foreach ( $scan_info as $plugin ) {
ini_set( 'user_agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:80.0) Gecko/20100101 Firefox/80.0' );
$api_url = "https://api.wordpress.org/plugins/info/1.1/?action=plugin_information&request[slug]=$plugin[plugin_name]&request[fields][active_installs]=1";
if ( get_http_response_code( $api_url ) != "200" ){
$result = false;
} else {
$result = json_decode( $api_url );
}
if ( $result ) {
$active_installs = str_pad(
number_format( $result->active_installs ),
9, ' ', STR_PAD_LEFT
) . '+';
} else {
// The plugins API returns `null` for nonexistent/removed plugins
$active_installs = ' REMOVED';
}
echo str_pad( $plugin['matches'], 7, ' ', STR_PAD_LEFT )
. ' '
. str_pad( $plugin['plugin_name'], $max_name_length )
. ' '
. "$active_installs\n";
}