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 MediaWiki\Content\ValidationParams;
8use StatusValue;
9use TextContentHandler;
10use const CONTENT_FORMAT_JSON;
11
17class MessageBundleContentHandler extends TextContentHandler {
18 public function __construct( $modelId = MessageBundleContent::CONTENT_MODEL_ID ) {
19 parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
20 }
21
22 protected function getContentClass(): string {
23 return MessageBundleContent::class;
24 }
25
26 public function makeEmptyContent(): Content {
27 $class = $this->getContentClass();
28 return new $class( '{}' );
29 }
30
31 public function validateSave( Content $content, ValidationParams $validationParams ) {
32 // This will give an informative error message when trying to change the content model
33 try {
34 if ( $content instanceof MessageBundleContent ) {
35 $content->validate();
36 }
37 return StatusValue::newGood();
38 } catch ( MalformedBundle $e ) {
39 // XXX: We have no context source nor is there Message::messageParam :(
40 return StatusValue::newFatal( 'translate-messagebundle-validation-error', wfMessage( $e ) );
41 }
42 }
43}