Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractRestoreAction
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 4
110
0.00% covered (danger)
0.00%
0 / 1
 getRestriction
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getRevisionFromRequest
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 checkPermissions
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
20
 showRestoreErrorPage
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\MediaWiki\Actions;
6
7use EditAction;
8use MediaWiki\Html\Html;
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Request\WebRequest;
11use MediaWiki\Revision\RevisionRecord;
12use MediaWiki\Status\Status;
13use PermissionsError;
14use ReadOnlyError;
15use UserBlockedError;
16
17/**
18 * @license GPL-2.0-or-later
19 */
20abstract class AbstractRestoreAction extends EditAction {
21
22    public function getRestriction(): string {
23        return 'edit';
24    }
25
26    protected function getRevisionFromRequest( WebRequest $req ): Status {
27        $restoreID = $req->getInt( 'restore' );
28        $revStore = MediaWikiServices::getInstance()->getRevisionStore();
29        $revToRestore = $revStore->getRevisionById( $restoreID );
30        if ( $revToRestore === null || $revToRestore->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
31            return Status::newFatal( $this->msg( 'entityschema-restore-bad-revisions' ) );
32        }
33
34        if ( $revToRestore->getPageId() !== $this->getTitle()->getArticleID() ) {
35            return Status::newFatal( $this->msg( 'entityschema-error-wrong-page-revisions' ) );
36        }
37
38        return Status::newGood( $revToRestore );
39    }
40
41    /**
42     * @throws ReadOnlyError
43     * @throws UserBlockedError
44     * @throws PermissionsError
45     */
46    protected function checkPermissions(): void {
47        $services = MediaWikiServices::getInstance();
48        $pm = $services->getPermissionManager();
49        $checkReplica = !$this->getRequest()->wasPosted();
50
51        $permissionErrors = $pm->getPermissionErrors(
52            $this->getRestriction(),
53            $this->getUser(),
54            $this->getTitle(),
55            $checkReplica ? $pm::RIGOR_FULL : $pm::RIGOR_SECURE
56        );
57        if ( $permissionErrors !== [] ) {
58            throw new PermissionsError( $this->getRestriction(), $permissionErrors );
59        }
60
61        if ( $services->getReadOnlyMode()->isReadOnly() ) {
62            throw new ReadOnlyError;
63        }
64    }
65
66    /**
67     * Output an error page showing the given status
68     *
69     * @param Status $status The status to report.
70     */
71    protected function showRestoreErrorPage( Status $status ): void {
72        $this->getOutput()->prepareErrorPage();
73        $this->getOutput()->setPageTitleMsg(
74            $this->msg( 'entityschema-restore-heading-failed' )
75        );
76        $this->getOutput()->setHTMLTitle(
77            $this->msg( 'errorpagetitle' )
78        );
79
80        $this->getOutput()->addHTML( Html::errorBox( $status->getMessage()->parse() ) );
81
82        $this->getOutput()->returnToMain( null, $this->getTitle() );
83    }
84
85}