Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
StorySlotDiffRenderer
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 3
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDiff
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 getText
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace MediaWiki\Extension\Wikistories;
4
5use LogicException;
6use MediaWiki\Content\Content;
7use MediaWiki\Content\TextContent;
8use SlotDiffRenderer;
9use TextSlotDiffRenderer;
10
11class StorySlotDiffRenderer extends SlotDiffRenderer {
12
13    public function __construct(
14        private readonly StoryConverter $storyConverter,
15        private readonly TextSlotDiffRenderer $textSlotDiffRenderer,
16    ) {
17    }
18
19    /**
20     * Get a diff between two content objects. One of them might be null (meaning a slot was
21     * created or removed), but both cannot be. $newContent (or if it's null then $oldContent)
22     * must have the same content model that was used to obtain this diff renderer.
23     *
24     * IMPORTANT: To develop/debug this code, you'll need to prevent diff caching. This can
25     * be done by adding the following code to your LocalSettings.php (or other config file).
26     * $wgHooks['AbortDiffCache'][] = function () { return false; };
27     *
28     * @param Content|null $oldContent
29     * @param Content|null $newContent
30     * @return string HTML, one or more <tr> tags.
31     */
32    public function getDiff( ?Content $oldContent = null, ?Content $newContent = null ): string {
33        $this->normalizeContents( $oldContent, $newContent, [ StoryContent::class, TextContent::class ] );
34
35        return $this->textSlotDiffRenderer->getTextDiff(
36            $this->getText( $oldContent ),
37            $this->getText( $newContent )
38        );
39    }
40
41    private function getText( Content $content ): string {
42        if ( $content instanceof StoryContent ) {
43            return $this->storyConverter->toLatest( $content )->getTextForDiff();
44        } elseif ( $content instanceof TextContent ) {
45            return $content->getText();
46        }
47        throw new LogicException( 'Cannot diff story with content with model: ' . $content->getModel() );
48    }
49}