robotstxt-telemetry/includes/class-robotstxt-telemetry-analysis.php
2026-08-20 14:31:43 +00:00

225 lines
5.5 KiB
PHP

<?php
/**
* Telemetry analysis helpers.
*
* @package RobotstxtTelemetry
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Robotstxt_Telemetry_Analysis' ) ) {
/**
* Class Robotstxt_Telemetry_Analysis
*/
class Robotstxt_Telemetry_Analysis {
/**
* Build analysis sections for a log entry.
*
* @param array<string,mixed> $log Log data.
* @return array<string,string>
*/
public static function build_sections( $log ) {
$host = isset( $log['host'] ) && is_string( $log['host'] ) ? strtolower( $log['host'] ) : '';
if ( 'api.wordpress.org' === $host ) {
return Robotstxt_Telemetry_Analysis_Api_WordPress_Org::build_sections( $log );
}
return self::build_generic_sections( $log );
}
/**
* Build generic analysis sections.
*
* @param array<string,mixed> $log Log data.
* @return array<string,string>
*/
private static function build_generic_sections( $log ) {
$sections = array();
$query_params = self::extract_query_params( $log );
if ( ! empty( $query_params ) ) {
$sections[ __( 'URL Parameters', 'robotstxt-telemetry' ) ] = self::format_params_lines( $query_params );
}
$body_params = self::extract_body_params( $log );
if ( ! empty( $body_params ) ) {
$sections[ __( 'Body Parameters', 'robotstxt-telemetry' ) ] = self::format_params_lines( $body_params );
}
return $sections;
}
/**
* Extract parameters from the URL query string.
*
* Prefers the redacted query_json column over re-parsing the raw URL.
*
* @param array<string,mixed> $log Log data.
* @return array<int|string,mixed>
*/
public static function extract_query_params( $log ) {
$url = isset( $log['url'] ) && is_string( $log['url'] ) ? $log['url'] : '';
$query_json = isset( $log['query_json'] ) && is_string( $log['query_json'] ) ? $log['query_json'] : '';
if ( '' === $url && '' === $query_json ) {
return array();
}
if ( '' !== $query_json ) {
$decoded = json_decode( $query_json, true );
if ( is_array( $decoded ) && ! empty( $decoded ) ) {
return self::normalize_params( $decoded );
}
}
$parts = explode( '?', $url, 2 );
$query = isset( $parts[1] ) ? $parts[1] : '';
if ( '' === $query ) {
return array();
}
return self::parse_query_string_flat( $query );
}
/**
* Extract parameters from the body.
*
* @param array<string,mixed> $log Log data.
* @return array<int|string,mixed>
*/
public static function extract_body_params( $log ) {
$body_json = isset( $log['body_json'] ) && is_string( $log['body_json'] ) ? $log['body_json'] : '';
$raw_body = isset( $log['raw_body'] ) && is_string( $log['raw_body'] ) ? $log['raw_body'] : '';
if ( '' !== $body_json ) {
$decoded = json_decode( $body_json, true );
if ( is_array( $decoded ) ) {
return self::normalize_params( $decoded );
}
if ( is_string( $decoded ) ) {
return self::parse_query_string_flat( $decoded );
}
}
if ( '' !== $raw_body ) {
return self::parse_query_string_flat( $raw_body );
}
return array();
}
/**
* Normalize a decoded parameter array to integer or string keys.
*
* @param array<mixed,mixed> $params Decoded parameters.
* @return array<int|string,mixed>
*/
public static function normalize_params( array $params ) {
$normalized = array();
foreach ( $params as $key => $value ) {
$normalized[ $key ] = $value;
}
return $normalized;
}
/**
* Parse a query string into a flat key/value array.
*
* @param string $query Query string.
* @return array<string,string|array<int,string>>
*/
public static function parse_query_string_flat( $query ) {
$params = array();
$pairs = explode( '&', $query );
foreach ( $pairs as $pair ) {
if ( '' === $pair ) {
continue;
}
$parts = explode( '=', $pair, 2 );
$key = rawurldecode( $parts[0] );
$value = isset( $parts[1] ) ? rawurldecode( $parts[1] ) : '';
if ( '' === $key ) {
continue;
}
if ( isset( $params[ $key ] ) ) {
if ( ! is_array( $params[ $key ] ) ) {
$params[ $key ] = array( $params[ $key ] );
}
$params[ $key ][] = $value;
continue;
}
$params[ $key ] = $value;
}
return $params;
}
/**
* Format parameters as line-per-entry output.
*
* @param array<int|string,mixed> $params Parameters.
* @return string
*/
public static function format_params_lines( $params ) {
$lines = array();
$flatten = self::flatten_params( $params );
foreach ( $flatten as $key => $value ) {
if ( is_array( $value ) ) {
$value = wp_json_encode( $value );
}
if ( null === $value ) {
$value = '';
} elseif ( is_scalar( $value ) ) {
$value = (string) $value;
}
if ( is_string( $value ) ) {
$lines[] = sprintf( '%s=%s', $key, $value );
}
}
return implode( "\n", $lines );
}
/**
* Flatten nested parameters into bracketed keys.
*
* @param array<int|string,mixed> $params Parameters to flatten.
* @param string|null $prefix Current prefix.
* @return array<string,mixed>
*/
public static function flatten_params( $params, $prefix = null ) {
$flat = array();
foreach ( $params as $key => $value ) {
$key_string = is_int( $key ) ? (string) $key : $key;
$full_key = null === $prefix ? $key_string : $prefix . '[' . $key_string . ']';
if ( is_array( $value ) ) {
$flat = array_merge( $flat, self::flatten_params( $value, $full_key ) );
continue;
}
$flat[ $full_key ] = $value;
}
return $flat;
}
}
}