| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Extension\AbuseFilter\Parser\RuleCheckerStatus; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class RunnerData { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
private $matchedFilters; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private $profilingData; |
| 23 |
|
| 24 |
|
| 25 |
private $totalRuntime; |
| 26 |
|
| 27 |
|
| 28 |
private $totalConditions; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public function __construct( |
| 37 |
array $matchedFilters = [], |
| 38 |
array $profilingData = [], |
| 39 |
float $totalRuntime = 0.0, |
| 40 |
int $totalConditions = 0 |
| 41 |
) { |
| 42 |
$this->matchedFilters = $matchedFilters; |
| 43 |
$this->profilingData = $profilingData; |
| 44 |
$this->totalRuntime = $totalRuntime; |
| 45 |
$this->totalConditions = $totalConditions; |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public function record( int $filterID, bool $global, RuleCheckerStatus $status, float $timeTaken ): void { |
| 57 |
$key = GlobalNameUtils::buildGlobalName( $filterID, $global ); |
| 58 |
if ( array_key_exists( $key, $this->matchedFilters ) ) { |
| 59 |
throw new LogicException( "Filter '$key' has already been recorded" ); |
| 60 |
} |
| 61 |
$this->matchedFilters[$key] = $status; |
| 62 |
$this->profilingData[$key] = [ |
| 63 |
'time' => $timeTaken, |
| 64 |
'conds' => $status->getCondsUsed(), |
| 65 |
'result' => $status->getResult() |
| 66 |
]; |
| 67 |
$this->totalRuntime += $timeTaken; |
| 68 |
$this->totalConditions += $status->getCondsUsed(); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function getMatchesMap(): array { |
| 77 |
return array_map( |
| 78 |
static function ( $status ) { |
| 79 |
return $status->getResult(); |
| 80 |
}, |
| 81 |
$this->matchedFilters |
| 82 |
); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function getAllFilters(): array { |
| 89 |
return array_keys( $this->matchedFilters ); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getMatchedFilters(): array { |
| 96 |
return array_keys( array_filter( $this->getMatchesMap() ) ); |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getProfilingData(): array { |
| 103 |
return $this->profilingData; |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public function getTotalRuntime(): float { |
| 110 |
return $this->totalRuntime; |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function getTotalConditions(): int { |
| 117 |
return $this->totalConditions; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public function toArray(): array { |
| 126 |
return [ |
| 127 |
'matches' => array_map( |
| 128 |
static function ( $status ) { |
| 129 |
return $status->toArray(); |
| 130 |
}, |
| 131 |
$this->matchedFilters |
| 132 |
), |
| 133 |
'profiling' => $this->profilingData, |
| 134 |
'condCount' => $this->totalConditions, |
| 135 |
'runtime' => $this->totalRuntime, |
| 136 |
]; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
public static function fromArray( array $value ): self { |
| 145 |
return new self( |
| 146 |
array_map( [ RuleCheckerStatus::class, 'fromArray' ], $value['matches'] ), |
| 147 |
$value['profiling'], |
| 148 |
$value['runtime'], |
| 149 |
$value['condCount'] |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
} |