Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
DumpPage
0.00% covered (danger)
0.00%
0 / 46
0.00% covered (danger)
0.00%
0 / 3
156
0.00% covered (danger)
0.00%
0 / 1
 execute
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 1
90
 dumpVote
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 sendHeaders
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Pages;
4
5/**
6 * Special:SecurePoll subpage for exporting encrypted election records.
7 */
8class DumpPage extends ActionPage {
9    /** @var bool|null */
10    public $headersSent;
11
12    /**
13     * Execute the subpage.
14     * @param array $params Array of subpage parameters.
15     */
16    public function execute( $params ) {
17        $out = $this->specialPage->getOutput();
18
19        if ( !count( $params ) ) {
20            $out->addWikiMsg( 'securepoll-too-few-params' );
21
22            return;
23        }
24
25        $electionId = intval( $params[0] );
26        $this->election = $this->context->getElection( $electionId );
27        if ( !$this->election ) {
28            $out->addWikiMsg( 'securepoll-invalid-election', $electionId );
29
30            return;
31        }
32        $this->initLanguage( $this->specialPage->getUser(), $this->election );
33
34        $out->setPageTitleMsg( $this->msg( 'securepoll-dump-title', $this->election->getMessage( 'title' ) ) );
35
36        if ( !$this->election->isFinished() ) {
37            $out->addWikiMsg(
38                'securepoll-dump-not-finished',
39                $this->specialPage->getLanguage()->date( $this->election->getEndDate() ),
40                $this->specialPage->getLanguage()->time( $this->election->getEndDate() )
41            );
42
43            return;
44        }
45
46        $isAdmin = $this->election->isAdmin( $this->specialPage->getUser() );
47        if ( $this->election->getProperty( 'voter-privacy' ) && !$isAdmin ) {
48            $out->addWikiMsg( 'securepoll-dump-private' );
49
50            return;
51        }
52
53        $this->headersSent = false;
54        $status = $this->election->dumpVotesToCallback(
55            [
56                $this,
57                'dumpVote'
58            ]
59        );
60        if ( !$status->isOK() && !$this->headersSent ) {
61            $out->addWikiTextAsInterface( $status->getWikiText() );
62
63            return;
64        }
65        if ( !$this->headersSent ) {
66            $this->sendHeaders();
67        }
68        echo "</election>\n</SecurePoll>\n";
69    }
70
71    public function dumpVote( $election, $row ) {
72        if ( !$this->headersSent ) {
73            $this->sendHeaders();
74        }
75        echo "<vote>\n" . htmlspecialchars( rtrim( $row->vote_record ) ) . "\n</vote>\n";
76    }
77
78    public function sendHeaders() {
79        $this->headersSent = true;
80        $this->specialPage->getOutput()->disable();
81        header( 'Content-Type: application/vnd.mediawiki.securepoll' );
82        $electionId = $this->election->getId();
83        $filename = urlencode( "$electionId-" . wfTimestampNow() . '.securepoll' );
84        header( "Content-Disposition: attachment; filename=$filename" );
85        echo "<SecurePoll>\n<election>\n" . $this->election->getConfXml();
86        $this->context->setLanguages( [ $this->election->getLanguage() ] );
87    }
88}