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 ContentHandler;
6use FileImporter\Data\ImportPlan;
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    /**
19     * @return string
20     */
21    public function getHtml( ImportPlan $importPlan, ContentHandler $contentHandler ) {
22        $newText = $importPlan->getRequest()->getIntendedText() ?? $importPlan->getFileInfoText();
23
24        return Html::openElement(
25                'form',
26                [
27                    'action' => $this->getPageTitle()->getLocalURL(),
28                    'method' => 'POST',
29                ]
30            ) .
31            Html::rawElement(
32                'div',
33                [],
34                $this->buildDiff(
35                    $importPlan->getInitialFileInfoText(),
36                    $newText,
37                    $contentHandler
38                )
39            ) .
40            ( new ImportIdentityFormSnippet( [
41                'clientUrl' => $importPlan->getRequest()->getUrl(),
42                'intendedFileName' => $importPlan->getFileName(),
43                'intendedRevisionSummary' => $importPlan->getRequest()->getIntendedSummary(),
44                'intendedWikitext' => $importPlan->getFileInfoText(),
45                'actionStats' => json_encode( $importPlan->getActionStats() ),
46                'validationWarnings' => json_encode( $importPlan->getValidationWarnings() ),
47                'importDetailsHash' => $importPlan->getRequest()->getImportDetailsHash(),
48                'automateSourceWikiCleanup' => $importPlan->getAutomateSourceWikiCleanUp(),
49                'automateSourceWikiDelete' => $importPlan->getAutomateSourceWikiDelete(),
50            ] ) )->getHtml() .
51            new ButtonInputWidget(
52                [
53                    'classes' => [ 'mw-importfile-backButton' ],
54                    'label' => $this->msg( 'fileimporter-to-preview' )->plain(),
55                    'type' => 'submit',
56                    'flags' => [ 'primary', 'progressive' ],
57                ]
58            ) .
59            Html::closeElement( 'form' );
60    }
61
62    /**
63     * @return string HTML
64     */
65    private function buildDiff( string $originalText, string $newText, ContentHandler $contentHandler ): string {
66        $originalContent = $contentHandler->unserializeContent( $originalText );
67        $newContent = $contentHandler->unserializeContent( $newText );
68
69        $diffEngine = $contentHandler->createDifferenceEngine( $this->getContext() );
70        $diffEngine->setContent( $originalContent, $newContent );
71
72        $diffEngine->showDiffStyle();
73        return $diffEngine->getDiff(
74            $this->msg( 'currentrev' )->parse(),
75            $this->msg( 'yourtext' )->parse()
76        );
77    }
78
79}