r94343 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94342‎ | r94343 | r94344 >
Date:12:58, 12 August 2011
Author:werdna
Status:old (Comments)
Tags:securepoll 
Comment:
Add a new type of ballot: Radio range with comment. Solicits the user for comments, and sticks it in the ballot. This means that the size of ballots changes, but not on the basis of the vote they take, which is the important reason why ballot size cannot differ by the user (to avoid revealing the actual votes).
Modified paths:
  • /trunk/extensions/SecurePoll/SecurePoll.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/ballots/Ballot.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/ballots/RadioRangeCommentBallot.php (added) (history)

Diff [purge]

Index: trunk/extensions/SecurePoll/SecurePoll.php
@@ -53,6 +53,7 @@
5454 'SecurePoll_ChooseBallot' => "$dir/includes/ballots/ChooseBallot.php",
5555 'SecurePoll_PreferentialBallot' => "$dir/includes/ballots/PreferentialBallot.php",
5656 'SecurePoll_RadioRangeBallot' => "$dir/includes/ballots/RadioRangeBallot.php",
 57+ 'SecurePoll_RadioRangeCommentBallot' => "$dir/includes/ballots/RadioRangeCommentBallot.php",
5758
5859 # crypt
5960 'SecurePoll_Crypt' => "$dir/includes/crypt/Crypt.php",
Index: trunk/extensions/SecurePoll/includes/ballots/RadioRangeCommentBallot.php
@@ -0,0 +1,114 @@
 2+<?php
 3+
 4+/**
 5+ * A ballot used specifically for the Wikimedia Referendum on the personal image filter.
 6+ * Allows voters to send comments in with their ballot
 7+ */
 8+class SecurePoll_RadioRangeCommentBallot extends SecurePoll_RadioRangeBallot {
 9+ public function getForm( $prevStatus = false ) {
 10+ $form = parent::getForm( $prevStatus );
 11+
 12+ $form .= Html::element('hr');
 13+ // Add the comment call
 14+ $callForComments = $this->election->parseMessage( 'comments' );
 15+ $form .= Html::rawElement( 'p', null, $callForComments );
 16+
 17+ // Add the comments boxes.
 18+ $form .= Html::textarea( 'securepoll_comments_native', '',
 19+ array( 'rows' => 10, 'cols' => 20 ) );
 20+ $form .= Html::textarea( 'securepoll_comments_en', '',
 21+ array( 'rows' => 10, 'cols' => 20 ) );
 22+
 23+ return $form;
 24+ }
 25+
 26+ public function submitForm() {
 27+ $status = parent::submitForm();
 28+
 29+ if ( ! $status->isGood() ) {
 30+ return $status;
 31+ }
 32+
 33+ // Load comments
 34+ global $wgRequest;
 35+
 36+ $commentNative = $wgRequest->getText( 'securepoll_comments_native' );
 37+ $commentEnglish = $wgRequest->getText( 'securepoll_comments_en' );
 38+
 39+ $record = rtrim($status->value);
 40+
 41+ $record .= '/'.strlen($commentNative).'/'.$commentNative;
 42+ $record .= '--/'.strlen($commentEnglish).'/'.$commentEnglish;
 43+
 44+ $status->value = $record;
 45+
 46+ return $status;
 47+ }
 48+
 49+ /**
 50+ * Copy and modify from parent function, complex to refactor.
 51+ */
 52+ function unpackRecord( $record ) {
 53+ $scores = array();
 54+ $itemLength = 8 + 8 + 11 + 7;
 55+ $questions = array();
 56+ foreach ( $this->election->getQuestions() as $question ) {
 57+ $questions[$question->getId()] = $question;
 58+ }
 59+ for ( $offset = 0; $offset < strlen( $record ); $offset += $itemLength ) {
 60+ if ( !preg_match( '/Q([0-9A-F]{8})-A([0-9A-F]{8})-S([+-][0-9]{10})--/A',
 61+ $record, $m, 0, $offset ) )
 62+ {
 63+ // Allow comments
 64+ if ( $record[$offset] == '/' ) {
 65+ break;
 66+ }
 67+
 68+ wfDebug( __METHOD__.": regex doesn't match\n" );
 69+ return false;
 70+ }
 71+ $qid = intval( base_convert( $m[1], 16, 10 ) );
 72+ $oid = intval( base_convert( $m[2], 16, 10 ) );
 73+ $score = intval( $m[3] );
 74+ if ( !isset( $questions[$qid] ) ) {
 75+ wfDebug( __METHOD__.": invalid question ID\n" );
 76+ return false;
 77+ }
 78+ list( $min, $max ) = $this->getMinMax( $questions[$qid] );
 79+ if ( $score < $min || $score > $max ) {
 80+ wfDebug( __METHOD__.": score out of range\n" );
 81+ return false;
 82+ }
 83+ $scores[$qid][$oid] = $score;
 84+ }
 85+
 86+ // Read comments
 87+ $scores['comment'] = array();
 88+
 89+ $scores['comment']['native'] = $this->readComment( $record, $offset );
 90+
 91+ if ( substr( $record, $offset, 2 ) !== '--' ) {
 92+ wfDebug( __METHOD__.": Invalid format\n" );
 93+ return false;
 94+ }
 95+ $offset += 2;
 96+
 97+ $scores['comment']['en'] = $this->readComment( $record, $offset );
 98+
 99+ if ( $offset < strlen($record) ) {
 100+ wfDebug( __METHOD__.": Invalid format\n" );
 101+ return false;
 102+ }
 103+
 104+ return $scores;
 105+ }
 106+
 107+ function readComment( $record, &$offset ) {
 108+ $commentOffset = strpos( $record, '/', $offset+1 );
 109+ $commentLength = intval(substr( $record, $offset+1,
 110+ ($commentOffset - $offset) - 1 ) );
 111+ $result = substr( $record, $commentOffset + 1, $commentLength );
 112+ $offset = $commentOffset + $commentLength + 1; // Fast-forward
 113+ return $result;
 114+ }
 115+}
Index: trunk/extensions/SecurePoll/includes/ballots/Ballot.php
@@ -97,6 +97,8 @@
9898 return new SecurePoll_ChooseBallot( $context, $election );
9999 case 'radio-range':
100100 return new SecurePoll_RadioRangeBallot( $context, $election );
 101+ case 'radio-range-comment':
 102+ return new SecurePoll_RadioRangeCommentBallot( $context, $election );
101103 default:
102104 throw new MWException( "Invalid ballot type: $type" );
103105 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r94521MFT r94343 r94344 r94415werdna15:04, 15 August 2011

Comments

#Comment by MZMcBride (talk | contribs)   20:28, 15 August 2011

Explicitly Wikimedia-specific code in trunk? Seems strange. I thought the reason for having Wikimedia branches was for code like this.

#Comment by 😂 (talk | contribs)   20:33, 15 August 2011

The cool thing about SecurePoll is various ballot types like this don't affect others. If you want to use SecurePoll but don't want this ballot, don't use it :)

#Comment by 😂 (talk | contribs)   14:26, 16 August 2011

"for ( $offset = 0; $offset < strlen( $record ); $offset += $itemLength ) {"

Move the strlen() call above the loop?

Status & tagging log