Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
MessageBundleContentHandler
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 5
132
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
 getContentClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeEmptyContent
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 validateSave
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 fillParserOutput
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use Content;
7use InvalidArgumentException;
8use MediaWiki\Content\Renderer\ContentParseParams;
9use MediaWiki\Content\ValidationParams;
10use ParserOutput;
11use StatusValue;
12use TextContentHandler;
13use const CONTENT_FORMAT_JSON;
14
15/**
16 * @author Niklas Laxström
17 * @license GPL-2.0-or-later
18 * @since 2021.05
19 */
20class 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            $parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) );
66            $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
67        } else {
68            $parserOutput->setText( null );
69        }
70    }
71}