Index: trunk/extensions/AJAXPoll/AJAXPoll.php |
— | — | @@ -40,267 +40,269 @@ |
41 | 41 | // Internationalization + AJAX function |
42 | 42 | $dir = dirname( __FILE__ ) . '/'; |
43 | 43 | $wgExtensionMessagesFiles['AJAXPoll'] = $dir . 'AJAXPoll.i18n.php'; |
44 | | -$wgAjaxExportList[] = 'submitVote'; |
| 44 | +$wgAjaxExportList[] = 'AJAXPoll::submitVote'; |
| 45 | +$wgHooks['ParserFirstCallInit'][] = 'AJAXPoll::AJAXPollParserInit'; |
45 | 46 | |
46 | | -$wgHooks['ParserFirstCallInit'][] = 'wfPoll'; |
| 47 | +class AJAXPoll { |
47 | 48 | |
48 | | -/** |
49 | | - * Register <poll> tag with the parser |
50 | | - * |
51 | | - * @param $parser Object: instance of Parser (not necessarily $wgParser) |
52 | | - * @return Boolean: true |
53 | | - */ |
54 | | -function wfPoll( &$parser ) { |
55 | | - $parser->setHook( 'poll', 'renderPoll' ); |
56 | | - return true; |
57 | | -} |
| 49 | + /** |
| 50 | + * Register <poll> tag with the parser |
| 51 | + * |
| 52 | + * @param $parser Object: instance of Parser (not necessarily $wgParser) |
| 53 | + * @return Boolean: true |
| 54 | + */ |
| 55 | + static function AJAXPollParserInit( $parser ) { |
| 56 | + $parser->setHook( 'poll', array( __CLASS__, 'AJAXPollRender' ) ); |
| 57 | + return true; |
| 58 | + } |
58 | 59 | |
59 | | -# The callback function for converting the input text to HTML output |
60 | | -function renderPoll( $input ) { |
61 | | - global $wgParser, $wgUser, $wgOut, $wgTitle, $wgScriptPath; |
| 60 | + # The callback function for converting the input text to HTML output |
| 61 | + static function AJAXPollRender( $input ) { |
| 62 | + global $wgParser, $wgUser, $wgOut, $wgTitle, $wgScriptPath; |
62 | 63 | |
63 | | - $wgParser->disableCache(); |
| 64 | + $wgParser->disableCache(); |
64 | 65 | |
65 | | - if ( $wgUser->getName() == '' ) { |
66 | | - $user = wfGetIP(); |
67 | | - } else { |
68 | | - $user = $wgUser->getName(); |
69 | | - } |
| 66 | + if ( $wgUser->getName() == '' ) { |
| 67 | + $user = wfGetIP(); |
| 68 | + } else { |
| 69 | + $user = $wgUser->getName(); |
| 70 | + } |
70 | 71 | |
71 | | - // ID of the poll |
72 | | - $ID = strtoupper( md5( $input ) ); |
| 72 | + // ID of the poll |
| 73 | + $ID = strtoupper( md5( $input ) ); |
73 | 74 | |
74 | | - $par = new Parser(); |
75 | | - $input = $par->parse( $input, $wgTitle, $wgOut->parserOptions() ); |
76 | | - $input = trim( strip_tags( $input->getText() ) ); |
77 | | - $lines = explode( "\n", trim( $input ) ); |
| 75 | + $par = new Parser(); |
| 76 | + $input = $par->parse( $input, $wgTitle, $wgOut->parserOptions() ); |
| 77 | + $input = trim( strip_tags( $input->getText() ) ); |
| 78 | + $lines = explode( "\n", trim( $input ) ); |
78 | 79 | |
79 | | - // Deprecating AJAX |
80 | | - /*if ( isset( $_POST['ajaxpoll-post-id'] ) && isset( $_POST['ajaxpoll-post-answer'] ) && $_POST['ajaxpoll-post-id'] == $ID ) { |
81 | | - submitVote( $_POST['ajaxpoll-post-id'], intval( $_POST['ajaxpoll-post-answer'] ) ); |
82 | | - }*/ |
| 80 | + // Deprecating AJAX |
| 81 | + /*if ( isset( $_POST['ajaxpoll-post-id'] ) && isset( $_POST['ajaxpoll-post-answer'] ) && $_POST['ajaxpoll-post-id'] == $ID ) { |
| 82 | + AJAXPoll::submitVote( $_POST['ajaxpoll-post-id'], intval( $_POST['ajaxpoll-post-answer'] ) ); |
| 83 | + }*/ |
83 | 84 | |
84 | | - $dbw = wfGetDB( DB_MASTER ); |
85 | | - $dbw->begin(); |
86 | | - /** |
87 | | - * Register poll in the database |
88 | | - */ |
89 | | - $row = $dbw->selectRow( |
90 | | - array( 'poll_info' ), |
91 | | - array( 'COUNT(poll_id) AS count' ), |
92 | | - array( 'poll_id' => $ID ), |
93 | | - __METHOD__ |
94 | | - ); |
95 | | - |
96 | | - if( empty( $row->count ) ) { |
97 | | - $dbw->insert( |
98 | | - 'poll_info', |
99 | | - array( |
100 | | - 'poll_id' => $ID, |
101 | | - 'poll_txt' => $input, |
102 | | - 'poll_date' => wfTimestampNow(), |
103 | | - 'poll_title' => $wgParser->mTitle->getText() |
104 | | - ), |
| 85 | + $dbw = wfGetDB( DB_MASTER ); |
| 86 | + $dbw->begin(); |
| 87 | + /** |
| 88 | + * Register poll in the database |
| 89 | + */ |
| 90 | + $row = $dbw->selectRow( |
| 91 | + array( 'poll_info' ), |
| 92 | + array( 'COUNT(poll_id) AS count' ), |
| 93 | + array( 'poll_id' => $ID ), |
105 | 94 | __METHOD__ |
106 | 95 | ); |
107 | | - } |
108 | | - $dbw->commit(); |
109 | 96 | |
110 | | - // Add CSS |
111 | | - $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/AJAXPoll/AJAXPoll.css' ); |
112 | | - switch( $lines[0] ) { |
113 | | - case 'STATS': |
114 | | - $retVal = buildStats( $ID, $user ); |
115 | | - break; |
116 | | - default: |
117 | | - $retVal = ' |
118 | | -<div id="ajaxpoll-container-' . $ID . '">' . buildHTML( $ID, $user, $lines ) . '</div>'; |
119 | | - break; |
| 97 | + if( empty( $row->count ) ) { |
| 98 | + $dbw->insert( |
| 99 | + 'poll_info', |
| 100 | + array( |
| 101 | + 'poll_id' => $ID, |
| 102 | + 'poll_txt' => $input, |
| 103 | + 'poll_date' => wfTimestampNow(), |
| 104 | + 'poll_title' => $wgParser->mTitle->getText() |
| 105 | + ), |
| 106 | + __METHOD__ |
| 107 | + ); |
| 108 | + } |
| 109 | + $dbw->commit(); |
| 110 | + |
| 111 | + // Add CSS |
| 112 | + $wgOut->addExtensionStyle( $wgScriptPath . '/extensions/AJAXPoll/AJAXPoll.css' ); |
| 113 | + switch( $lines[0] ) { |
| 114 | + case 'STATS': |
| 115 | + $retVal = AJAXPoll::buildStats( $ID, $user ); |
| 116 | + break; |
| 117 | + default: |
| 118 | + $retVal = ' |
| 119 | +<div id="ajaxpoll-container-' . $ID . '">' . AJAXPoll::buildHTML( $ID, $user, $lines ) . '</div>'; |
| 120 | + break; |
| 121 | + } |
| 122 | + return $retVal; |
120 | 123 | } |
121 | | - return $retVal; |
122 | | -} |
123 | 124 | |
124 | | -function buildStats( $ID, $user ) { |
125 | | - $dbw = wfGetDB( DB_MASTER ); |
| 125 | + private static function buildStats( $ID, $user ) { |
126 | 126 | |
127 | | - $res = $dbw->select( |
128 | | - 'poll_vote', |
129 | | - array( |
130 | | - 'COUNT(*)', |
131 | | - 'COUNT(DISTINCT poll_id)', |
132 | | - 'COUNT(DISTINCT poll_user)', |
133 | | - 'TIMEDIFF(NOW(), MAX(poll_date))' |
134 | | - ), |
135 | | - array(), |
136 | | - __METHOD__ |
137 | | - ); |
138 | | - $tab = $dbw->fetchRow( $res ); |
| 127 | + $dbw = wfGetDB( DB_MASTER ); |
139 | 128 | |
140 | | - $clock = explode( ':', $tab[3] ); |
| 129 | + $res = $dbw->select( |
| 130 | + 'poll_vote', |
| 131 | + array( |
| 132 | + 'COUNT(*)', |
| 133 | + 'COUNT(DISTINCT poll_id)', |
| 134 | + 'COUNT(DISTINCT poll_user)', |
| 135 | + 'TIMEDIFF(NOW(), MAX(poll_date))' |
| 136 | + ), |
| 137 | + array(), |
| 138 | + __METHOD__ |
| 139 | + ); |
| 140 | + $tab = $dbw->fetchRow( $res ); |
141 | 141 | |
142 | | - if ( $clock[0] == '00' && $clock[1] == '00' ) { |
143 | | - $x = $clock[2]; |
144 | | - $y = 'second'; |
145 | | - } elseif( $clock[0] == '00' ) { |
146 | | - $x = $clock[1]; |
147 | | - $y = 'minute'; |
148 | | - } else { |
149 | | - if ( $clock[0] < 24 ) { |
150 | | - $x = $clock[0]; |
151 | | - $y = 'hour'; |
| 142 | + $clock = explode( ':', $tab[3] ); |
| 143 | + |
| 144 | + if ( $clock[0] == '00' && $clock[1] == '00' ) { |
| 145 | + $x = $clock[2]; |
| 146 | + $y = 'second'; |
| 147 | + } elseif( $clock[0] == '00' ) { |
| 148 | + $x = $clock[1]; |
| 149 | + $y = 'minute'; |
152 | 150 | } else { |
153 | | - $x = floor( $hr / 24 ); |
154 | | - $y = 'day'; |
| 151 | + if ( $clock[0] < 24 ) { |
| 152 | + $x = $clock[0]; |
| 153 | + $y = 'hour'; |
| 154 | + } else { |
| 155 | + $x = floor( $hr / 24 ); |
| 156 | + $y = 'day'; |
| 157 | + } |
155 | 158 | } |
156 | | - } |
157 | 159 | |
158 | | - $clockago = $x . ' ' . $y . ( $x > 1 ? 's' : '' ); |
| 160 | + $clockago = $x . ' ' . $y . ( $x > 1 ? 's' : '' ); |
159 | 161 | |
160 | | - $res = $dbw->select( |
161 | | - 'poll_vote', |
162 | | - 'COUNT(*)', |
163 | | - array( 'DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= poll_date' ), |
164 | | - __METHOD__ |
165 | | - ); |
166 | | - $tab2 = $dbw->fetchRow( $res ); |
| 162 | + $res = $dbw->select( |
| 163 | + 'poll_vote', |
| 164 | + 'COUNT(*)', |
| 165 | + array( 'DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= poll_date' ), |
| 166 | + __METHOD__ |
| 167 | + ); |
| 168 | + $tab2 = $dbw->fetchRow( $res ); |
167 | 169 | |
168 | | - return "There are $tab[1] polls and $tab[0] votes given by $tab[2] different people.<br /> |
169 | | - The last vote has been given $clockago ago.<br/> |
170 | | - During the last 48 hours, $tab2[0] votes have been given."; |
171 | | -} |
| 170 | + return "There are $tab[1] polls and $tab[0] votes given by $tab[2] different people.<br /> |
| 171 | + The last vote has been given $clockago ago.<br/> |
| 172 | + During the last 48 hours, $tab2[0] votes have been given."; |
| 173 | + } |
172 | 174 | |
173 | | -function submitVote( $ID, $answer ) { |
174 | | - global $wgUser,$wgOut; |
| 175 | + public static function submitVote( $ID, $answer ) { |
| 176 | + global $wgUser,$wgOut; |
175 | 177 | |
176 | | - $dbw = wfGetDB( DB_MASTER ); |
| 178 | + $dbw = wfGetDB( DB_MASTER ); |
177 | 179 | |
178 | | - if ( $wgUser->getName() == '' ) { |
179 | | - $user = wfGetIP(); |
180 | | - } else { |
181 | | - $user = $wgUser->getName(); |
182 | | - } |
| 180 | + if ( $wgUser->getName() == '' ) { |
| 181 | + $user = wfGetIP(); |
| 182 | + } else { |
| 183 | + $user = $wgUser->getName(); |
| 184 | + } |
183 | 185 | |
184 | | - if ( $wgUser->isAllowed( 'bot' ) ) { |
185 | | - return buildHTML( $ID, $user ); |
186 | | - } |
| 186 | + if ( $wgUser->isAllowed( 'bot' ) ) { |
| 187 | + return AJAXPoll::buildHTML( $ID, $user ); |
| 188 | + } |
187 | 189 | |
188 | | - $answer = ++$answer; |
| 190 | + $answer = ++$answer; |
189 | 191 | |
190 | | - $q = $dbw->select( |
191 | | - 'poll_vote', |
192 | | - 'COUNT(*) AS c', |
193 | | - array( |
194 | | - 'poll_id' => $ID, |
195 | | - 'poll_user' => $user |
196 | | - ), |
197 | | - __METHOD__ |
198 | | - ); |
199 | | - $row = $dbw->fetchRow( $q ); |
200 | | - |
201 | | - if ( $row['c'] > 0 ) { |
202 | | - |
203 | | - $updateQuery = $dbw->update( |
| 192 | + $q = $dbw->select( |
204 | 193 | 'poll_vote', |
| 194 | + 'COUNT(*) AS c', |
205 | 195 | array( |
206 | | - 'poll_answer' => $answer, |
207 | | - 'poll_date' => wfTimestampNow() |
208 | | - ), |
209 | | - array( |
210 | 196 | 'poll_id' => $ID, |
211 | 197 | 'poll_user' => $user |
212 | 198 | ), |
213 | 199 | __METHOD__ |
214 | 200 | ); |
215 | | - $dbw->commit(); |
216 | | - $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; |
| 201 | + $row = $dbw->fetchRow( $q ); |
217 | 202 | |
218 | | - } else { |
| 203 | + if ( $row['c'] > 0 ) { |
219 | 204 | |
220 | | - $insertQuery = $dbw->insert( |
221 | | - 'poll_vote', |
222 | | - array( |
223 | | - 'poll_id' => $ID, |
224 | | - 'poll_user' => $user, |
225 | | - 'poll_ip' => wfGetIP(), |
226 | | - 'poll_answer' => $answer, |
227 | | - 'poll_date' => wfTimestampNow() |
228 | | - ), |
229 | | - __METHOD__ |
230 | | - ); |
231 | | - $dbw->commit(); |
232 | | - $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; |
| 205 | + $updateQuery = $dbw->update( |
| 206 | + 'poll_vote', |
| 207 | + array( |
| 208 | + 'poll_answer' => $answer, |
| 209 | + 'poll_date' => wfTimestampNow() |
| 210 | + ), |
| 211 | + array( |
| 212 | + 'poll_id' => $ID, |
| 213 | + 'poll_user' => $user |
| 214 | + ), |
| 215 | + __METHOD__ |
| 216 | + ); |
| 217 | + $dbw->commit(); |
| 218 | + $pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error'; |
233 | 219 | |
234 | | - } |
| 220 | + } else { |
235 | 221 | |
236 | | - return buildHTML( $ID, $user, '', $pollContainerText ); |
| 222 | + $insertQuery = $dbw->insert( |
| 223 | + 'poll_vote', |
| 224 | + array( |
| 225 | + 'poll_id' => $ID, |
| 226 | + 'poll_user' => $user, |
| 227 | + 'poll_ip' => wfGetIP(), |
| 228 | + 'poll_answer' => $answer, |
| 229 | + 'poll_date' => wfTimestampNow() |
| 230 | + ), |
| 231 | + __METHOD__ |
| 232 | + ); |
| 233 | + $dbw->commit(); |
| 234 | + $pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error'; |
237 | 235 | |
238 | | -} |
| 236 | + } |
239 | 237 | |
240 | | -function buildHTML( $ID, $user, $lines = '', $extra_from_ajax = '' ) { |
241 | | - global $wgTitle, $wgLang, $wgUseAjax; |
| 238 | + return AJAXPoll::buildHTML( $ID, $user, '', $pollContainerText ); |
242 | 239 | |
243 | | - $dbw = wfGetDB( DB_SLAVE ); |
| 240 | + } |
244 | 241 | |
245 | | - $q = $dbw->select( |
246 | | - 'poll_info', |
247 | | - array( 'poll_txt', 'poll_date' ), |
248 | | - array( 'poll_id' => $ID ), |
249 | | - __METHOD__ |
250 | | - ); |
251 | | - $row = $dbw->fetchRow( $q ); |
| 242 | + private static function buildHTML( $ID, $user, $lines = '', $extra_from_ajax = '' ) { |
| 243 | + global $wgTitle, $wgLang, $wgUseAjax; |
252 | 244 | |
253 | | - if ( empty( $lines ) ) { |
254 | | - $lines = explode( "\n", trim( $row['poll_txt'] ) ); |
255 | | - } |
| 245 | + $dbw = wfGetDB( DB_SLAVE ); |
256 | 246 | |
257 | | - $start_date = $row['poll_date']; |
| 247 | + $q = $dbw->select( |
| 248 | + 'poll_info', |
| 249 | + array( 'poll_txt', 'poll_date' ), |
| 250 | + array( 'poll_id' => $ID ), |
| 251 | + __METHOD__ |
| 252 | + ); |
| 253 | + $row = $dbw->fetchRow( $q ); |
258 | 254 | |
259 | | - $q = $dbw->select( |
260 | | - 'poll_vote', |
261 | | - array( 'poll_answer', 'COUNT(*)' ), |
262 | | - array( 'poll_id' => $ID ), |
263 | | - __METHOD__, |
264 | | - array( 'GROUP BY' => 'poll_answer' ) |
265 | | - ); |
| 255 | + if ( empty( $lines ) ) { |
| 256 | + $lines = explode( "\n", trim( $row['poll_txt'] ) ); |
| 257 | + } |
266 | 258 | |
267 | | - $poll_result = array(); |
| 259 | + $start_date = $row['poll_date']; |
268 | 260 | |
269 | | - while ( $row = $q->fetchRow() ) { |
270 | | - $poll_result[$row[0]] = $row[1]; |
271 | | - } |
| 261 | + $q = $dbw->select( |
| 262 | + 'poll_vote', |
| 263 | + array( 'poll_answer', 'COUNT(*)' ), |
| 264 | + array( 'poll_id' => $ID ), |
| 265 | + __METHOD__, |
| 266 | + array( 'GROUP BY' => 'poll_answer' ) |
| 267 | + ); |
272 | 268 | |
273 | | - $amountOfVotes = array_sum( $poll_result ); |
| 269 | + $poll_result = array(); |
274 | 270 | |
275 | | - // Did we vote? |
276 | | - $q = $dbw->select( |
277 | | - 'poll_vote', |
278 | | - array( 'poll_answer', 'poll_date' ), |
279 | | - array( |
280 | | - 'poll_id' => $ID, |
281 | | - 'poll_user' => $user |
282 | | - ), |
283 | | - __METHOD__ |
284 | | - ); |
| 271 | + while ( $row = $q->fetchRow() ) { |
| 272 | + $poll_result[$row[0]] = $row[1]; |
| 273 | + } |
285 | 274 | |
286 | | - if ( $row = $dbw->fetchRow( $q ) ) { |
287 | | - $ourLastVoteDate = wfMsg( |
288 | | - 'ajaxpoll-your-vote', |
289 | | - $lines[$row[0] - 1], |
290 | | - $wgLang->timeanddate( wfTimestamp( TS_MW, $row[1] ), true /* adjust? */ ) |
| 275 | + $amountOfVotes = array_sum( $poll_result ); |
| 276 | + |
| 277 | + // Did we vote? |
| 278 | + $q = $dbw->select( |
| 279 | + 'poll_vote', |
| 280 | + array( 'poll_answer', 'poll_date' ), |
| 281 | + array( |
| 282 | + 'poll_id' => $ID, |
| 283 | + 'poll_user' => $user |
| 284 | + ), |
| 285 | + __METHOD__ |
291 | 286 | ); |
292 | | - } |
293 | 287 | |
294 | | - if ( is_object( $wgTitle ) ) { |
295 | | - if( !empty( $extra_from_ajax ) ) { |
296 | | - $attributes = ' style="display: block;"'; |
297 | | - $ajaxMessage = wfMsg( $extra_from_ajax ); |
298 | | - } else { |
299 | | - $attributes = ''; |
300 | | - $ajaxMessage = ''; |
| 288 | + if ( $row = $dbw->fetchRow( $q ) ) { |
| 289 | + $ourLastVoteDate = wfMsg( |
| 290 | + 'ajaxpoll-your-vote', |
| 291 | + $lines[$row[0] - 1], |
| 292 | + $wgLang->timeanddate( wfTimestamp( TS_MW, $row[1] ), true /* adjust? */ ) |
| 293 | + ); |
301 | 294 | } |
302 | | - // HTML output has to be on one line thanks to a MediaWiki bug |
303 | | - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
304 | | - $ret = '<div id="ajaxpoll-id-' . $ID . '" class="ajaxpoll"> |
| 295 | + |
| 296 | + if ( is_object( $wgTitle ) ) { |
| 297 | + if( !empty( $extra_from_ajax ) ) { |
| 298 | + $attributes = ' style="display: block;"'; |
| 299 | + $ajaxMessage = wfMsg( $extra_from_ajax ); |
| 300 | + } else { |
| 301 | + $attributes = ''; |
| 302 | + $ajaxMessage = ''; |
| 303 | + } |
| 304 | + // HTML output has to be on one line thanks to a MediaWiki bug |
| 305 | + // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
| 306 | + $ret = '<div id="ajaxpoll-id-' . $ID . '" class="ajaxpoll"> |
305 | 307 | <div id="ajaxpoll-ajax-' . $ID . '" class="ajaxpoll-ajax"' . $attributes . '>' . $ajaxMessage . '</div> |
306 | 308 | <script>var tmp; |
307 | 309 | function mover(x){ |
— | — | @@ -317,59 +319,61 @@ |
318 | 320 | </script> |
319 | 321 | <div class="ajaxpoll-question">' . strip_tags( $lines[0] ) . '</div>'; |
320 | 322 | |
321 | | - // Different message depending on if the user has already voted or not. |
322 | | - $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' ); |
323 | | - $ret .= '<div class="ajaxpoll-misc">' . $message . ' |
| 323 | + // Different message depending on if the user has already voted or not. |
| 324 | + $message = ( isset( $row[0] ) ) ? $ourLastVoteDate : wfMsg( 'ajaxpoll-no-vote' ); |
| 325 | + $ret .= '<div class="ajaxpoll-misc">' . $message . ' |
324 | 326 | </div>'; |
325 | 327 | |
326 | | - $ret .= '<form method="post" action="' . $wgTitle->getLocalURL() . |
327 | | - '" id="ajaxpoll-answer-id-' . $ID . '"><input type="hidden" name="ajaxpoll-post-id" value="' . $ID . '" />'; |
| 328 | + $ret .= '<form method="post" action="' . $wgTitle->getLocalURL() . |
| 329 | + '" id="ajaxpoll-answer-id-' . $ID . '"><input type="hidden" name="ajaxpoll-post-id" value="' . $ID . '" />'; |
328 | 330 | |
329 | | - for ( $i = 1; $i < count( $lines ); $i++ ) { |
330 | | - $ans_no = $i - 1; |
| 331 | + for ( $i = 1; $i < count( $lines ); $i++ ) { |
| 332 | + $ans_no = $i - 1; |
331 | 333 | |
332 | | - if ( $amountOfVotes == 0 ) { |
333 | | - $percent = 0; |
334 | | - } else { |
335 | | - $percent = $wgLang->formatNum( round( ( isset( $poll_result[$i + 1] ) ? $poll_result[$i + 1] : 0 ) * 100 / $amountOfVotes, 2 ) ); |
336 | | - } |
| 334 | + if ( $amountOfVotes == 0 ) { |
| 335 | + $percent = 0; |
| 336 | + } else { |
| 337 | + $percent = $wgLang->formatNum( round( ( isset( $poll_result[$i + 1] ) ? $poll_result[$i + 1] : 0 ) * 100 / $amountOfVotes, 2 ) ); |
| 338 | + } |
337 | 339 | |
338 | | - $our = ( isset( $row[0] ) && ( $row[0] - 1 == $i ) ); |
| 340 | + $our = ( isset( $row[0] ) && ( $row[0] - 1 == $i ) ); |
339 | 341 | |
340 | | - // If AJAX is enabled, as it is by default in modern MWs, we can |
341 | | - // just use sajax library function here for that AJAX-y feel. |
342 | | - // If not, we'll have to submit the form old-school way... |
343 | | - if ( $wgUseAjax ) { |
344 | | - $submitJS = "sajax_do_call(\"submitVote\",[\"" . $ID . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $ID . "\")[0]);"; |
345 | | - } else { |
346 | | - $submitJS = "$(\"#ajaxpoll-answer-id-" . $ID . "\").submit();"; |
347 | | - } |
| 342 | + // If AJAX is enabled, as it is by default in modern MWs, we can |
| 343 | + // just use sajax library function here for that AJAX-y feel. |
| 344 | + // If not, we'll have to submit the form old-school way... |
| 345 | + if ( $wgUseAjax ) { |
| 346 | + $submitJS = "sajax_do_call(\"AJAXPoll::submitVote\",[\"" . $ID . "\",\"" . $i . "\"], $(\"#ajaxpoll-container-" . $ID . "\")[0]);"; |
| 347 | + } else { |
| 348 | + $submitJS = "$(\"#ajaxpoll-answer-id-" . $ID . "\").submit();"; |
| 349 | + } |
348 | 350 | |
349 | | - // HTML output has to be on one line thanks to a MediaWiki bug |
350 | | - // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
351 | | - $ret .= " |
| 351 | + // HTML output has to be on one line thanks to a MediaWiki bug |
| 352 | + // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=1319 |
| 353 | + $ret .= " |
352 | 354 | <div id='ajaxpoll-answer-" . $ans_no . "' class='ajaxpoll-answer'><div class='ajaxpoll-answer-name'><label for='ajaxpoll-answer-radio-" . $ans_no . "' onclick='$(\"#ajaxpoll-ajax-" . $ID . "\").html(\"" . wfMsg( 'ajaxpoll-submitting' ) . "\");$(\"#ajaxpoll-ajax-" . $ID . "\").css(\"display\",\"block\");$(this).addClass(\"ajaxpoll-checkevent\").prop(\"checked\",true); " . $submitJS . "'><input type='radio' id='ajaxpoll-post-answer-" . $ans_no . "' name='ajaxpoll-post-answer' value='" . $i . "'" . ( $our ? 'checked=true ' : '' ) . "/>" . strip_tags( $lines[$i] ) . |
353 | 355 | "</label></div><div class='ajaxpoll-answer-vote" . ( $our ? ' ajaxpoll-our-vote' : '' ) ."' onmouseover='mover(this)' onmouseout='mout(this);'><span title='" . wfMsg( 'ajaxpoll-percent-votes', sprintf( $percent ) ) . "'>" . ( ( isset( $poll_result ) && !empty( $poll_result[$i + 1] ) ) ? $poll_result[$i + 1] : 0 ) . "</span><div style='width: " . $percent . "%;" . ( $percent == 0 ? ' border:0;' : '' ) . "'></div></div> |
354 | 356 | </div> |
355 | 357 | "; |
356 | 358 | } |
357 | 359 | |
358 | | - $ret .= '</form>'; |
| 360 | + $ret .= '</form>'; |
359 | 361 | |
360 | | - // Display information about the poll (creation date, amount of votes) |
361 | | - $pollSummary = wfMsgExt( |
362 | | - 'ajaxpoll-info', |
363 | | - 'parsemag', // parse PLURAL |
364 | | - $amountOfVotes, // amount of votes |
365 | | - $wgLang->timeanddate( wfTimestamp( TS_MW, $start_date ), true /* adjust? */ ) |
366 | | - ); |
| 362 | + // Display information about the poll (creation date, amount of votes) |
| 363 | + $pollSummary = wfMsgExt( |
| 364 | + 'ajaxpoll-info', |
| 365 | + 'parsemag', // parse PLURAL |
| 366 | + $amountOfVotes, // amount of votes |
| 367 | + $wgLang->timeanddate( wfTimestamp( TS_MW, $start_date ), true /* adjust? */ ) |
| 368 | + ); |
367 | 369 | |
368 | | - $ret .= '<div id="ajaxpoll-info-' . $ID . '" class="ajaxpoll-info">' . $pollSummary . '</div>'; |
| 370 | + $ret .= '<div id="ajaxpoll-info-' . $ID . '" class="ajaxpoll-info">' . $pollSummary . '</div>'; |
369 | 371 | |
370 | | - $ret .= '</div>'; |
371 | | - } else { |
372 | | - $ret = ''; |
| 372 | + $ret .= '</div>'; |
| 373 | + } else { |
| 374 | + $ret = ''; |
| 375 | + } |
| 376 | + |
| 377 | + return $ret; |
373 | 378 | } |
374 | 379 | |
375 | | - return $ret; |
376 | | -} |
| 380 | +} /* class AJAXPoll */ |