2014-09-15 08:17:29 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
2014-12-02 00:38:01 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
2014-09-15 08:17:29 +00:00
|
|
|
*
|
2014-12-02 00:38:01 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
2014-09-15 08:17:29 +00:00
|
|
|
*
|
2014-12-02 00:38:01 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
2014-09-15 08:17:29 +00:00
|
|
|
*/
|
|
|
|
|
|
2021-08-24 12:17:12 +00:00
|
|
|
use MediaWiki\Content\Renderer\ContentParseParams;
|
2021-07-21 01:03:59 +00:00
|
|
|
use MediaWiki\Content\Transform\PreSaveTransformParams;
|
|
|
|
|
|
2014-09-15 08:17:29 +00:00
|
|
|
/**
|
2022-02-08 11:05:10 +00:00
|
|
|
* Content handler for JSON text.
|
|
|
|
|
*
|
|
|
|
|
* Useful for maintaining JSON that can be viewed and edit directly by users.
|
2014-12-02 00:38:01 +00:00
|
|
|
*
|
|
|
|
|
* @author Ori Livneh <ori@wikimedia.org>
|
|
|
|
|
* @author Kunal Mehta <legoktm@gmail.com>
|
|
|
|
|
*
|
2014-09-15 08:17:29 +00:00
|
|
|
* @since 1.24
|
2022-02-08 11:05:10 +00:00
|
|
|
* @stable to extend
|
2014-12-02 00:38:01 +00:00
|
|
|
* @ingroup Content
|
2014-09-15 08:17:29 +00:00
|
|
|
*/
|
|
|
|
|
class JsonContentHandler extends CodeContentHandler {
|
|
|
|
|
|
2022-02-08 11:05:10 +00:00
|
|
|
/**
|
|
|
|
|
* @param string $modelId
|
|
|
|
|
* @stable to call
|
|
|
|
|
*/
|
2014-09-15 08:17:29 +00:00
|
|
|
public function __construct( $modelId = CONTENT_MODEL_JSON ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
|
2014-09-15 08:17:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getContentClass() {
|
2016-08-13 04:16:37 +00:00
|
|
|
return JsonContent::class;
|
2014-09-15 08:17:29 +00:00
|
|
|
}
|
2016-09-08 06:18:47 +00:00
|
|
|
|
|
|
|
|
public function makeEmptyContent() {
|
|
|
|
|
$class = $this->getContentClass();
|
|
|
|
|
return new $class( '{}' );
|
|
|
|
|
}
|
2021-07-21 01:03:59 +00:00
|
|
|
|
|
|
|
|
public function preSaveTransform(
|
|
|
|
|
Content $content,
|
|
|
|
|
PreSaveTransformParams $pstParams
|
|
|
|
|
): Content {
|
2021-08-09 13:39:19 +00:00
|
|
|
$shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
|
|
|
|
|
$content,
|
|
|
|
|
$pstParams
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $shouldCallDeprecatedMethod ) {
|
|
|
|
|
return $this->callDeprecatedContentPST(
|
|
|
|
|
$content,
|
|
|
|
|
$pstParams
|
|
|
|
|
);
|
2021-08-05 15:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 01:03:59 +00:00
|
|
|
'@phan-var JsonContent $content';
|
|
|
|
|
|
2022-05-18 21:25:07 +00:00
|
|
|
// FIXME: WikiPage::doUserEditContent invokes PST before validation. As such, native
|
|
|
|
|
// data may be invalid (though PST result is discarded later in that case).
|
2021-07-21 01:03:59 +00:00
|
|
|
if ( !$content->isValid() ) {
|
|
|
|
|
return $content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$contentClass = $this->getContentClass();
|
|
|
|
|
return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) );
|
|
|
|
|
}
|
2021-08-24 12:17:12 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the HTML and add the appropriate styles.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.38
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @param ContentParseParams $cpoParams
|
2022-01-11 20:15:36 +00:00
|
|
|
* @param ParserOutput &$parserOutput The output object to fill (reference).
|
2021-08-24 12:17:12 +00:00
|
|
|
*/
|
|
|
|
|
protected function fillParserOutput(
|
|
|
|
|
Content $content,
|
|
|
|
|
ContentParseParams $cpoParams,
|
2022-01-11 20:15:36 +00:00
|
|
|
ParserOutput &$parserOutput
|
2021-08-24 12:17:12 +00:00
|
|
|
) {
|
|
|
|
|
'@phan-var JsonContent $content';
|
2022-05-18 21:25:07 +00:00
|
|
|
// FIXME: WikiPage::doUserEditContent generates parser output before validation.
|
2021-08-24 12:17:12 +00:00
|
|
|
// As such, native data may be invalid (though output is discarded later in that case).
|
|
|
|
|
if ( $cpoParams->getGenerateHtml() && $content->isValid() ) {
|
2022-01-11 20:15:36 +00:00
|
|
|
$parserOutput->setText( $content->rootValueTable( $content->getData()->getValue() ) );
|
|
|
|
|
$parserOutput->addModuleStyles( [ 'mediawiki.content.json' ] );
|
2021-08-24 12:17:12 +00:00
|
|
|
} else {
|
2022-04-22 05:47:40 +00:00
|
|
|
$parserOutput->setText( null );
|
2021-08-24 12:17:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
2014-09-15 08:17:29 +00:00
|
|
|
}
|