| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter; |
| 4 |
|
| 5 |
use LogicException; |
| 6 |
use MediaWiki\Config\ServiceOptions; |
| 7 |
use MediaWiki\Extension\AbuseFilter\Consequences\ConsequencesRegistry; |
| 8 |
use MediaWiki\Extension\AbuseFilter\Filter\Filter; |
| 9 |
use MediaWiki\Extension\AbuseFilter\Filter\Flags; |
| 10 |
use MediaWiki\Extension\AbuseFilter\Filter\LastEditInfo; |
| 11 |
use MediaWiki\Extension\AbuseFilter\Filter\MutableFilter; |
| 12 |
use MediaWiki\Extension\AbuseFilter\Filter\Specs; |
| 13 |
use MediaWiki\Json\FormatJson; |
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
class FilterImporter { |
| 23 |
public const SERVICE_NAME = 'AbuseFilterFilterImporter'; |
| 24 |
|
| 25 |
public const CONSTRUCTOR_OPTIONS = [ |
| 26 |
'AbuseFilterValidGroups', |
| 27 |
'AbuseFilterIsCentral', |
| 28 |
]; |
| 29 |
|
| 30 |
private const TEMPLATE_KEYS = [ |
| 31 |
'rules', |
| 32 |
'name', |
| 33 |
'comments', |
| 34 |
'group', |
| 35 |
'actions', |
| 36 |
'enabled', |
| 37 |
'deleted', |
| 38 |
'privacylevel', |
| 39 |
'global' |
| 40 |
]; |
| 41 |
|
| 42 |
|
| 43 |
private $options; |
| 44 |
|
| 45 |
|
| 46 |
private $consequencesRegistry; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public function __construct( ServiceOptions $options, ConsequencesRegistry $consequencesRegistry ) { |
| 53 |
$options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 54 |
$this->options = $options; |
| 55 |
$this->consequencesRegistry = $consequencesRegistry; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function encodeData( Filter $filter, array $actions ): string { |
| 64 |
$data = [ |
| 65 |
'rules' => $filter->getRules(), |
| 66 |
'name' => $filter->getName(), |
| 67 |
'comments' => $filter->getComments(), |
| 68 |
'group' => $filter->getGroup(), |
| 69 |
'actions' => $filter->getActions(), |
| 70 |
'enabled' => $filter->isEnabled(), |
| 71 |
'deleted' => $filter->isDeleted(), |
| 72 |
'privacylevel' => $filter->getPrivacyLevel(), |
| 73 |
'global' => $filter->isGlobal() |
| 74 |
]; |
| 75 |
|
| 76 |
if ( array_keys( $data ) !== self::TEMPLATE_KEYS ) { |
| 77 |
|
| 78 |
throw new LogicException( 'Bad keys' ); |
| 79 |
} |
| 80 |
|
| 81 |
return FormatJson::encode( [ 'data' => $data, 'actions' => $actions ] ); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function decodeData( string $rawData ): Filter { |
| 90 |
$validGroups = $this->options->get( 'AbuseFilterValidGroups' ); |
| 91 |
$globalFiltersEnabled = $this->options->get( 'AbuseFilterIsCentral' ); |
| 92 |
|
| 93 |
$data = FormatJson::decode( $rawData ); |
| 94 |
if ( !$this->isValidImportData( $data ) ) { |
| 95 |
throw new InvalidImportDataException( $rawData ); |
| 96 |
} |
| 97 |
[ 'data' => $filterData, 'actions' => $actions ] = wfObjectToArray( $data ); |
| 98 |
|
| 99 |
return new MutableFilter( |
| 100 |
new Specs( |
| 101 |
$filterData['rules'], |
| 102 |
$filterData['comments'], |
| 103 |
$filterData['name'], |
| 104 |
array_keys( $actions ), |
| 105 |
|
| 106 |
in_array( $filterData['group'], $validGroups, true ) ? $filterData['group'] : 'default' |
| 107 |
), |
| 108 |
new Flags( |
| 109 |
(bool)$filterData['enabled'], |
| 110 |
(bool)$filterData['deleted'], |
| 111 |
(int)$filterData['privacylevel'], |
| 112 |
|
| 113 |
$filterData['global'] && $globalFiltersEnabled |
| 114 |
), |
| 115 |
$actions, |
| 116 |
new LastEditInfo( |
| 117 |
0, |
| 118 |
'', |
| 119 |
'' |
| 120 |
) |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
private function isValidImportData( $data ): bool { |
| 131 |
if ( !is_object( $data ) ) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
|
| 135 |
$arr = get_object_vars( $data ); |
| 136 |
|
| 137 |
$expectedKeys = [ 'data' => true, 'actions' => true ]; |
| 138 |
if ( count( $arr ) !== count( $expectedKeys ) || array_diff_key( $arr, $expectedKeys ) ) { |
| 139 |
return false; |
| 140 |
} |
| 141 |
|
| 142 |
if ( !is_object( $arr['data'] ) || !( is_object( $arr['actions'] ) || $arr['actions'] === [] ) ) { |
| 143 |
return false; |
| 144 |
} |
| 145 |
|
| 146 |
if ( array_keys( get_object_vars( $arr['data'] ) ) !== self::TEMPLATE_KEYS ) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
$allActions = $this->consequencesRegistry->getAllActionNames(); |
| 151 |
foreach ( $arr['actions'] as $action => $params ) { |
| 152 |
if ( !in_array( $action, $allActions, true ) || !is_array( $params ) ) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
return true; |
| 158 |
} |
| 159 |
} |