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 MediaWiki\Actions\EditAction;
8use MediaWiki\Exception\PermissionsError;
9use MediaWiki\Exception\ReadOnlyError;
10use MediaWiki\Exception\UserBlockedError;
11use MediaWiki\Html\Html;
12use MediaWiki\MediaWikiServices;
13use MediaWiki\Request\WebRequest;
14use MediaWiki\Revision\RevisionRecord;
15use MediaWiki\Status\Status;
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    /** @return Status<RevisionRecord> */
27    protected function getRevisionFromRequest( WebRequest $req ): Status {
28        $restoreID = $req->getInt( 'restore' );
29        $revStore = MediaWikiServices::getInstance()->getRevisionStore();
30        $revToRestore = $revStore->getRevisionById( $restoreID );
31        if ( $revToRestore === null || $revToRestore->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
32            return Status::newFatal( $this->msg( 'entityschema-restore-bad-revisions' ) );
33        }
34
35        if ( $revToRestore->getPageId() !== $this->getTitle()->getArticleID() ) {
36            return Status::newFatal( $this->msg( 'entityschema-error-wrong-page-revisions' ) );
37        }
38
39        return Status::newGood( $revToRestore );
40    }
41
42    /**
43     * @throws ReadOnlyError
44     * @throws UserBlockedError
45     * @throws PermissionsError
46     */
47    protected function checkPermissions(): void {
48        $services = MediaWikiServices::getInstance();
49        $pm = $services->getPermissionManager();
50        $checkReplica = !$this->getRequest()->wasPosted();
51
52        $permissionErrors = $pm->getPermissionErrors(
53            $this->getRestriction(),
54            $this->getUser(),
55            $this->getTitle(),
56            $checkReplica ? $pm::RIGOR_FULL : $pm::RIGOR_SECURE
57        );
58        if ( $permissionErrors !== [] ) {
59            throw new PermissionsError( $this->getRestriction(), $permissionErrors );
60        }
61
62        if ( $services->getReadOnlyMode()->isReadOnly() ) {
63            throw new ReadOnlyError;
64        }
65    }
66
67    /**
68     * Output an error page showing the given status
69     *
70     * @param Status $status The status to report.
71     */
72    protected function showRestoreErrorPage( Status $status ): void {
73        $this->getOutput()->prepareErrorPage();
74        $this->getOutput()->setPageTitleMsg(
75            $this->msg( 'entityschema-restore-heading-failed' )
76        );
77        $this->getOutput()->setHTMLTitle(
78            $this->msg( 'errorpagetitle' )
79        );
80
81        $this->getOutput()->addHTML( Html::errorBox( $status->getMessage()->parse() ) );
82
83        $this->getOutput()->returnToMain( null, $this->getTitle() );
84    }
85
86}