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
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}
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput)
@inheritDoc