160 lines
4.3 KiB
PHP
160 lines
4.3 KiB
PHP
<?php
|
|
/**
|
|
* Telemetry analysis for api.wordpress.org.
|
|
*
|
|
* @package RobotstxtTelemetry
|
|
*/
|
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
exit;
|
|
}
|
|
|
|
if ( ! class_exists( 'Robotstxt_Telemetry_Analysis_Api_WordPress_Org' ) ) {
|
|
/**
|
|
* Class Robotstxt_Telemetry_Analysis_Api_WordPress_Org
|
|
*/
|
|
class Robotstxt_Telemetry_Analysis_Api_WordPress_Org {
|
|
|
|
/**
|
|
* Build analysis sections for api.wordpress.org.
|
|
*
|
|
* @param array<string,mixed> $log Log data.
|
|
* @return array<string,string>
|
|
*/
|
|
public static function build_sections( $log ) {
|
|
$sections = array();
|
|
|
|
$query_params = Robotstxt_Telemetry_Analysis::extract_query_params( $log );
|
|
if ( ! empty( $query_params ) ) {
|
|
$sections = array_merge( $sections, self::group_query_params( $query_params ) );
|
|
}
|
|
|
|
$body_params = Robotstxt_Telemetry_Analysis::extract_body_params( $log );
|
|
if ( ! empty( $body_params ) ) {
|
|
$sections = array_merge( $sections, self::group_body_params( $body_params ) );
|
|
}
|
|
|
|
return $sections;
|
|
}
|
|
|
|
/**
|
|
* Group query parameters.
|
|
*
|
|
* @param array<int|string,mixed> $params Query parameters.
|
|
* @return array<string,string>
|
|
*/
|
|
private static function group_query_params( $params ) {
|
|
$sections = array();
|
|
|
|
$core_keys = array(
|
|
'version',
|
|
'php',
|
|
'locale',
|
|
'mysql',
|
|
'local_package',
|
|
'blogs',
|
|
'users',
|
|
'multisite_enabled',
|
|
'initial_db_version',
|
|
);
|
|
|
|
$core = array();
|
|
foreach ( $core_keys as $key ) {
|
|
if ( array_key_exists( $key, $params ) ) {
|
|
$core[ $key ] = $params[ $key ];
|
|
unset( $params[ $key ] );
|
|
}
|
|
}
|
|
|
|
$extensions = self::pluck_bracketed( $params, 'extensions' );
|
|
$platform = self::pluck_bracketed( $params, 'platform_flags' );
|
|
$images = self::pluck_bracketed( $params, 'image_support' );
|
|
|
|
if ( ! empty( $core ) ) {
|
|
$sections[ __( 'Environment', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $core );
|
|
}
|
|
|
|
if ( ! empty( $extensions ) ) {
|
|
$sections[ __( 'Extensions', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $extensions );
|
|
}
|
|
|
|
if ( ! empty( $platform ) ) {
|
|
$sections[ __( 'Platform Flags', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $platform );
|
|
}
|
|
|
|
if ( ! empty( $images ) ) {
|
|
$sections[ __( 'Image Support', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $images );
|
|
}
|
|
|
|
if ( ! empty( $params ) ) {
|
|
$sections[ __( 'Other URL Parameters', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $params );
|
|
}
|
|
|
|
return $sections;
|
|
}
|
|
|
|
/**
|
|
* Group body parameters.
|
|
*
|
|
* @param array<int|string,mixed> $params Body parameters.
|
|
* @return array<string,string>
|
|
*/
|
|
private static function group_body_params( $params ) {
|
|
$sections = array();
|
|
|
|
if ( isset( $params['translations'] ) ) {
|
|
$translations = $params['translations'];
|
|
unset( $params['translations'] );
|
|
|
|
if ( is_string( $translations ) ) {
|
|
$decoded = json_decode( $translations, true );
|
|
if ( is_array( $decoded ) ) {
|
|
$translations = $decoded;
|
|
}
|
|
}
|
|
|
|
if ( is_array( $translations ) ) {
|
|
$sections[ __( 'Translations', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( Robotstxt_Telemetry_Analysis::normalize_params( $translations ) );
|
|
} elseif ( is_scalar( $translations ) ) {
|
|
$sections[ __( 'Translations', 'robotstxt-telemetry' ) ] = (string) $translations;
|
|
}
|
|
}
|
|
|
|
if ( ! empty( $params ) ) {
|
|
$sections[ __( 'Other Body Parameters', 'robotstxt-telemetry' ) ] = Robotstxt_Telemetry_Analysis::format_params_lines( $params );
|
|
}
|
|
|
|
return $sections;
|
|
}
|
|
|
|
/**
|
|
* Extract bracketed parameters into a nested array.
|
|
*
|
|
* @param array<int|string,mixed> $params Parameters (will be mutated).
|
|
* @param string $prefix Prefix name.
|
|
* @return array<string,mixed>
|
|
*/
|
|
private static function pluck_bracketed( &$params, $prefix ) {
|
|
$result = array();
|
|
|
|
foreach ( $params as $key => $value ) {
|
|
$key = (string) $key;
|
|
|
|
if ( 0 !== strpos( $key, $prefix . '[' ) ) {
|
|
continue;
|
|
}
|
|
|
|
$inner_key = substr( $key, strlen( $prefix ) + 1 );
|
|
$inner_key = rtrim( $inner_key, ']' );
|
|
if ( '' === $inner_key ) {
|
|
continue;
|
|
}
|
|
|
|
$result[ $inner_key ] = $value;
|
|
unset( $params[ $key ] );
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|
|
}
|