Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
60.87% covered (warning)
60.87%
14 / 23
14.29% covered (danger)
14.29%
1 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
JsonContentHandler
60.87% covered (warning)
60.87%
14 / 23
14.29% covered (danger)
14.29%
1 / 7
20.63
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getContentClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 makeEmptyContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 supportsPreloadContent
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 validateSave
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
 preSaveTransform
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 fillParserOutput
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
3.03
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21use MediaWiki\Content\Renderer\ContentParseParams;
22use MediaWiki\Content\Transform\PreSaveTransformParams;
23use MediaWiki\Content\ValidationParams;
24use MediaWiki\Parser\ParserOutput;
25
26/**
27 * Content handler for JSON text.
28 *
29 * Useful for maintaining JSON that can be viewed and edited directly by users.
30 *
31 * @author Ori Livneh <ori@wikimedia.org>
32 * @author Kunal Mehta <legoktm@gmail.com>
33 *
34 * @since 1.24
35 * @stable to extend
36 * @ingroup Content
37 */
38class JsonContentHandler extends CodeContentHandler {
39
40    /**
41     * @param string $modelId
42     * @stable to call
43     */
44    public function __construct( $modelId = CONTENT_MODEL_JSON ) {
45        parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
46    }
47
48    /**
49     * @return class-string<JsonContent>
50     */
51    protected function getContentClass() {
52        return JsonContent::class;
53    }
54
55    public function makeEmptyContent() {
56        $class = $this->getContentClass();
57        return new $class( '{}' );
58    }
59
60    /**
61     * Enables EditPage's preload feature on .json pages as well as for extensions like MassMessage
62     * that subclass {@see JsonContentHandler}.
63     *
64     * @return true
65     */
66    public function supportsPreloadContent(): bool {
67        return true;
68    }
69
70    /**
71     * @param Content $content
72     * @param ValidationParams $validationParams
73     * @return StatusValue
74     */
75    public function validateSave( Content $content, ValidationParams $validationParams ) {
76        $status = parent::validateSave( $content, $validationParams );
77        '@phan-var JsonContent $content';
78        if ( !$status->isOK() ) {
79            if ( !$content->getData()->isGood() ) {
80                return StatusValue::newFatal( $content->getData()->getMessage( 'invalid-json-data' ) );
81            } else {
82                return $status;
83            }
84        }
85        $this->getHookRunner()->onJsonValidateSave( $content, $validationParams->getPageIdentity(), $status );
86        return $status;
87    }
88
89    public function preSaveTransform(
90        Content $content,
91        PreSaveTransformParams $pstParams
92    ): Content {
93        '@phan-var JsonContent $content';
94
95        // FIXME: WikiPage::doUserEditContent invokes PST before validation. As such, native
96        // data may be invalid (though PST result is discarded later in that case).
97        if ( !$content->isValid() ) {
98            return $content;
99        }
100
101        $contentClass = $this->getContentClass();
102        return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) );
103    }
104
105    /**
106     * Set the HTML and add the appropriate styles.
107     *
108     * @since 1.38
109     * @param Content $content
110     * @param ContentParseParams $cpoParams
111     * @param ParserOutput &$parserOutput The output object to fill (reference).
112     */
113    protected function fillParserOutput(
114        Content $content,
115        ContentParseParams $cpoParams,
116        ParserOutput &$parserOutput
117    ) {
118        '@phan-var JsonContent $content';
119        // FIXME: WikiPage::doUserEditContent generates parser output before validation.
120        // As such, native data may be invalid (though output is discarded later in that case).
121        if ( $cpoParams->getGenerateHtml() ) {
122            if ( $content->isValid() ) {
123                $parserOutput->setRawText( $content->rootValueTable( $content->getData()->getValue() ) );
124            } else {
125                $error = wfMessage( 'invalid-json-data' )->parse();
126                $parserOutput->setRawText( $error );
127            }
128
129            $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
130        } else {
131            $parserOutput->setRawText( null );
132        }
133    }
134}