Debian Codegraph @

extensions/AbuseFilter/includes/EditBox/EditBoxBuilder.php

raw symbols SCIP
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 * Base class for classes responsible for building filter edit boxes
19 */
20 abstract class EditBoxBuilder {
21 /** @var AbuseFilterPermissionManager */
22 protected $afPermManager;
23
24 /** @var KeywordsManager */
25 protected $keywordsManager;
26
27 /** @var MessageLocalizer */
28 protected $localizer;
29
30 /** @var Authority */
31 protected $authority;
32
33 /** @var OutputPage */
34 protected $output;
35
36 /**
37 * @param AbuseFilterPermissionManager $afPermManager
38 * @param KeywordsManager $keywordsManager
39 * @param MessageLocalizer $messageLocalizer
40 * @param Authority $authority
41 * @param OutputPage $output
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 * @param string $rules
59 * @param bool $addResultDiv
60 * @param bool $externalForm
61 * @param bool $needsModifyRights
62 * @param-taint $rules none
63 * @return string
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 * @return DropdownInputWidget
111 */
112 private function getSuggestionsDropdown(): DropdownInputWidget {
113 $rawDropdown = $this->keywordsManager->getBuilderValues();
114
115 // The array needs to be rearranged to be understood by OOUI. It comes with the format
116 // [ group-msg-key => [ text-to-add => text-msg-key ] ] and we need it as
117 // [ group-msg => [ text-msg => text-to-add ] ]
118 // Also, the 'other' element must be the first one.
119 $dropdownOptions = [ $this->localizer->msg( 'abusefilter-edit-builder-select' )->text() => 'other' ];
120 foreach ( $rawDropdown as $group => $values ) {
121 // Give grep a chance to find the usages:
122 // abusefilter-edit-builder-group-op-arithmetic
123 // abusefilter-edit-builder-group-op-comparison
124 // abusefilter-edit-builder-group-op-bool
125 // abusefilter-edit-builder-group-misc
126 // abusefilter-edit-builder-group-funcs
127 // abusefilter-edit-builder-group-vars
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 // Force all operators and functions to be always shown as left to right text
133 // with the help of control characters:
134 // * 202A is LEFT-TO-RIGHT EMBEDDING (LRE)
135 // * 202C is POP DIRECTIONAL FORMATTING (PDF)
136 // This has to be done with control characters because
137 // markup cannot be used within <option> elements.
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 * Get an additional widget that "controls" the editor, and is placed next to it
164 * Precondition: the user has full rights.
165 *
166 * @return Widget
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 * Generate the HTML for the actual edit box
179 *
180 * @param string $rules
181 * @param bool $isUserAllowed
182 * @param bool $externalForm
183 * @return string
184 */
185 abstract protected function getEditBox( string $rules, bool $isUserAllowed, bool $externalForm ): string;
186
187 }