Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
69 / 69
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
ConfirmationFormRenderer
100.00% covered (success)
100.00%
69 / 69
100.00% covered (success)
100.00%
5 / 5
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 showUndoRestoreConfirmationForm
100.00% covered (success)
100.00%
35 / 35
100.00% covered (success)
100.00%
1 / 1
3
 getSummaryInput
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 getCancelLink
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 getEditButton
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace EntitySchema\Presentation;
6
7use MediaWiki\Html\Html;
8use MediaWiki\Linker\Linker;
9use MediaWiki\Title\Title;
10use MediaWiki\User\User;
11use MessageLocalizer;
12use OOUI\ButtonInputWidget;
13use OOUI\ButtonWidget;
14use OOUI\FieldLayout;
15use OOUI\HtmlSnippet;
16use OOUI\TextInputWidget;
17
18/**
19 * @license GPL-2.0-or-later
20 */
21class ConfirmationFormRenderer {
22
23    private MessageLocalizer $msgLocalizer;
24
25    public function __construct( MessageLocalizer $msgLocalizer ) {
26        $this->msgLocalizer = $msgLocalizer;
27    }
28
29    /**
30     * Shows a form that can be used to confirm the requested undo/restore action.
31     */
32    public function showUndoRestoreConfirmationForm(
33        array $args,
34        string $formName,
35        Title $title,
36        User $user,
37        int $undidRevision = 0
38    ): string {
39        $args = array_merge(
40            [
41                'action' => 'submit',
42            ],
43            $args
44        );
45
46        $actionUrl = $title->getLocalURL( $args );
47
48        $formHTML = '';
49
50        $formHTML .= Html::openElement( 'div', [ 'style' => 'margin-top: 1em;' ] );
51
52        $formHTML .= Html::openElement( 'form', [
53            'id' => $formName,
54            'name' => $formName,
55            'method' => 'post',
56            'action' => $actionUrl,
57            'enctype' => 'multipart/form-data',
58        ] );
59
60        $formHTML .= "<div class='editOptions'>\n";
61
62        $labelText = $this->msgLocalizer->msg( 'entityschema-summary-generated' )->escaped();
63        $formHTML .= $this->getSummaryInput( $labelText );
64        $formHTML .= "<div class='editButtons'>\n";
65        $formHTML .= $this->getEditButton() . "\n";
66        $formHTML .= $this->getCancelLink( $title );
67        $formHTML .= '</div>'; // editButtons
68        // @phan-suppress-next-line PhanPluginDuplicateAdjacentStatement
69        $formHTML .= '</div>'; // editOptions
70
71        $hidden = [
72            'wpEditToken' => $user->getEditToken(),
73            'wpBaseRev' => $title->getLatestRevID(),
74        ];
75        if ( $undidRevision ) {
76            $hidden['wpUndidRevision'] = $undidRevision;
77        }
78        foreach ( $hidden as $name => $value ) {
79            $formHTML .= "\n" . Html::hidden( $name, $value ) . "\n";
80        }
81
82        $formHTML .= Html::closeElement( 'form' );
83        $formHTML .= Html::closeElement( 'div' );
84
85        return $formHTML;
86    }
87
88    /**
89     * Generate standard summary input and label (wgSummary), compatible to EditPage.
90     *
91     * @param string $labelText The html to place inside the label
92     *
93     * @return string HTML
94     */
95    private function getSummaryInput( string $labelText ): string {
96        $inputAttrs = [
97                'name' => 'wpSummary',
98                'maxLength' => 200,
99                'size' => 60,
100                'spellcheck' => 'true',
101            ] + Linker::tooltipAndAccesskeyAttribs( 'summary' );
102        return ( new FieldLayout(
103            new TextInputWidget( $inputAttrs ),
104            [
105                'label' => new HtmlSnippet( $labelText ),
106                'align' => 'top',
107                'id' => 'wpSummaryLabel',
108                'classes' => [ 'mw-summary' ],
109            ]
110        ) )->toString();
111    }
112
113    /**
114     * Returns a cancel link back to viewing the entity's page
115     *
116     * @return string HTML
117     */
118    private function getCancelLink( Title $title ): string {
119        return ( new ButtonWidget( [
120            'id' => 'mw-editform-cancel',
121            'href' => $title->getLocalURL(),
122            'label' => $this->msgLocalizer->msg( 'cancel' )->text(),
123            'framed' => false,
124            'flags' => 'destructive',
125        ] ) )->toString();
126    }
127
128    /**
129     * @return string HTML
130     */
131    private function getEditButton(): string {
132        global $wgEditSubmitButtonLabelPublish;
133        $msgKey = $wgEditSubmitButtonLabelPublish ? 'publishchanges' : 'savearticle';
134        return ( new ButtonInputWidget( [
135            'name' => 'wpSave',
136            'value' => $this->msgLocalizer->msg( $msgKey )->text(),
137            'label' => $this->msgLocalizer->msg( $msgKey )->text(),
138            'accessKey' => $this->msgLocalizer->msg( 'accesskey-save' )->plain(),
139            'flags' => [ 'primary', 'progressive' ],
140            'type' => 'submit',
141            'title' => $this->msgLocalizer->msg( 'tooltip-save' )->text()
142                . ' [' . $this->msgLocalizer->msg( 'accesskey-save' )->text() . ']',
143        ] ) )->toString();
144    }
145
146}