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