Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / content / JsonContentHandler.php
blob5219063579c6123af0b7698004471eb2e231f25e
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 namespace MediaWiki\Content;
23 use MediaWiki\Content\Renderer\ContentParseParams;
24 use MediaWiki\Content\Transform\PreSaveTransformParams;
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Parser\ParserOutput;
27 use StatusValue;
29 /**
30 * Content handler for JSON text.
32 * Useful for maintaining JSON that can be viewed and edited directly by users.
34 * @author Ori Livneh <ori@wikimedia.org>
35 * @author Kunal Mehta <legoktm@gmail.com>
37 * @since 1.24
38 * @stable to extend
39 * @ingroup Content
41 class JsonContentHandler extends CodeContentHandler {
43 /**
44 * @param string $modelId
45 * @stable to call
47 public function __construct( $modelId = CONTENT_MODEL_JSON ) {
48 parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
51 /**
52 * @return class-string<JsonContent>
54 protected function getContentClass() {
55 return JsonContent::class;
58 public function makeEmptyContent() {
59 $class = $this->getContentClass();
60 return new $class( '{}' );
63 /**
64 * Enables EditPage's preload feature on .json pages as well as for extensions like MassMessage
65 * that subclass {@see JsonContentHandler}.
67 * @return true
69 public function supportsPreloadContent(): bool {
70 return true;
73 /**
74 * @param Content $content
75 * @param ValidationParams $validationParams
76 * @return StatusValue
78 public function validateSave( Content $content, ValidationParams $validationParams ) {
79 $status = parent::validateSave( $content, $validationParams );
80 '@phan-var JsonContent $content';
81 if ( !$status->isOK() ) {
82 if ( !$content->getData()->isGood() ) {
83 return StatusValue::newFatal( $content->getData()->getMessage( 'invalid-json-data' ) );
84 } else {
85 return $status;
88 $this->getHookRunner()->onJsonValidateSave( $content, $validationParams->getPageIdentity(), $status );
89 return $status;
92 public function preSaveTransform(
93 Content $content,
94 PreSaveTransformParams $pstParams
95 ): Content {
96 '@phan-var JsonContent $content';
98 // FIXME: WikiPage::doUserEditContent invokes PST before validation. As such, native
99 // data may be invalid (though PST result is discarded later in that case).
100 if ( !$content->isValid() ) {
101 return $content;
104 $contentClass = $this->getContentClass();
105 return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) );
109 * Set the HTML and add the appropriate styles.
111 * @since 1.38
112 * @param Content $content
113 * @param ContentParseParams $cpoParams
114 * @param ParserOutput &$parserOutput The output object to fill (reference).
116 protected function fillParserOutput(
117 Content $content,
118 ContentParseParams $cpoParams,
119 ParserOutput &$parserOutput
121 '@phan-var JsonContent $content';
122 // FIXME: WikiPage::doUserEditContent generates parser output before validation.
123 // As such, native data may be invalid (though output is discarded later in that case).
124 if ( $cpoParams->getGenerateHtml() ) {
125 if ( $content->isValid() ) {
126 $parserOptions = $cpoParams->getParserOptions();
127 if ( $cpoParams->getParserOptions()->getUseParsoid() ) {
128 $title = MediaWikiServices::getInstance()->getTitleFactory()
129 ->newFromPageReference( $cpoParams->getPage() );
130 $parser = MediaWikiServices::getInstance()->getParsoidParserFactory()
131 ->create();
132 $parserOutput = $parser->parse(
133 // It is necessary to pass a Content rather than a
134 // string in order for Parsoid to handle the
135 // contentmodel correctly.
136 $content, $title, $parserOptions,
137 true, true, $cpoParams->getRevId()
139 // Register the use of the 'parsoid' option again, since
140 // we have a new $parserOutput now.
141 $parserOptions->getUseParsoid();
142 } else {
143 $parserOutput->setRawText( $content->rootValueTable( $content->getData()->getValue() ) );
145 } else {
146 $error = wfMessage( 'invalid-json-data' )->parse();
147 $parserOutput->setRawText( $error );
150 $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
151 } else {
152 $parserOutput->setRawText( null );
156 /** @deprecated class alias since 1.43 */
157 class_alias( JsonContentHandler::class, 'JsonContentHandler' );