wiki.techinc.nl/includes/content/JsonContentHandler.php
Roman Stolar a68e641f9d Move Content::getParserOutput & AbstractContent::fillParserOutput to ContentHandler
Update/Create override classes of ContentHandler.
Soft-deprecate and remove method from Content and classes that override them.

Bug: T287158
Change-Id: Idfcfbfe1a196cd69a04ca357281d08bb3d097ce2
2021-09-29 13:10:51 +03:00

102 lines
2.9 KiB
PHP

<?php
/**
* 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.
*
* 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.
*
* 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
*/
use MediaWiki\Content\Renderer\ContentParseParams;
use MediaWiki\Content\Transform\PreSaveTransformParams;
/**
* Content handler for JSON.
*
* @author Ori Livneh <ori@wikimedia.org>
* @author Kunal Mehta <legoktm@gmail.com>
*
* @since 1.24
* @ingroup Content
*/
class JsonContentHandler extends CodeContentHandler {
public function __construct( $modelId = CONTENT_MODEL_JSON ) {
parent::__construct( $modelId, [ CONTENT_FORMAT_JSON ] );
}
/**
* @return string
*/
protected function getContentClass() {
return JsonContent::class;
}
public function makeEmptyContent() {
$class = $this->getContentClass();
return new $class( '{}' );
}
public function preSaveTransform(
Content $content,
PreSaveTransformParams $pstParams
): Content {
$shouldCallDeprecatedMethod = $this->shouldCallDeprecatedContentTransformMethod(
$content,
$pstParams
);
if ( $shouldCallDeprecatedMethod ) {
return $this->callDeprecatedContentPST(
$content,
$pstParams
);
}
'@phan-var JsonContent $content';
// FIXME: WikiPage::doEditContent invokes PST before validation. As such, native data
// may be invalid (though PST result is discarded later in that case).
if ( !$content->isValid() ) {
return $content;
}
$contentClass = $this->getContentClass();
return new $contentClass( JsonContent::normalizeLineEndings( $content->beautifyJSON() ) );
}
/**
* Set the HTML and add the appropriate styles.
*
* @since 1.38
* @param Content $content
* @param ContentParseParams $cpoParams
* @param ParserOutput &$output The output object to fill (reference).
*/
protected function fillParserOutput(
Content $content,
ContentParseParams $cpoParams,
ParserOutput &$output
) {
'@phan-var JsonContent $content';
// FIXME: WikiPage::doEditContent generates parser output before validation.
// As such, native data may be invalid (though output is discarded later in that case).
if ( $cpoParams->getGenerateHtml() && $content->isValid() ) {
$output->setText( $content->rootValueTable( $content->getData()->getValue() ) );
$output->addModuleStyles( 'mediawiki.content.json' );
} else {
$output->setText( '' );
}
}
}