Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 59
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
UnarchivePage
0.00% covered (danger)
0.00%
0 / 59
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 / 57
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 UnarchivePage extends ActionPage {
15    /** @var JobQueueGroup */
16    private $jobQueueGroup;
17
18    public function __construct(
19        SpecialSecurePoll $specialPage,
20        JobQueueGroup $jobQueueGroup
21    ) {
22        parent::__construct( $specialPage );
23        $this->jobQueueGroup = $jobQueueGroup;
24    }
25
26    /**
27     * Execute the subpage.
28     * @param array $params Array of subpage parameters.
29     */
30    public function execute( $params ) {
31        $out = $this->specialPage->getOutput();
32        $out->enableOOUI();
33
34        $out->returnToMain( false, SpecialPage::getTitleFor( 'SecurePoll' ) );
35
36        if ( !count( $params ) ) {
37            $out->prependHTML( ( new MessageWidget( [
38                'label' => $this->msg( 'securepoll-too-few-params' )->text(),
39                'type' => 'error',
40            ] ) ) );
41            return;
42        }
43
44        $electionId = intval( $params[0] );
45        $this->election = $this->context->getElection( $electionId );
46        if ( !$this->election ) {
47            $out->prependHTML( ( new MessageWidget( [
48                'label' => $this->msg( 'securepoll-invalid-election', $electionId )->text(),
49                'type' => 'error',
50            ] ) ) );
51            return;
52        }
53
54        $out->setPageTitleMsg( $this->msg( 'securepoll-unarchive-title', $this->election->getMessage( 'title' ) ) );
55
56        $isAdmin = $this->election->isAdmin( $this->specialPage->getUser() );
57
58        if ( !$isAdmin ) {
59            $out->prependHTML( ( new MessageWidget( [
60                'label' => $this->msg( 'securepoll-archive-private' )->text(),
61                'type' => 'error',
62            ] ) ) );
63            return;
64        }
65
66        if ( !$this->election->isFinished() ) {
67            $out->prependHTML( ( new MessageWidget( [
68                'label' => $this->msg( 'securepoll-archive-not-finished' )->text(),
69                'type' => 'error',
70            ] ) ) );
71            return;
72        }
73
74        $dbr = $this->election->context->getDB( DB_REPLICA );
75        $isArchived = $dbr->newSelectQueryBuilder()
76            ->select( 'pr_value' )
77            ->from( 'securepoll_properties' )
78            ->where( [
79                'pr_entity' => $this->election->getId(),
80                'pr_key' => 'is-archived',
81            ] )
82            ->caller( __METHOD__ )
83            ->fetchField();
84
85        if ( $isArchived ) {
86            // If a row exists, it's archived; unarchive it
87            $this->jobQueueGroup->push(
88                new JobSpecification(
89                    'securePollUnarchiveElection',
90                    [ 'electionId' => $electionId ],
91                    []
92                )
93            );
94            $out->prependHTML( ( new MessageWidget( [
95                'label' => $this->msg( 'securepoll-unarchive-in-progress' )->text(),
96                'type' => 'success',
97            ] ) ) );
98        } else {
99            // Not archived
100            $out->prependHTML( ( new MessageWidget( [
101                'label' => $this->msg( 'securepoll-already-unarchived-error' )->text(),
102                'type' => 'error',
103            ] ) ) );
104        }
105    }
106}