Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
FileInfoDiffPage
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 getHtml
100.00% covered (success)
100.00%
37 / 37
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            ( new ImportIdentityFormSnippet( [
38                'clientUrl' => $importPlan->getRequest()->getUrl(),
39                'intendedFileName' => $importPlan->getFileName(),
40                'intendedRevisionSummary' => $importPlan->getRequest()->getIntendedSummary(),
41                'intendedWikitext' => $importPlan->getFileInfoText(),
42                'actionStats' => json_encode( $importPlan->getActionStats() ),
43                'validationWarnings' => json_encode( $importPlan->getValidationWarnings() ),
44                'importDetailsHash' => $importPlan->getRequest()->getImportDetailsHash(),
45                'automateSourceWikiCleanup' => $importPlan->getAutomateSourceWikiCleanUp(),
46                'automateSourceWikiDelete' => $importPlan->getAutomateSourceWikiDelete(),
47            ] ) )->getHtml() .
48            new ButtonInputWidget(
49                [
50                    'classes' => [ 'mw-importfile-backButton' ],
51                    'label' => $this->msg( 'fileimporter-to-preview' )->plain(),
52                    'type' => 'submit',
53                    'flags' => [ 'primary', 'progressive' ],
54                ]
55            ) .
56            Html::closeElement( 'form' );
57    }
58
59    /**
60     * @return string HTML
61     */
62    private function buildDiff( string $originalText, string $newText, ContentHandler $contentHandler ): string {
63        $originalContent = $contentHandler->unserializeContent( $originalText );
64        $newContent = $contentHandler->unserializeContent( $newText );
65
66        $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
67        $diffEngine->setContent( $originalContent, $newContent );
68
69        $diffEngine->showDiffStyle();
70        return $diffEngine->getDiff(
71            $this->msg( 'currentrev' )->parse(),
72            $this->msg( 'yourtext' )->parse()
73        );
74    }
75
76}