| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
/** |
| 8 |
* Selects bounded malformed fixed-sink lines for support evidence. |
| 9 |
* |
| 10 |
* Corrupt records cannot participate in request ranking because they have no |
| 11 |
* readable request identity. They still prove that the durable sink was |
| 12 |
* reached, so a small newest-first reserve keeps them without letting one |
| 13 |
* unbounded line consume the support payload. |
| 14 |
*/ |
| 15 |
final class ABJ_404_Solution_MalformedCheckpointEvidence { |
| 16 |
|
| 17 |
/** Keep the newest bounded corruption samples outside ordinary ranking. */ |
| 18 |
private const MAX_RESERVED_LINES = 4; |
| 19 |
|
| 20 |
/** A corrupt line must never consume the bounded support payload by itself. */ |
| 21 |
private const MAX_RESERVED_LINE_BYTES = 1024; |
| 22 |
|
| 23 |
private const TRUNCATION_SUFFIX = '...[malformed line truncated]'; |
| 24 |
|
| 25 |
/** |
| 26 |
* @param array<int, string> $lines JSONL lines, oldest first. |
| 27 |
* @return array<int, string> |
| 28 |
*/ |
| 29 |
public static function select(array $lines): array { |
| 30 |
$selected = array(); |
| 31 |
foreach ($lines as $line) { |
| 32 |
if (is_array(json_decode($line, true))) { |
| 33 |
continue; |
| 34 |
} |
| 35 |
if (strlen($line) > self::MAX_RESERVED_LINE_BYTES) { |
| 36 |
$prefixBytes = self::MAX_RESERVED_LINE_BYTES |
| 37 |
- strlen(self::TRUNCATION_SUFFIX); |
| 38 |
$line = substr($line, 0, max(0, $prefixBytes)) |
| 39 |
. self::TRUNCATION_SUFFIX; |
| 40 |
} |
| 41 |
$selected[] = $line; |
| 42 |
if (count($selected) > self::MAX_RESERVED_LINES) { |
| 43 |
array_shift($selected); |
| 44 |
} |
| 45 |
} |
| 46 |
return $selected; |
| 47 |
} |
| 48 |
} |
| 49 |
|