r64483 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64482‎ | r64483 | r64484 >
Date:11:55, 1 April 2010
Author:happy-melon
Status:resolved
Tags:
Comment:
Follow-ups to r58887. There might be some stuff from my configuration interface work jumbled in; but hopefully that's kept separate.
Modified paths:
  • /trunk/extensions/SecurePoll/includes/ballots/Ballot.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/entities/Election.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/entities/Entity.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/entities/Question.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/main/Context.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/main/Store.php (modified) (history)
  • /trunk/extensions/SecurePoll/includes/talliers/PluralityTallier.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SecurePoll/includes/talliers/PluralityTallier.php
@@ -35,7 +35,7 @@
3636
3737 foreach ( $this->tally as $oid => $rank ) {
3838 $option = $this->optionsById[$oid];
39 - $s .= '<tr><td>' . $option->getMessage( 'text' ) . "</td>\n" .
 39+ $s .= '<tr><td>' . $option->parseMessageInline( 'text' ) . "</td>\n" .
4040 '<td>' . $this->tally[$oid] . "</td>\n" .
4141 "</tr>\n";
4242 }
Index: trunk/extensions/SecurePoll/includes/ballots/Ballot.php
@@ -176,6 +176,53 @@
177177 function formatStatus( $status ) {
178178 return $status->sp_getHTML( $this->usedErrorIds );
179179 }
 180+
 181+ /**
 182+ * Get the way the voter cast their vote previously, if we're allowed
 183+ * to show that information.
 184+ * @return false on failure or if cast ballots are hidden, or the output
 185+ * of unpackRecord().
 186+ */
 187+ function getCurrentVote(){
 188+
 189+ if( !$this->election->getOption( 'show-change' ) ){
 190+ return false;
 191+ }
 192+
 193+ $auth = $this->election->getAuth();
 194+
 195+ # Get voter from session
 196+ $voter = $auth->getVoterFromSession( $this->election );
 197+ # If there's no session, try creating one.
 198+ # This will fail if the user is not authorised to vote in the election
 199+ if ( !$voter ) {
 200+ $status = $auth->newAutoSession( $this->election );
 201+ if ( $status->isOK() ) {
 202+ $voter = $status->value;
 203+ } else {
 204+ return false;
 205+ }
 206+ }
 207+
 208+ $store = $this->context->getStore();
 209+ $status = $store->callbackValidVotes(
 210+ $this->election->info['id'],
 211+ array( $this, 'getCurrentVoteCallback' ),
 212+ $voter->getId()
 213+ );
 214+ if( !$status->isOK() ){
 215+ return false;
 216+ }
 217+
 218+ return isset( $this->currentVote )
 219+ ? $this->unpackRecord( $this->currentVote )
 220+ : false;
 221+ }
 222+
 223+ function getCurrentVoteCallback( $store, $record ){
 224+ $this->currentVote = $record;
 225+ return Status::newGood();
 226+ }
180227 }
181228
182229 class SecurePoll_BallotStatus extends Status {
Index: trunk/extensions/SecurePoll/includes/entities/Entity.php
@@ -12,6 +12,7 @@
1313 */
1414 class SecurePoll_Entity {
1515 var $id;
 16+ var $electionId;
1617 var $context;
1718 var $messagesLoaded = array();
1819 var $properties;
@@ -26,7 +27,12 @@
2728 function __construct( $context, $type, $info ) {
2829 $this->context = $context;
2930 $this->type = $type;
30 - $this->id = isset( $info['id'] ) ? $info['id'] : false;
 31+ $this->id = isset( $info['id'] )
 32+ ? $info['id']
 33+ : false;
 34+ $this->electionId = isset( $info['election'] )
 35+ ? $info['election']
 36+ : null;
3137 }
3238
3339 /**
@@ -53,6 +59,16 @@
5460 function getId() {
5561 return $this->id;
5662 }
 63+
 64+ /**
 65+ * Get the parent election
 66+ * @return Int
 67+ */
 68+ public function getElection() {
 69+ return $this->electionId !== null
 70+ ? $this->context->getElection( $this->electionId )
 71+ : null;
 72+ }
5773
5874 /**
5975 * Get the child entity objects. When the messages of an object are loaded,
Index: trunk/extensions/SecurePoll/includes/entities/Election.php
@@ -53,17 +53,20 @@
5454 */
5555 class SecurePoll_Election extends SecurePoll_Entity {
5656 var $questions, $auth, $ballot;
57 - var $title, $ballotType, $tallyType, $primaryLang, $startDate, $endDate, $authType;
 57+ var $id, $title, $ballotType, $tallyType, $primaryLang;
 58+ var $startDate, $endDate, $authType;
5859
5960 /**
6061 * Constructor.
6162 *
62 - * Do not use this constructor directly, instead use SecurePoll_Context::getElection().
 63+ * Do not use this constructor directly, instead use
 64+ * SecurePoll_Context::getElection().
6365 *
6466 * @param $id integer
6567 */
6668 function __construct( $context, $info ) {
6769 parent::__construct( $context, 'election', $info );
 70+ $this->id = $info['id'];
6871 $this->title = $info['title'];
6972 $this->ballotType = $info['ballot'];
7073 $this->tallyType = $info['tally'];
@@ -85,6 +88,13 @@
8689 'unqualified-error',
8790 );
8891 }
 92+
 93+ /**
 94+ * Get the election's parent election... hmm...
 95+ */
 96+ function getElection() {
 97+ return $this->id;
 98+ }
8999
90100 /**
91101 * Get a list of child entity objects. See SecurePoll_Entity.
Index: trunk/extensions/SecurePoll/includes/entities/Question.php
@@ -18,14 +18,13 @@
1919 foreach ( $info['options'] as $optionInfo ) {
2020 $this->options[] = new SecurePoll_Option( $context, $optionInfo );
2121 }
22 - $this->electionId = $info['election'];
2322 }
2423
2524 /**
2625 * Get a list of localisable message names.
2726 */
2827 function getMessageNames() {
29 - $ballot = $this->context->getElection( $this->electionId )->getBallot();
 28+ $ballot = $this->getElection()->getBallot();
3029 return array_merge( $ballot->getMessageNames( $this ), array( 'text' ) );
3130
3231 }
Index: trunk/extensions/SecurePoll/includes/main/Store.php
@@ -32,6 +32,14 @@
3333 * mapping IDs and property keys to values.
3434 */
3535 function getProperties( $ids );
 36+
 37+ /**
 38+ * Get the type of one or more SecurePoll entities.
 39+ * @param $ids Int
 40+ * @return String
 41+ */
 42+ function getEntityType( $id );
 43+
3644
3745 /**
3846 * Get information about a set of elections, specifically the data that
@@ -204,7 +212,10 @@
205213 );
206214 $options = array();
207215 }
208 - $options[] = array( 'id' => $row->op_entity );
 216+ $options[] = array(
 217+ 'id' => $row->op_entity,
 218+ 'election' => $row->op_election,
 219+ );
209220 $questionId = $row->qu_entity;
210221 $electionId = $row->qu_election;
211222 }
@@ -218,25 +229,43 @@
219230 return $questions;
220231 }
221232
222 - function callbackValidVotes( $electionId, $callback ) {
 233+ function callbackValidVotes( $electionId, $callback, $voterId=null ) {
223234 $dbr = $this->getDB();
 235+ $where = array(
 236+ 'vote_election' => $electionId,
 237+ 'vote_current' => 1,
 238+ 'vote_struck' => 0
 239+ );
 240+ if( $voterId !== null ){
 241+ $where['vote_voter'] = $voterId;
 242+ }
224243 $res = $dbr->select(
225244 'securepoll_votes',
226 - array( 'vote_record' ),
227 - array(
228 - 'vote_election' => $electionId,
229 - 'vote_current' => 1,
230 - 'vote_struck' => 0
231 - ), __METHOD__
 245+ '*',
 246+ $where,
 247+ __METHOD__
232248 );
 249+
233250 foreach ( $res as $row ) {
234251 $status = call_user_func( $callback, $this, $row->vote_record );
235 - if ( $status && !$status->isOK() ) {
 252+ if( $status instanceof Status && !$status->isOK() ){
236253 return $status;
237254 }
238255 }
239256 return Status::newGood();
240257 }
 258+
 259+ function getEntityType( $id ){
 260+ $db = $this->getDB();
 261+ $res = $db->selectRow(
 262+ 'securepoll_entity',
 263+ '*',
 264+ array( 'en_id' => $id ),
 265+ __METHOD__ );
 266+ return $row
 267+ ? $row->en_type
 268+ : false;
 269+ }
241270 }
242271
243272 /**
@@ -325,6 +354,12 @@
326355 }
327356 return Status::newGood();
328357 }
 358+
 359+ function getEntityType( $id ){
 360+ return array_key_exists( $this->entityInfo[$id] )
 361+ ? $this->entityInfo[$id]['type']
 362+ : false;
 363+ }
329364 }
330365
331366 /**
@@ -348,7 +383,7 @@
349384 'auth'
350385 ),
351386 'question' => array( 'id', 'election' ),
352 - 'option' => array( 'id' ),
 387+ 'option' => array( 'id', 'election' ),
353388 );
354389
355390 /** The type of each entity child and its corresponding (plural) info element */
@@ -548,6 +583,11 @@
549584 wfDebug( __METHOD__.": missing id element in <$entityType>\n" );
550585 return false;
551586 }
 587+
 588+ # This has to be done after the element is fully parsed, or you
 589+ # have to require 'id' to be above any children in the XML doc.
 590+ $this->addParentIds( $info, $info['type'], $info['id'] );
 591+
552592 $id = $info['id'];
553593 if ( isset( $info['title'] ) ) {
554594 $this->idsByName[$info['title']] = $id;
@@ -559,6 +599,21 @@
560600 $this->properties[$id] = $properties;
561601 return $info;
562602 }
 603+
 604+ /**
 605+ * Propagate parent ids to child elements
 606+ */
 607+ public function addParentIds( &$info, $key, $id ) {
 608+ foreach ( self::$childTypes[$info['type']] as $childType ) {
 609+ if( isset( $info[$childType] ) ) {
 610+ foreach ( $info[$childType] as &$child ) {
 611+ $child[$key] = $id;
 612+ # Recurse
 613+ $this->addParentIds( $child, $key, $id );
 614+ }
 615+ }
 616+ }
 617+ }
563618
564619 /**
565620 * When the cursor is positioned on an element node, this reads the entire
Index: trunk/extensions/SecurePoll/includes/main/Context.php
@@ -21,6 +21,9 @@
2222
2323 /** Message text cache */
2424 var $messageCache = array();
 25+
 26+ /** election cache */
 27+ var $electionCache = array();
2528
2629 /**
2730 * Which messages are loaded. 2-d array: language and entity ID, value arbitrary.
@@ -91,18 +94,26 @@
9295 $this->messageCache = $this->messagesLoaded = array();
9396 $this->store = $store;
9497 }
 98+
 99+ /** Get the type of a particular entity **/
 100+ function getEntityType( $id ){
 101+ return $this->getStore()->getEntityType( $id );
 102+ }
95103
96104 /**
97105 * Get an election object from the store, with a given entity ID. Returns
98106 * false if it does not exist.
99107 */
100108 function getElection( $id ) {
101 - $info = $this->getStore()->getElectionInfo( array( $id ) );
102 - if ( $info ) {
103 - return $this->newElection( reset( $info ) );
104 - } else {
105 - return false;
 109+ if( !isset( $this->electionCache[$id] ) ){
 110+ $info = $this->getStore()->getElectionInfo( array( $id ) );
 111+ if ( $info ) {
 112+ $this->electionCache[$id] = $this->newElection( reset( $info ) );
 113+ } else {
 114+ $this->electionCache[$id] = false;
 115+ }
106116 }
 117+ return $this->electionCache[$id];
107118 }
108119
109120 /**

Follow-up revisions

RevisionCommit summaryAuthorDate
r81235SecurePoll: fixes for r64483:...tstarling06:13, 31 January 2011

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r58887Fix code flow so that messages added to questions by the ballot type (ie colu...happy-melon22:26, 10 November 2009

Status & tagging log