wiki.techinc.nl/includes/content/JsonContentHandler.php
Roman Stolar 42442e01ff Move Content::preloadTransform to ContentHandler
Update ContentTransformer to access ContentHandler::preLoadTransform through the service.
Prepare object to hold a data that required for ContentHandler::preLoadTranform params.

This is a fully backwards compatible change.
We are doing hard deprecation via MWDebug::detectDeprecatedOverride.

However, with the ContentHandler calling Content and
Content calling ContentHandler, it doesn't matter whether
callers use Content or ContentHandler. This will allow us
to naturally convert all callers.

Bug: T287157
Change-Id: I89537e1e7d24c6e15252b2b51890a0bd81ea3e6b
2021-08-17 15:17:34 +00:00

77 lines
2.1 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\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() ) );
}
}