Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PageSlotDiffRenderer
0.00% covered (danger)
0.00%
0 / 30
0.00% covered (danger)
0.00%
0 / 5
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getExtraCacheKeys
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getDiff
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 createLevelDiffs
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
6
 createTextDiffOutput
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace ProofreadPage\Page;
4
5use MediaWiki\Content\Content;
6use MediaWiki\Context\IContextSource;
7use MediaWiki\Html\Html;
8use ProofreadPage\DiffFormatterUtils;
9use SlotDiffRenderer;
10
11/**
12 * SlotDiffRenderer for Page: pages content model
13 */
14class PageSlotDiffRenderer extends SlotDiffRenderer {
15
16    /**
17     * @var IContextSource
18     */
19    private $context;
20
21    /**
22     * @var DiffFormatterUtils
23     */
24    private $diffFormatterUtils;
25
26    /**
27     * @param IContextSource $context
28     */
29    public function __construct( IContextSource $context ) {
30        $this->context = $context;
31        $this->diffFormatterUtils = new DiffFormatterUtils();
32    }
33
34    /** @inheritDoc */
35    public function getExtraCacheKeys() {
36        return [
37            // required because the diff view contains localized strings such
38            // as the section headers
39            $this->context->getLanguage()->getCode()
40        ];
41    }
42
43    /**
44     * @param PageContent|null $oldContent
45     * @param PageContent|null $newContent
46     * @return string
47     * @suppress PhanParamSignatureMismatch Intentional mismatching Content
48     */
49    public function getDiff( ?Content $oldContent = null, ?Content $newContent = null ) {
50        $this->normalizeContents( $oldContent, $newContent, PageContent::class );
51
52        return $this->createLevelDiffs(
53                $oldContent->getLevel(), $newContent->getLevel()
54            ) . $this->createTextDiffOutput(
55                $oldContent->getHeader(), $newContent->getHeader(), 'proofreadpage_header'
56            ) . $this->createTextDiffOutput(
57                $oldContent->getBody(), $newContent->getBody(), 'proofreadpage_body'
58            ) . $this->createTextDiffOutput(
59                $oldContent->getFooter(), $newContent->getFooter(), 'proofreadpage_footer'
60            );
61    }
62
63    /**
64     * Create the diffs for a PageLevel
65     *
66     * @param PageLevel $old
67     * @param PageLevel $new
68     * @return string
69     */
70    private function createLevelDiffs( PageLevel $old, PageLevel $new ) {
71        if ( $old->equals( $new ) ) {
72            return '';
73        }
74
75        return $this->diffFormatterUtils->createHeader(
76                $this->context->msg( 'proofreadpage_page_status' )->parse()
77            ) . Html::openElement( 'tr' ) . $this->diffFormatterUtils->createDeletedLine(
78                $this->context->msg( $old->getLevelCategoryKey() )->plain()
79            ) . $this->diffFormatterUtils->createAddedLine(
80                $this->context->msg( $new->getLevelCategoryKey() )->plain()
81            ) . Html::closeElement( 'tr' );
82    }
83
84    /**
85     * Create the diffs for a textual content
86     *
87     * @param Content $oldContent
88     * @param Content $newContent
89     * @param string $headerMsg the message to use for header
90     * @return string
91     */
92    private function createTextDiffOutput( Content $oldContent, Content $newContent, $headerMsg ) {
93        $diffRenderer = $newContent->getContentHandler()->getSlotDiffRenderer( $this->context );
94        $diff = $diffRenderer->getDiff( $oldContent, $newContent );
95        if ( $diff === '' ) {
96            return '';
97        }
98
99        return $this->diffFormatterUtils->createHeader( $this->context->msg( $headerMsg )->escaped() ) .
100            $diff;
101    }
102}