v0.2.0
This commit is contained in:
commit
8a85a4d9af
1 changed files with 3258 additions and 0 deletions
|
|
@ -0,0 +1,158 @@
|
|||
<?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 $log Log data.
|
||||
* @return array
|
||||
*/
|
||||
public static function build_sections( array $log ): array {
|
||||
$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 $params Query parameters.
|
||||
* @return array
|
||||
*/
|
||||
private static function group_query_params( array $params ): array {
|
||||
$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 $params Body parameters.
|
||||
* @return array
|
||||
*/
|
||||
private static function group_body_params( array $params ): array {
|
||||
$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( $translations );
|
||||
} else {
|
||||
$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 $params Parameters (will be mutated).
|
||||
* @param string $prefix Prefix name.
|
||||
* @return array
|
||||
*/
|
||||
private static function pluck_bracketed( array &$params, string $prefix ): array {
|
||||
$result = array();
|
||||
|
||||
foreach ( $params as $key => $value ) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue