Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageBundleContentHandler.php
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
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
54 protected function fillParserOutput(
55 Content $content,
56 ContentParseParams $cpoParams,
57 ParserOutput &$parserOutput
58 ) {
59 if ( !$content instanceof MessageBundleContent ) {
60 throw new InvalidArgumentException(
61 'Expected $content to be MessageBundleContent; got: ' . get_class( $content )
62 );
63 }
64
65 if ( $cpoParams->getGenerateHtml() && $content->isValid() ) {
66 $parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) );
67 $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
68 } else {
69 $parserOutput->setText( null );
70 }
71 }
72}
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput)
Set the HTML and add the appropriate styles.