Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FileInfoDiffPage
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getHtml
100.00% covered (success)
100.00%
27 / 27
100.00% covered (success)
100.00%
1 / 1
1
 buildDiff
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace FileImporter\Html;
4
5use FileImporter\Data\ImportPlan;
6use MediaWiki\Content\ContentHandler;
7use MediaWiki\Html\Html;
8use OOUI\ButtonInputWidget;
9
10/**
11 * Page to display a diff from changed file info text.
12 *
13 * @license GPL-2.0-or-later
14 * @author Christoph Jauera <christoph.jauera@wikimedia.de>
15 */
16class FileInfoDiffPage extends SpecialPageHtmlFragment {
17
18    public function getHtml( ImportPlan $importPlan, ContentHandler $contentHandler ): string {
19        $newText = $importPlan->getRequest()->getIntendedText() ?? $importPlan->getFileInfoText();
20
21        return Html::openElement(
22                'form',
23                [
24                    'action' => $this->getPageTitle()->getLocalURL(),
25                    'method' => 'POST',
26                ]
27            ) .
28            Html::rawElement(
29                'div',
30                [],
31                $this->buildDiff(
32                    $importPlan->getInitialFileInfoText(),
33                    $newText,
34                    $contentHandler
35                )
36            ) .
37            ImportIdentityFormSnippet::newFromImportPlan( $importPlan )->getHtml() .
38            new ButtonInputWidget(
39                [
40                    'classes' => [ 'mw-importfile-backButton' ],
41                    'label' => $this->msg( 'fileimporter-to-preview' )->plain(),
42                    'type' => 'submit',
43                    'flags' => [ 'primary', 'progressive' ],
44                ]
45            ) .
46            Html::closeElement( 'form' );
47    }
48
49    /**
50     * @return string HTML
51     */
52    private function buildDiff( string $originalText, string $newText, ContentHandler $contentHandler ): string {
53        $originalContent = $contentHandler->unserializeContent( $originalText );
54        $newContent = $contentHandler->unserializeContent( $newText );
55
56        $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
57        $diffEngine->setContent( $originalContent, $newContent );
58
59        $diffEngine->showDiffStyle();
60        return $diffEngine->getDiff(
61            $this->msg( 'currentrev' )->parse(),
62            $this->msg( 'yourtext' )->parse()
63        );
64    }
65
66}