Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
51.43% |
18 / 35 |
|
42.86% |
3 / 7 |
CRAP | |
0.00% |
0 / 1 |
JsonContentHandler | |
52.94% |
18 / 34 |
|
42.86% |
3 / 7 |
30.61 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
getContentClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
makeEmptyContent | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
supportsPreloadContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
validateSave | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
preSaveTransform | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
fillParserOutput | |
44.44% |
8 / 18 |
|
0.00% |
0 / 1 |
6.74 |
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 | |
21 | namespace MediaWiki\Content; |
22 | |
23 | use MediaWiki\Content\Renderer\ContentParseParams; |
24 | use MediaWiki\Content\Transform\PreSaveTransformParams; |
25 | use MediaWiki\MediaWikiServices; |
26 | use MediaWiki\Parser\ParserOutput; |
27 | use StatusValue; |
28 | |
29 | /** |
30 | * Content handler for JSON text. |
31 | * |
32 | * Useful for maintaining JSON that can be viewed and edited directly by users. |
33 | * |
34 | * @author Ori Livneh <ori@wikimedia.org> |
35 | * @author Kunal Mehta <legoktm@gmail.com> |
36 | * |
37 | * @since 1.24 |
38 | * @stable to extend |
39 | * @ingroup Content |
40 | */ |
41 | class JsonContentHandler extends CodeContentHandler { |
42 | |
43 | /** |
44 | * @param string $modelId |
45 | * @stable to call |
46 | */ |
47 | public function __construct( $modelId = CONTENT_MODEL_JSON ) { |
48 | parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] ); |
49 | } |
50 | |
51 | /** |
52 | * @return class-string<JsonContent> |
53 | */ |
54 | protected function getContentClass() { |
55 | return JsonContent::class; |
56 | } |
57 | |
58 | public function makeEmptyContent() { |
59 | $class = $this->getContentClass(); |
60 | return new $class( '{}' ); |
61 | } |
62 | |
63 | /** |
64 | * Enables EditPage's preload feature on .json pages as well as for extensions like MassMessage |
65 | * that subclass {@see JsonContentHandler}. |
66 | * |
67 | * @return true |
68 | */ |
69 | public function supportsPreloadContent(): bool { |
70 | return true; |
71 | } |
72 | |
73 | /** |
74 | * @param Content $content |
75 | * @param ValidationParams $validationParams |
76 | * @return StatusValue |
77 | */ |
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; |
86 | } |
87 | } |
88 | $this->getHookRunner()->onJsonValidateSave( $content, $validationParams->getPageIdentity(), $status ); |
89 | return $status; |
90 | } |
91 | |
92 | public function preSaveTransform( |
93 | Content $content, |
94 | PreSaveTransformParams $pstParams |
95 | ): Content { |
96 | '@phan-var JsonContent $content'; |
97 | |
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; |
102 | } |
103 | |
104 | $contentClass = $this->getContentClass(); |
105 | return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) ); |
106 | } |
107 | |
108 | /** |
109 | * Set the HTML and add the appropriate styles. |
110 | * |
111 | * @since 1.38 |
112 | * @param Content $content |
113 | * @param ContentParseParams $cpoParams |
114 | * @param ParserOutput &$parserOutput The output object to fill (reference). |
115 | */ |
116 | protected function fillParserOutput( |
117 | Content $content, |
118 | ContentParseParams $cpoParams, |
119 | ParserOutput &$parserOutput |
120 | ) { |
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() |
138 | ); |
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() ) ); |
144 | } |
145 | } else { |
146 | $error = wfMessage( 'invalid-json-data' )->parse(); |
147 | $parserOutput->setRawText( $error ); |
148 | } |
149 | |
150 | $parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] ); |
151 | } else { |
152 | $parserOutput->setRawText( null ); |
153 | } |
154 | } |
155 | } |
156 | /** @deprecated class alias since 1.43 */ |
157 | class_alias( JsonContentHandler::class, 'JsonContentHandler' ); |