Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.10% covered (warning)
88.10%
37 / 42
60.00% covered (warning)
60.00%
3 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
UndoViewAction
88.10% covered (warning)
88.10%
37 / 42
60.00% covered (warning)
60.00%
3 / 5
7.08
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 getName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 show
77.78% covered (warning)
77.78%
14 / 18
0.00% covered (danger)
0.00%
0 / 1
3.10
 showConfirmationForm
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
1
 displayUndoDiff
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\MediaWiki\Actions;
6
7use Diff\DiffOp\Diff\Diff;
8use EntitySchema\MediaWiki\Content\EntitySchemaSlotDiffRenderer;
9use EntitySchema\Presentation\ConfirmationFormRenderer;
10use EntitySchema\Presentation\DiffRenderer;
11use MediaWiki\Context\IContextSource;
12use MediaWiki\Page\Article;
13use MediaWiki\Revision\RevisionStore;
14
15/**
16 * @license GPL-2.0-or-later
17 */
18class UndoViewAction extends AbstractUndoAction {
19
20    private EntitySchemaSlotDiffRenderer $slotDiffRenderer;
21
22    public function __construct(
23        Article $article,
24        IContextSource $context,
25        EntitySchemaSlotDiffRenderer $slotDiffRenderer,
26        RevisionStore $revisionStore
27    ) {
28        parent::__construct( $article, $context, $revisionStore );
29        $this->slotDiffRenderer = $slotDiffRenderer;
30    }
31
32    public function getName(): string {
33        return 'edit';
34    }
35
36    public function show(): void {
37        $this->getOutput()->enableOOUI();
38
39        $this->getOutput()->setPageTitleMsg(
40            $this->msg(
41                'entityschema-undo-heading',
42                $this->getTitle()->getTitleValue()->getText()
43            )
44        );
45
46        $req = $this->getContext()->getRequest();
47        $diffStatus = $this->getDiffFromRequest( $req );
48        if ( !$diffStatus->isOK() ) {
49            $this->showUndoErrorPage( $diffStatus );
50            return;
51        }
52
53        $patchStatus = $this->tryPatching( $diffStatus->getValue() );
54        if ( !$patchStatus->isOK() ) {
55            $this->showUndoErrorPage( $patchStatus );
56            return;
57        }
58
59        $this->displayUndoDiff( $diffStatus->getValue() );
60        $this->showConfirmationForm( $req->getInt( 'undo' ) );
61    }
62
63    /**
64     * Shows a form that can be used to confirm the requested undo/restore action.
65     */
66    private function showConfirmationForm( int $undidRevision = 0 ): void {
67        $req = $this->getRequest();
68        $renderer = new ConfirmationFormRenderer( $this );
69        $confFormHTML = $renderer->showUndoRestoreConfirmationForm(
70            [
71                'undo' => $req->getInt( 'undo' ),
72                'undoafter' => $req->getInt( 'undoafter' ),
73            ],
74            'restore',
75            $this->getTitle(),
76            $this->getUser(),
77            $undidRevision
78        );
79
80        $this->getOutput()->addHTML( $confFormHTML );
81    }
82
83    private function displayUndoDiff( Diff $diff ): void {
84
85        $helper = new DiffRenderer( $this, $this->slotDiffRenderer );
86        $this->getOutput()->addHTML(
87            $helper->renderSchemaDiffTable(
88                $this->slotDiffRenderer->renderSchemaDiffRows( $diff ),
89                $this->msg( 'entityschema-undo-old-revision' )
90            )
91        );
92
93        $this->getOutput()->addModuleStyles( 'mediawiki.diff.styles' );
94    }
95
96}