Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 101 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
SecurePollContentHandler | |
0.00% |
0 / 101 |
|
0.00% |
0 / 5 |
812 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDataFromElection | |
0.00% |
0 / 84 |
|
0.00% |
0 / 1 |
506 | |||
makeContentFromElection | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
6 | |||
canBeUsedOn | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
getContentClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\SecurePoll; |
4 | |
5 | use MediaWiki\Content\ContentHandler; |
6 | use MediaWiki\Content\JsonContentHandler; |
7 | use MediaWiki\Extension\SecurePoll\Entities\Election; |
8 | use MediaWiki\Extension\SecurePoll\Exceptions\InvalidDataException; |
9 | use MediaWiki\Json\FormatJson; |
10 | use MediaWiki\Title\Title; |
11 | |
12 | /** |
13 | * SecurePoll Content Handler |
14 | * |
15 | * @file |
16 | * @ingroup Extensions |
17 | * @ingroup SecurePoll |
18 | * |
19 | * @author Brad Jorsch <bjorsch@wikimedia.org> |
20 | */ |
21 | class SecurePollContentHandler extends JsonContentHandler { |
22 | /** @inheritDoc */ |
23 | public function __construct( $modelId = 'SecurePoll' ) { |
24 | parent::__construct( $modelId ); |
25 | } |
26 | |
27 | /** |
28 | * Load data from an election as a PHP array structure |
29 | * |
30 | * @param Election $election |
31 | * @param string $subpage Subpage to get content for |
32 | * @param bool $useExclusion |
33 | * @return array |
34 | */ |
35 | public static function getDataFromElection( |
36 | Election $election, |
37 | $subpage = '', |
38 | $useExclusion = false |
39 | ) { |
40 | if ( $subpage === '' ) { |
41 | $properties = $election->getAllProperties(); |
42 | if ( $useExclusion ) { |
43 | $excludedNames = array_flip( $election->getPropertyDumpExclusion() ); |
44 | |
45 | foreach ( $properties as $k => $v ) { |
46 | if ( isset( $excludedNames[$k] ) ) { |
47 | $properties[$k] = '<redacted>'; |
48 | } |
49 | } |
50 | unset( |
51 | $properties['list_job-key'], |
52 | $properties['list_total-count'], |
53 | $properties['list_complete-count'] |
54 | ); |
55 | } |
56 | $data = [ |
57 | 'id' => $election->getId(), |
58 | 'title' => $election->title, |
59 | 'ballot' => $election->ballotType, |
60 | 'tally' => $election->tallyType, |
61 | 'lang' => $election->getLanguage(), |
62 | 'startDate' => wfTimestamp( TS_ISO_8601, $election->getStartDate() ), |
63 | 'endDate' => wfTimestamp( TS_ISO_8601, $election->getEndDate() ), |
64 | 'authType' => $election->authType, |
65 | 'properties' => $properties, |
66 | 'questions' => [], |
67 | ]; |
68 | |
69 | foreach ( $election->getQuestions() as $question ) { |
70 | $properties = $question->getAllProperties(); |
71 | if ( $useExclusion ) { |
72 | $excludedNames = array_flip( $question->getPropertyDumpExclusion() ); |
73 | foreach ( $properties as $k => $v ) { |
74 | if ( isset( $excludedNames[$k] ) ) { |
75 | $properties[$k] = '<redacted>'; |
76 | } |
77 | } |
78 | } |
79 | $q = [ |
80 | 'id' => $question->getId(), |
81 | 'properties' => $properties, |
82 | 'options' => [], |
83 | ]; |
84 | |
85 | foreach ( $question->getOptions() as $option ) { |
86 | $properties = $option->getAllProperties(); |
87 | if ( $useExclusion ) { |
88 | $excludedNames = array_flip( $option->getPropertyDumpExclusion() ); |
89 | foreach ( $properties as $k => $v ) { |
90 | if ( isset( $excludedNames[$k] ) ) { |
91 | $properties[$k] = '<redacted>'; |
92 | } |
93 | } |
94 | } |
95 | $o = [ |
96 | 'id' => $option->getId(), |
97 | 'properties' => $properties, |
98 | ]; |
99 | $q['options'][] = $o; |
100 | } |
101 | |
102 | $data['questions'][] = $q; |
103 | } |
104 | } elseif ( preg_match( '#^msg/(\S+)$#', $subpage, $m ) ) { |
105 | $lang = $m[1]; |
106 | $data = [ |
107 | 'id' => $election->getId(), |
108 | 'lang' => $lang, |
109 | 'messages' => [], |
110 | 'questions' => [], |
111 | ]; |
112 | foreach ( $election->getMessageNames() as $name ) { |
113 | $value = $election->getRawMessage( $name, $lang ); |
114 | if ( $value !== false ) { |
115 | $data['messages'][$name] = $value; |
116 | } |
117 | } |
118 | |
119 | foreach ( $election->getQuestions() as $question ) { |
120 | $q = [ |
121 | 'id' => $question->getId(), |
122 | 'messages' => [], |
123 | 'options' => [], |
124 | ]; |
125 | foreach ( $question->getMessageNames() as $name ) { |
126 | $value = $question->getRawMessage( $name, $lang ); |
127 | if ( $value !== false ) { |
128 | $q['messages'][$name] = $value; |
129 | } |
130 | } |
131 | |
132 | foreach ( $question->getOptions() as $option ) { |
133 | $o = [ |
134 | 'id' => $option->getId(), |
135 | 'messages' => [], |
136 | ]; |
137 | foreach ( $option->getMessageNames() as $name ) { |
138 | $value = $option->getRawMessage( $name, $lang ); |
139 | if ( $value !== false ) { |
140 | $o['messages'][$name] = $value; |
141 | } |
142 | } |
143 | $q['options'][] = $o; |
144 | } |
145 | |
146 | $data['questions'][] = $q; |
147 | } |
148 | } else { |
149 | throw new InvalidDataException( __METHOD__ . ': Unsupported subpage format' ); |
150 | } |
151 | |
152 | return $data; |
153 | } |
154 | |
155 | /** |
156 | * Create a SecurePollContent for an election |
157 | * |
158 | * @param Election $election |
159 | * @param string $subpage Subpage to get content for |
160 | * @return array ( Title, SecurePollContent ) |
161 | */ |
162 | public static function makeContentFromElection( Election $election, $subpage = '' ) { |
163 | $json = FormatJson::encode( |
164 | self::getDataFromElection( $election, $subpage, true ), |
165 | false, |
166 | FormatJson::ALL_OK |
167 | ); |
168 | $title = Title::makeTitle( |
169 | NS_SECUREPOLL, |
170 | $election->getId() . ( $subpage === '' ? '' : "/$subpage" ) |
171 | ); |
172 | |
173 | return [ |
174 | $title, |
175 | ContentHandler::makeContent( $json, $title, 'SecurePoll' ) |
176 | ]; |
177 | } |
178 | |
179 | /** @inheritDoc */ |
180 | public function canBeUsedOn( Title $title ) { |
181 | global $wgSecurePollUseNamespace; |
182 | |
183 | return $wgSecurePollUseNamespace && $title->getNamespace() == NS_SECUREPOLL; |
184 | } |
185 | |
186 | /** @inheritDoc */ |
187 | protected function getContentClass() { |
188 | return SecurePollContent::class; |
189 | } |
190 | } |