Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
94.34% |
50 / 53 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
IndexSlotDiffRenderer | |
94.34% |
50 / 53 |
|
83.33% |
5 / 6 |
18.06 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
getExtraCacheKeys | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getDiff | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
9 | |||
createRedirectionDiff | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
createIndexContentDiff | |
100.00% |
15 / 15 |
|
100.00% |
1 / 1 |
2 | |||
createTextDiffOutput | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace ProofreadPage\Index; |
4 | |
5 | use MediaWiki\Content\Content; |
6 | use MediaWiki\Content\WikitextContent; |
7 | use MediaWiki\Context\IContextSource; |
8 | use MediaWiki\Title\Title; |
9 | use ProofreadPage\DiffFormatterUtils; |
10 | use SlotDiffRenderer; |
11 | |
12 | /** |
13 | * SlotDiffRenderer for Index: pages content model |
14 | */ |
15 | class IndexSlotDiffRenderer extends SlotDiffRenderer { |
16 | |
17 | /** |
18 | * @var IContextSource |
19 | */ |
20 | private $context; |
21 | |
22 | /** |
23 | * @var DiffFormatterUtils |
24 | */ |
25 | private $diffFormatterUtils; |
26 | |
27 | /** |
28 | * @var CustomIndexFieldsParser |
29 | */ |
30 | private $customIndexFieldsParser; |
31 | |
32 | /** |
33 | * @var SlotDiffRenderer |
34 | */ |
35 | private $wikitextSlotDiffRenderer; |
36 | |
37 | /** |
38 | * @inheritDoc |
39 | */ |
40 | public function __construct( |
41 | IContextSource $contextSource, |
42 | CustomIndexFieldsParser $customIndexFieldsParser, |
43 | SlotDiffRenderer $wikitextSlotDiffRenderer |
44 | ) { |
45 | $this->context = $contextSource; |
46 | $this->diffFormatterUtils = new DiffFormatterUtils(); |
47 | $this->customIndexFieldsParser = $customIndexFieldsParser; |
48 | $this->wikitextSlotDiffRenderer = $wikitextSlotDiffRenderer; |
49 | } |
50 | |
51 | /** @inheritDoc */ |
52 | public function getExtraCacheKeys() { |
53 | return [ |
54 | // required because the diff view contains localized strings such |
55 | // as the section headers |
56 | $this->context->getLanguage()->getCode() |
57 | ]; |
58 | } |
59 | |
60 | /** |
61 | * @inheritDoc |
62 | */ |
63 | public function getDiff( ?Content $oldContent = null, ?Content $newContent = null ) { |
64 | $this->normalizeContents( |
65 | $oldContent, $newContent, |
66 | [ IndexContent::class, IndexRedirectContent::class ] |
67 | ); |
68 | |
69 | $diff = ''; |
70 | if ( |
71 | $oldContent instanceof IndexRedirectContent || |
72 | $newContent instanceof IndexRedirectContent |
73 | ) { |
74 | $oldRedirectTitle = ( $oldContent instanceof IndexRedirectContent ) |
75 | ? $oldContent->getRedirectTarget() |
76 | : null; |
77 | $newRedirectTitle = ( $newContent instanceof IndexRedirectContent ) |
78 | ? $newContent->getRedirectTarget() |
79 | : null; |
80 | $diff .= $this->createRedirectionDiff( $oldRedirectTitle, $newRedirectTitle ); |
81 | } |
82 | if ( $oldContent instanceof IndexContent || $newContent instanceof IndexContent ) { |
83 | $oldContent = ( $oldContent instanceof IndexContent ) ? $oldContent : new IndexContent( [] ); |
84 | $newContent = ( $newContent instanceof IndexContent ) ? $newContent : new IndexContent( [] ); |
85 | $diff .= $this->createIndexContentDiff( $oldContent, $newContent ); |
86 | } |
87 | return $diff; |
88 | } |
89 | |
90 | /** |
91 | * @param Title|null $oldTitle |
92 | * @param Title|null $newTitle |
93 | * @return string |
94 | */ |
95 | private function createRedirectionDiff( ?Title $oldTitle = null, ?Title $newTitle = null ) { |
96 | $old = ( $oldTitle === null ) ? '' : $oldTitle->getFullText(); |
97 | $new = ( $newTitle === null ) ? '' : $newTitle->getFullText(); |
98 | return $this->createTextDiffOutput( $old, $new, |
99 | $this->context->msg( 'isredirect' )->escaped() |
100 | ); |
101 | } |
102 | |
103 | /** |
104 | * @param IndexContent $old |
105 | * @param IndexContent $new |
106 | * @return string |
107 | */ |
108 | private function createIndexContentDiff( IndexContent $old, IndexContent $new ) { |
109 | $oldCustomFields = $this->customIndexFieldsParser->parseCustomIndexFields( $old ); |
110 | $newCustomFields = $this->customIndexFieldsParser->parseCustomIndexFields( $new ); |
111 | $diff = ''; |
112 | |
113 | foreach ( $oldCustomFields as $oldField ) { |
114 | $diff .= $this->createTextDiffOutput( |
115 | $oldField->getStringValue(), |
116 | $newCustomFields[$oldField->getKey()]->getStringValue(), |
117 | $oldField->getLabel() |
118 | ); |
119 | } |
120 | |
121 | $diff .= $this->createTextDiffOutput( |
122 | implode( "\n", $old->getCategories() ), |
123 | implode( "\n", $new->getCategories() ), |
124 | $this->context->msg( 'proofreadpage-index-field-category-label' )->escaped() |
125 | ); |
126 | |
127 | return $diff; |
128 | } |
129 | |
130 | /** |
131 | * @param string $oldText |
132 | * @param string $newText |
133 | * @param string $header |
134 | * @return string |
135 | */ |
136 | private function createTextDiffOutput( $oldText, $newText, $header ) { |
137 | $diff = $this->wikitextSlotDiffRenderer->getDiff( |
138 | new WikitextContent( $oldText ), |
139 | new WikitextContent( $newText ) |
140 | ); |
141 | if ( $diff === '' ) { |
142 | return ''; |
143 | } |
144 | return $this->diffFormatterUtils->createHeader( $header ) . $diff; |
145 | } |
146 | } |