Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
77.42% covered (warning)
77.42%
48 / 62
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArchivePage
77.42% covered (warning)
77.42%
48 / 62
50.00% covered (danger)
50.00%
1 / 2
8.74
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 execute
76.67% covered (warning)
76.67%
46 / 60
0.00% covered (danger)
0.00%
0 / 1
7.62
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Pages;
4
5use MediaWiki\Extension\SecurePoll\SpecialSecurePoll;
6use MediaWiki\JobQueue\JobQueueGroup;
7use MediaWiki\JobQueue\JobSpecification;
8use MediaWiki\SpecialPage\SpecialPage;
9use OOUI\MessageWidget;
10
11/**
12 * SecurePoll subpage for archiving past elections
13 */
14class ArchivePage extends ActionPage {
15    /** @var JobQueueGroup */
16    private $jobQueueGroup;
17
18    /**
19     * @param SpecialSecurePoll $specialPage
20     * @param JobQueueGroup $jobQueueGroup
21     */
22    public function __construct(
23        SpecialSecurePoll $specialPage,
24        JobQueueGroup $jobQueueGroup
25    ) {
26        parent::__construct( $specialPage );
27        $this->jobQueueGroup = $jobQueueGroup;
28    }
29
30    /**
31     * Execute the subpage.
32     * @param array $params Array of subpage parameters.
33     */
34    public function execute( $params ) {
35        $out = $this->specialPage->getOutput();
36        $out->enableOOUI();
37
38        $out->returnToMain( false, SpecialPage::getTitleFor( 'SecurePoll' ) );
39
40        if ( !count( $params ) ) {
41            $out->addWikiMsg( 'securepoll-too-few-params' );
42            return;
43        }
44
45        $electionId = intval( $params[0] );
46        $this->election = $this->context->getElection( $electionId );
47        if ( !$this->election ) {
48            $out->addWikiMsg( 'securepoll-invalid-election', $electionId );
49            return;
50        }
51
52        $out->setPageTitleMsg( $this->msg( 'securepoll-archive-title', $this->election->getMessage( 'title' ) ) );
53
54        $isAdmin = $this->election->isAdmin( $this->specialPage->getUser() );
55
56        if ( !$isAdmin ) {
57            $out->prependHTML( ( new MessageWidget( [
58                'label' => $this->msg( 'securepoll-archive-private' )->text(),
59                'type' => 'error',
60            ] ) ) );
61            return;
62        }
63
64        if ( !$this->election->isFinished() ) {
65            $out->prependHTML( ( new MessageWidget( [
66                'label' => $this->msg( 'securepoll-archive-not-finished' )->text(),
67                'type' => 'error',
68            ] ) ) );
69            return;
70        }
71
72        $token = $this->specialPage->getContext()->getCsrfTokenSet()->getToken();
73        $request = $this->specialPage->getRequest();
74        $tokenMatch = $token->match( $request->getVal( 'token' ) );
75        if ( !$tokenMatch ) {
76            $out->prependHTML( ( new MessageWidget( [
77                'label' => $this->msg( 'securepoll-archive-token-error' )->text(),
78                'type' => 'error',
79            ] ) ) );
80            return;
81        }
82
83        // Already archived?
84        $dbr = $this->election->context->getDB( DB_REPLICA );
85        $isArchived = $dbr->newSelectQueryBuilder()
86            ->select( 'pr_value' )
87            ->from( 'securepoll_properties' )
88            ->where( [
89                'pr_entity' => $this->election->getId(),
90                'pr_key' => 'is-archived',
91            ] )
92            ->caller( __METHOD__ )
93            ->fetchField();
94
95        if ( !$isArchived ) {
96            // Not archived if row doesn't exist; go ahead and archive
97            $this->jobQueueGroup->push(
98                new JobSpecification(
99                    'securePollArchiveElection',
100                    [ 'electionId' => $electionId ],
101                    []
102                )
103            );
104            $out->prependHTML( ( new MessageWidget( [
105                'label' => $this->msg( 'securepoll-archive-in-progress' )->text(),
106                'type' => 'success',
107            ] ) ) );
108        } else {
109            // Already archived
110            $out->prependHTML( ( new MessageWidget( [
111                'label' => $this->msg( 'securepoll-already-archived-error' )->text(),
112                'type' => 'error',
113            ] ) ) );
114        }
115    }
116}