Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
3.60% |
4 / 111 |
|
11.11% |
1 / 9 |
CRAP | |
0.00% |
0 / 1 |
SpecialSecurePollLog | |
3.60% |
4 / 111 |
|
11.11% |
1 / 9 |
275.87 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
execute | |
50.00% |
1 / 2 |
|
0.00% |
0 / 1 |
1.12 | |||
requiresWrite | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDisplayFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFormFields | |
0.00% |
0 / 57 |
|
0.00% |
0 / 1 |
2 | |||
setFormDefaults | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
alterForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
onSubmit | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
20 | |||
checkIfElectionExists | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll; |
4 | |
5 | use MediaWiki\Extension\SecurePoll\Pages\ActionPage; |
6 | use MediaWiki\HTMLForm\HTMLForm; |
7 | use MediaWiki\SpecialPage\FormSpecialPage; |
8 | use MediaWiki\User\UserFactory; |
9 | |
10 | class SpecialSecurePollLog extends FormSpecialPage { |
11 | /** @var Context */ |
12 | private $context; |
13 | |
14 | /** @var UserFactory */ |
15 | private $userFactory; |
16 | |
17 | /** |
18 | * @inheritDoc |
19 | */ |
20 | public function __construct( UserFactory $userFactory ) { |
21 | parent::__construct( 'SecurePollLog', 'securepoll-create-poll' ); |
22 | $this->context = new Context(); |
23 | $this->userFactory = $userFactory; |
24 | } |
25 | |
26 | /** |
27 | * @inheritDoc |
28 | */ |
29 | public function execute( $par ) { |
30 | parent::execute( $par ); |
31 | |
32 | $this->getOutput()->addModules( 'ext.securepoll.htmlform' ); |
33 | } |
34 | |
35 | /** |
36 | * @inheritDoc |
37 | */ |
38 | public function requiresWrite() { |
39 | return false; |
40 | } |
41 | |
42 | /** |
43 | * @inheritDoc |
44 | */ |
45 | protected function getDisplayFormat() { |
46 | return 'ooui'; |
47 | } |
48 | |
49 | /** |
50 | * @inheritDoc |
51 | */ |
52 | protected function getFormFields() { |
53 | $fields = []; |
54 | |
55 | $fields['type'] = [ |
56 | 'name' => 'type', |
57 | 'type' => 'select', |
58 | 'options-messages' => [ |
59 | 'securepolllog-form-type-option-all' => 'all', |
60 | 'securepolllog-form-type-option-voter' => 'voter', |
61 | 'securepolllog-form-type-option-admin' => 'admin', |
62 | ], |
63 | 'default' => 'all', |
64 | ]; |
65 | |
66 | $fields['electionName'] = [ |
67 | 'name' => 'election_name', |
68 | 'type' => 'text', |
69 | 'label-message' => 'securepolllog-form-electionname-label', |
70 | 'default' => '', |
71 | 'validation-callback' => [ |
72 | $this, |
73 | 'checkIfElectionExists', |
74 | ], |
75 | ]; |
76 | |
77 | $fields['performer'] = [ |
78 | 'name' => 'performer', |
79 | 'type' => 'user', |
80 | 'label-message' => 'securepolllog-form-performer-label', |
81 | 'exists' => true, |
82 | 'default' => '', |
83 | ]; |
84 | |
85 | $fields['target'] = [ |
86 | 'name' => 'target', |
87 | 'type' => 'user', |
88 | 'label-message' => 'securepolllog-form-target-label', |
89 | 'exists' => true, |
90 | 'default' => '', |
91 | ]; |
92 | |
93 | $fields['date'] = [ |
94 | 'name' => 'date', |
95 | 'type' => 'date', |
96 | 'label-message' => 'securepolllog-form-date-label', |
97 | 'default' => '', |
98 | 'max' => gmdate( 'M-d-Y' ), |
99 | ]; |
100 | |
101 | $fields['actions'] = [ |
102 | 'name' => 'actions', |
103 | 'type' => 'radio', |
104 | 'cssclass' => 'securepolllog-actions-radio', |
105 | 'label-message' => 'securepolllog-form-action-label', |
106 | 'options-messages' => [ |
107 | 'securepolllog-form-action-option-addadmin' => ActionPage::LOG_TYPE_ADDADMIN, |
108 | 'securepolllog-form-action-option-removeadmin' => ActionPage::LOG_TYPE_REMOVEADMIN, |
109 | 'securepolllog-form-action-option-both' => -1, |
110 | ], |
111 | 'default' => -1, |
112 | 'flatlist' => true, |
113 | ]; |
114 | |
115 | $this->setFormDefaults( $fields ); |
116 | |
117 | return $fields; |
118 | } |
119 | |
120 | /** |
121 | * Set default values for form fields if the form has already |
122 | * been submitted. |
123 | * |
124 | * @param array &$fields |
125 | */ |
126 | private function setFormDefaults( &$fields ) { |
127 | $request = $this->getRequest(); |
128 | if ( !$request->wasPosted() ) { |
129 | return; |
130 | } |
131 | |
132 | foreach ( $fields as $fieldname => $value ) { |
133 | $requestParam = $value['name']; |
134 | if ( $request->getVal( $requestParam ) !== null ) { |
135 | $fields[$fieldname]['default'] = $request->getVal( $requestParam ); |
136 | } |
137 | } |
138 | } |
139 | |
140 | /** |
141 | * @inheritDoc |
142 | */ |
143 | protected function alterForm( HTMLForm $form ) { |
144 | $form->setMethod( 'get' ); |
145 | } |
146 | |
147 | /** |
148 | * @inheritDoc |
149 | */ |
150 | public function onSubmit( array $data ) { |
151 | $form = $this->getForm(); |
152 | $form->prepareForm(); |
153 | $form->displayForm( true ); |
154 | |
155 | // Get date components |
156 | if ( $data['date'] ) { |
157 | $date = explode( '-', $data['date'] ); |
158 | $year = (int)$date[0]; |
159 | $month = (int)$date[1]; |
160 | $day = (int)$date[2]; |
161 | } else { |
162 | $year = 0; |
163 | $month = 0; |
164 | $day = 0; |
165 | } |
166 | |
167 | // Transform action codes to integer(s) in an array |
168 | // (to match LOG_TYPE_ADDADMIN and LOG_TYPE_REMOVEADMIN) |
169 | if ( (int)$data['actions'] === -1 ) { |
170 | $data['actions'] = [ ActionPage::LOG_TYPE_ADDADMIN, ActionPage::LOG_TYPE_REMOVEADMIN ]; |
171 | } else { |
172 | $data['actions'] = [ (int)$data['actions'] ]; |
173 | } |
174 | |
175 | $pager = new SecurePollLogPager( |
176 | $this->context, |
177 | $this->userFactory, |
178 | $data['type'], |
179 | $data['performer'], |
180 | $data['type'] === 'voter' ? '' : $data['target'], |
181 | $data['electionName'], |
182 | $year, |
183 | $month, |
184 | $day, |
185 | $data['actions'] |
186 | ); |
187 | |
188 | $this->getOutput()->addHTML( |
189 | $pager->getNavigationBar() . |
190 | $pager->getBody() . |
191 | $pager->getNavigationBar() |
192 | ); |
193 | return true; |
194 | } |
195 | |
196 | /** |
197 | * Check that the election id exists |
198 | * |
199 | * Given a title, return the id of the election or false if it doesn't exist |
200 | * |
201 | * @internal For use by the HTMLFormField |
202 | * @param string $value |
203 | * @return bool|string true on success, string on error |
204 | */ |
205 | public function checkIfElectionExists( $value ) { |
206 | $election = $this->context->getElectionByTitle( $value ); |
207 | if ( !$value || $election ) { |
208 | return true; |
209 | } |
210 | return $this->msg( |
211 | 'securepolllog-election-does-not-exist', |
212 | $value |
213 | )->parse(); |
214 | } |
215 | } |