Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageBundleContentHandler.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use InvalidArgumentException;
7use MediaWiki\Content\Content;
8use MediaWiki\Content\Renderer\ContentParseParams;
9use MediaWiki\Content\TextContentHandler;
10use MediaWiki\Content\ValidationParams;
11use MediaWiki\Parser\ParserOutput;
12use StatusValue;
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() ) {
66 $parserOutput->setRawText( $content->rootValueTable( $content->getData()->getValue() ) );
67 $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
68 } else {
69 $parserOutput->setRawText( null );
70 }
71 }
72}
fillParserOutput(Content $content, ContentParseParams $cpoParams, ParserOutput &$parserOutput)
@inheritDoc