| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\AbuseFilter\EditBox; |
| 4 |
|
| 5 |
use MediaWiki\Extension\AbuseFilter\AbuseFilterPermissionManager; |
| 6 |
use MediaWiki\Extension\AbuseFilter\KeywordsManager; |
| 7 |
use MediaWiki\Html\Html; |
| 8 |
use MediaWiki\Output\OutputPage; |
| 9 |
use MediaWiki\Permissions\Authority; |
| 10 |
use MessageLocalizer; |
| 11 |
use OOUI\ButtonWidget; |
| 12 |
use OOUI\DropdownInputWidget; |
| 13 |
use OOUI\FieldLayout; |
| 14 |
use OOUI\FieldsetLayout; |
| 15 |
use OOUI\Widget; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
abstract class EditBoxBuilder { |
| 21 |
|
| 22 |
protected $afPermManager; |
| 23 |
|
| 24 |
|
| 25 |
protected $keywordsManager; |
| 26 |
|
| 27 |
|
| 28 |
protected $localizer; |
| 29 |
|
| 30 |
|
| 31 |
protected $authority; |
| 32 |
|
| 33 |
|
| 34 |
protected $output; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public function __construct( |
| 44 |
AbuseFilterPermissionManager $afPermManager, |
| 45 |
KeywordsManager $keywordsManager, |
| 46 |
MessageLocalizer $messageLocalizer, |
| 47 |
Authority $authority, |
| 48 |
OutputPage $output |
| 49 |
) { |
| 50 |
$this->afPermManager = $afPermManager; |
| 51 |
$this->keywordsManager = $keywordsManager; |
| 52 |
$this->localizer = $messageLocalizer; |
| 53 |
$this->authority = $authority; |
| 54 |
$this->output = $output; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public function buildEditBox( |
| 66 |
string $rules, |
| 67 |
bool $addResultDiv = true, |
| 68 |
bool $externalForm = false, |
| 69 |
bool $needsModifyRights = true |
| 70 |
): string { |
| 71 |
$this->output->addModules( 'ext.abuseFilter.edit' ); |
| 72 |
$this->output->enableOOUI(); |
| 73 |
|
| 74 |
$isUserAllowed = $needsModifyRights ? |
| 75 |
$this->afPermManager->canEdit( $this->authority ) : |
| 76 |
$this->afPermManager->canUseTestTools( $this->authority ); |
| 77 |
if ( !$isUserAllowed ) { |
| 78 |
$addResultDiv = false; |
| 79 |
} |
| 80 |
|
| 81 |
$output = $this->getEditBox( $rules, $isUserAllowed, $externalForm ); |
| 82 |
|
| 83 |
if ( $isUserAllowed ) { |
| 84 |
$dropdown = $this->getSuggestionsDropdown(); |
| 85 |
|
| 86 |
$formElements = [ |
| 87 |
new FieldLayout( $dropdown ), |
| 88 |
new FieldLayout( $this->getEditorControls() ) |
| 89 |
]; |
| 90 |
|
| 91 |
$fieldSet = new FieldsetLayout( [ |
| 92 |
'items' => $formElements, |
| 93 |
'classes' => [ 'mw-abusefilter-edit-buttons', 'mw-abusefilter-javascript-tools' ] |
| 94 |
] ); |
| 95 |
|
| 96 |
$output .= $fieldSet; |
| 97 |
} |
| 98 |
|
| 99 |
if ( $addResultDiv ) { |
| 100 |
$output .= Html::element( |
| 101 |
'div', |
| 102 |
[ 'id' => 'mw-abusefilter-syntaxresult', 'style' => 'display: none;' ] |
| 103 |
); |
| 104 |
} |
| 105 |
|
| 106 |
return $output; |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
private function getSuggestionsDropdown(): DropdownInputWidget { |
| 113 |
$rawDropdown = $this->keywordsManager->getBuilderValues(); |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
$dropdownOptions = [ $this->localizer->msg( 'abusefilter-edit-builder-select' )->text() => 'other' ]; |
| 120 |
foreach ( $rawDropdown as $group => $values ) { |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$localisedGroup = $this->localizer->msg( "abusefilter-edit-builder-group-$group" )->text(); |
| 129 |
$dropdownOptions[ $localisedGroup ] = array_flip( $values ); |
| 130 |
$newKeys = array_map( |
| 131 |
function ( $key ) use ( $group, $dropdownOptions, $localisedGroup ) { |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
$operatorExample = "\u{202A}" . |
| 139 |
$dropdownOptions[ $localisedGroup ][ $key ] . |
| 140 |
"\u{202C}"; |
| 141 |
return $this->localizer->msg( |
| 142 |
"abusefilter-edit-builder-$group-$key", |
| 143 |
$operatorExample |
| 144 |
)->text(); |
| 145 |
}, |
| 146 |
array_keys( $dropdownOptions[ $localisedGroup ] ) |
| 147 |
); |
| 148 |
$dropdownOptions[ $localisedGroup ] = array_combine( |
| 149 |
$newKeys, |
| 150 |
$dropdownOptions[ $localisedGroup ] |
| 151 |
); |
| 152 |
} |
| 153 |
|
| 154 |
$dropdownList = Html::listDropdownOptionsOoui( $dropdownOptions ); |
| 155 |
return new DropdownInputWidget( [ |
| 156 |
'name' => 'wpFilterBuilder', |
| 157 |
'inputId' => 'wpFilterBuilder', |
| 158 |
'options' => $dropdownList |
| 159 |
] ); |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
protected function getEditorControls(): Widget { |
| 169 |
return new ButtonWidget( |
| 170 |
[ |
| 171 |
'label' => $this->localizer->msg( 'abusefilter-edit-check' )->text(), |
| 172 |
'id' => 'mw-abusefilter-syntaxcheck' |
| 173 |
] |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
abstract protected function getEditBox( string $rules, bool $isUserAllowed, bool $externalForm ): string; |
| 186 |
|
| 187 |
} |