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