Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
MessageBundleContentHandler | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
getContentClass | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
makeEmptyContent | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
validateSave | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
12 | |||
fillParserOutput | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
30 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\MessageBundleTranslation; |
5 | |
6 | use InvalidArgumentException; |
7 | use MediaWiki\Content\Content; |
8 | use MediaWiki\Content\Renderer\ContentParseParams; |
9 | use MediaWiki\Content\TextContentHandler; |
10 | use MediaWiki\Content\ValidationParams; |
11 | use MediaWiki\Parser\ParserOutput; |
12 | use StatusValue; |
13 | use const CONTENT_FORMAT_JSON; |
14 | |
15 | /** |
16 | * @author Niklas Laxström |
17 | * @license GPL-2.0-or-later |
18 | * @since 2021.05 |
19 | */ |
20 | class MessageBundleContentHandler extends TextContentHandler { |
21 | public function __construct( $modelId = MessageBundleContent::CONTENT_MODEL_ID ) { |
22 | parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] ); |
23 | } |
24 | |
25 | protected function getContentClass(): string { |
26 | return MessageBundleContent::class; |
27 | } |
28 | |
29 | public function makeEmptyContent(): Content { |
30 | $class = $this->getContentClass(); |
31 | return new $class( '{}' ); |
32 | } |
33 | |
34 | public function validateSave( Content $content, ValidationParams $validationParams ) { |
35 | // This will give an informative error message when trying to change the content model |
36 | try { |
37 | if ( $content instanceof MessageBundleContent ) { |
38 | $content->validate(); |
39 | } |
40 | return StatusValue::newGood(); |
41 | } catch ( MalformedBundle $e ) { |
42 | // XXX: We have no context source nor is there Message::messageParam :( |
43 | return StatusValue::newFatal( 'translate-messagebundle-validation-error', wfMessage( $e ) ); |
44 | } |
45 | } |
46 | |
47 | /** @inheritDoc */ |
48 | protected function fillParserOutput( |
49 | Content $content, |
50 | ContentParseParams $cpoParams, |
51 | ParserOutput &$parserOutput |
52 | ) { |
53 | if ( !$content instanceof MessageBundleContent ) { |
54 | throw new InvalidArgumentException( |
55 | 'Expected $content to be MessageBundleContent; got: ' . get_class( $content ) |
56 | ); |
57 | } |
58 | |
59 | $label = $content->getMetadata()->getLabel(); |
60 | if ( $label !== null ) { |
61 | $parserOutput->setDisplayTitle( $label ); |
62 | } |
63 | |
64 | if ( $cpoParams->getGenerateHtml() && $content->isValid() ) { |
65 | /** @param $content JsonContent::class */ |
66 | $parserOutput->setRawText( $content->rootValueTable( $content->getData()->getValue() ) ); |
67 | $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] ); |
68 | } else { |
69 | $parserOutput->setRawText( null ); |
70 | } |
71 | } |
72 | } |