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
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
;
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>
41 class JsonContentHandler
extends CodeContentHandler
{
44 * @param string $modelId
47 public function __construct( $modelId = CONTENT_MODEL_JSON
) {
48 parent
::__construct( $modelId, [ CONTENT_FORMAT_JSON
] );
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( '{}' );
64 * Enables EditPage's preload feature on .json pages as well as for extensions like MassMessage
65 * that subclass {@see JsonContentHandler}.
69 public function supportsPreloadContent(): bool {
74 * @param Content $content
75 * @param ValidationParams $validationParams
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' ) );
88 $this->getHookRunner()->onJsonValidateSave( $content, $validationParams->getPageIdentity(), $status );
92 public function preSaveTransform(
94 PreSaveTransformParams
$pstParams
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() ) {
104 $contentClass = $this->getContentClass();
105 return new $contentClass( JsonContent
::normalizeLineEndings( $content->beautifyJSON() ) );
109 * Set the HTML and add the appropriate styles.
112 * @param Content $content
113 * @param ContentParseParams $cpoParams
114 * @param ParserOutput &$parserOutput The output object to fill (reference).
116 protected function fillParserOutput(
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()
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();
143 $parserOutput->setRawText( $content->rootValueTable( $content->getData()->getValue() ) );
146 $error = wfMessage( 'invalid-json-data' )->parse();
147 $parserOutput->setRawText( $error );
150 $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
152 $parserOutput->setRawText( null );
156 /** @deprecated class alias since 1.43 */
157 class_alias( JsonContentHandler
::class, 'JsonContentHandler' );