r96539 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r96538‎ | r96539 | r96540 >
Date:02:13, 8 September 2011
Author:werdna
Status:deferred
Tags:securepoll 
Comment:
Add another script to dump votes as a CSV with votes without comments included
Modified paths:
  • /trunk/extensions/SecurePoll/cli/dumpVoteCsv.php (added) (history)
  • /trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php
@@ -5,9 +5,17 @@
66 */
77 class SecurePoll_CommentDumper extends SecurePoll_ElectionTallier {
88 var $csvHandle;
 9+ var $skipEmptyComments;
 10+ private $countSoFar;
 11+
 12+ public function __construct( $context, $election, $skipEmptyComments = true ) {
 13+ parent::__construct( $context, $election );
 14+ $this->skipEmptyComments = $skipEmptyComments;
 15+ }
916
1017 function execute() {
1118 $this->csvHandle = fopen( 'php://temp', 'r+' );
 19+ $this->countSoFar = 0;
1220 return parent::execute();
1321 }
1422
@@ -21,6 +29,8 @@
2230 * @return Status
2331 */
2432 function addRecord( $store, $record ) {
 33+ $this->countSoFar++;
 34+ wfDebug( "Processing vote {$this->countSoFar}\n" );
2535 # Decrypt and unpack
2636 if ( $this->crypt ) {
2737 $status = $this->crypt->decrypt( $record );
@@ -36,7 +46,9 @@
3747 unset($scores['comment']);
3848
3949 // Short circuit if the comments are empty
40 - if ( $comments['native'] == '' && $comments['en'] == '' ) {
 50+ if ( $this->skipEmptyComments &&
 51+ $comments['native'] == '' && $comments['en'] == '' )
 52+ {
4153 return Status::newGood();
4254 }
4355
Index: trunk/extensions/SecurePoll/cli/dumpVoteCsv.php
@@ -0,0 +1,69 @@
 2+<?php
 3+
 4+/**
 5+ * Dump all votes from an election from a dump file or local database.
 6+ *
 7+ * For the purposes of the Personal Image Filter referendum, this script
 8+ * dumps the answers to all questions in key order.
 9+ *
 10+ * Can be used to tally very large numbers of votes, when the web interface is
 11+ * not feasible.
 12+ */
 13+
 14+$optionsWithArgs = array( 'name' );
 15+require( dirname(__FILE__).'/cli.inc' );
 16+
 17+$wgTitle = Title::newFromText( 'Special:SecurePoll' );
 18+
 19+$usage = <<<EOT
 20+Usage:
 21+ php dumpVoteCsv.php [--html] --name <election name>
 22+ php dumpVoteCsv.php [--html] <dump file>
 23+EOT;
 24+
 25+if ( !isset( $options['name'] ) && !isset( $args[0] ) ) {
 26+ spFatal( $usage );
 27+}
 28+
 29+if ( !class_exists( 'SecurePoll_Context' ) ) {
 30+ if ( isset( $options['name'] ) ) {
 31+ spFatal( "Cannot load from database when SecurePoll is not installed" );
 32+ }
 33+ require( dirname( __FILE__ ) . '/../SecurePoll.php' );
 34+}
 35+
 36+$context = new SecurePoll_Context;
 37+if ( !isset( $options['name'] ) ) {
 38+ $context = SecurePoll_Context::newFromXmlFile( $args[0] );
 39+ if ( !$context ) {
 40+ spFatal( "Unable to parse XML file \"{$args[0]}\"" );
 41+ }
 42+ $electionIds = $context->getStore()->getAllElectionIds();
 43+ if ( !count( $electionIds ) ) {
 44+ spFatal( "No elections found in XML file \"{$args[0]}\"" );
 45+ }
 46+ $election = $context->getElection( reset( $electionIds ) );
 47+} else {
 48+ $election = $context->getElectionByTitle( $options['name'] );
 49+ if ( !$election ) {
 50+ spFatal( "The specified election does not exist." );
 51+ }
 52+}
 53+
 54+$tallier = new SecurePoll_CommentDumper( $context, $election, false );
 55+$status = $tallier->execute();
 56+if ( !$status->isOK() ) {
 57+ spFatal( "Tally error: " . $status->getWikiText() );
 58+}
 59+//$tallier = $status->value;
 60+if ( isset( $options['html'] ) ) {
 61+ echo $tallier->getHtmlResult();
 62+} else {
 63+ echo $tallier->getTextResult();
 64+}
 65+
 66+
 67+function spFatal( $message ) {
 68+ fwrite( STDERR, rtrim( $message ) . "\n" );
 69+ exit( 1 );
 70+}

Status & tagging log