http://www.mediawiki.org/wiki/Special:Code/MediaWiki/95737

Revision: 95737
Author:   werdna
Date:     2011-08-30 08:03:41 +0000 (Tue, 30 Aug 2011)
Log Message:
-----------
SecurePoll: Create interface to dump comments from elections that include them. 
Quick and dirty for getting the results of the PIF election

Modified Paths:
--------------
    trunk/extensions/SecurePoll/SecurePoll.php
    trunk/extensions/SecurePoll/includes/talliers/HistogramRangeTallier.php

Added Paths:
-----------
    trunk/extensions/SecurePoll/cli/dumpComments.php
    trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php

Modified: trunk/extensions/SecurePoll/SecurePoll.php
===================================================================
--- trunk/extensions/SecurePoll/SecurePoll.php  2011-08-30 07:23:31 UTC (rev 
95736)
+++ trunk/extensions/SecurePoll/SecurePoll.php  2011-08-30 08:03:41 UTC (rev 
95737)
@@ -95,6 +95,7 @@
        'SecurePoll_SchulzeTallier' => 
"$dir/includes/talliers/SchulzeTallier.php",
        'SecurePoll_AlternativeVoteTallier' => 
"$dir/includes/talliers/AlternativeVoteTallier.php",
        'SecurePoll_Tallier' => "$dir/includes/talliers/Tallier.php",
+       'SecurePoll_CommentDumper' => 
"$dir/includes/talliers/CommentDumper.php",
 
        # user
        'SecurePoll_Auth' => "$dir/includes/user/Auth.php",

Added: trunk/extensions/SecurePoll/cli/dumpComments.php
===================================================================
--- trunk/extensions/SecurePoll/cli/dumpComments.php                            
(rev 0)
+++ trunk/extensions/SecurePoll/cli/dumpComments.php    2011-08-30 08:03:41 UTC 
(rev 95737)
@@ -0,0 +1,69 @@
+<?php
+
+/**
+ * Dump the comments from an election from a dump file or local database.
+ *
+ * For the purposes of the Personal Image Filter referendum, this script
+ * dumps the answers to all questions in key order.
+ *
+ * Can be used to tally very large numbers of votes, when the web interface is 
+ * not feasible.
+ */
+
+$optionsWithArgs = array( 'name' );
+require( dirname(__FILE__).'/cli.inc' );
+
+$wgTitle = Title::newFromText( 'Special:SecurePoll' );
+
+$usage = <<<EOT
+Usage: 
+  php dumpComments.php [--html] --name <election name>
+  php dumpComments.php [--html] <dump file>
+EOT;
+
+if ( !isset( $options['name'] ) && !isset( $args[0] ) ) {
+       spFatal( $usage );
+}
+
+if ( !class_exists( 'SecurePoll_Context' ) ) {
+       if ( isset( $options['name'] ) ) {
+               spFatal( "Cannot load from database when SecurePoll is not 
installed" );
+       }
+       require( dirname( __FILE__ ) . '/../SecurePoll.php' );
+}
+
+$context = new SecurePoll_Context;
+if ( !isset( $options['name'] ) ) {
+       $context = SecurePoll_Context::newFromXmlFile( $args[0] );
+       if ( !$context ) {
+               spFatal( "Unable to parse XML file \"{$args[0]}\"" );
+       }
+       $electionIds = $context->getStore()->getAllElectionIds();
+       if ( !count( $electionIds ) ) {
+               spFatal( "No elections found in XML file \"{$args[0]}\"" );
+       }
+       $election = $context->getElection( reset( $electionIds ) );
+} else {
+       $election = $context->getElectionByTitle( $options['name'] );
+       if ( !$election ) {
+               spFatal( "The specified election does not exist." );
+       }
+}
+
+$tallier = new SecurePoll_CommentDumper( $context, $election );
+$status = $tallier->execute();
+if ( !$status->isOK() ) {
+       spFatal( "Tally error: " . $status->getWikiText() );
+}
+//$tallier = $status->value;
+if ( isset( $options['html'] ) ) {
+       echo $tallier->getHtmlResult();
+} else {
+       echo $tallier->getTextResult();
+}
+
+
+function spFatal( $message ) {
+       fwrite( STDERR, rtrim( $message ) . "\n" );
+       exit( 1 );
+}

Added: trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php
===================================================================
--- trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php             
                (rev 0)
+++ trunk/extensions/SecurePoll/includes/talliers/CommentDumper.php     
2011-08-30 08:03:41 UTC (rev 95737)
@@ -0,0 +1,62 @@
+<?php
+
+/**
+ * A class that dumps the comments from an election
+ */
+class SecurePoll_CommentDumper extends SecurePoll_ElectionTallier {
+       var $csvHandle;
+
+       function execute() {
+               $this->csvHandle = fopen( 'php://temp', 'r+' );
+               return parent::execute();
+       }
+
+       /**
+        * Add a record. This is the callback function for 
SecurePoll_Store::callbackValidVotes(). 
+        * On error, the Status object returned here will be passed through 
back to 
+        * the caller of callbackValidVotes().
+        *
+        * @param $store SecurePoll_Store
+        * @param $record string Encrypted, packed record.
+        * @return Status
+        */
+       function addRecord( $store, $record ) {
+               # Decrypt and unpack
+               if ( $this->crypt ) {
+                       $status = $this->crypt->decrypt( $record );
+                       if ( !$status->isOK() ) {
+                               return $status;
+                       }
+                       $record = $status->value;
+               }
+               $record = rtrim( $record );
+               $scores = $this->ballot->unpackRecord( $record );
+               
+               $comments = $scores['comment'];
+               unset($scores['comment']);
+               $output = array( $comments['native'], $comments['en'] );
+               
+               ksort( $output );
+               
+               foreach ( $scores as $question ) {
+                       ksort( $question );
+                       $output = array_merge( $output, $question );
+               }
+               
+               fputcsv( $this->csvHandle, $output );
+
+               return Status::newGood();
+       }
+       
+       function getHtmlResult() {
+               return $this->getTextResult();
+       }
+       
+       /**
+        * Get text formatted results for this tally. Should only be called 
after 
+        * execute().
+        */
+       function getTextResult() {
+               return stream_get_contents( $this->csvHandle, -1, 0 );
+       }
+}

Modified: 
trunk/extensions/SecurePoll/includes/talliers/HistogramRangeTallier.php
===================================================================
--- trunk/extensions/SecurePoll/includes/talliers/HistogramRangeTallier.php     
2011-08-30 07:23:31 UTC (rev 95736)
+++ trunk/extensions/SecurePoll/includes/talliers/HistogramRangeTallier.php     
2011-08-30 08:03:41 UTC (rev 95737)
@@ -80,7 +80,7 @@
        }
 
        function getTextResult() {
-               throw new MWException( __METHOD__.': not yet implemented' );
+               return $this->getHtmlResult();
        }
 }
 


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to