Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ArchivePage
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 2
56
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 / 51
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2
3namespace MediaWiki\Extension\SecurePoll\Pages;
4
5use JobQueueGroup;
6use JobSpecification;
7use MediaWiki\Extension\SecurePoll\SpecialSecurePoll;
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        // Already archived?
73        $dbr = $this->election->context->getDB( DB_REPLICA );
74        $isArchived = $dbr->newSelectQueryBuilder()
75            ->select( 'pr_value' )
76            ->from( 'securepoll_properties' )
77            ->where( [
78                'pr_entity' => $this->election->getId(),
79                'pr_key' => 'is-archived',
80            ] )
81            ->caller( __METHOD__ )
82            ->fetchField();
83
84        if ( !$isArchived ) {
85            // Not archived if row doesn't exist; go ahead and archive
86            $this->jobQueueGroup->push(
87                new JobSpecification(
88                    'securePollArchiveElection',
89                    [ 'electionId' => $electionId ],
90                    []
91                )
92            );
93            $out->prependHTML( ( new MessageWidget( [
94                'label' => $this->msg( 'securepoll-archive-in-progress' )->text(),
95                'type' => 'success',
96            ] ) ) );
97        } else {
98            // Already archived
99            $out->prependHTML( ( new MessageWidget( [
100                'label' => $this->msg( 'securepoll-already-archived-error' )->text(),
101                'type' => 'error',
102            ] ) ) );
103        }
104    }
105}