Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
CommentDumper
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 addRecord
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
56
 getHtmlResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTextResult
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Talliers;
4
5use MediaWiki\Extension\SecurePoll\Ballots\Ballot;
6use MediaWiki\Extension\SecurePoll\Crypt\Crypt;
7use MediaWiki\Extension\SecurePoll\Store\Store;
8use MediaWiki\Status\Status;
9
10/**
11 * A class that dumps the comments from an election
12 */
13class CommentDumper extends ElectionTallier {
14    /** @var Ballot|null */
15    public $ballot;
16    /** @var resource|null */
17    public $csvHandle;
18    /** @var Crypt|null */
19    public $crypt;
20    /** @var bool */
21    public $skipEmptyComments;
22    /** @var int|null */
23    private $countSoFar;
24
25    public function __construct( $context, $election, $skipEmptyComments = true ) {
26        parent::__construct( $context, $election );
27        $this->skipEmptyComments = $skipEmptyComments;
28    }
29
30    public function execute() {
31        $this->csvHandle = fopen( 'php://temp', 'r+' );
32        $this->countSoFar = 0;
33
34        return parent::execute();
35    }
36
37    /**
38     * Add a record. This is the callback function for SecurePoll_Store::callbackValidVotes().
39     * On error, the Status object returned here will be passed through back to
40     * the caller of callbackValidVotes().
41     *
42     * @param Store $store
43     * @param string $record Encrypted, packed record.
44     * @return Status
45     */
46    public function addRecord( $store, $record ) {
47        $this->countSoFar++;
48        wfDebug( "Processing vote {$this->countSoFar}\n" );
49        # Decrypt and unpack
50        if ( $this->crypt ) {
51            $status = $this->crypt->decrypt( $record );
52            if ( !$status->isOK() ) {
53                return $status;
54            }
55            $record = $status->value;
56        }
57        $record = rtrim( $record );
58        $scores = $this->ballot->unpackRecord( $record );
59
60        $comments = $scores['comment'];
61        unset( $scores['comment'] );
62
63        // Short circuit if the comments are empty
64        if ( $this->skipEmptyComments && $comments['native'] == '' && $comments['en'] == '' ) {
65            return Status::newGood();
66        }
67
68        $output = [
69            $comments['native'],
70            $comments['en']
71        ];
72
73        ksort( $output );
74
75        foreach ( $scores as $question ) {
76            ksort( $question );
77            $output = array_merge( $output, $question );
78        }
79
80        fputcsv( $this->csvHandle, $output );
81
82        return Status::newGood();
83    }
84
85    /**
86     * @inheritDoc
87     * Get text formatted results for this tally. Should only be called after
88     * execute().
89     */
90    public function getHtmlResult() {
91        return $this->getTextResult();
92    }
93
94    /**
95     * @inheritDoc
96     *
97     */
98    public function getTextResult() {
99        return stream_get_contents( $this->csvHandle, -1, 0 );
100    }
101}