Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
RadioRangeCommentBallot | |
0.00% |
0 / 52 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
getForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
submitForm | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
unpackRecord | |
0.00% |
0 / 39 |
|
0.00% |
0 / 1 |
110 | |||
readComment | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll\Ballots; |
4 | |
5 | use LogicException; |
6 | |
7 | /** |
8 | * A ballot used specifically for the Wikimedia Referendum on the personal image filter. |
9 | * Allows voters to send comments in with their ballot. |
10 | * |
11 | * Now deprecated in favour of the comment feature in VotePage. |
12 | */ |
13 | class RadioRangeCommentBallot extends RadioRangeBallot { |
14 | public function getForm( $prevStatus = false ) { |
15 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
16 | throw new LogicException( 'This ballot type has been archived and can no longer be used for voting.' ); |
17 | } |
18 | |
19 | public function submitForm() { |
20 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
21 | throw new LogicException( 'This ballot type has been archived and can no longer be used for voting.' ); |
22 | } |
23 | |
24 | /** |
25 | * Copy and modify from parent function, complex to refactor. |
26 | * @param string $record |
27 | * @return array|bool |
28 | */ |
29 | public function unpackRecord( $record ) { |
30 | $scores = []; |
31 | $itemLength = 8 + 8 + 11 + 7; |
32 | $questions = []; |
33 | foreach ( $this->election->getQuestions() as $question ) { |
34 | $questions[$question->getId()] = $question; |
35 | } |
36 | for ( $offset = 0, $len = strlen( $record ); $offset < $len; $offset += $itemLength ) { |
37 | if ( !preg_match( |
38 | '/Q([0-9A-F]{8})-A([0-9A-F]{8})-S([+-][0-9]{10})--/A', |
39 | $record, |
40 | $m, |
41 | 0, |
42 | $offset |
43 | ) |
44 | ) { |
45 | // Allow comments |
46 | if ( $record[$offset] == '/' ) { |
47 | break; |
48 | } |
49 | |
50 | wfDebug( __METHOD__ . ": regex doesn't match\n" ); |
51 | |
52 | return false; |
53 | } |
54 | $qid = intval( base_convert( $m[1], 16, 10 ) ); |
55 | $oid = intval( base_convert( $m[2], 16, 10 ) ); |
56 | $score = intval( $m[3] ); |
57 | if ( !isset( $questions[$qid] ) ) { |
58 | wfDebug( __METHOD__ . ": invalid question ID\n" ); |
59 | |
60 | return false; |
61 | } |
62 | [ $min, $max ] = $this->getMinMax( $questions[$qid] ); |
63 | if ( $score < $min || $score > $max ) { |
64 | wfDebug( __METHOD__ . ": score out of range\n" ); |
65 | |
66 | return false; |
67 | } |
68 | $scores[$qid][$oid] = $score; |
69 | } |
70 | |
71 | // Read comments |
72 | $scores['comment'] = []; |
73 | |
74 | $scores['comment']['native'] = $this->readComment( $record, $offset ); |
75 | |
76 | if ( substr( $record, $offset, 2 ) !== '--' ) { |
77 | wfDebug( __METHOD__ . ": Invalid format\n" ); |
78 | |
79 | return false; |
80 | } |
81 | $offset += 2; |
82 | |
83 | $scores['comment']['en'] = $this->readComment( $record, $offset ); |
84 | |
85 | if ( $offset < strlen( $record ) ) { |
86 | wfDebug( __METHOD__ . ": Invalid format\n" ); |
87 | |
88 | return false; |
89 | } |
90 | |
91 | return $scores; |
92 | } |
93 | |
94 | public function readComment( $record, &$offset ) { |
95 | $commentOffset = strpos( $record, '/', $offset + 1 ); |
96 | $commentLength = intval( |
97 | substr( |
98 | $record, |
99 | $offset + 1, |
100 | ( $commentOffset - $offset ) - 1 |
101 | ) |
102 | ); |
103 | $result = substr( $record, $commentOffset + 1, $commentLength ); |
104 | // Fast-forward |
105 | $offset = $commentOffset + $commentLength + 1; |
106 | |
107 | return $result; |
108 | } |
109 | } |