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