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