2012-02-19 19:31:24 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* AJAX Poll class
|
|
|
|
* Created by Dariusz Siedlecki, based on the work by Eric David.
|
|
|
|
* Licensed under the GFDL.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @ingroup Extensions
|
|
|
|
* @author Dariusz Siedlecki <datrio@gmail.com>
|
|
|
|
* @author Jack Phoenix <jack@countervandalism.net>
|
|
|
|
* @author Thomas Gries
|
|
|
|
* @maintainer Thomas Gries
|
|
|
|
* @link http://www.mediawiki.org/wiki/Extension:AJAX_Poll Documentation
|
|
|
|
*/
|
|
|
|
|
|
|
|
class AJAXPoll {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register <poll> tag with the parser
|
|
|
|
*
|
|
|
|
* @param $parser Object: instance of Parser (not necessarily $wgParser)
|
|
|
|
* @return Boolean: true
|
|
|
|
*/
|
2012-03-14 20:19:16 +00:00
|
|
|
static function onParserInit( $parser ) {
|
2012-03-13 22:31:46 +00:00
|
|
|
global $wgOut;
|
2012-02-19 19:31:24 +00:00
|
|
|
$parser->setHook( 'poll', array( __CLASS__, 'AJAXPollRender' ) );
|
2012-03-13 22:31:46 +00:00
|
|
|
$wgOut->addModules( 'ext.ajaxpoll' );
|
2012-02-19 19:31:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
# The callback function for converting the input text to HTML output
|
2013-05-26 10:03:15 +00:00
|
|
|
static function AJAXPollRender( $input, $args = array(), Parser $parser ) {
|
2012-03-19 18:45:57 +00:00
|
|
|
global $wgUser, $wgOut, $wgTitle, $wgScriptPath, $wgUseAjax;
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-09 23:24:38 +00:00
|
|
|
$parser->disableCache();
|
2012-03-19 18:45:57 +00:00
|
|
|
$parser->addTrackingCategory( 'ajaxpoll-tracking-category' );
|
2012-03-09 19:23:28 +00:00
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
if ( $wgUser->getName() == '' ) {
|
2013-05-26 06:48:12 +00:00
|
|
|
$userName = wfGetIP();
|
2012-02-19 19:31:24 +00:00
|
|
|
} else {
|
2013-05-26 06:48:12 +00:00
|
|
|
$userName = $wgUser->getName();
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ID of the poll
|
2012-03-12 22:13:51 +00:00
|
|
|
$id = strtoupper( md5( $input ) );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
$par = new Parser();
|
|
|
|
$input = $par->parse( $input, $wgTitle, $wgOut->parserOptions() );
|
|
|
|
$input = trim( strip_tags( $input->getText() ) );
|
|
|
|
$lines = explode( "\n", trim( $input ) );
|
|
|
|
|
2012-03-17 21:13:10 +00:00
|
|
|
// compatibility for non-ajax requests - just in case
|
|
|
|
if ( !$wgUseAjax ) {
|
|
|
|
$responseId = "ajaxpoll-post-id";
|
|
|
|
$responseAnswer = "ajaxpoll-post-answer-$id";
|
2013-05-19 09:54:00 +00:00
|
|
|
$responseToken = "ajaxPollToken";
|
|
|
|
|
|
|
|
if ( isset( $_POST[$responseId] )
|
2013-06-04 20:01:09 +00:00
|
|
|
&& isset( $_POST[$responseAnswer] )
|
2013-05-19 09:54:00 +00:00
|
|
|
&& ( $_POST[$responseId] == $id )
|
|
|
|
&& isset( $_POST[$responseToken] ) ) {
|
2013-05-26 10:03:15 +00:00
|
|
|
self::submitVote( $id, intval( $_POST[$responseAnswer] ), $_POST[$responseToken] );
|
2012-03-17 21:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-04 20:01:09 +00:00
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2012-03-09 19:26:20 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2012-03-12 22:13:51 +00:00
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
/**
|
|
|
|
* Register poll in the database
|
|
|
|
*/
|
2012-03-12 22:13:51 +00:00
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
$row = $dbw->selectRow(
|
2012-03-14 20:28:16 +00:00
|
|
|
array( 'ajaxpoll_info' ),
|
2012-02-19 19:31:24 +00:00
|
|
|
array( 'COUNT(poll_id) AS count' ),
|
2012-03-12 22:13:51 +00:00
|
|
|
array( 'poll_id' => $id ),
|
2012-02-19 19:31:24 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
$showResultsBeforeVoting = null;
|
|
|
|
if ( array_key_exists( 'show-results-before-voting', $args ) ) {
|
|
|
|
if ( strval( $args['show-results-before-voting'] ) !== '0' ) {
|
|
|
|
$showResultsBeforeVoting = '1';
|
|
|
|
} else {
|
|
|
|
$showResultsBeforeVoting = '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
if( empty( $row->count ) ) {
|
|
|
|
$dbw->insert(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_info',
|
2012-02-19 19:31:24 +00:00
|
|
|
array(
|
2012-03-12 22:13:51 +00:00
|
|
|
'poll_id' => $id,
|
2013-05-26 10:03:15 +00:00
|
|
|
'poll_show_results_before_voting' => $showResultsBeforeVoting,
|
2012-02-19 19:31:24 +00:00
|
|
|
'poll_txt' => $input,
|
|
|
|
'poll_date' => wfTimestampNow(),
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2013-05-26 10:03:15 +00:00
|
|
|
} else {
|
|
|
|
$dbw->update(
|
|
|
|
'ajaxpoll_info',
|
|
|
|
array(
|
|
|
|
'poll_show_results_before_voting' => $showResultsBeforeVoting,
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'poll_id' => $id,
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-09 19:27:55 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
switch( $lines[0] ) {
|
|
|
|
case 'STATS':
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret = self::buildStats( $id, $userName );
|
2012-02-19 19:31:24 +00:00
|
|
|
break;
|
|
|
|
default:
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret = Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-container-' . $id
|
|
|
|
),
|
|
|
|
self::buildHTML( $id, $userName, $lines )
|
|
|
|
);
|
2012-02-19 19:31:24 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-06-04 20:01:09 +00:00
|
|
|
return $ret;
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-26 06:48:12 +00:00
|
|
|
private static function buildStats( $id, $userName ) {
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2013-05-26 10:03:15 +00:00
|
|
|
$dbr->begin( __METHOD__ );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$res = $dbr->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-02-19 19:31:24 +00:00
|
|
|
array(
|
|
|
|
'COUNT(*)',
|
|
|
|
'COUNT(DISTINCT poll_id)',
|
|
|
|
'COUNT(DISTINCT poll_user)',
|
|
|
|
'TIMEDIFF(NOW(), MAX(poll_date))'
|
|
|
|
),
|
|
|
|
array(),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2012-03-12 22:13:51 +00:00
|
|
|
$tab = $dbr->fetchRow( $res );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
$clock = explode( ':', $tab[3] );
|
|
|
|
|
|
|
|
if ( $clock[0] == '00' && $clock[1] == '00' ) {
|
|
|
|
$x = $clock[2];
|
|
|
|
$y = 'second';
|
|
|
|
} elseif( $clock[0] == '00' ) {
|
|
|
|
$x = $clock[1];
|
|
|
|
$y = 'minute';
|
|
|
|
} else {
|
|
|
|
if ( $clock[0] < 24 ) {
|
|
|
|
$x = $clock[0];
|
|
|
|
$y = 'hour';
|
|
|
|
} else {
|
|
|
|
$x = floor( $hr / 24 );
|
|
|
|
$y = 'day';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$clockago = $x . ' ' . $y . ( $x > 1 ? 's' : '' );
|
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$res = $dbr->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-02-19 19:31:24 +00:00
|
|
|
'COUNT(*)',
|
|
|
|
array( 'DATE_SUB(CURDATE(), INTERVAL 2 DAY) <= poll_date' ),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2012-03-12 22:13:51 +00:00
|
|
|
$tab2 = $dbr->fetchRow( $res );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
$dbr->commit( __METHOD__ );
|
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
return "There are $tab[1] polls and $tab[0] votes given by $tab[2] different people.<br />
|
2013-05-26 10:03:15 +00:00
|
|
|
The last vote has been given $clockago ago.<br/>
|
|
|
|
During the last 48 hours, $tab2[0] votes have been given.";
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 09:54:00 +00:00
|
|
|
public static function submitVote( $id, $answer, $token ) {
|
|
|
|
global $wgUser,$wgOut,$wgRequest;
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2013-05-26 07:33:05 +00:00
|
|
|
$dbw->begin( __METHOD__ );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
if ( $wgUser->getName() == '' ) {
|
2013-05-26 06:48:12 +00:00
|
|
|
$userName = wfGetIP();
|
2012-02-19 19:31:24 +00:00
|
|
|
} else {
|
2013-05-26 06:48:12 +00:00
|
|
|
$userName = $wgUser->getName();
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-19 09:54:00 +00:00
|
|
|
if ( !$wgUser->matchEditToken( $token, $id ) ) {
|
|
|
|
$pollContainerText = 'ajaxpoll-error-csrf-wrong-token';
|
2013-05-26 10:03:15 +00:00
|
|
|
return self::buildHTML( $id, $userName, '', $pollContainerText );
|
2013-05-19 09:54:00 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 11:36:01 +00:00
|
|
|
if ( !$wgUser->isAllowed( 'ajaxpoll-vote' ) || $wgUser->isAllowed( 'bot' ) ) {
|
2013-05-26 10:03:15 +00:00
|
|
|
return self::buildHTML( $id, $userName );
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
if ( $answer != 0 ) {
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
$answer = ++$answer;
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
$q = $dbw->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-03-12 23:56:59 +00:00
|
|
|
'COUNT(*) AS count',
|
2012-02-19 19:31:24 +00:00
|
|
|
array(
|
2012-03-12 22:13:51 +00:00
|
|
|
'poll_id' => $id,
|
2013-05-26 06:48:12 +00:00
|
|
|
'poll_user' => $userName
|
2012-02-19 19:31:24 +00:00
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2012-03-12 23:56:59 +00:00
|
|
|
$row = $dbw->fetchRow( $q );
|
|
|
|
|
|
|
|
if ( $row['count'] > 0 ) {
|
|
|
|
|
|
|
|
$updateQuery = $dbw->update(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-03-12 23:56:59 +00:00
|
|
|
array(
|
|
|
|
'poll_answer' => $answer,
|
|
|
|
'poll_date' => wfTimestampNow()
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'poll_id' => $id,
|
2013-05-26 06:48:12 +00:00
|
|
|
'poll_user' => $userName,
|
2012-03-12 23:56:59 +00:00
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2013-05-26 07:33:05 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2012-03-12 23:56:59 +00:00
|
|
|
$pollContainerText = ( $updateQuery ) ? 'ajaxpoll-vote-update' : 'ajaxpoll-vote-error';
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
} else {
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
$insertQuery = $dbw->insert(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-03-12 23:56:59 +00:00
|
|
|
array(
|
|
|
|
'poll_id' => $id,
|
2013-05-26 06:48:12 +00:00
|
|
|
'poll_user' => $userName,
|
2012-03-12 23:56:59 +00:00
|
|
|
'poll_ip' => wfGetIP(),
|
|
|
|
'poll_answer' => $answer,
|
|
|
|
'poll_date' => wfTimestampNow()
|
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2013-05-26 07:33:05 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2012-03-12 23:56:59 +00:00
|
|
|
$pollContainerText = ( $insertQuery ) ? 'ajaxpoll-vote-add' : 'ajaxpoll-vote-error';
|
|
|
|
|
|
|
|
}
|
2013-06-04 20:01:09 +00:00
|
|
|
|
2012-03-12 23:56:59 +00:00
|
|
|
} else { // revoking a vote
|
|
|
|
|
|
|
|
$deleteQuery = $dbw->delete(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-02-19 19:31:24 +00:00
|
|
|
array(
|
2012-03-12 22:13:51 +00:00
|
|
|
'poll_id' => $id,
|
2013-05-26 06:48:12 +00:00
|
|
|
'poll_user' => $userName,
|
2012-02-19 19:31:24 +00:00
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
2013-05-26 07:33:05 +00:00
|
|
|
$dbw->commit( __METHOD__ );
|
2012-03-12 23:56:59 +00:00
|
|
|
$pollContainerText = ( $deleteQuery ) ? 'ajaxpoll-vote-revoked' : 'ajaxpoll-vote-error';
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
return self::buildHTML( $id, $userName, '', $pollContainerText );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
private static function escapeContent( $string ) {
|
|
|
|
return htmlspecialchars( Sanitizer::decodeCharReferences( $string ), ENT_QUOTES );
|
|
|
|
}
|
|
|
|
|
2013-05-26 06:48:12 +00:00
|
|
|
private static function buildHTML( $id, $userName, $lines = '', $extra_from_ajax = '' ) {
|
2012-03-10 09:26:52 +00:00
|
|
|
global $wgTitle, $wgUser, $wgLang, $wgUseAjax;
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$q = $dbr->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_info',
|
2013-05-26 10:03:15 +00:00
|
|
|
array( 'poll_txt', 'poll_date', 'poll_show_results_before_voting' ),
|
2012-03-12 22:13:51 +00:00
|
|
|
array( 'poll_id' => $id ),
|
2012-02-19 19:31:24 +00:00
|
|
|
__METHOD__
|
|
|
|
);
|
2012-03-12 22:13:51 +00:00
|
|
|
$row = $dbr->fetchRow( $q );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
if ( empty( $lines ) ) {
|
|
|
|
$lines = explode( "\n", trim( $row['poll_txt'] ) );
|
|
|
|
}
|
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
if ( $row['poll_show_results_before_voting'] !== null ) {
|
|
|
|
$showResultsBeforeVoting = ( $row['poll_show_results_before_voting'] === '1' );
|
|
|
|
} else {
|
|
|
|
$showResultsBeforeVoting = $wgUser->isAllowed( 'ajaxpoll-view-results-before-vote' );
|
|
|
|
}
|
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
$start_date = $row['poll_date'];
|
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$q = $dbr->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-02-19 19:31:24 +00:00
|
|
|
array( 'poll_answer', 'COUNT(*)' ),
|
2012-03-12 22:13:51 +00:00
|
|
|
array( 'poll_id' => $id ),
|
2012-02-19 19:31:24 +00:00
|
|
|
__METHOD__,
|
|
|
|
array( 'GROUP BY' => 'poll_answer' )
|
|
|
|
);
|
|
|
|
|
|
|
|
$poll_result = array();
|
|
|
|
|
|
|
|
while ( $row = $q->fetchRow() ) {
|
|
|
|
$poll_result[$row[0]] = $row[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
$amountOfVotes = array_sum( $poll_result );
|
|
|
|
|
|
|
|
// Did we vote?
|
2013-05-26 10:03:15 +00:00
|
|
|
$userVoted = false;
|
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
$q = $dbr->select(
|
2012-03-14 20:28:16 +00:00
|
|
|
'ajaxpoll_vote',
|
2012-02-19 19:31:24 +00:00
|
|
|
array( 'poll_answer', 'poll_date' ),
|
|
|
|
array(
|
2012-03-12 22:13:51 +00:00
|
|
|
'poll_id' => $id,
|
2013-05-26 06:48:12 +00:00
|
|
|
'poll_user' => $userName
|
2012-02-19 19:31:24 +00:00
|
|
|
),
|
|
|
|
__METHOD__
|
|
|
|
);
|
|
|
|
|
2012-03-12 22:13:51 +00:00
|
|
|
if ( $row = $dbr->fetchRow( $q ) ) {
|
2013-05-26 07:33:05 +00:00
|
|
|
$ourLastVoteDate = wfMessage(
|
2012-02-19 19:31:24 +00:00
|
|
|
'ajaxpoll-your-vote',
|
|
|
|
$lines[$row[0] - 1],
|
|
|
|
$wgLang->timeanddate( wfTimestamp( TS_MW, $row[1] ), true /* adjust? */ )
|
2013-05-26 07:33:05 +00:00
|
|
|
)->escaped();
|
2013-05-26 10:03:15 +00:00
|
|
|
$userVoted = true;
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( is_object( $wgTitle ) ) {
|
2013-06-04 20:01:09 +00:00
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
if( !empty( $extra_from_ajax ) ) {
|
2013-06-04 20:01:09 +00:00
|
|
|
$style = 'display:inline-block';
|
2013-05-26 07:33:05 +00:00
|
|
|
$ajaxMessage = wfMessage( $extra_from_ajax )->escaped();
|
2012-02-19 19:31:24 +00:00
|
|
|
} else {
|
2013-06-04 20:01:09 +00:00
|
|
|
$style = 'display:none';
|
2012-02-19 19:31:24 +00:00
|
|
|
$ajaxMessage = '';
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret = Html::element( 'script',
|
|
|
|
array(),
|
|
|
|
'var useAjax=' . ( !empty($wgUseAjax) ? "true" : "false" ) . ';'
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret .= Html::openElement( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-id-' . $id,
|
|
|
|
'class' => 'ajaxpoll'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret .= Html::element( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-ajax-' . $id,
|
|
|
|
'class' => 'ajaxpoll-ajax',
|
|
|
|
'style' => $style
|
|
|
|
),
|
|
|
|
$ajaxMessage
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret .= Html::rawElement( 'div',
|
|
|
|
array( 'class' => 'ajaxpoll-question' ),
|
|
|
|
self::escapeContent( $lines[0] )
|
|
|
|
);
|
|
|
|
|
|
|
|
// Different messages depending whether the user has already voted
|
|
|
|
// or has not voted, or is entitled to vote
|
2012-03-10 09:26:52 +00:00
|
|
|
|
2012-03-13 01:04:59 +00:00
|
|
|
$canRevoke = false;
|
|
|
|
|
2012-03-10 09:26:52 +00:00
|
|
|
if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
|
2012-03-12 23:56:59 +00:00
|
|
|
if ( isset( $row[0] ) ) {
|
|
|
|
$message = $ourLastVoteDate;
|
2012-03-13 01:04:59 +00:00
|
|
|
$canRevoke = true;
|
2013-05-26 07:33:05 +00:00
|
|
|
$lines[] = wfMessage( 'ajaxpoll-revoke-vote' )->text();
|
2012-03-12 23:56:59 +00:00
|
|
|
} else {
|
2013-05-26 10:03:15 +00:00
|
|
|
if ( $showResultsBeforeVoting ) {
|
|
|
|
$message = wfMessage( 'ajaxpoll-no-vote' )->text();
|
|
|
|
} else {
|
|
|
|
$message = wfMessage( 'ajaxpoll-no-vote-results-after-voting' )->text();
|
|
|
|
}
|
2012-03-12 23:56:59 +00:00
|
|
|
}
|
2012-03-10 09:26:52 +00:00
|
|
|
} else {
|
2013-05-26 07:33:05 +00:00
|
|
|
$message = wfMessage( 'ajaxpoll-vote-permission' )->text();
|
2012-03-10 09:26:52 +00:00
|
|
|
}
|
2013-05-26 10:03:15 +00:00
|
|
|
|
|
|
|
if ( !$wgUser->isAllowed( 'ajaxpoll-view-results' ) ) {
|
|
|
|
|
|
|
|
$message .= "<br/>" . wfMessage( 'ajaxpoll-view-results-permission' )->text();
|
|
|
|
|
|
|
|
} elseif ( !$userVoted
|
|
|
|
&& !$wgUser->isAllowed( 'ajaxpoll-view-results-before-vote' )
|
|
|
|
&& !$showResultsBeforeVoting ) {
|
|
|
|
|
|
|
|
if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
|
|
|
|
$message .= "<br/>" . wfMessage( 'ajaxpoll-view-results-before-vote-permission' )->text();
|
|
|
|
} else {
|
|
|
|
$message .= "<br/>" . wfMessage( 'ajaxpoll-view-results-permission' )->text();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret .= Html::rawElement( 'div',
|
|
|
|
array( 'class' => 'ajaxpoll-misc' ),
|
|
|
|
$message
|
|
|
|
);
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret .= Html::rawElement( 'form',
|
|
|
|
array(
|
|
|
|
'method' => 'post',
|
|
|
|
'action' => $wgTitle->getLocalURL(),
|
|
|
|
'id' => 'ajaxpoll-answer-id-' . $id
|
|
|
|
),
|
|
|
|
Html::element( 'input',
|
|
|
|
array(
|
|
|
|
'type' => 'hidden',
|
|
|
|
'name' => 'ajaxpoll-post-id',
|
|
|
|
'value' => $id
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
for ( $i = 1; $i < count( $lines ); $i++ ) {
|
2012-03-12 23:56:59 +00:00
|
|
|
|
2012-03-13 01:04:59 +00:00
|
|
|
$vote = !( $canRevoke && ( $i == count( $lines ) - 1 ) );
|
|
|
|
|
2012-03-15 07:46:55 +00:00
|
|
|
// answers are counted from 1 ... n
|
|
|
|
// last answer is pseudo-answer for "I want to revoke vote"
|
|
|
|
// and becomes answer number 0
|
|
|
|
|
|
|
|
$answer = ( $vote ) ? $i : 0;
|
|
|
|
$xid = $id . "-" . $answer;
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
if ( ( $amountOfVotes !== 0 ) && ( isset( $poll_result[$i + 1] ) ) ) {
|
|
|
|
$pollResult = $poll_result[$i + 1];
|
|
|
|
$percent = round( $pollResult * 100 / $amountOfVotes, 2 );
|
2012-02-19 19:31:24 +00:00
|
|
|
} else {
|
2013-06-04 20:01:09 +00:00
|
|
|
$pollResult = 0;
|
|
|
|
$percent = 0;
|
2012-02-19 19:31:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If AJAX is enabled, as it is by default in modern MWs, we can
|
|
|
|
// just use sajax library function here for that AJAX-y feel.
|
|
|
|
// If not, we'll have to submit the form old-school way...
|
2012-03-10 09:26:52 +00:00
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$border = ( $percent == 0 ) ? ' border:0;' : '';
|
|
|
|
$isOurVote = ( isset( $row[0] ) && ( $row[0] - 1 == $i ) );
|
|
|
|
|
|
|
|
$resultBar = '';
|
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
if ( $wgUser->isAllowed( 'ajaxpoll-view-results' )
|
2013-06-04 20:01:09 +00:00
|
|
|
&& ( $showResultsBeforeVoting || ( !$showResultsBeforeVoting && $userVoted ) )
|
|
|
|
&& $vote ) {
|
|
|
|
|
|
|
|
$resultBar = Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'class' => 'ajaxpoll-answer-vote' . ( $isOurVote ? ' ajaxpoll-our-vote' : '' )
|
|
|
|
),
|
|
|
|
Html::rawElement( 'span',
|
|
|
|
array(
|
|
|
|
'title' => wfMessage( 'ajaxpoll-percent-votes' )->numParams( $percent )->escaped()
|
|
|
|
),
|
|
|
|
self::escapeContent( $pollResult )
|
|
|
|
) .
|
|
|
|
Html::element( 'div',
|
|
|
|
array(
|
|
|
|
'style' => 'width:' . $percent . '%;' . $border
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-10 09:26:52 +00:00
|
|
|
if ( $wgUser->isAllowed( 'ajaxpoll-vote' ) ) {
|
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret .= Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-answer-' . $xid,
|
|
|
|
'class' => 'ajaxpoll-answer',
|
|
|
|
'poll' => $id,
|
|
|
|
'answer' => $answer,
|
|
|
|
),
|
|
|
|
Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'class' => 'ajaxpoll-answer-name' . ( $vote ? ' ajaxpoll-answer-name-revoke' : '' )
|
|
|
|
),
|
|
|
|
Html::rawElement( 'label',
|
|
|
|
array( 'for' => 'ajaxpoll-post-answer-' . $xid ),
|
|
|
|
Html::element( 'input',
|
|
|
|
array(
|
|
|
|
'type' => 'radio',
|
|
|
|
'id' => 'ajaxpoll-post-answer-' . $xid,
|
|
|
|
'name' => 'ajaxpoll-post-answer-' . $id,
|
|
|
|
'value' => $answer,
|
|
|
|
'checked' => $isOurVote ? "true" : false,
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
self::escapeContent( $lines[$i] )
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
$resultBar
|
|
|
|
);
|
2012-03-10 09:26:52 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret .= Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-answer-' . $xid,
|
|
|
|
'class' => 'ajaxpoll-answer',
|
|
|
|
'poll' => $id,
|
|
|
|
'answer' => $answer
|
|
|
|
),
|
|
|
|
Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'class' => 'ajaxpoll-answer-name'
|
|
|
|
),
|
|
|
|
Html::rawElement( 'label',
|
|
|
|
array(
|
|
|
|
'for' => 'ajaxpoll-post-answer-' . $xid,
|
|
|
|
'onclick' => '$("#ajaxpoll-ajax-"' . $xid . '").html("' . wfMessage( 'ajaxpoll-vote-permission' )->text() . '").css("display","block");'
|
|
|
|
),
|
|
|
|
Html::element( 'input',
|
|
|
|
array(
|
|
|
|
'disabled' => 'disabled',
|
|
|
|
'type' => 'radio',
|
|
|
|
'id' => 'ajaxpoll-post-answer-' . $xid,
|
|
|
|
'name' => 'ajaxpoll-post-answer-' . $id,
|
|
|
|
'value' => $answer
|
|
|
|
)
|
|
|
|
) .
|
|
|
|
self::escapeContent( $lines[$i] )
|
|
|
|
)
|
|
|
|
),
|
|
|
|
$resultBar
|
|
|
|
);
|
|
|
|
|
2012-03-10 09:26:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2013-05-19 09:54:00 +00:00
|
|
|
$ret .= Html::Hidden( 'ajaxPollToken', $wgUser->getEditToken( $id ) ) .
|
|
|
|
Xml::closeElement( 'form' );
|
2012-02-19 19:31:24 +00:00
|
|
|
|
|
|
|
// Display information about the poll (creation date, amount of votes)
|
2013-06-04 20:01:09 +00:00
|
|
|
|
2013-05-26 07:33:05 +00:00
|
|
|
$pollSummary = wfMessage(
|
2012-02-19 19:31:24 +00:00
|
|
|
'ajaxpoll-info',
|
|
|
|
$amountOfVotes, // amount of votes
|
|
|
|
$wgLang->timeanddate( wfTimestamp( TS_MW, $start_date ), true /* adjust? */ )
|
2013-05-26 07:33:05 +00:00
|
|
|
)->text();
|
2012-02-19 19:31:24 +00:00
|
|
|
|
2013-06-04 20:01:09 +00:00
|
|
|
$ret .= Html::rawElement( 'div',
|
|
|
|
array(
|
|
|
|
'id' => 'ajaxpoll-info-' . $id,
|
|
|
|
'class' => 'ajaxpoll-info'
|
|
|
|
),
|
|
|
|
self::escapeContent( $pollSummary ) .
|
|
|
|
Html::element( 'div',
|
|
|
|
array( 'class' => 'ajaxpoll-id-info' ),
|
|
|
|
'poll-id ' . $id
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
$ret .= Html::closeElement( 'div' ) .
|
|
|
|
Html::element( 'br' );
|
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
} else {
|
|
|
|
$ret = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ret;
|
|
|
|
}
|
|
|
|
|
2012-03-14 20:19:16 +00:00
|
|
|
public static function onLoadExtensionSchemaUpdates( $updater = null ) {
|
|
|
|
if ( $updater === null ) {
|
|
|
|
// no < 1.17 support
|
|
|
|
} else {
|
|
|
|
// >= 1.17 support
|
2013-01-01 08:42:59 +00:00
|
|
|
$db = $updater->getDB();
|
2012-03-14 20:19:16 +00:00
|
|
|
|
2013-05-26 10:03:15 +00:00
|
|
|
$patchPath = dirname( __FILE__ ) . '/patches/';
|
|
|
|
|
2013-01-01 08:42:59 +00:00
|
|
|
if ( $db->tableExists( 'poll_info' ) ) {
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
# poll_info.poll_title field was dropped in AJAXPoll version 1.72
|
2013-06-04 20:01:09 +00:00
|
|
|
$updater->dropExtensionField(
|
2012-03-14 21:43:56 +00:00
|
|
|
'poll_info',
|
|
|
|
'poll_title',
|
2013-05-26 10:03:15 +00:00
|
|
|
$patchPath . 'drop-field--poll_info-poll_title.sql'
|
2012-03-14 21:43:56 +00:00
|
|
|
);
|
|
|
|
$updater->addExtensionTable(
|
|
|
|
'ajaxpoll_info',
|
2013-05-26 10:03:15 +00:00
|
|
|
$patchPath . 'rename-table--poll_info.sql'
|
2012-03-14 21:43:56 +00:00
|
|
|
);
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
} else {
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
$updater->addExtensionTable(
|
|
|
|
'ajaxpoll_info',
|
2013-05-26 10:03:15 +00:00
|
|
|
$patchPath . 'create-table--ajaxpoll_info.sql'
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $db->tableExists( 'ajaxpoll_info' ) ) {
|
|
|
|
$updater->addExtensionField(
|
|
|
|
'ajaxpoll_info',
|
|
|
|
'poll_show_results_before_voting',
|
|
|
|
$patchPath . 'add-field--ajaxpoll_info-poll_show_results_before_voting.sql'
|
2012-03-14 21:43:56 +00:00
|
|
|
);
|
|
|
|
}
|
2012-03-14 20:19:16 +00:00
|
|
|
|
2013-01-01 08:42:59 +00:00
|
|
|
if ( $db->tableExists( 'poll_vote' ) ) {
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
$updater->addExtensionTable(
|
2013-05-26 10:03:15 +00:00
|
|
|
'poll_vote',
|
|
|
|
$patchPath . 'rename-table--poll_vote.sql'
|
2012-03-14 21:43:56 +00:00
|
|
|
);
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
} else {
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
$updater->addExtensionTable(
|
|
|
|
'ajaxpoll_vote',
|
2013-05-26 10:03:15 +00:00
|
|
|
$patchPath . 'create-table--ajaxpoll_vote.sql'
|
2012-03-14 21:43:56 +00:00
|
|
|
);
|
2013-05-26 10:03:15 +00:00
|
|
|
|
2012-03-14 21:43:56 +00:00
|
|
|
}
|
2012-03-14 20:19:16 +00:00
|
|
|
|
|
|
|
}
|
2012-03-14 21:43:56 +00:00
|
|
|
|
2012-03-14 20:19:16 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-19 19:31:24 +00:00
|
|
|
} /* class AJAXPoll */
|